Feat: Allow Creation of MRs for Forked Target (#303)

feat: Adds MR creation for project forks
This commit is contained in:
Harrison (Harry) Cramer
2024-06-10 15:04:31 -04:00
committed by GitHub
parent 816b87cf91
commit 53d5647380
7 changed files with 123 additions and 185 deletions

View File

@@ -14,6 +14,7 @@ local miscellaneous = require("gitlab.actions.miscellaneous")
---@field target? string
---@field title? string
---@field description? string
---@field forked_project_id number?
---@field template_file? string
---@field delete_branch boolean?
---@field squash boolean?
@@ -29,6 +30,8 @@ local M = {
target = "",
title = "",
description = "",
forked_project_id = state.settings.create_mr.fork.enabled and state.settings.create_mr.fork.forked_project_id
or nil,
},
}
@@ -37,6 +40,7 @@ M.reset_state = function()
M.mr.title = ""
M.mr.target = ""
M.mr.description = ""
M.mr.forked_project_id = nil
end
---1. If the user has already begun writing an MR, prompt them to
@@ -157,8 +161,12 @@ M.add_title = function(mr)
prompt = "",
default_value = "",
on_close = function() end,
on_submit = function(_value)
M.open_confirmation_popup(mr)
on_submit = function()
if state.settings.create_mr.fork.enabled and state.settings.create_mr.fork.forked_project_id == nil then
M.open_fork_popup(mr)
else
M.open_confirmation_popup(mr)
end
end,
on_change = function(value)
mr.title = value
@@ -167,6 +175,33 @@ M.add_title = function(mr)
input:mount()
end
---Sets the ID of the base project when working from a fork
---@param mr Mr
M.open_fork_popup = function(mr)
local input = Input({
position = "50%",
relative = "editor",
size = state.settings.create_mr.title_input.width,
border = {
style = state.settings.create_mr.title_input.border,
text = {
top = "Forked Project ID",
},
},
}, {
prompt = "",
default_value = "",
on_close = function() end,
on_submit = function()
M.open_confirmation_popup(mr)
end,
on_change = function(value)
mr.forked_project_id = tonumber(value)
end,
})
input:mount()
end
---5. Show the final popup.
---The function will render a popup containing the MR title and MR description,
---target branch, and the "delete_branch" and "squash" options. All fields are editable.
@@ -179,11 +214,13 @@ M.open_confirmation_popup = function(mr)
return
end
local layout, title_popup, description_popup, target_popup, delete_branch_popup, squash_popup = M.create_layout()
local layout, title_popup, description_popup, target_popup, delete_branch_popup, squash_popup, forked_project_id_popup =
M.create_layout()
local popups = {
title_popup,
description_popup,
forked_project_id_popup,
delete_branch_popup,
squash_popup,
target_popup,
@@ -199,12 +236,14 @@ M.open_confirmation_popup = function(mr)
local target = vim.fn.trim(u.get_buffer_text(M.target_bufnr))
local delete_branch = u.string_to_bool(u.get_buffer_text(M.delete_branch_bufnr))
local squash = u.string_to_bool(u.get_buffer_text(M.squash_bufnr))
local forked_project_id = tonumber(u.get_buffer_text(M.forked_project_id_bufnr))
M.mr = {
title = title,
description = description,
target = target,
delete_branch = delete_branch,
squash = squash,
forked_project_id = forked_project_id,
}
layout:unmount()
M.layout_visible = false
@@ -220,6 +259,10 @@ M.open_confirmation_popup = function(mr)
vim.api.nvim_buf_set_lines(M.target_bufnr, 0, -1, false, { mr.target })
vim.api.nvim_buf_set_lines(M.delete_branch_bufnr, 0, -1, false, { u.bool_to_string(delete_branch) })
vim.api.nvim_buf_set_lines(M.squash_bufnr, 0, -1, false, { u.bool_to_string(squash) })
if state.settings.create_mr.fork.enabled then
local forked_id = state.settings.create_mr.fork.forked_project_id or mr.forked_project_id
vim.api.nvim_buf_set_lines(M.forked_project_id_bufnr, 0, -1, false, { tostring(forked_id) })
end
u.switch_can_edit_buf(M.delete_branch_bufnr, false)
u.switch_can_edit_buf(M.squash_bufnr, false)
@@ -236,6 +279,7 @@ M.open_confirmation_popup = function(mr)
state.set_popup_keymaps(target_popup, M.create_mr, M.select_new_target, popup_opts)
state.set_popup_keymaps(delete_branch_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts)
state.set_popup_keymaps(squash_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts)
state.set_popup_keymaps(forked_project_id_popup, M.create_mr, nil, popup_opts)
miscellaneous.set_cycle_popups_keymaps(popups)
vim.api.nvim_set_current_buf(M.description_bufnr)
@@ -261,6 +305,7 @@ M.create_mr = function()
local target = u.get_buffer_text(M.target_bufnr):gsub("\n", " ")
local delete_branch = u.string_to_bool(u.get_buffer_text(M.delete_branch_bufnr))
local squash = u.string_to_bool(u.get_buffer_text(M.squash_bufnr))
local forked_project_id = tonumber(u.get_buffer_text(M.forked_project_id_bufnr))
local body = {
title = title,
@@ -268,8 +313,11 @@ M.create_mr = function()
target_branch = target,
delete_branch = delete_branch,
squash = squash,
forked_project_id = forked_project_id,
}
vim.print(body)
job.run_job("/create_mr", "POST", body, function(data)
u.notify(data.message, vim.log.levels.INFO)
M.reset_state()
@@ -291,18 +339,23 @@ M.create_layout = function()
local squash_title = vim.o.columns > 110 and "Squash commits" or "Squash"
local squash_popup = Popup(u.create_box_popup_state(squash_title, false))
M.squash_bufnr = squash_popup.bufnr
local forked_project_id_popup = Popup(u.create_box_popup_state("Forked Project ID", false))
M.forked_project_id_bufnr = forked_project_id_popup.bufnr
local internal_layout
internal_layout = Layout.Box({
local boxes = {}
if state.settings.create_mr.fork.enabled then
table.insert(boxes, Layout.Box(forked_project_id_popup, { size = { width = 20 } }))
end
table.insert(boxes, Layout.Box(delete_branch_popup, { size = { width = #delete_title + 4 } }))
table.insert(boxes, Layout.Box(squash_popup, { size = { width = #squash_title + 4 } }))
table.insert(boxes, Layout.Box(target_branch_popup, { grow = 1 }))
local internal_layout = Layout.Box({
Layout.Box({
Layout.Box(title_popup, { grow = 1 }),
}, { size = 3 }),
Layout.Box(description_popup, { grow = 1 }),
Layout.Box({
Layout.Box(delete_branch_popup, { size = { width = #delete_title + 4 } }),
Layout.Box(squash_popup, { size = { width = #squash_title + 4 } }),
Layout.Box(target_branch_popup, { grow = 1 }),
}, { size = 3 }),
Layout.Box(boxes, { size = 3 }),
}, { dir = "col" })
local layout = Layout({
@@ -316,7 +369,13 @@ M.create_layout = function()
layout:mount()
return layout, title_popup, description_popup, target_branch_popup, delete_branch_popup, squash_popup
return layout,
title_popup,
description_popup,
target_branch_popup,
delete_branch_popup,
squash_popup,
forked_project_id_popup
end
return M

View File

@@ -15,11 +15,14 @@ M.clear_diagnostics = function()
end
-- Display options for the diagnostic
local display_opts = {
virtual_text = state.settings.discussion_signs.virtual_text,
severity_sort = true,
underline = false,
}
local create_display_opts = function()
return {
virtual_text = state.settings.discussion_signs.virtual_text,
severity_sort = true,
underline = false,
signs = state.settings.discussion_signs.use_diagnostic_signs,
}
end
---Takes some range information and data about a discussion
---and creates a diagnostic to be placed in the reviewer
@@ -121,10 +124,10 @@ M.refresh_diagnostics = function()
end
local new_diagnostics = M.parse_new_diagnostics(filtered_discussions)
set_diagnostics_in_new_sha(diagnostics_namespace, new_diagnostics, display_opts)
set_diagnostics_in_new_sha(diagnostics_namespace, new_diagnostics, create_display_opts())
local old_diagnostics = M.parse_old_diagnostics(filtered_discussions)
set_diagnostics_in_old_sha(diagnostics_namespace, old_diagnostics, display_opts)
set_diagnostics_in_old_sha(diagnostics_namespace, old_diagnostics, create_display_opts())
end)
if not ok then

View File

@@ -2,7 +2,6 @@ local u = require("gitlab.utils")
local state = require("gitlab.state")
local List = require("gitlab.utils.list")
local discussion_sign_name = require("gitlab.indicators.diagnostics").discussion_sign_name
local namespace = require("gitlab.indicators.diagnostics").diagnostics_namespace
local M = {}
M.clear_signs = function()
@@ -32,9 +31,8 @@ M.set_signs = function(diagnostics, bufnr)
for _, diagnostic in ipairs(diagnostics) do
---@type SignTable[]
local existing_signs =
vim.fn.sign_getplaced(vim.api.nvim_get_current_buf(), { group = "gitlab_discussion" })[1].signs
vim.fn.sign_getplaced(vim.api.nvim_get_current_buf(), { group = discussion_sign_name })[1].signs
local sign_id = string.format("%s__%d", namespace, diagnostic.lnum)
if diagnostic.end_lnum then
local linenr = diagnostic.lnum + 1
while linenr <= diagnostic.end_lnum do
@@ -44,7 +42,7 @@ M.set_signs = function(diagnostics, bufnr)
end)
if conflicting_comment_sign == nil then
vim.fn.sign_place(
sign_id,
linenr,
discussion_sign_name,
"DiagnosticSign" .. M.severity .. gitlab_range,
bufnr,
@@ -55,7 +53,7 @@ M.set_signs = function(diagnostics, bufnr)
end
vim.fn.sign_place(
sign_id,
diagnostic.lnum + 1,
discussion_sign_name,
"DiagnosticSign" .. M.severity .. gitlab_comment,
bufnr,

View File

@@ -116,6 +116,10 @@ M.settings = {
template_file = nil,
delete_branch = false,
squash = false,
fork = {
enabled = false,
forked_project_id = nil,
},
title_input = {
width = 40,
border = "rounded",
@@ -149,6 +153,7 @@ M.settings = {
skip_resolved_discussion = false,
severity = vim.diagnostic.severity.INFO,
virtual_text = false,
use_diagnostic_signs = true,
icons = {
comment = "→|",
range = " |",