Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Jan Noort committed Jun 16, 2024
1 parent 687108c commit 8b87dc0
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .busted
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
return {
_all = {
coverage = false,
lpath = "lua/?.lua;lua/?/init.lua",
lua = "~/.luarocks/bin/nlua",
lpath = "./lua_modules/share/lua/5.1/?.lua;./lua_modules/share/lua/5.1/?/init.lua;lua/?.lua;lua/?/init.lua",
lua = "./lua_modules/bin/nlua",
},
default = {
verbose = true
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
pull_request: ~
push:
branches:
- main
- master

jobs:
build:
Expand All @@ -17,5 +17,7 @@ jobs:
- uses: actions/checkout@v3
- name: Run tests
uses: nvim-neorocks/nvim-busted-action@v1
env:
AZURE_DEVOPS_EXT_PAT: ${{ secrets.AZURE_DEVOPS_EXT_PAT_ADOPURE }}
with:
nvim_version: ${{ matrix.neovim_version }}
4 changes: 3 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---@diagnostic disable-next-line lowercase-global
std = {
globals = { "vim", "describe", "it" },
globals = { "vim", "describe", "it", "setup", "io" },
read_globals = {
"setup",
"io",
"require",
"error",
"table",
Expand Down
3 changes: 3 additions & 0 deletions adopure.nvim-0.0.1-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ dependencies = {
}
test_dependencies = {
"nlua",
"plenary.nvim",
"nui.nvim",
"telescope.nvim",
}
build = {
type = "builtin",
Expand Down
30 changes: 20 additions & 10 deletions lua/ado.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end
---@param sub_impl string[]
---@param subcommand_args string[]
---@param subcommand string
local function prompt_target(sub_impl, subcommand_args, subcommand)
local function execute_or_prompt(sub_impl, subcommand_args, subcommand)
if #subcommand_args == 0 then
vim.ui.select(vim.tbl_keys(sub_impl), { "Choose target" }, function(choice)
if not choice then
Expand All @@ -30,7 +30,9 @@ local function prompt_target(sub_impl, subcommand_args, subcommand)
end)
return
end
local load_opts = function() return load("return " .. (subcommand_args[2] or ""), 't')() end
local load_opts = function()
return load("return " .. (subcommand_args[2] or ""), "t")()
end
local ok, opts = pcall(load_opts)
sub_impl[subcommand_args[1]](ok and opts or {})
end
Expand All @@ -39,24 +41,32 @@ end
---@field impl fun(args:string[])
---@field complete_args? string[]

---Initial load of state_manager with all open PRs;
---@return StateManager
function M.load_state_manager()
if not state_manager then
local context = require("ado.state").AdoContext:new()
state_manager = require("ado.state").StateManager:new(context)
end
assert(state_manager, "StateManager should not be nil after loading;")
return state_manager
end

---@type table<string, SubCommand>
local subcommand_tbl = {
load = {
complete_args = { "context", "threads" },
impl = function(args)
local sub_impl = {
context = function(opts)
if not state_manager then
local context = require("ado.state").AdoContext:new()
state_manager = require("ado.state").StateManager:new(context)
end
state_manager:choose_and_activate(opts)
local manager = M.load_state_manager()
manager:choose_and_activate(opts)
end,
threads = function(opts)
M.get_loaded_state():load_pull_request_threads(opts)
end,
}
prompt_target(sub_impl, args, "load")
execute_or_prompt(sub_impl, args, "load")
end,
},
submit = {
Expand All @@ -73,7 +83,7 @@ local subcommand_tbl = {
require("ado.thread").update_thread_status(M.get_loaded_state(), opts)
end,
}
prompt_target(sub_impl, args, "submit")
execute_or_prompt(sub_impl, args, "submit")
end,
},
open = {
Expand All @@ -93,7 +103,7 @@ local subcommand_tbl = {
require("ado.thread").open_thread_window(M.get_loaded_state(), opts)
end,
}
prompt_target(sub_impl, args, "open")
execute_or_prompt(sub_impl, args, "open")
end,
},
}
Expand Down
43 changes: 34 additions & 9 deletions lua/ado/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ function M.get_pull_requests_iteration_changes(pull_request, iteration)
return result.changeEntries, err
end

---Get pull request threads from Azure DevOps
---@param pull_request PullRequest
---Get not deleted pull request threads from Azure DevOps
---@param state AdoState
---@return Thread[] threads, string|nil err
function M.get_pull_request_threads(pull_request)
local iteration = "$baseIteration=1&iteration=6"
function M.get_pull_request_threads(state)
local iteration = "$baseIteration=1&iteration=" .. state.active_pull_request_iteration.id
---@type RequestResult
local result, err = get_azure_devops(
pull_request.url .. "/threads?" .. table.concat({ GIT_API_VERSION, iteration }, "&"),
state.active_pull_request.url .. "/threads?" .. table.concat({ GIT_API_VERSION, iteration }, "&"),
"pull request threads"
)
if not result then
Expand All @@ -124,7 +124,12 @@ function M.get_pull_request_threads(pull_request)

---@type Thread[]
local threads = result.value
return threads, err
local active_threads = vim.iter(threads)
:filter(function(thread) ---@param thread Thread
return not thread.isDeleted
end)
:totable()
return active_threads, err
end

---patch request to azure devops
Expand All @@ -145,9 +150,12 @@ local function submit_azure_devops(url, http_verb, request_type, body)
end
return nil, "Failed to " .. http_verb .. " " .. request_type .. "; " .. details
end
---@type Thread|Comment|Reviewer|nil
local result = vim.fn.json_decode(response.body)
return result, nil
if type(response.body) == "string" and #response.body ~=0 then
---@type Thread|Comment|Reviewer|nil
local result = vim.fn.json_decode(response.body)
return result, nil
end
return nil, nil
end

---Create new pull request comment thread
Expand Down Expand Up @@ -198,6 +206,22 @@ function M.update_pull_request_thread(pull_request, thread)
return result, err
end

---Create new pull request comment reply
---@param pull_request PullRequest
---@param thread Thread
---@param comment_id number
---@return Thread|nil thread, string|nil err
function M.delete_pull_request_comment(pull_request, thread, comment_id)
---@type Thread|nil
local result, err = submit_azure_devops(
table.concat({ pull_request.url, "/threads/", thread.id, "/comments/", comment_id, "?", GIT_API_VERSION }),
"DELETE",
"pull request thread update",
nil
)
return result, err
end

---Create new pull request vote
---@param pull_request PullRequest
---@param vote PullRequestVote
Expand All @@ -221,4 +245,5 @@ function M.submit_vote(pull_request, vote)
)
return result, err
end

return M
2 changes: 1 addition & 1 deletion lua/ado/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function InternalConfig:access_token()
"or check the docs to find other ways of configuring it.",
}, " ")
assert(self.pat_token, message)
return require("b64").enc(":" .. self.pat_token)
return vim.base64.encode(":" .. self.pat_token)
end

local config = InternalConfig:new()
Expand Down
15 changes: 0 additions & 15 deletions lua/ado/git.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
local M = {}

---Get git repository name
---@return string Repository name
function M.get_repo_name()
local get_git_repo_job = require("plenary.job"):new({
command = "git",
args = { "rev-parse", "--show-toplevel" },
cwd = ".",
})
get_git_repo_job:start()

local repository_path = require("plenary.path"):new(require("ado.utils").await_result(get_git_repo_job)[1])
local path_parts = vim.split(repository_path.filename, repository_path.path.sep)
return path_parts[#path_parts]
end

---@param remote_url string
---@return string organization_url
---@return string project_name
Expand Down
15 changes: 10 additions & 5 deletions lua/ado/review.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,23 @@ end
---Submit vote of choice on pull request
---@param state AdoState
---@param _ table
function M.submit_vote(state,_)
function M.submit_vote(state, _)
vim.ui.select(vim.tbl_keys(M.pull_request_vote), { prompt = "Select vote;" }, function(vote)
if not vote then
vim.notify("No vote chosen;", 2)
return
end
local reviewer, err = require("ado.api").submit_vote(state.active_pull_request, M.pull_request_vote[vote])

if err or not reviewer then
local new_reviewer, err = require("ado.api").submit_vote(state.active_pull_request, M.pull_request_vote[vote])
if err or not new_reviewer then
error(err or "Expected Reviewer but not nil;")
end
-- TODO: Render vote somewhere?
for _, reviewer in pairs(state.active_pull_request.reviewers) do
if reviewer.displayName == new_reviewer.displayName then
reviewer.vote = new_reviewer.vote
return
end
end
table.insert(state.active_pull_request.reviewers, new_reviewer)
end)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/ado/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end

---@param _ table
function AdoState:load_pull_request_threads(_)
local pull_request_threads, err = require("ado.api").get_pull_request_threads(self.active_pull_request)
local pull_request_threads, err = require("ado.api").get_pull_request_threads(self)
if err then
error(err)
end
Expand Down
26 changes: 20 additions & 6 deletions lua/ado/thread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ local function get_pull_request_comment_thread_context(state, thread_context)
return nil, "File not changed in this Pull Request;"
end

---@param bufnr number
---@param mark_id number
---@param state AdoState
---@param thread_to_open Thread
local function add_comment_reply(bufnr, mark_id, state, thread_to_open)
---@type CommentReply
local comment_reply = { bufnr = bufnr, mark_id = mark_id, thread = thread_to_open }
table.insert(state.comment_replies, comment_reply)
end

---@param state AdoState
---@param bufnr number
---@param comment_creation CommentCreate
Expand Down Expand Up @@ -80,7 +90,9 @@ local function submit_thread(state, bufnr, comment_creation)
return err or "Expected Thread but got nil;"
end
table.insert(state.pull_request_threads, thread)
require("ado.render").render_reply_thread(namespace, thread)
local mark_id
bufnr, mark_id = require("ado.render").render_reply_thread(namespace, thread)
add_comment_reply(bufnr, mark_id, state, thread)
end

---@return number line_start, number col_start, number line_end, number col_end
Expand Down Expand Up @@ -176,9 +188,7 @@ function M.open_thread_window(state, opts)
return
end
local bufnr, mark_id = require("ado.render").render_reply_thread(namespace, thread_to_open)
---@type CommentReply
local comment_reply = { bufnr = bufnr, mark_id = mark_id, thread = thread_to_open }
table.insert(state.comment_replies, comment_reply)
add_comment_reply(bufnr, mark_id, state, thread_to_open)
end

---@param bufnr number
Expand Down Expand Up @@ -231,8 +241,7 @@ function M.update_thread_status(state, _)
end

---@type Thread
---@diagnostic disable-next-line: missing-fields
local thread = {
local thread = { ---@diagnostic disable-line: missing-fields
id = comment_reply.thread.id,
status = choice,
}
Expand All @@ -258,6 +267,11 @@ function M.submit_comment(state, _)
if err then
error(err)
end
state.comment_creations = vim.iter(state.comment_creations)
:filter(function(state_comment_creation) ---@param state_comment_creation CommentCreate
return comment_creation.bufnr ~= state_comment_creation.bufnr
end)
:totable()
return
end

Expand Down
6 changes: 0 additions & 6 deletions spec/example_spec.lua

This file was deleted.

32 changes: 32 additions & 0 deletions spec/load_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
describe("Load command", function()
---@type StateManager|nil
local state_manager
local function get_secret_value()
local secret = os.getenv("AZURE_DEVOPS_EXT_PAT")
if secret then
return secret
end
local handle = io.popen("pass show AZURE_DEVOPS_EXT_PAT_ADOPURE")
assert(handle, "Handle not nil;")
return handle:read()
end

setup(function() ---@diagnostic disable-line: undefined-global
local secret = get_secret_value()
vim.g.adopure = { pat_token = secret }
end)

it("can retrieve PRs", function()
require("plenary.path")
state_manager = require("ado").load_state_manager()
assert.are.same(#state_manager.pull_requests, 1)
end)

it("can activate a PR", function()
state_manager = require("ado").load_state_manager()
state_manager:set_state_by_choice(state_manager.pull_requests[1])

assert.are.same(state_manager.state.active_pull_request.title, "Updated pr-test-file.md PR title")
end)
end)

Loading

0 comments on commit 8b87dc0

Please sign in to comment.