Simplify Go Endpoints + Add Tests (#120)
This MR represents a major refactor of the Go codebase, as well as introducing tests for the handlers. The MR also introduces an endpoint to shutdown or restart the Go server, which may be useful for clients who want to refresh the state of the plugin after checking out branches. Finally, this MR adds a contributing document for users who want to make feature changes.
This commit is contained in:
committed by
GitHub
parent
10b0b596ae
commit
93fe3e8bd6
@@ -101,7 +101,7 @@ local M = {
|
||||
---callback with data
|
||||
---@param callback fun(data: DiscussionData): nil
|
||||
M.load_discussions = function(callback)
|
||||
job.run_job("/discussions", "POST", { blacklist = state.settings.discussion_tree.blacklist }, function(data)
|
||||
job.run_job("/discussions/list", "POST", { blacklist = state.settings.discussion_tree.blacklist }, function(data)
|
||||
M.discussions = data.discussions
|
||||
M.unlinked_discussions = data.unlinked_discussions
|
||||
callback(data)
|
||||
@@ -645,7 +645,7 @@ M.toggle_discussion_resolved = function(tree)
|
||||
resolved = not note.resolved,
|
||||
}
|
||||
|
||||
job.run_job("/discussion/resolve", "PUT", body, function(data)
|
||||
job.run_job("/discussions/resolve", "PUT", body, function(data)
|
||||
u.notify(data.message, vim.log.levels.INFO)
|
||||
M.redraw_resolved_status(tree, note, not note.resolved)
|
||||
end)
|
||||
|
||||
@@ -34,8 +34,8 @@ M.open = function()
|
||||
if not pipeline then
|
||||
return
|
||||
end
|
||||
local body = { pipeline_id = pipeline.id }
|
||||
job.run_job("/pipeline", "GET", body, function(data)
|
||||
|
||||
job.run_job("/pipeline/" .. pipeline.id, "GET", nil, function(data)
|
||||
local pipeline_jobs = u.reverse(type(data.Jobs) == "table" and data.Jobs or {})
|
||||
M.pipeline_jobs = pipeline_jobs
|
||||
|
||||
@@ -92,13 +92,12 @@ M.retrigger = function()
|
||||
if not pipeline then
|
||||
return
|
||||
end
|
||||
local body = { pipeline_id = pipeline.id }
|
||||
if pipeline.status ~= "failed" then
|
||||
u.notify("Pipeline is not in a failed state!", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
job.run_job("/pipeline", "POST", body, function()
|
||||
job.run_job("/pipeline/" .. pipeline.id, "POST", nil, function()
|
||||
u.notify("Pipeline re-triggered!", vim.log.levels.INFO)
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -29,12 +29,13 @@ function async:fetch(dependencies, i, argTable)
|
||||
|
||||
local dependency = dependencies[i]
|
||||
|
||||
-- Do not call endpoint unless refresh is required
|
||||
-- If we have data already and refresh is not required, skip this API call
|
||||
if state[dependency.state] ~= nil and not dependency.refresh then
|
||||
self:fetch(dependencies, i + 1, argTable)
|
||||
return
|
||||
end
|
||||
|
||||
-- Call the API, set the data, and then call the next API
|
||||
job.run_job(dependency.endpoint, "GET", dependency.body, function(data)
|
||||
state[dependency.state] = data[dependency.key]
|
||||
self:fetch(dependencies, i + 1, argTable)
|
||||
@@ -54,11 +55,13 @@ M.sequence = function(dependencies, cb)
|
||||
end
|
||||
end
|
||||
|
||||
-- If go server is already running, then start fetching the values in sequence
|
||||
if state.go_server_running then
|
||||
handler:fetch(dependencies, 1, argTable)
|
||||
return
|
||||
end
|
||||
|
||||
-- Otherwise, start the go server and start fetching the values
|
||||
server.start(function()
|
||||
state.go_server_running = true
|
||||
handler:fetch(dependencies, 1, argTable)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
-- to Gitlab and returning the data
|
||||
local state = require("gitlab.state")
|
||||
local u = require("gitlab.utils")
|
||||
local job = require("gitlab.job")
|
||||
local M = {}
|
||||
|
||||
-- Starts the Go server and call the callback provided
|
||||
@@ -60,7 +61,12 @@ M.start = function(callback)
|
||||
end
|
||||
end,
|
||||
on_exit = function(job_id, exit_code)
|
||||
u.notify("Golang gitlab server exited: job_id: " .. job_id .. ", exit_code: " .. exit_code, vim.log.levels.ERROR)
|
||||
if exit_code ~= 0 then
|
||||
u.notify(
|
||||
"Golang gitlab server exited: job_id: " .. job_id .. ", exit_code: " .. exit_code,
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
end
|
||||
end,
|
||||
})
|
||||
if job_id <= 0 then
|
||||
@@ -95,4 +101,41 @@ M.build = function(override)
|
||||
return true
|
||||
end
|
||||
|
||||
-- Shuts down the Go server and clears out all old gitlab.nvim state
|
||||
M.shutdown = function(cb)
|
||||
if not state.go_server_running then
|
||||
vim.notify("The gitlab.nvim server is not running", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
job.run_job("/shutdown", "POST", { restart = false }, function(data)
|
||||
state.go_server_running = false
|
||||
state.clear_data()
|
||||
if cb then
|
||||
cb()
|
||||
else
|
||||
u.notify(data.message, vim.log.levels.INFO)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
-- Restarts the Go server and clears out all gitlab.nvim state
|
||||
M.restart = function(cb)
|
||||
if not state.go_server_running then
|
||||
vim.notify("The gitlab.nvim server is not running", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
job.run_job("/shutdown", "POST", { restart = true }, function(data)
|
||||
state.go_server_running = false
|
||||
M.start(function()
|
||||
state.go_server_running = true
|
||||
state.clear_data()
|
||||
if cb then
|
||||
cb()
|
||||
else
|
||||
u.notify(data.message, vim.log.levels.INFO)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -235,7 +235,21 @@ end
|
||||
M.dependencies = {
|
||||
info = { endpoint = "/info", key = "info", state = "INFO", refresh = false },
|
||||
revisions = { endpoint = "/mr/revisions", key = "Revisions", state = "MR_REVISIONS", refresh = false },
|
||||
project_members = { endpoint = "/members", key = "ProjectMembers", state = "PROJECT_MEMBERS", refresh = false },
|
||||
project_members = {
|
||||
endpoint = "/project/members",
|
||||
key = "ProjectMembers",
|
||||
state = "PROJECT_MEMBERS",
|
||||
refresh = false,
|
||||
},
|
||||
}
|
||||
|
||||
-- This function clears out all of the previously fetched data. It's used
|
||||
-- to reset the plugin state when the Go server is restarted
|
||||
M.clear_data = function()
|
||||
M.INFO = nil
|
||||
for _, dep in ipairs(M.dependencies) do
|
||||
M[dep.state] = nil
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user