Add/Remove Reviewers and Assignees (#38)

Adds APIs for the ability to add or remove reviewers and assignees to a merge request. The eligible reviewers and assignees are pulled from the current members of a project.
This commit is contained in:
Harrison (Harry) Cramer
2023-08-16 21:42:53 -04:00
committed by GitHub
parent 6274746d4b
commit 844e093294
10 changed files with 368 additions and 44 deletions

View File

@@ -0,0 +1,71 @@
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')
end
M.delete_assignee = function()
M.delete_popup('assignee')
end
M.add_reviewer = function()
M.add_popup('reviewer')
end
M.delete_reviewer = function()
M.delete_popup('reviewer')
end
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',
format_item = function(user)
return user.username .. " (" .. user.name .. ")"
end
}, function(choice)
if not choice then return end
local current_ids = u.extract(current, 'id')
table.insert(current_ids, choice.id)
local json = vim.json.encode({ ids = current_ids })
job.run_job("mr/" .. type, "PUT", json, function(data)
vim.notify(data.message, vim.log.levels.INFO)
state.INFO[plural] = data[plural]
end)
end)
end
M.delete_popup = function(type)
local plural = type .. 's'
local current = state.INFO[plural]
vim.ui.select(current, {
prompt = 'Choose ' .. type .. ' to delete',
format_item = function(user)
return user.username .. " (" .. user.name .. ")"
end
}, function(choice)
if not choice then return end
local ids = u.extract(M.filter_eligible(current, { choice }), 'id')
local json = vim.json.encode({ ids = ids })
job.run_job("mr/" .. type, "PUT", json, function(data)
vim.notify(data.message, vim.log.levels.INFO)
state.INFO[plural] = data[plural]
end)
end)
end
M.filter_eligible = function(current, to_remove)
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
end
return res
end
return M

View File

@@ -1,13 +1,18 @@
local state = require("gitlab.state")
local discussions = require("gitlab.discussions")
local summary = require("gitlab.summary")
local keymaps = require("gitlab.keymaps")
local comment = require("gitlab.comment")
local job = require("gitlab.job")
local u = require("gitlab.utils")
local state = require("gitlab.state")
local discussions = require("gitlab.discussions")
local summary = require("gitlab.summary")
local assignees_and_reviewers = require("gitlab.assignees_and_reviewers")
local keymaps = require("gitlab.keymaps")
local comment = require("gitlab.comment")
local job = require("gitlab.job")
local u = require("gitlab.utils")
-- Ensures the plugin's state is initialized prior to running other calls. This state contains the basic information about the current merge request, like description, author, etc
local ensureState = function(callback)
-- Function names prefixed with "ensure" will ensure the plugin's state
-- is initialized prior to running other calls. These functions run
-- API calls if the state isn't initialized, which will set state containing
-- information that's necessary for other API calls, like description,
-- author, reviewer, etc.
local ensureState = function(callback)
return function()
if type(state.INFO) ~= "table" then
job.run_job("info", "GET", nil, function(data)
@@ -20,20 +25,37 @@ local ensureState = function(callback)
end
end
local ensureProjectMembers = function(callback)
return function()
if type(state.PROJECT_MEMBERS) ~= "table" then
job.run_job("members", "GET", nil, function(data)
state.PROJECT_MEMBERS = data.ProjectMembers
callback()
end)
else
callback()
end
end
end
-- Root Module Scope
local M = {}
M.summary = ensureState(summary.summary)
M.approve = ensureState(job.approve)
M.revoke = ensureState(job.revoke)
M.create_comment = ensureState(comment.create_comment)
M.list_discussions = ensureState(discussions.list_discussions)
M.edit_comment = ensureState(comment.edit_comment)
M.delete_comment = ensureState(comment.delete_comment)
M.reply = ensureState(discussions.reply)
M.state = state
local M = {}
M.summary = ensureState(summary.summary)
M.approve = ensureState(job.approve)
M.revoke = ensureState(job.revoke)
M.create_comment = ensureState(comment.create_comment)
M.list_discussions = ensureState(discussions.list_discussions)
M.edit_comment = ensureState(comment.edit_comment)
M.delete_comment = ensureState(comment.delete_comment)
M.add_reviewer = ensureProjectMembers(ensureState(assignees_and_reviewers.add_reviewer))
M.delete_reviewer = ensureProjectMembers(ensureState(assignees_and_reviewers.delete_reviewer))
M.add_assignee = ensureProjectMembers(ensureState(assignees_and_reviewers.add_assignee))
M.delete_assignee = ensureProjectMembers(ensureState(assignees_and_reviewers.delete_assignee))
M.reply = ensureState(discussions.reply)
M.state = state
-- Builds the binary (if not built); starts the Go server; sets the keymaps
M.setup = function(args)
M.setup = function(args)
if args == nil then args = {} end
local file_path = u.current_file_path()
local parent_dir = vim.fn.fnamemodify(file_path, ":h:h:h:h")
@@ -75,7 +97,7 @@ M.setup = function(args)
end
-- Builds the Go binary
M.build = function()
M.build = function()
local command = string.format("cd %s && make", state.BIN_PATH)
local installCode = os.execute(command .. "> /dev/null")
if installCode ~= 0 then
@@ -87,7 +109,7 @@ end
-- Initializes state for the project based on the arguments
-- provided in the `.gitlab.nvim` file per project, and the args provided in the setup function
M.setPluginConfiguration = function(args)
M.setPluginConfiguration = function(args)
local config_file_path = vim.fn.getcwd() .. "/.gitlab.nvim"
local config_file_content = u.read_file(config_file_path)
if config_file_content == nil then

View File

@@ -27,7 +27,7 @@ end
M.edit_description = function(text)
local jsonTable = { description = text }
local json = vim.json.encode(jsonTable)
job.run_job("mr", "PUT", json, function(data)
job.run_job("mr/description", "PUT", json, function(data)
vim.notify(data.message, vim.log.levels.INFO)
state.INFO.description = data.mr.description
end)

View File

@@ -244,15 +244,6 @@ local current_file_path = function()
return vim.fn.fnamemodify(path, ':p')
end
-- Function to join two tables
local function join_tables(table1, table2)
for _, value in ipairs(table2) do
table.insert(table1, value)
end
return table1
end
local random = math.random
local function uuid()
local template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
@@ -266,6 +257,36 @@ local attach_uuid = function(str)
return { text = str, id = uuid() }
end
local join_tables = function(table1, table2)
for _, value in ipairs(table2) do
table.insert(table1, value)
end
return table1
end
local contains = function(array, search_value)
for _, value in ipairs(array) do
if value == search_value then
return true
end
end
return false
end
local extract = function(t, property)
local resultTable = {}
for _, value in ipairs(t) do
if value[property] then
table.insert(resultTable, value[property])
end
end
return resultTable
end
M.extract = extract
M.contains = contains
M.attach_uuid = attach_uuid
M.join_tables = join_tables
M.get_relative_file_path = get_relative_file_path