Feat: Upload Files (#59)

This MR adds the ability to add files to comments, notes, replys, and MR descriptions via a picker.

The file will get uploaded to Gitlab and the filepath will be automatically added into the current popup buffer at the current line. You can then save the changes with the normal save functionality.
This commit is contained in:
Harrison (Harry) Cramer
2023-09-08 10:02:01 -04:00
committed by GitHub
parent 45329f4d69
commit 4e473dab7e
11 changed files with 166 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ local state = require("gitlab.state")
local job = require("gitlab.job")
local u = require("gitlab.utils")
local discussions = require("gitlab.actions.discussions")
local miscellaneous = require("gitlab.actions.miscellaneous")
local reviewer = require("gitlab.reviewer")
local M = {}
@@ -17,14 +18,14 @@ M.create_comment = function()
comment_popup:mount()
state.set_popup_keymaps(comment_popup, function(text)
M.confirm_create_comment(text)
end)
end, miscellaneous.attach_file)
end
M.create_note = function()
note_popup:mount()
state.set_popup_keymaps(note_popup, function(text)
M.confirm_create_comment(text, true)
end)
end, miscellaneous.attach_file)
end
-- This function (settings.popup.perform_action) will send the comment to the Go server

View File

@@ -10,6 +10,7 @@ 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%"))
@@ -72,7 +73,7 @@ M.reply = function(tree)
local discussion_node = M.get_root_node(tree, node)
local id = tostring(discussion_node.id)
reply_popup:mount()
state.set_popup_keymaps(reply_popup, M.send_reply(tree, id))
state.set_popup_keymaps(reply_popup, M.send_reply(tree, id), miscellaneous.attach_file)
end
-- This function will send the reply to the Go API

View File

@@ -1,5 +1,7 @@
local state = require("gitlab.state")
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
@@ -16,4 +18,33 @@ M.open_in_browser = function()
end
end
M.attach_file = function()
local attachment_dir = state.settings.attachment_dir
if not attachment_dir or attachment_dir == '' then
vim.notify("Must provide valid attachment_dir in plugin setup", vim.log.levels.ERROR)
return
end
local files = u.list_files_in_folder(attachment_dir)
if files == nil then
vim.notify(string.format("Could not list files in %s", attachment_dir), vim.log.levels.ERROR)
return
end
vim.ui.select(files, {
prompt = 'Choose attachment',
}, function(choice)
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)
local markdown = data.markdown
local current_line = u.get_current_line_number()
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(bufnr, current_line - 1, current_line, false, { markdown })
end)
end)
end
return M

View File

@@ -4,6 +4,7 @@
local Popup = require("nui.popup")
local job = require("gitlab.job")
local state = require("gitlab.state")
local miscellaneous = require("gitlab.actions.miscellaneous")
local u = require("gitlab.utils")
local M = {}
@@ -23,7 +24,7 @@ M.summary = function()
vim.schedule(function()
vim.api.nvim_buf_set_lines(currentBuffer, 0, -1, false, lines)
descriptionPopup.border:set_text("top", title, "center")
state.set_popup_keymaps(descriptionPopup, M.edit_description)
state.set_popup_keymaps(descriptionPopup, M.edit_description, miscellaneous.attach_file)
end)
end

View File

@@ -59,7 +59,6 @@ M.build = function(override)
local command = string.format(cmd, state.settings.bin_path)
local null = u.is_windows() and " >NUL" or " > /dev/null"
print(command .. null)
local installCode = os.execute(command .. null)
if installCode ~= 0 then
vim.notify("Could not install gitlab.nvim!", vim.log.levels.ERROR)

View File

@@ -11,6 +11,7 @@ M.settings = {
port = 21036,
log_path = (vim.fn.stdpath("cache") .. "/gitlab.nvim.log"),
reviewer = "delta",
attachment_dir = '',
popup = {
exit = "<Esc>",
perform_action = "<leader>s",

View File

@@ -281,6 +281,37 @@ M.switch_can_edit_buf = function(buf, bool)
vim.api.nvim_buf_set_option(buf, "readonly", not bool)
end
M.list_files_in_folder = function(folder_path)
if vim.fn.isdirectory(folder_path) == 0 then
return nil
end
local folder_ok, folder = pcall(vim.fn.readdir, folder_path)
if not folder_ok then return nil end
local files = {}
if folder ~= nil then
for _, file in ipairs(folder) do
local file_path = folder_path .. (M.is_windows() and "\\" or '/') .. file
local timestamp = vim.fn.getftime(file_path)
table.insert(files, { name = file, timestamp = timestamp })
end
end
-- Sort the table by timestamp in descending order (newest first)
table.sort(files, function(a, b)
return a.timestamp > b.timestamp
end)
local result = {}
for _, file in ipairs(files) do
table.insert(result, file.name)
end
return result
end
M.reverse = function(list)
local rev = {}
for i = #list, 1, -1 do