Change to HTTP Model (#5)

This commit is contained in:
Harrison (Harry) Cramer
2023-05-19 17:28:58 -07:00
committed by GitHub
parent fe0d09d582
commit 63fc025070
21 changed files with 689 additions and 430 deletions

View File

@@ -1,18 +0,0 @@
local u = require("gitlab.utils")
local state = require("gitlab.state")
local Job = require("plenary.job")
local M = {}
-- Approves the current merge request
M.approve = function()
if u.base_invalid() then return end
Job:new({
command = state.BIN,
args = { "approve", state.PROJECT_ID },
on_stdout = u.print_success,
on_stderr = u.print_error
}):start()
end
return M

View File

@@ -1,7 +1,7 @@
local Menu = require("nui.menu")
local NuiTree = require("nui.tree")
local notify = require("notify")
local Job = require("plenary.job")
local job = require("gitlab.job")
local state = require("gitlab.state")
local u = require("gitlab.utils")
local keymaps = require("gitlab.keymaps")
@@ -56,20 +56,9 @@ M.confirm_create_comment = function(text)
end
end
Job:new({
command = state.BIN,
args = {
"comment",
state.PROJECT_ID,
current_line_number,
relative_file_path,
text,
sha
},
-- TODO: Render the tree after comment creation. Refresh?
on_stdout = u.print_success,
on_stderr = u.print_error
}):start()
local json = string.format('{ "line_number": %d, "file_name": "%s", "comment": "%s" }', current_line_number,
relative_file_path, text)
job.run_job("comment", "POST", json)
end
M.delete_comment = function()
@@ -118,29 +107,18 @@ M.delete_comment = function()
local discussion_id = node:get_id()
discussion_id = string.sub(discussion_id, 2) -- Remove the "-" at the start
note_id = string.sub(note_id, 2) -- Remove the "-" at the start
Job:new({
command = state.BIN,
args = {
"deleteComment",
state.PROJECT_ID,
discussion_id,
note_id,
},
on_stdout = function(_, line)
vim.schedule(function()
if line ~= nil and line ~= "" then
notify(line, "info")
state.tree:remove_node("-" .. note_id)
local discussion_node = state.tree:get_node("-" .. discussion_id)
if not discussion_node:has_children() then
state.tree:remove_node("-" .. discussion_id)
end
state.tree:render()
end
end)
end,
on_stderr = u.print_error
}):start()
local json = string.format('{"discussion_id": "%s", "note_id": %d}', discussion_id, note_id)
job.run_job("comment", "DELETE", json, function(data)
notify(data.message, "success")
state.tree:remove_node("-" .. note_id)
local discussion_node = state.tree:get_node("-" .. discussion_id)
if not discussion_node:has_children() then
state.tree:remove_node("-" .. discussion_id)
end
state.tree:render()
end)
end
end,
})
@@ -179,45 +157,31 @@ M.edit_comment = function()
end
M.send_edits = function(text)
Job:new({
command = state.BIN,
args = {
"editComment",
state.PROJECT_ID,
state.ACTIVE_DISCUSSION,
state.ACTIVE_NOTE,
text,
},
on_stdout = function(_, line)
local note = vim.json.decode(line)
if note == nil then
notify("There was an issue editing the note", "error")
return
local escapedText = string.gsub(text, "\n", "\\n")
local json = string.format('{"discussion_id": "%s", "note_id": %s, "comment": "%s"}', state.ACTIVE_DISCUSSION,
state.ACTIVE_NOTE, escapedText)
job.run_job("comment", "PATCH", json, function()
vim.schedule(function()
local node = state.tree:get_node("-" .. state.ACTIVE_NOTE)
local childrenIds = node:get_child_ids()
for _, value in ipairs(childrenIds) do
state.tree:remove_node(value)
end
vim.schedule(function()
local node = state.tree:get_node("-" .. state.ACTIVE_NOTE)
local newNoteTextNodes = {}
for bodyLine in text:gmatch("[^\n]+") do
table.insert(newNoteTextNodes, NuiTree.Node({ text = bodyLine, is_body = true }, {}))
end
local childrenIds = node:get_child_ids()
for _, value in ipairs(childrenIds) do
state.tree:remove_node(value)
end
state.tree:set_nodes(newNoteTextNodes, "-" .. state.ACTIVE_NOTE)
local newNoteTextNodes = {}
for bodyLine in note.body:gmatch("[^\n]+") do
table.insert(newNoteTextNodes, NuiTree.Node({ text = bodyLine, is_body = true }, {}))
end
state.tree:set_nodes(newNoteTextNodes, "-" .. state.ACTIVE_NOTE)
state.tree:render()
local buf = vim.api.nvim_get_current_buf()
u.darken_metadata(buf, '')
notify("Edited comment!")
end)
end,
on_stderr = u.print_error
}):start()
state.tree:render()
local buf = vim.api.nvim_get_current_buf()
u.darken_metadata(buf, '')
notify("Edited comment!")
end)
end)
end
return M

