Adds Formatting to the CI (#69)

This MR adds linting and formatting to the CI pipeline for the repository for both the Golang and Lua code.
This commit is contained in:
Harrison (Harry) Cramer
2023-10-30 23:54:38 -04:00
committed by GitHub
parent 3a67424fec
commit a055c4c988
31 changed files with 484 additions and 453 deletions

View File

@@ -1,38 +1,40 @@
-- This module is responsible for the assignment of reviewers
-- and assignees in Gitlab, those who must review an MR.
local u = require("gitlab.utils")
local job = require("gitlab.job")
local state = require("gitlab.state")
local M = {}
local u = require("gitlab.utils")
local job = require("gitlab.job")
local state = require("gitlab.state")
local M = {}
M.add_assignee = function()
M.add_popup('assignee')
M.add_assignee = function()
M.add_popup("assignee")
end
M.delete_assignee = function()
M.delete_popup('assignee')
M.delete_popup("assignee")
end
M.add_reviewer = function()
M.add_popup('reviewer')
M.add_reviewer = function()
M.add_popup("reviewer")
end
M.delete_reviewer = function()
M.delete_popup('reviewer')
M.delete_popup("reviewer")
end
M.add_popup = function(type)
local plural = type .. 's'
M.add_popup = function(type)
local plural = type .. "s"
local current = state.INFO[plural]
local eligible = M.filter_eligible(state.PROJECT_MEMBERS, current)
vim.ui.select(eligible, {
prompt = 'Choose ' .. type .. ' to add',
prompt = "Choose " .. type .. " to add",
format_item = function(user)
return user.username .. " (" .. user.name .. ")"
end
end,
}, function(choice)
if not choice then return end
local current_ids = u.extract(current, 'id')
if not choice then
return
end
local current_ids = u.extract(current, "id")
table.insert(current_ids, choice.id)
local body = { ids = current_ids }
job.run_job("/mr/" .. type, "PUT", body, function(data)
@@ -42,17 +44,19 @@ M.add_popup = function(type)
end)
end
M.delete_popup = function(type)
local plural = type .. 's'
M.delete_popup = function(type)
local plural = type .. "s"
local current = state.INFO[plural]
vim.ui.select(current, {
prompt = 'Choose ' .. type .. ' to delete',
prompt = "Choose " .. type .. " to delete",
format_item = function(user)
return user.username .. " (" .. user.name .. ")"
end
end,
}, function(choice)
if not choice then return end
local ids = u.extract(M.filter_eligible(current, { choice }), 'id')
if not choice then
return
end
local ids = u.extract(M.filter_eligible(current, { choice }), "id")
local body = { ids = ids }
job.run_job("/mr/" .. type, "PUT", body, function(data)
vim.notify(data.message, vim.log.levels.INFO)
@@ -62,10 +66,12 @@ M.delete_popup = function(type)
end
M.filter_eligible = function(current, to_remove)
local ids = u.extract(to_remove, 'id')
local ids = u.extract(to_remove, "id")
local res = {}
for _, member in ipairs(current) do
if not u.contains(ids, member.id) then table.insert(res, member) end
if not u.contains(ids, member.id) then
table.insert(res, member)
end
end
return res
end

View File

@@ -1,21 +1,21 @@
-- This module is responsible for the discussion tree. That includes things like
-- editing existing notes in the tree, replying to notes in the tree,
-- and marking discussions as resolved/unresolved.
local Split = require("nui.split")
local Popup = require("nui.popup")
local Menu = require("nui.menu")
local NuiTree = require("nui.tree")
local Layout = require("nui.layout")
local job = require("gitlab.job")
local u = require("gitlab.utils")
local state = require("gitlab.state")
local reviewer = require("gitlab.reviewer")
local miscellaneous = require("gitlab.actions.miscellaneous")
local Split = require("nui.split")
local Popup = require("nui.popup")
local Menu = require("nui.menu")
local NuiTree = require("nui.tree")
local Layout = require("nui.layout")
local job = require("gitlab.job")
local u = require("gitlab.utils")
local state = require("gitlab.state")
local reviewer = require("gitlab.reviewer")
local miscellaneous = require("gitlab.actions.miscellaneous")
local edit_popup = Popup(u.create_popup_state("Edit Comment", "80%", "80%"))
local reply_popup = Popup(u.create_popup_state("Reply", "80%", "80%"))
local edit_popup = Popup(u.create_popup_state("Edit Comment", "80%", "80%"))
local reply_popup = Popup(u.create_popup_state("Reply", "80%", "80%"))
local M = {
local M = {
layout_visible = false,
layout = nil,
layout_buf = nil,
@@ -27,7 +27,7 @@ local M = {
-- Opens the discussion tree, sets the keybindings. It also
-- creates the tree for notes (which are not linked to specific lines of code)
M.toggle = function()
M.toggle = function()
if M.layout_visible then
M.layout:unmount()
M.layout_visible = false
@@ -55,20 +55,24 @@ M.toggle = function()
M.discussions = data.discussions
M.unlinked_discussions = data.unlinked_discussions
if type(data.discussions) == "table" then M.rebuild_discussion_tree() end
if type(data.unlinked_discussions) == "table" then M.rebuild_unlinked_discussion_tree() end
if type(data.discussions) == "table" then
M.rebuild_discussion_tree()
end
if type(data.unlinked_discussions) == "table" then
M.rebuild_unlinked_discussion_tree()
end
M.switch_can_edit_bufs(true)
M.add_empty_titles({
{ linked_section.bufnr, data.discussions, "No Discussions for this MR" },
{ unlinked_section.bufnr, data.unlinked_discussions, "No Notes (Unlinked Discussions) for this MR" }
{ linked_section.bufnr, data.discussions, "No Discussions for this MR" },
{ unlinked_section.bufnr, data.unlinked_discussions, "No Notes (Unlinked Discussions) for this MR" },
})
M.switch_can_edit_bufs(false)
end)
end
-- The reply popup will mount in a window when you trigger it (settings.discussion_tree.reply) when hovering over a node in the discussion tree.
M.reply = function(tree)
M.reply = function(tree)
local node = tree:get_node()
local discussion_node = M.get_root_node(tree, node)
local id = tostring(discussion_node.id)
@@ -77,7 +81,7 @@ M.reply = function(tree)
end
-- This function will send the reply to the Go API
M.send_reply = function(tree, discussion_id)
M.send_reply = function(tree, discussion_id)
return function(text)
local body = { discussion_id = discussion_id, reply = text }
job.run_job("/reply", "POST", body, function(data)
@@ -88,7 +92,7 @@ M.send_reply = function(tree, discussion_id)
end
-- This function (settings.discussion_tree.delete_comment) will trigger a popup prompting you to delete the current comment
M.delete_comment = function(tree, unlinked)
M.delete_comment = function(tree, unlinked)
local menu = Menu({
position = "50%",
size = {
@@ -118,14 +122,14 @@ M.delete_comment = function(tree, unlinked)
},
on_submit = function(item)
M.send_deletion(tree, item, unlinked)
end
end,
})
menu:mount()
end
-- This function will actually send the deletion to Gitlab
-- when you make a selection, and re-render the tree
M.send_deletion = function(tree, item, unlinked)
M.send_deletion = function(tree, item, unlinked)
if item.text == "Confirm" then
local current_node = tree:get_node()
@@ -151,8 +155,8 @@ M.send_deletion = function(tree, item, unlinked)
end
M.switch_can_edit_bufs(true)
M.add_empty_titles({
{ M.linked_section_bufnr, M.discussions, "No Discussions for this MR" },
{ M.unlinked_section_bufnr, M.unlinked_discussions, "No Notes (Unlinked Discussions) for this MR" }
{ M.linked_section_bufnr, M.discussions, "No Discussions for this MR" },
{ M.unlinked_section_bufnr, M.unlinked_discussions, "No Notes (Unlinked Discussions) for this MR" },
})
M.switch_can_edit_bufs(false)
end)
@@ -160,7 +164,7 @@ M.send_deletion = function(tree, item, unlinked)
end
-- This function (settings.discussion_tree.edit_comment) will open the edit popup for the current comment in the discussion tree
M.edit_comment = function(tree, unlinked)
M.edit_comment = function(tree, unlinked)
local current_node = tree:get_node()
local note_node = M.get_note_node(tree, current_node)
local root_node = M.get_root_node(tree, current_node)
@@ -171,7 +175,7 @@ M.edit_comment = function(tree, unlinked)
local children_ids = note_node:get_child_ids()
for _, child_id in ipairs(children_ids) do
local child_node = tree:get_node(child_id)
if (not child_node:has_children()) then
if not child_node:has_children() then
local line = tree:get_node(child_id).text
table.insert(lines, line)
end
@@ -179,17 +183,19 @@ M.edit_comment = function(tree, unlinked)
local currentBuffer = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(currentBuffer, 0, -1, false, lines)
state.set_popup_keymaps(edit_popup,
M.send_edits(tree, tostring(root_node.id), note_node.root_note_id or note_node.id, unlinked))
state.set_popup_keymaps(
edit_popup,
M.send_edits(tostring(root_node.id), note_node.root_note_id or note_node.id, unlinked)
)
end
-- This function sends the edited comment to the Go server
M.send_edits = function(tree, discussion_id, note_id, unlinked)
M.send_edits = function(discussion_id, note_id, unlinked)
return function(text)
local body = {
discussion_id = discussion_id,
note_id = note_id,
comment = text
comment = text,
}
job.run_job("/comment", "PATCH", body, function(data)
vim.notify(data.message, vim.log.levels.INFO)
@@ -205,9 +211,11 @@ M.send_edits = function(tree, discussion_id, note_id, unlinked)
end
-- This comment (settings.discussion_tree.toggle_resolved) will toggle the resolved status of the current discussion and send the change to the Go server
M.toggle_resolved = function(tree)
M.toggle_resolved = function(tree)
local note = tree:get_node()
if not note or not note.resolvable then return end
if not note or not note.resolvable then
return
end
local body = {
discussion_id = note.id,
@@ -232,7 +240,7 @@ M.jump_to_reviewer = function(tree)
end
-- This function (settings.discussion_tree.jump_to_file) will jump to the file changed in a new tab
M.jump_to_file = function(tree)
M.jump_to_file = function(tree)
local file_name, new_line, old_line, error = M.get_note_location(tree)
if error ~= nil then
vim.notify(error, vim.log.levels.ERROR)
@@ -243,11 +251,15 @@ M.jump_to_file = function(tree)
end
-- This function (settings.discussion_tree.toggle_node) expands/collapses the current node and its children
M.toggle_node = function(tree)
M.toggle_node = function(tree)
local node = tree:get_node()
if node == nil then return end
if node == nil then
return
end
local children = node:get_child_ids()
if node == nil then return end
if node == nil then
return
end
if node:is_expanded() then
node:collapse()
for _, child in ipairs(children) do
@@ -263,12 +275,11 @@ M.toggle_node = function(tree)
tree:render()
end
--
-- 🌲 Helper Functions
--
M.rebuild_discussion_tree = function()
M.rebuild_discussion_tree = function()
M.switch_can_edit_bufs(true)
vim.api.nvim_buf_set_lines(M.linked_section_bufnr, 0, -1, false, {})
local discussion_tree_nodes = M.add_discussions_to_table(M.discussions)
@@ -290,15 +301,17 @@ M.rebuild_unlinked_discussion_tree = function()
M.switch_can_edit_bufs(false)
end
M.switch_can_edit_bufs = function(bool)
M.switch_can_edit_bufs = function(bool)
u.switch_can_edit_buf(M.unlinked_section_bufnr, bool)
u.switch_can_edit_buf(M.linked_section_bufnr, bool)
end
M.add_discussion = function(arg)
M.add_discussion = function(arg)
local discussion = arg.data.discussion
if arg.unlinked then
if type(M.unlinked_discussions) ~= "table" then M.unlinked_discussions = {} end
if type(M.unlinked_discussions) ~= "table" then
M.unlinked_discussions = {}
end
table.insert(M.unlinked_discussions, 1, discussion)
local bufinfo = vim.fn.getbufinfo(M.unlinked_section_bufnr)
if u.table_size(bufinfo) ~= 0 then
@@ -306,7 +319,9 @@ M.add_discussion = function(arg)
end
return
end
if type(M.discussions) ~= "table" then M.discussions = {} end
if type(M.discussions) ~= "table" then
M.discussions = {}
end
table.insert(M.discussions, 1, discussion)
local bufinfo = vim.fn.getbufinfo(M.unlinked_section_bufnr)
if u.table_size(bufinfo) ~= 0 then
@@ -314,32 +329,30 @@ M.add_discussion = function(arg)
end
end
M.create_layout = function()
local linked_section = Split({ enter = true })
M.create_layout = function()
local linked_section = Split({ enter = true })
local unlinked_section = Split({})
local position = state.settings.discussion_tree.position
local size = state.settings.discussion_tree.size
local relative = state.settings.discussion_tree.relative
local position = state.settings.discussion_tree.position
local size = state.settings.discussion_tree.size
local relative = state.settings.discussion_tree.relative
local layout = Layout(
local layout = Layout(
{
position = position,
size = size,
relative = relative,
},
Layout.Box({
Layout.Box(linked_section, { size = "50%" }),
Layout.Box(unlinked_section, { size = "50%" }),
},
{ dir = (position == "left" and "col" or "row") }
)
Layout.Box(linked_section, { size = "50%" }),
Layout.Box(unlinked_section, { size = "50%" }),
}, { dir = (position == "left" and "col" or "row") })
)
return linked_section, unlinked_section, layout
end
M.add_empty_titles = function(args)
M.add_empty_titles = function(args)
local ns_id = vim.api.nvim_create_namespace("GitlabNamespace")
vim.cmd("highlight default TitleHighlight guifg=#787878")
for _, section in ipairs(args) do
@@ -347,81 +360,75 @@ M.add_empty_titles = function(args)
if type(data) ~= "table" or #data == 0 then
vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, { title })
local linnr = 1
vim.api.nvim_buf_set_extmark(bufnr, ns_id, linnr - 1, 0,
{ end_row = linnr - 1, end_col = string.len(title), hl_group = 'TitleHighlight' })
vim.api.nvim_buf_set_extmark(
bufnr,
ns_id,
linnr - 1,
0,
{ end_row = linnr - 1, end_col = string.len(title), hl_group = "TitleHighlight" }
)
end
end
end
M.set_tree_keymaps = function(tree, bufnr, unlinked)
vim.keymap.set('n',
state.settings.discussion_tree.edit_comment,
function() M.edit_comment(tree, unlinked) end,
{ buffer = bufnr }
)
vim.keymap.set('n',
state.settings.discussion_tree.delete_comment,
function() M.delete_comment(tree, unlinked) end,
{ buffer = bufnr }
)
vim.keymap.set('n',
state.settings.discussion_tree.toggle_resolved,
function() M.toggle_resolved(tree) end,
{ buffer = bufnr }
)
vim.keymap.set('n',
state.settings.discussion_tree.toggle_node,
function() M.toggle_node(tree, unlinked) end,
{ buffer = bufnr }
)
vim.keymap.set('n',
state.settings.discussion_tree.reply,
function() M.reply(tree) end,
{ buffer = bufnr }
)
M.set_tree_keymaps = function(tree, bufnr, unlinked)
vim.keymap.set("n", state.settings.discussion_tree.edit_comment, function()
M.edit_comment(tree, unlinked)
end, { buffer = bufnr })
vim.keymap.set("n", state.settings.discussion_tree.delete_comment, function()
M.delete_comment(tree, unlinked)
end, { buffer = bufnr })
vim.keymap.set("n", state.settings.discussion_tree.toggle_resolved, function()
M.toggle_resolved(tree)
end, { buffer = bufnr })
vim.keymap.set("n", state.settings.discussion_tree.toggle_node, function()
M.toggle_node(tree, unlinked)
end, { buffer = bufnr })
vim.keymap.set("n", state.settings.discussion_tree.reply, function()
M.reply(tree)
end, { buffer = bufnr })
if not unlinked then
vim.keymap.set('n', state.settings.discussion_tree.jump_to_file, function()
vim.keymap.set("n", state.settings.discussion_tree.jump_to_file, function()
M.jump_to_file(tree)
end, { buffer = bufnr }
)
vim.keymap.set('n', state.settings.discussion_tree.jump_to_reviewer,
function() M.jump_to_reviewer(tree) end,
{ buffer = bufnr }
)
end, { buffer = bufnr })
vim.keymap.set("n", state.settings.discussion_tree.jump_to_reviewer, function()
M.jump_to_reviewer(tree)
end, { buffer = bufnr })
end
end
M.redraw_resolved_status = function(tree, note, mark_resolved)
local current_text = tree.nodes.by_id["-" .. note.id].text
local target = mark_resolved and 'resolved' or 'unresolved'
local current = mark_resolved and 'unresolved' or 'resolved'
local target = mark_resolved and "resolved" or "unresolved"
local current = mark_resolved and "unresolved" or "resolved"
local function set_property(key, val)
tree.nodes.by_id["-" .. note.id][key] = val
end
local has_symbol = function(s)
return state.settings.discussion_tree[s] ~= nil and state.settings.discussion_tree[s] ~= ''
return state.settings.discussion_tree[s] ~= nil and state.settings.discussion_tree[s] ~= ""
end
set_property('resolved', mark_resolved)
set_property("resolved", mark_resolved)
if not has_symbol(current) and not has_symbol(target) then return end
if not has_symbol(current) and not has_symbol(target) then
return
end
if not has_symbol(current) and has_symbol(target) then
set_property('text', (current_text .. " " .. state.settings.discussion_tree[target]))
set_property("text", (current_text .. " " .. state.settings.discussion_tree[target]))
elseif has_symbol(current) and not has_symbol(target) then
set_property('text', u.remove_last_chunk(current_text))
set_property("text", u.remove_last_chunk(current_text))
else
set_property('text', (u.remove_last_chunk(current_text) .. " " .. state.settings.discussion_tree[target]))
set_property("text", (u.remove_last_chunk(current_text) .. " " .. state.settings.discussion_tree[target]))
end
tree:render()
end
M.replace_text = function(data, discussion_id, note_id, text)
M.replace_text = function(data, discussion_id, note_id, text)
for i, discussion in ipairs(data) do
if discussion.id == discussion_id then
for j, note in ipairs(discussion.notes) do
@@ -434,8 +441,8 @@ M.replace_text = function(data, discussion_id, note_id, text)
end
end
M.get_root_node = function(tree, node)
if (not node.is_root) then
M.get_root_node = function(tree, node)
if not node.is_root then
local parent_id = node:get_parent_id()
return M.get_root_node(tree, tree:get_node(parent_id))
else
@@ -443,37 +450,42 @@ M.get_root_node = function(tree, node)
end
end
M.get_note_node = function(tree, node)
if (not node.is_note) then
M.get_note_node = function(tree, node)
if not node.is_note then
local parent_id = node:get_parent_id()
if parent_id == nil then return node end
if parent_id == nil then
return node
end
return M.get_note_node(tree, tree:get_node(parent_id))
else
return node
end
end
local attach_uuid = function(str)
local attach_uuid = function(str)
return { text = str, id = u.uuid() }
end
M.build_note_body = function(note, resolve_info)
M.build_note_body = function(note, resolve_info)
local text_nodes = {}
for bodyLine in note.body:gmatch("[^\n]+") do
local line = attach_uuid(bodyLine)
table.insert(text_nodes, NuiTree.Node({
new_line = (type(note.position) == "table" and note.position.new_line),
old_line = (type(note.position) == "table" and note.position.old_line),
text = line.text,
id = line.id,
is_body = true
}, {}))
table.insert(
text_nodes,
NuiTree.Node({
new_line = (type(note.position) == "table" and note.position.new_line),
old_line = (type(note.position) == "table" and note.position.old_line),
text = line.text,
id = line.id,
is_body = true,
}, {})
)
end
local resolve_symbol = ''
local resolve_symbol = ""
if resolve_info ~= nil and resolve_info.resolvable then
resolve_symbol = resolve_info.resolved and state.settings.discussion_tree.resolved or
state.settings.discussion_tree.unresolved
resolve_symbol = resolve_info.resolved and state.settings.discussion_tree.resolved
or state.settings.discussion_tree.unresolved
end
local noteHeader = "@" .. note.author.username .. " " .. u.format_date(note.created_at) .. " " .. resolve_symbol
@@ -481,7 +493,7 @@ M.build_note_body = function(note, resolve_info)
return noteHeader, text_nodes
end
M.build_note = function(note, resolve_info)
M.build_note = function(note, resolve_info)
local text, text_nodes = M.build_note_body(note, resolve_info)
local note_node = NuiTree.Node({
text = text,
@@ -495,23 +507,22 @@ M.build_note = function(note, resolve_info)
return note_node, text, text_nodes
end
M.add_reply_to_tree = function(tree, note, discussion_id)
M.add_reply_to_tree = function(tree, note, discussion_id)
local note_node = M.build_note(note)
note_node:expand()
tree:add_node(note_node, discussion_id and ("-" .. discussion_id) or nil)
tree:render()
end
M.add_discussions_to_table = function(items)
local t = {}
for _, discussion in ipairs(items) do
local discussion_children = {}
-- These properties are filled in by the first note
local root_text = ''
local root_note_id = ''
local root_file_name = ''
local root_text = ""
local root_note_id = ""
local root_file_name = ""
local root_id = 0
local root_text_nodes = {}
local resolvable = false
@@ -521,7 +532,7 @@ M.add_discussions_to_table = function(items)
for j, note in ipairs(discussion.notes) do
if j == 1 then
__, root_text, root_text_nodes = M.build_note(note, { resolved = note.resolved, resolvable = note.resolvable })
_, root_text, root_text_nodes = M.build_note(note, { resolved = note.resolved, resolvable = note.resolvable })
root_file_name = (type(note.position) == "table" and note.position.new_path)
root_new_line = (type(note.position) == "table" and note.position.new_line)
@@ -548,7 +559,7 @@ M.add_discussions_to_table = function(items)
new_line = root_new_line,
old_line = root_old_line,
resolvable = resolvable,
resolved = resolved
resolved = resolved,
}, body)
table.insert(t, root_node)
@@ -557,11 +568,15 @@ M.add_discussions_to_table = function(items)
return t
end
M.get_note_location = function(tree)
M.get_note_location = function(tree)
local node = tree:get_node()
if node == nil then return nil, nil, nil, "Could not get node" end
if node == nil then
return nil, nil, nil, "Could not get node"
end
local discussion_node = M.get_root_node(tree, node)
if discussion_node == nil then return nil, nil, nil, "Could not get discussion node" end
if discussion_node == nil then
return nil, nil, nil, "Could not get discussion node"
end
return discussion_node.file_name, discussion_node.new_line, discussion_node.old_line
end

View File

@@ -1,7 +1,7 @@
local state = require("gitlab.state")
local u = require("gitlab.utils")
local job = require("gitlab.job")
local M = {}
local state = require("gitlab.state")
local u = require("gitlab.utils")
local job = require("gitlab.job")
local M = {}
M.open_in_browser = function()
local url = state.INFO.web_url
@@ -18,9 +18,9 @@ M.open_in_browser = function()
end
end
M.attach_file = function()
M.attach_file = function()
local attachment_dir = state.settings.attachment_dir
if not attachment_dir or attachment_dir == '' then
if not attachment_dir or attachment_dir == "" then
vim.notify("Must provide valid attachment_dir in plugin setup", vim.log.levels.ERROR)
return
end
@@ -33,9 +33,11 @@ M.attach_file = function()
end
vim.ui.select(files, {
prompt = 'Choose attachment',
prompt = "Choose attachment",
}, function(choice)
if not choice then return end
if not choice then
return
end
local full_path = attachment_dir .. (u.is_windows() and "\\" or "/") .. choice
local body = { file_path = full_path, file_name = choice }
job.run_job("/mr/attachment", "POST", body, function(data)

View File

@@ -1,11 +1,11 @@
-- This module is responsible for the MR pipline
-- This lets the user see the current status of the pipeline
-- and retrigger the pipeline from within the editor
local Popup = require("nui.popup")
local state = require("gitlab.state")
local job = require("gitlab.job")
local u = require("gitlab.utils")
local M = {
local Popup = require("nui.popup")
local state = require("gitlab.state")
local job = require("gitlab.job")
local u = require("gitlab.utils")
local M = {
pipeline_jobs = nil,
pipeline_popup = nil,
}
@@ -53,8 +53,15 @@ M.open = function()
table.insert(lines, "")
table.insert(lines, "Jobs:")
for _, pipeline_job in ipairs(pipeline_jobs) do
table.insert(lines,
string.format("%s (%s) %s", state.settings.pipeline[pipeline_job.status], pipeline_job.status, pipeline_job.name))
table.insert(
lines,
string.format(
"%s (%s) %s",
state.settings.pipeline[pipeline_job.status],
pipeline_job.status,
pipeline_job.name
)
)
end
vim.schedule(function()
@@ -72,7 +79,7 @@ M.open = function()
end)
end
M.retrigger = function()
M.retrigger = function()
local pipeline = get_pipeline()
if not pipeline then
return
@@ -83,9 +90,8 @@ M.retrigger = function()
return
end
job.run_job("/pipeline", "POST", body, function(data)
job.run_job("/pipeline", "POST", body, function()
vim.notify("Pipeline re-triggered!", vim.log.levels.INFO)
pipeline = data.Pipeline
end)
end
@@ -101,7 +107,9 @@ M.see_logs = function()
local j = nil
for _, pipeline_job in ipairs(M.pipeline_jobs) do
if pipeline_job.name == last_word then j = pipeline_job end
if pipeline_job.name == last_word then
j = pipeline_job
end
end
if j == nil then
@@ -147,19 +155,24 @@ M.color_status = function(status, bufnr, status_line, linnr)
vim.cmd(string.format("highlight default StatusHighlight guifg=%s", state.settings.pipeline[status]))
local status_to_color_map = {
created = 'DiagnosticWarn',
pending = 'DiagnosticWarn',
preparing = 'DiagnosticWarn',
scheduled = 'DiagnosticWarn',
running = 'DiagnosticWarn',
canceled = 'DiagnosticWarn',
skipped = 'DiagnosticWarn',
failed = 'DiagnosticError',
success = 'DiagnosticOK',
created = "DiagnosticWarn",
pending = "DiagnosticWarn",
preparing = "DiagnosticWarn",
scheduled = "DiagnosticWarn",
running = "DiagnosticWarn",
canceled = "DiagnosticWarn",
skipped = "DiagnosticWarn",
failed = "DiagnosticError",
success = "DiagnosticOK",
}
vim.api.nvim_buf_set_extmark(bufnr, ns_id, linnr - 1, 0,
{ end_row = linnr - 1, end_col = string.len(status_line), hl_group = status_to_color_map[status] })
vim.api.nvim_buf_set_extmark(
bufnr,
ns_id,
linnr - 1,
0,
{ end_row = linnr - 1, end_col = string.len(status_line), hl_group = status_to_color_map[status] }
)
end
return M

View File

@@ -1,23 +1,22 @@
-- This module is responsible for the MR description
-- This lets the user open the description in a popup and
-- send edits to the description back to Gitlab
local Layout = require("nui.layout")
local Popup = require("nui.popup")
local job = require("gitlab.job")
local u = require("gitlab.utils")
local state = require("gitlab.state")
local Layout = require("nui.layout")
local Popup = require("nui.popup")
local job = require("gitlab.job")
local u = require("gitlab.utils")
local state = require("gitlab.state")
local miscellaneous = require("gitlab.actions.miscellaneous")
local M = {
local M = {
layout_visible = false,
layout = nil,
layout_buf = nil,
title_bufnr = nil,
description_bufnr = nil
description_bufnr = nil,
}
-- The function will render the MR description in a popup
M.summary = function()
M.summary = function()
if M.layout_visible then
M.layout:unmount()
M.layout_visible = false
@@ -48,14 +47,18 @@ M.summary = function()
vim.schedule(function()
vim.api.nvim_buf_set_lines(currentBuffer, 0, -1, false, lines)
vim.api.nvim_buf_set_lines(title_popup.bufnr, 0, -1, false, { title })
state.set_popup_keymaps(description_popup, M.edit_summary, miscellaneous.attach_file,
{ cb = exit, action_before_close = true })
state.set_popup_keymaps(
description_popup,
M.edit_summary,
miscellaneous.attach_file,
{ cb = exit, action_before_close = true }
)
state.set_popup_keymaps(title_popup, M.edit_summary, nil, { cb = exit, action_before_close = true })
end)
end
-- This function will PUT the new description to the Go server
M.edit_summary = function()
M.edit_summary = function()
local description = u.get_buffer_text(M.description_bufnr)
local title = u.get_buffer_text(M.title_bufnr):gsub("\n", " ")
local body = { title = title, description = description }
@@ -68,22 +71,22 @@ M.edit_summary = function()
end)
end
local top_popup = {
local top_popup = {
buf_options = {
filetype = 'markdown'
filetype = "markdown",
},
focusable = true,
border = {
style = "rounded",
text = {
top = "Merge Request"
top = "Merge Request",
},
},
}
local bottom_popup = {
buf_options = {
filetype = 'markdown'
filetype = "markdown",
},
enter = true,
focusable = true,
@@ -92,7 +95,7 @@ local bottom_popup = {
},
}
M.create_layout = function()
M.create_layout = function()
local title_popup = Popup(top_popup)
M.title_bufnr = title_popup.bufnr
local description_popup = Popup(bottom_popup)

View File

@@ -1,28 +1,27 @@
-- This module is responsible for calling APIs in sequence. It provides
-- an abstraction around the APIs that lets us ensure state.
local server = require("gitlab.server")
local job = require("gitlab.job")
local state = require("gitlab.state")
local u = require("gitlab.utils")
local job = require("gitlab.job")
local state = require("gitlab.state")
local M = {}
local M = {}
Async = {
cb = nil
local async = {
cb = nil,
}
function Async:new(o)
function async:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Async:init(cb)
function async:init(cb)
self.cb = cb
end
function Async:fetch(dependencies, i, argTable)
function async:fetch(dependencies, i, argTable)
if i > #dependencies then
self.cb(argTable)
return
@@ -45,7 +44,7 @@ end
-- Will call APIs in sequence and set global state
M.sequence = function(dependencies, cb)
return function(argTable)
local handler = Async:new()
local handler = async:new()
handler:init(cb)
if not state.is_gitlab_project then

View File

@@ -1,51 +1,55 @@
local u = require("gitlab.utils")
local async = require("gitlab.async")
local server = require("gitlab.server")
local state = require("gitlab.state")
local reviewer = require("gitlab.reviewer")
local discussions = require("gitlab.actions.discussions")
local summary = require("gitlab.actions.summary")
local u = require("gitlab.utils")
local async = require("gitlab.async")
local server = require("gitlab.server")
local state = require("gitlab.state")
local reviewer = require("gitlab.reviewer")
local discussions = require("gitlab.actions.discussions")
local summary = require("gitlab.actions.summary")
local assignees_and_reviewers = require("gitlab.actions.assignees_and_reviewers")
local comment = require("gitlab.actions.comment")
local pipeline = require("gitlab.actions.pipeline")
local approvals = require("gitlab.actions.approvals")
local miscellaneous = require("gitlab.actions.miscellaneous")
local comment = require("gitlab.actions.comment")
local pipeline = require("gitlab.actions.pipeline")
local approvals = require("gitlab.actions.approvals")
local miscellaneous = require("gitlab.actions.miscellaneous")
local info = state.dependencies.info
local project_members = state.dependencies.project_members
local revisions = state.dependencies.revisions
local info = state.dependencies.info
local project_members = state.dependencies.project_members
local revisions = state.dependencies.revisions
return {
setup = function(args)
if args == nil then args = {} end
server.build() -- Builds the Go binary if it doesn't exist
setup = function(args)
if args == nil then
args = {}
end
server.build() -- Builds the Go binary if it doesn't exist
state.setPluginConfiguration() -- Sets configuration from `.gitlab.nvim` file
state.merge_settings(args) -- Sets keymaps and other settings from setup function
reviewer.init() -- Picks and initializes reviewer (default is Delta)
state.merge_settings(args) -- Sets keymaps and other settings from setup function
reviewer.init() -- Picks and initializes reviewer (default is Delta)
u.has_reviewer(args.reviewer or "delta")
end,
-- Global Actions 🌎
summary = async.sequence({ info }, summary.summary),
approve = async.sequence({ info }, approvals.approve),
revoke = async.sequence({ info }, approvals.revoke),
add_reviewer = async.sequence({ info, project_members }, assignees_and_reviewers.add_reviewer),
delete_reviewer = async.sequence({ info, project_members }, assignees_and_reviewers.delete_reviewer),
add_assignee = async.sequence({ info, project_members }, assignees_and_reviewers.add_assignee),
delete_assignee = async.sequence({ info, project_members }, assignees_and_reviewers.delete_assignee),
create_comment = async.sequence({ info, revisions }, comment.create_comment),
create_multiline_comment = async.sequence({ info, revisions }, comment.create_multiline_comment),
summary = async.sequence({ info }, summary.summary),
approve = async.sequence({ info }, approvals.approve),
revoke = async.sequence({ info }, approvals.revoke),
add_reviewer = async.sequence({ info, project_members }, assignees_and_reviewers.add_reviewer),
delete_reviewer = async.sequence({ info, project_members }, assignees_and_reviewers.delete_reviewer),
add_assignee = async.sequence({ info, project_members }, assignees_and_reviewers.add_assignee),
delete_assignee = async.sequence({ info, project_members }, assignees_and_reviewers.delete_assignee),
create_comment = async.sequence({ info, revisions }, comment.create_comment),
create_multiline_comment = async.sequence({ info, revisions }, comment.create_multiline_comment),
create_comment_suggestion = async.sequence({ info, revisions }, comment.create_comment_suggestion),
create_note = async.sequence({ info }, comment.create_note),
review = async.sequence({ u.merge(info, { refresh = true }) }, function() reviewer.open() end),
pipeline = async.sequence({ info }, pipeline.open),
create_note = async.sequence({ info }, comment.create_note),
review = async.sequence({ u.merge(info, { refresh = true }) }, function()
reviewer.open()
end),
pipeline = async.sequence({ info }, pipeline.open),
-- Discussion Tree Actions 🌴
toggle_discussions = async.sequence({ info }, discussions.toggle),
edit_comment = async.sequence({ info }, discussions.edit_comment),
delete_comment = async.sequence({ info }, discussions.delete_comment),
toggle_resolved = async.sequence({ info }, discussions.toggle_resolved),
reply = async.sequence({ info }, discussions.reply),
toggle_discussions = async.sequence({ info }, discussions.toggle),
edit_comment = async.sequence({ info }, discussions.edit_comment),
delete_comment = async.sequence({ info }, discussions.delete_comment),
toggle_resolved = async.sequence({ info }, discussions.toggle_resolved),
reply = async.sequence({ info }, discussions.reply),
-- Other functions 🤷
state = state,
print_settings = state.print_settings,
open_in_browser = async.sequence({ info }, miscellaneous.open_in_browser),
state = state,
print_settings = state.print_settings,
open_in_browser = async.sequence({ info }, miscellaneous.open_in_browser),
}

View File

@@ -1,7 +1,7 @@
-- This module is responsible for making API calls to the Go server and
-- running the callbacks associated with those jobs when the JSON is returned
local Job = require("plenary.job")
local M = {}
local M = {}
M.run_job = function(endpoint, method, body, callback)
local state = require("gitlab.state")
@@ -24,7 +24,9 @@ M.run_job = function(endpoint, method, body, callback)
local data_ok, data = pcall(vim.json.decode, output)
if not data_ok then
local msg = string.format("Failed to parse JSON from %s endpoint", endpoint)
if (type(output) == "string") then msg = string.format(msg .. ", got: '%s'", output) end
if type(output) == "string" then
msg = string.format(msg .. ", got: '%s'", output)
end
vim.notify(string.format(msg, endpoint, output), vim.log.levels.WARN)
return
end
@@ -42,12 +44,12 @@ M.run_job = function(endpoint, method, body, callback)
end
end, 0)
end,
on_stderr = function(_, output)
on_stderr = function()
vim.defer_fn(function()
vim.notify("Could not run command!", vim.log.levels.ERROR)
end, 0)
end,
on_exit = function(msg, status)
on_exit = function(_, status)
vim.defer_fn(function()
if status ~= 0 then
vim.notify(string.format("Go server exited with non-zero code: %d", status), vim.log.levels.ERROR)

View File

@@ -246,11 +246,8 @@ M.get_review_buffer_lines = function(review_buffer_range)
return lines
end
---Return content between start_line and end_line
---@param start_line integer
---@param end_line integer
---@return string[] | nil
M.get_lines = function(start_line, end_line)
--- This function is not supported for delta
M.get_lines = function()
vim.notify("Getting lines in delta is not supported yet", vim.log.levels.ERROR)
return nil
end

View File

@@ -12,18 +12,20 @@ M.start = function(callback)
local parsed_port = nil
local callback_called = false
local command = state.settings.bin
.. " "
.. state.settings.project_id
.. " "
.. state.settings.gitlab_url
.. " "
.. port
.. " "
.. state.settings.auth_token
.. " "
.. "'" .. vim.json.encode(state.settings.debug) .. "'"
.. " "
.. state.settings.log_path
.. " "
.. state.settings.project_id
.. " "
.. state.settings.gitlab_url
.. " "
.. port
.. " "
.. state.settings.auth_token
.. " "
.. "'"
.. vim.json.encode(state.settings.debug)
.. "'"
.. " "
.. state.settings.log_path
local job_id = vim.fn.jobstart(command, {
on_stdout = function(_, data)
@@ -61,7 +63,7 @@ M.start = function(callback)
vim.notify(err_msg, vim.log.levels.ERROR)
end
end,
on_exit = function(job_id, exit_code, ...)
on_exit = function(job_id, exit_code)
vim.notify(
"Golang gitlab server exited: job_id: " .. job_id .. ", exit_code: " .. exit_code,
vim.log.levels.ERROR
@@ -88,7 +90,7 @@ M.build = function(override)
end
local cmd = u.is_windows() and "cd %s\\cmd && go build -o bin.exe && move bin.exe ..\\"
or "cd %s/cmd && go build -o bin && mv bin ../bin"
or "cd %s/cmd && go build -o bin && mv bin ../bin"
local command = string.format(cmd, state.settings.bin_path)
local null = u.is_windows() and " >NUL" or " > /dev/null"

View File

@@ -3,11 +3,11 @@
-- This module is also responsible for ensuring that the state of the plugin
-- is valid via dependencies
local u = require("gitlab.utils")
local M = {}
local u = require("gitlab.utils")
local M = {}
-- These are the default settings for the plugin
M.settings = {
M.settings = {
port = nil, -- choose random port
debug = { go_request = false, go_response = false },
log_path = (vim.fn.stdpath("cache") .. "/gitlab.nvim.log"),
@@ -62,12 +62,14 @@ M.settings = {
}
-- Merges user settings into the default settings, overriding them
M.merge_settings = function(args)
if args == nil then return end
M.merge_settings = function(args)
if args == nil then
return
end
M.settings = u.merge(M.settings, args)
end
M.print_settings = function()
M.print_settings = function()
u.P(M.settings)
end
@@ -110,15 +112,21 @@ end
local function exit(popup, cb)
popup:unmount()
if cb ~= nil then cb() end
if cb ~= nil then
cb()
end
end
-- These keymaps are buffer specific and are set dynamically when popups mount
M.set_popup_keymaps = function(popup, action, linewise_action, opts)
if opts == nil then opts = {} end
vim.keymap.set('n', M.settings.popup.exit, function() exit(popup, opts.cb) end, { buffer = popup.bufnr })
if opts == nil then
opts = {}
end
vim.keymap.set("n", M.settings.popup.exit, function()
exit(popup, opts.cb)
end, { buffer = popup.bufnr })
if action ~= nil then
vim.keymap.set('n', M.settings.popup.perform_action, function()
vim.keymap.set("n", M.settings.popup.perform_action, function()
local text = u.get_buffer_text(popup.bufnr)
if opts.action_before_close then
action(text, popup.bufnr)
@@ -131,7 +139,7 @@ M.set_popup_keymaps = function(popup, action, linewise_action, opts)
end
if linewise_action ~= nil then
vim.keymap.set('n', M.settings.popup.perform_linewise_action, function()
vim.keymap.set("n", M.settings.popup.perform_linewise_action, function()
local bufnr = vim.api.nvim_get_current_buf()
local linnr = vim.api.nvim_win_get_cursor(0)[1]
local text = u.get_line_content(bufnr, linnr)
@@ -145,12 +153,10 @@ end
-- before calling an action. They are used to set global state that's required
-- for each of the actions to occur. This is necessary because some Gitlab behaviors (like
-- adding a reviewer) requires some initial state.
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 }
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 },
}
return M

View File

@@ -6,7 +6,7 @@ M.get_current_line_number = function()
end
M.has_reviewer = function(reviewer)
local has_reviewer = false
local has_reviewer
if reviewer == "diffview" then
has_reviewer = vim.fn.exists(":DiffviewOpen") ~= 0
else
@@ -253,10 +253,7 @@ end
M.get_line_content = function(bufnr, start)
local current_buffer = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(bufnr ~= nil and bufnr or current_buffer, start - 1, start, false)
for _, line in ipairs(lines) do
return line
end
return lines[1]
end
M.get_win_from_buf = function(bufnr)
@@ -386,7 +383,7 @@ M.get_lines_from_hunks = function(hunks, target_line, is_new)
new_line = target_line,
in_hunk = false,
}
-- target line is within the current hunk
-- target line is within the current hunk
elseif hunk.new_line <= target_line and target_line <= (hunk.new_line + hunk.new_range) then
-- this is interesting magic of gitlab calculation
return {
@@ -394,7 +391,7 @@ M.get_lines_from_hunks = function(hunks, target_line, is_new)
new_line = target_line,
in_hunk = true,
}
-- target line is after the current hunk
-- target line is after the current hunk
else
current_new_line = hunk.new_line + hunk.new_range
current_old_line = hunk.old_line + hunk.old_range
@@ -415,14 +412,14 @@ M.get_lines_from_hunks = function(hunks, target_line, is_new)
new_line = current_new_line + (target_line - current_old_line),
in_hunk = false,
}
-- target line is within the current hunk
-- target line is within the current hunk
elseif hunk.old_line <= target_line and target_line <= (hunk.old_line + hunk.old_range) then
return {
old_line = target_line,
new_line = hunk.new_line,
in_hunk = true,
}
-- target line is after the current hunk
-- target line is after the current hunk
else
current_new_line = hunk.new_line + hunk.new_range
current_old_line = hunk.old_line + hunk.old_range