• removes the <esc> keybinding for popups which was causing folks to lose their changes
• deprecates the backup register.
• updates go-gitlab to latest in order to get "drafts" functionality
• fixes issues with labels not deleting correctly
• creates a new data() function to get data from the plugin directly, see :h gitlab.nvim.data
• fixes issues with line values not being computed directly, blocking jumps to/from discussion tree

This is a #MINOR release.
This commit is contained in:
Harrison (Harry) Cramer
2024-04-07 21:45:19 -04:00
committed by GitHub
parent 12c4acb297
commit 36f512cd6d
19 changed files with 299 additions and 177 deletions

View File

@@ -196,10 +196,6 @@ M.add_title = function(mr)
mr.title = value
end,
})
input:map("n", "<Esc>", function()
input:unmount()
end, { noremap = true })
input:mount()
end

View File

@@ -0,0 +1,45 @@
local u = require("gitlab.utils")
local async = require("gitlab.async")
local state = require("gitlab.state")
local M = {}
local user = state.dependencies.user
local info = state.dependencies.info
local labels = state.dependencies.labels
local project_members = state.dependencies.project_members
local revisions = state.dependencies.revisions
local latest_pipeline = state.dependencies.latest_pipeline
M.data = function(resources, cb)
if type(resources) ~= "table" or type(cb) ~= "function" then
u.notify("The data function must be passed a resources table and a callback function", vim.log.levels.ERROR)
return
end
local all_resources = {
info = info,
user = user,
labels = labels,
project_members = project_members,
revisions = revisions,
pipeline = latest_pipeline,
}
local api_calls = {}
for _, resource in ipairs(resources) do
local api_call = all_resources[resource.type]
table.insert(api_calls, u.merge(api_call, { refresh = resource.refresh }))
end
-- TODO: Build an async "parallel" that fetches the resources
-- in parallel where possible to speed up this API
return async.sequence(api_calls, function()
local data = {}
for k, v in pairs(all_resources) do
data[k] = state[v.state]
end
cb(data)
end)()
end
return M

View File

@@ -398,10 +398,6 @@ local function get_new_line(node)
return node.new_line
end
if range.start.new_line ~= nil then
return range.start.new_line
end
local _, start_new_line = common.parse_line_code(range.start.line_code)
return start_new_line
end
@@ -417,10 +413,6 @@ local function get_old_line(node)
return node.old_line
end
if range.start.old_line ~= nil then
return range.start.old_line
end
local start_old_line, _ = common.parse_line_code(range.start.line_code)
return start_old_line
end

View File

@@ -15,18 +15,11 @@ M.delete_label = function()
end
local refresh_label_state = function(labels)
state.INFO.labels = List.new(labels):reduce(function(agg, label)
return agg .. "," .. label
end, "")
state.INFO.labels = labels
end
local get_current_labels = function()
local label_string = state.INFO.labels
local current_labels = {}
for value in label_string:gmatch("[^,]+") do
table.insert(current_labels, value)
end
return current_labels
return state.INFO.labels
end
local get_all_labels = function()
@@ -45,16 +38,11 @@ M.add_popup = function(type)
if not choice then
return
end
local label_string = state.INFO.labels
local new_labels = {}
for value in label_string:gmatch("[^,]+") do
table.insert(new_labels, value)
end
table.insert(new_labels, choice)
local body = { labels = new_labels }
table.insert(current_labels, choice)
local body = { labels = current_labels }
job.run_job("/mr/" .. type, "PUT", body, function(data)
u.notify(data.message, vim.log.levels.INFO)
refresh_label_state(data.labels)
end)
end)

View File