View File

@@ -1,5 +1,6 @@
local u = require("gitlab.utils")
local NuiTree = require("nui.tree")
local job = require("gitlab.job")
local notify = require("notify")
local state = require("gitlab.state")
local Job = require("plenary.job")
@@ -17,87 +18,79 @@ M.reply = function()
end
M.send_reply = function(text)
Job:new({
command = state.BIN,
args = {
"reply",
state.PROJECT_ID,
state.ACTIVE_DISCUSSION,
text,
},
on_stdout = function(_, line)
local note = vim.json.decode(line)
if note == nil then
notify("There was an issue creating the note", "error")
return
end
local escapedText = string.gsub(text, "\n", "\\n")
local json = string.format('{"discussion_id": "%s", "reply": "%s"}', state.ACTIVE_DISCUSSION, escapedText)
job.run_job("reply", "POST", json, function(data)
local note_node = M.build_note(data.note)
note_node:expand()
local note_node = M.build_note(note)
note_node:expand()
state.tree:add_node(note_node, "-" .. state.ACTIVE_DISCUSSION)
vim.schedule(function()
state.tree:render()
local buf = vim.api.nvim_get_current_buf()
u.darken_metadata(buf, '')
notify("Sent reply!")
end)
end,
on_stderr = u.print_error
}):start()
state.tree:add_node(note_node, "-" .. state.ACTIVE_DISCUSSION)
vim.schedule(function()
state.tree:render()
local buf = vim.api.nvim_get_current_buf()
u.darken_metadata(buf, '')
notify("Sent reply!")
end)
end)
end
-- Places all of the discussions into a readable list
M.list_discussions = function()
if u.base_invalid() then return end
Job:new({
command = state.BIN,
args = { "listDiscussions", state.PROJECT_ID },
on_stdout = function(_, line)
local discussions = vim.json.decode(line)
if (type(discussions) == 'userdata') then
notify("No discussions found for this MR", "warn")
return
end
M.discussions = discussions
vim.schedule(function()
vim.cmd.tabnew()
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_command("vsplit")
vim.api.nvim_buf_set_option(buf, 'filetype', 'markdown')
vim.api.nvim_set_current_buf(buf)
local allDiscussions = {}
for i, discussion in ipairs(discussions) do
local discussionChildren = {}
for _, note in ipairs(discussion.notes) do
local note_node = M.build_note(note)
if i == 1 then
note_node:expand()
end
table.insert(discussionChildren, note_node)
end
local discussionNode = NuiTree.Node({
text = discussion.id,
id = discussion.id,
is_discussion = true
},
discussionChildren)
if i == 1 then
discussionNode:expand()
end
table.insert(allDiscussions, discussionNode)
command = "curl",
args = { "-s", "localhost:8081/" .. "discussions" },
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 status == "error" then
notify("Could not fetch discussions!", "error")
return
end
state.tree = NuiTree({ nodes = allDiscussions, bufnr = buf })
M.discussions = data.discussions
vim.schedule(function()
vim.cmd.tabnew()
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_command("vsplit")
vim.api.nvim_buf_set_option(buf, 'filetype', 'markdown')
vim.api.nvim_set_current_buf(buf)
local allDiscussions = {}
for i, discussion in ipairs(data.discussions) do
local discussionChildren = {}
for _, note in ipairs(discussion.notes) do
local note_node = M.build_note(note)
if i == 1 then
note_node:expand()
end
table.insert(discussionChildren, note_node)
end
local discussionNode = NuiTree.Node({
text = discussion.id,
id = discussion.id,
is_discussion = true
},
discussionChildren)
if i == 1 then
discussionNode:expand()
end
table.insert(allDiscussions, discussionNode)
end
state.tree = NuiTree({ nodes = allDiscussions, bufnr = buf })
M.set_tree_keymaps(buf)
M.set_tree_keymaps(buf)
state.tree:render()
vim.api.nvim_buf_set_option(buf, 'filetype', 'markdown')
u.darken_metadata(buf, '')
M.jump_to_file()
end)
state.tree:render()
vim.api.nvim_buf_set_option(buf, 'filetype', 'markdown')
u.darken_metadata(buf, '')
M.jump_to_file()
end)
end
end,
on_stderr = function(_, output)
notify("Could not run approve command!", "error")
error(output)
end,
on_stderr = u.print_error,
}):start()
end

