These errors need to be wrapped in a delay call in order to actually work, otherwise we run into this error: https://www.reddit.com/r/neovim/comments/qz4wy6/how_can_i_do_this/
52 lines
1.3 KiB
Lua
52 lines
1.3 KiB
Lua
local Job = require("plenary.job")
|
|
local state = require("gitlab.state")
|
|
local M = {}
|
|
|
|
M.run_job = function(endpoint, method, body, callback)
|
|
local args = { "-s", "-X", (method or "POST"), string.format("localhost:%s/", state.PORT) .. endpoint }
|
|
|
|
if body ~= nil then
|
|
table.insert(args, 1, "-d")
|
|
table.insert(args, 2, body)
|
|
end
|
|
Job:new({
|
|
command = "curl",
|
|
args = args,
|
|
on_stdout = function(_, output)
|
|
local data_ok, data = pcall(vim.json.decode, output)
|
|
if data_ok and data ~= nil then
|
|
local status = (data.status >= 200 and data.status < 300) and "success" or "error"
|
|
if callback ~= nil then
|
|
callback(data)
|
|
else
|
|
vim.defer_fn(function()
|
|
vim.notify(data.message, vim.log.levels.DEBUG)
|
|
end, 0)
|
|
end
|
|
else
|
|
vim.defer_fn(function()
|
|
vim.notify("Could not parse command output!", vim.log.levels.ERROR)
|
|
end, 0)
|
|
end
|
|
end,
|
|
on_stderr = function(_, output)
|
|
vim.defer_fn(function()
|
|
vim.notify("Could not run command!", vim.log.levels.ERROR)
|
|
end, 0)
|
|
end
|
|
}):start()
|
|
end
|
|
|
|
-- Approves the current merge request
|
|
M.approve = function()
|
|
M.run_job("approve", "POST")
|
|
end
|
|
|
|
-- Revokes approval for the current merge request
|
|
M.revoke = function()
|
|
M.run_job("revoke", "POST")
|
|
end
|
|
|
|
|
|
return M
|