@@ -7,12 +7,12 @@ local job = require("gitlab.job")
local u = require("gitlab.utils")
local M = {
pipeline_jobs = nil,
latest_pipeline = nil,
pipeline_popup = nil,
}
local function get_pipeline()
local pipeline = state.INFO.head_pipeline or state.INFO.pipeline
local function get_latest_pipeline()
local pipeline = state.PIPELINE and state.PIPELINE.latest_pipeline
if type(pipeline) ~= "table" or (type(pipeline) == "table" and u.table_size(pipeline) == 0) then
u.notify("Pipeline not found", vim.log.levels.WARN)
return
@@ -20,97 +20,93 @@ local function get_pipeline()
return pipeline
end
M.get_pipeline_status = function()
local pipeline = get_pipeline()
if pipeline == nil then
return nil
local function get_pipeline_jobs()
M.latest_pipeline = get_latest_pipeline()
if not M.latest_pipeline then
return
end
return string.format("%s (%s)", state.settings.pipeline[pipeline.status], pipeline.status)
return u.reverse(type(state.PIPELINE.jobs) == "table" and state.PIPELINE.jobs or {})
end
-- The function will render the Pipeline state in a popup
M.open = function()
local pipeline = get_pipeline()
if not pipeline then
M.pipeline_jobs = get_pipeline_jobs()
M.latest_pipeline = get_latest_pipeline()
if M.latest_pipeline == nil then
return
end
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
local width = string.len(M.latest_pipeline.web_url) + 10
local height = 6 + #M.pipeline_jobs + 3
local width = string.len(pipeline.web_url) + 10
local height = 6 + #pipeline_jobs + 3
local pipeline_popup =
Popup(u.create_popup_state("Loading Pipeline...", state.settings.popup.pipeline, width, height, 60))
M.pipeline_popup = pipeline_popup
pipeline_popup:mount()
local pipeline_popup =
Popup(u.create_popup_state("Loading Pipeline...", state.settings.popup.pipeline, width, height, 60))
M.pipeline_popup = pipeline_popup
pipeline_popup:mount()
local bufnr = vim.api.nvim_get_current_buf()
vim.opt_local.wrap = false
local bufnr = vim.api.nvim_get_current_buf()
vim.opt_local.wrap = false
local lines = {}
local lines = {}
u.switch_can_edit_buf(bufnr, true)
table.insert(lines, "Status: " .. M.get_pipeline_status(false))
table.insert(lines, "")
table.insert(lines, string.format("Last Run: %s", u.time_since(M.latest_pipeline.created_at)))
table.insert(lines, string.format("Url: %s", M.latest_pipeline.web_url))
table.insert(lines, string.format("Triggered By: %s", M.latest_pipeline.source))
u.switch_can_edit_buf(bufnr, true)
table.insert(lines, "Status: " .. M.get_pipeline_status())
table.insert(lines, "")
table.insert(lines, string.format("Last Run: %s", u.time_since(pipeline.created_at)))
table.insert(lines, string.format("Url: %s", pipeline.web_url))
table.insert(lines, string.format("Triggered By: %s", pipeline.source))
table.insert(lines, "")
table.insert(lines, "Jobs:")
table.insert(lines, "")
table.insert(lines, "Jobs:")
local longest_title = u.get_longest_string(u.map(M.pipeline_jobs, function(v)
return v.name
end))
local longest_title = u.get_longest_string(u.map(pipeline_jobs, function(v)
return v.name
end))
local function row_offset(name)
local offset = longest_title - string.len(name)
local res = string.rep(" ", offset + 5)
return res
end
local function row_offset(name)
local offset = longest_title - string.len(name)
local res = string.rep(" ", offset + 5)
return res
for _, pipeline_job in ipairs(M.pipeline_jobs) do
local offset = row_offset(pipeline_job.name)
local row = string.format(
"%s%s %s (%s)",
pipeline_job.name,
offset,
state.settings.pipeline[pipeline_job.status] or "*",
pipeline_job.status or ""
)
table.insert(lines, row)
end
vim.schedule(function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
M.color_status(M.latest_pipeline.status, bufnr, lines[1], 1)
for i, pipeline_job in ipairs(M.pipeline_jobs) do
M.color_status(pipeline_job.status, bufnr, lines[7 + i], 7 + i)
end
for _, pipeline_job in ipairs(pipeline_jobs) do
local offset = row_offset(pipeline_job.name)
local row = string.format(
"%s%s %s (%s)",
pipeline_job.name,
offset,
state.settings.pipeline[pipeline_job.status] or "*",
pipeline_job.status or ""
)
table.insert(lines, row)
end
vim.schedule(function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
M.color_status(pipeline.status, bufnr, lines[1], 1)
for i, pipeline_job in ipairs(pipeline_jobs) do
M.color_status(pipeline_job.status, bufnr, lines[7 + i], 7 + i)
end
pipeline_popup.border:set_text("top", "Pipeline Status", "center")
state.set_popup_keymaps(pipeline_popup, M.retrigger, M.see_logs)
u.switch_can_edit_buf(bufnr, false)
end)
pipeline_popup.border:set_text("top", "Pipeline Status", "center")
state.set_popup_keymaps(pipeline_popup, M.retrigger, M.see_logs)
u.switch_can_edit_buf(bufnr, false)
end)
end
M.retrigger = function()
local pipeline = get_pipeline()
if not pipeline then
M.latest_pipeline = get_latest_pipeline()
if not M.latest_pipeline then
return
end
if pipeline.status ~= "failed" then
if M.latest_pipeline.status ~= "failed" then
u.notify("Pipeline is not in a failed state!", vim.log.levels.WARN)
return
end
job.run_job("/pipeline/" .. pipeline.id, "POST", nil, function()
job.run_job("/pipeline/" .. M.latest_pipeline.id, "POST", nil, function()
u.notify("Pipeline re-triggered!", vim.log.levels.INFO)
end)
end
@@ -173,6 +169,42 @@ M.see_logs = function()
end)
end
---Returns the user-defined symbol representing the status
---of the current pipeline. Takes an optional argument to
---colorize the pipeline icon.
---@param wrap_with_color boolean
---@return string
M.get_pipeline_icon = function(wrap_with_color)
M.latest_pipeline = get_latest_pipeline()
if not M.latest_pipeline then
return ""
end
local symbol = state.settings.pipeline[M.latest_pipeline.status]
if not wrap_with_color then
return symbol
end
if M.latest_pipeline.status == "failed" then
return "%#DiagnosticError#" .. symbol
end
if M.latest_pipeline.status == "success" then
return "%#DiagnosticOk#" .. symbol
end
return "%#DiagnosticWarn#" .. symbol
end
---Returns the status of the latest pipeline and the symbol
--representing the status of the current pipeline. Takes an optional argument to
---colorize the pipeline icon.
---@param wrap_with_color boolean
---@return string
M.get_pipeline_status = function(wrap_with_color)
M.latest_pipeline = get_latest_pipeline()
if not M.latest_pipeline then
return ""
end
return string.format("%s (%s)", M.get_pipeline_icon(wrap_with_color), M.latest_pipeline.status)
end
M.color_status = function(status, bufnr, status_line, linnr)
local ns_id = vim.api.nvim_create_namespace("GitlabNamespace")
vim.cmd(string.format("highlight default StatusHighlight guifg=%s", state.settings.pipeline[status]))