View File

@@ -1,19 +1,18 @@
local Job = require("plenary.job")
local curl = require("plenary.curl")
local state = require("gitlab.state")
local notify = require("notify")
local discussions = require("gitlab.discussions")
local summary = require("gitlab.summary")
local keymaps = require("gitlab.keymaps")
local comment = require("gitlab.comment")
local approve = require("gitlab.approve")
local revoke = require("gitlab.revoke")
local job = require("gitlab.job")
local u = require("gitlab.utils")
-- Root Module Scope
local M = {}
M.summary = summary.summary
M.approve = approve.approve
M.revoke = revoke.revoke
M.approve = job.approve
M.revoke = job.revoke
M.create_comment = comment.create_comment
M.list_discussions = discussions.list_discussions
M.edit_comment = comment.edit_comment
@@ -21,8 +20,6 @@ M.delete_comment = comment.delete_comment
M.reply = discussions.reply
-- Builds the Go binary, initializes the plugin, fetches MR info
local projectData = {}
M.build = function(args)
if args == nil then args = {} end
local command = string.format("cd %s && make", state.BIN_PATH)
@@ -42,6 +39,9 @@ M.setup = function(args, build_only)
state.BIN = parent_dir .. "/bin"
if args == nil then args = {} end
if args.dev == true then
M.build(args)
end
local binExists = io.open(state.BIN, "r")
if not binExists or args.dev == true then
@@ -78,29 +78,34 @@ M.setup = function(args, build_only)
state.BASE_BRANCH = args.base_branch
end
local error_message = "Failed to set up gitlab.nvim, could not get project information."
if u.is_gitlab_repo() then
Job:new({
command = state.BIN,
args = { "info", state.PROJECT_ID },
on_stdout = function(_, line)
table.insert(projectData, line)
end,
on_stderr = u.print_error,
on_exit = function()
if projectData[1] ~= nil then
local parsed_ok, data = pcall(vim.json.decode, projectData[1])
if parsed_ok ~= true then
notify("Failed calling setup. Could not get project data.", "error")
else
state.INFO = data
local port = args.port or 21036
vim.fn.jobstart(state.BIN .. " " .. state.PROJECT_ID .. " " .. port, {
on_stdout = function(job_id)
if job_id <= 0 then
notify(error_message, "error")
return
else
local response_ok, response = pcall(curl.get, "localhost:" .. port .. "/info",
{ timeout = 750 })
if response == nil or not response_ok then
notify(error_message, "error")
return
end
local body = response.body
local parsed_ok, data = pcall(vim.json.decode, body)
if parsed_ok ~= true then
notify(error_message, "error")
return
end
state.INFO = data
keymaps.set_keymap_keys(args.keymaps)
keymaps.set_keymaps()
end
end,
}):start()
end
})
end
keymaps.set_keymap_keys(args.keymaps)
keymaps.set_keymaps()
end
M.current_file_path = function()

46
lua/gitlab/job.lua Normal file
View File

@@ -0,0 +1,46 @@
local notify = require("notify")
local Job = require("plenary.job")
local M = {}
M.run_job = function(endpoint, method, body, callback)
local args = { "-s", "-X", (method or "POST"), "localhost:8081/" .. 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
notify(data.message, status)
end
else
notify("Could not parse command output!", "error")
end
end,
on_stderr = function(_, output)
notify("Could not run command!", "error")
error(output)
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

View File

@@ -1,17 +0,0 @@
local u = require("gitlab.utils")
local state = require("gitlab.state")
local M = {}
local Job = require("plenary.job")
-- Revokes approval for the current merge request
M.revoke = function()
if u.base_invalid() then return end
Job:new({
command = state.BIN,
args = { "revoke", state.PROJECT_ID },
on_stdout = u.print_success,
on_stderr = u.print_error
}):start()
end
return M

View File

@@ -239,7 +239,6 @@ local split_diff_view_filename = function(filename)
return hash, path
end
M.get_relative_file_path = get_relative_file_path
M.get_current_line_number = get_current_line_number
M.get_buffer_text = get_buffer_text