View File

@@ -8,7 +8,6 @@ local u = require("gitlab.utils")
local List = require("gitlab.utils.list")
local state = require("gitlab.state")
local miscellaneous = require("gitlab.actions.miscellaneous")
local pipeline = require("gitlab.actions.pipeline")
local M = {
layout_visible = false,
@@ -134,12 +133,16 @@ M.build_info_lines = function()
assignees = { title = "Assignees", content = u.make_readable_list(info.assignees, "name") },
reviewers = { title = "Reviewers", content = u.make_readable_list(info.reviewers, "name") },
branch = { title = "Branch", content = info.source_branch },
labels = { title = "Labels", content = u.make_comma_separated_readable(info.labels) },
labels = { title = "Labels", content = table.concat(info.labels, ", ") },
target_branch = { title = "Target Branch", content = state.INFO.target_branch },
pipeline = {
title = "Pipeline Status",
content = function()
return pipeline.get_pipeline_status()
local pipeline = state.INFO.pipeline
if type(pipeline) ~= "table" or (type(pipeline) == "table" and u.table_size(pipeline) == 0) then
return ""
end
return pipeline.status
end,
},
}

View File

@@ -8,6 +8,7 @@ local reviewer = require("gitlab.reviewer")
local discussions = require("gitlab.actions.discussions")
local merge = require("gitlab.actions.merge")
local summary = require("gitlab.actions.summary")
local data = require("gitlab.actions.data")
local assignees_and_reviewers = require("gitlab.actions.assignees_and_reviewers")
local comment = require("gitlab.actions.comment")
local pipeline = require("gitlab.actions.pipeline")
@@ -19,6 +20,7 @@ local user = state.dependencies.user
local info = state.dependencies.info
local labels_dep = state.dependencies.labels
local project_members = state.dependencies.project_members
local latest_pipeline = state.dependencies.latest_pipeline
local revisions = state.dependencies.revisions
return {
@@ -34,7 +36,10 @@ return {
emoji.init() -- Read in emojis for lookup purposes
end,
-- Global Actions 🌎
summary = async.sequence({ u.merge(info, { refresh = true }), labels_dep }, summary.summary),
summary = async.sequence({
u.merge(info, { refresh = true }),
labels_dep,
}, 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),
@@ -55,7 +60,7 @@ return {
close_review = function()
reviewer.close()
end,
pipeline = async.sequence({ info }, pipeline.open),
pipeline = async.sequence({ latest_pipeline }, pipeline.open),
merge = async.sequence({ u.merge(info, { refresh = true }) }, merge.merge),
-- Discussion Tree Actions 🌴
toggle_discussions = async.sequence({ info, user }, discussions.toggle),
@@ -65,6 +70,7 @@ return {
reply = async.sequence({ info }, discussions.reply),
-- Other functions 🤷
state = state,
data = data.data,
print_settings = state.print_settings,
open_in_browser = async.sequence({ info }, function()
if state.INFO.web_url == nil then

View File

@@ -26,7 +26,6 @@ M.settings = {
attachment_dir = "",
help = "g?",
popup = {
exit = "<Esc>",
perform_action = "<leader>s",
perform_linewise_action = "<leader>l",
width = "40%",
@@ -40,7 +39,6 @@ M.settings = {
help = nil,
pipeline = nil,
squash_message = nil,
backup_register = nil,
},
discussion_tree = {
auto_open = true,
@@ -270,10 +268,6 @@ 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)
end, { buffer = popup.bufnr, desc = "Exit popup" })
if action ~= "Help" then -- Don't show help on the help popup
vim.keymap.set("n", M.settings.help, function()
local help = require("gitlab.actions.help")
@@ -283,9 +277,6 @@ M.set_popup_keymaps = function(popup, action, linewise_action, opts)
if action ~= nil then
vim.keymap.set("n", M.settings.popup.perform_action, function()
local text = u.get_buffer_text(popup.bufnr)
if M.settings.popup.backup_register ~= nil then
vim.cmd("0,$yank " .. M.settings.popup.backup_register)
end
if opts.action_before_close then
action(text, popup.bufnr)
exit(popup, opts)
@@ -304,6 +295,13 @@ M.set_popup_keymaps = function(popup, action, linewise_action, opts)
linewise_action(text)
end, { buffer = popup.bufnr, desc = "Perform linewise action" })
end
vim.api.nvim_create_autocmd("BufUnload", {
buffer = popup.bufnr,
callback = function()
exit(popup, opts)
end,
})
end
-- Dependencies
@@ -314,6 +312,7 @@ end
M.dependencies = {
user = { endpoint = "/users/me", key = "user", state = "USER", refresh = false },
info = { endpoint = "/mr/info", key = "info", state = "INFO", refresh = false },
latest_pipeline = { endpoint = "/pipeline", key = "latest_pipeline", state = "PIPELINE", refresh = true },
labels = { endpoint = "/mr/label", key = "labels", state = "LABELS", refresh = false },
revisions = { endpoint = "/mr/revisions", key = "Revisions", state = "MR_REVISIONS", refresh = false },
project_members = {