Bugfix: Don't Start Server Right Away (#40)

This MR simplifies the plugin quite a bit by only attempting to start the Go server after you specifically try to run a command. This streamlines working on feature branches and removes the need for a `base_branch` property entirely.
This commit is contained in:
Harrison (Harry) Cramer
2023-08-17 16:22:53 -04:00
committed by GitHub
parent f4aafe46a9
commit f5bf720b61
9 changed files with 91 additions and 156 deletions

View File

@@ -71,18 +71,8 @@ If you don't want to write your authentication token into a dotfile, you may pro
export GITLAB_TOKEN="your_gitlab_token" export GITLAB_TOKEN="your_gitlab_token"
``` ```
By default, the plugin will interact with MRs against a "main" branch. You can configure this by passing in the `base_branch` option to the `.gitlab.nvim` configuration file for your project.
```
project_id=112415
auth_token=your_gitlab_token
gitlab_url=https://my-personal-gitlab-instance.com/
base_branch=master
```
## Configuring the Plugin ## Configuring the Plugin
Here is the default setup function. All of these values are optional, and if you call this function with no values the defaults will be used: Here is the default setup function. All of these values are optional, and if you call this function with no values the defaults will be used:
```lua ```lua

View File

@@ -25,7 +25,7 @@ func main() {
var c Client var c Client
if err := c.init(branchName); err != nil { if err := c.init(branchName); err != nil {
log.Fatalf("Failure: Failed to initialize client: %v", err) log.Fatalf("Failed to initialize Gitlab client: %v", err)
} }
m := http.NewServeMux() m := http.NewServeMux()

View File

@@ -13,14 +13,12 @@ local edit_popup = Popup(u.create_popup_state("Edit Comment", "80%", "80
-- Function that fires to open the comment popup -- Function that fires to open the comment popup
M.create_comment = function() M.create_comment = function()
if u.base_invalid() then return end
comment_popup:mount() comment_popup:mount()
keymaps.set_popup_keymaps(comment_popup, M.confirm_create_comment) keymaps.set_popup_keymaps(comment_popup, M.confirm_create_comment)
end end
-- Actually sends the comment to Gitlab -- Actually sends the comment to Gitlab
M.confirm_create_comment = function(text) M.confirm_create_comment = function(text)
if u.base_invalid() then return end
local relative_file_path = u.get_relative_file_path() local relative_file_path = u.get_relative_file_path()
local current_line_number = u.get_current_line_number() local current_line_number = u.get_current_line_number()
if relative_file_path == nil then return end if relative_file_path == nil then return end
@@ -110,7 +108,6 @@ end
-- Function that opens the edit popup from the discussion tree -- Function that opens the edit popup from the discussion tree
M.edit_comment = function() M.edit_comment = function()
if u.base_invalid() then return end
local current_node = state.tree:get_node() local current_node = state.tree:get_node()
local note_node = discussions.get_note_node(current_node) local note_node = discussions.get_note_node(current_node)
local root_node = discussions.get_root_node(current_node) local root_node = discussions.get_root_node(current_node)

View File

@@ -11,7 +11,6 @@ local M = {}
local replyPopup = Popup(u.create_popup_state("Reply", "80%", "80%")) local replyPopup = Popup(u.create_popup_state("Reply", "80%", "80%"))
M.reply = function(discussion_id) M.reply = function(discussion_id)
if u.base_invalid() then return end
replyPopup:mount() replyPopup:mount()
keymaps.set_popup_keymaps(replyPopup, M.send_reply(discussion_id)) keymaps.set_popup_keymaps(replyPopup, M.send_reply(discussion_id))
end end
@@ -28,7 +27,6 @@ end
-- Places all of the discussions into a readable list -- Places all of the discussions into a readable list
M.list_discussions = function() M.list_discussions = function()
if u.base_invalid() then return end
job.run_job("discussions", "GET", nil, function(data) job.run_job("discussions", "GET", nil, function(data)
if type(data.discussions) ~= "table" then if type(data.discussions) ~= "table" then
vim.notify("No discussions for this MR") vim.notify("No discussions for this MR")

View File

@@ -7,55 +7,10 @@ local comment = require("gitlab.comment")
local job = require("gitlab.job") local job = require("gitlab.job")
local u = require("gitlab.utils") local u = require("gitlab.utils")
-- 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)
state.INFO = data.info
callback()
end)
else
callback()
end
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 = {} local M = {}
M.summary = ensureState(summary.summary) M.args = nil
M.approve = ensureState(job.approve)
M.revoke = ensureState(job.revoke)
M.list_discussions = ensureState(discussions.list_discussions)
M.create_comment = ensureState(comment.create_comment)
M.edit_comment = ensureState(comment.edit_comment)
M.delete_comment = ensureState(comment.delete_comment)
M.toggle_resolved = ensureState(comment.toggle_resolved)
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 -- Builds the binary (if not built) and sets the plugin arguments
M.setup = function(args) M.setup = function(args)
if args == nil then args = {} end if args == nil then args = {} end
local file_path = u.current_file_path() local file_path = u.current_file_path()
@@ -67,7 +22,29 @@ M.setup = function(args)
if binary_exists == nil then M.build() end if binary_exists == nil then M.build() end
if not M.setPluginConfiguration(args) then return end -- Return if not a valid gitlab project if not M.setPluginConfiguration(args) then return end -- Return if not a valid gitlab project
M.args = args -- The ensureState function won't start without args
end
-- 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.
-- This plugin will start the Go server and will call the "info"
-- endpoint and set the state of the MR.
M.go_server_running = false
M.ensureState = function(callback)
return function()
if not M.args then
vim.notify("The gitlab.nvim state was not set. Do you have a .gitlab.nvim file configured?", vim.log.levels.ERROR)
return
end
if M.go_server_running then
callback()
return
end
local command = state.BIN local command = state.BIN
.. " " .. " "
@@ -87,15 +64,40 @@ M.setup = function(args)
vim.notify("Could not start gitlab.nvim binary", vim.log.levels.ERROR) vim.notify("Could not start gitlab.nvim binary", vim.log.levels.ERROR)
return return
else else
keymaps.set_keymap_keys(args.keymaps) -- Once the Go binary has go_server_running, call the info endpoint to set global state
keymaps.set_keymaps() keymaps.set_keymap_keys(M.args.keymaps)
M.go_server_running = true
job.run_job("info", "GET", nil, function(data)
state.INFO = data.info
callback()
end)
end end
end, end,
on_stderr = function(_, error) on_stderr = function(_, errors)
vim.notify(error[1], vim.log.levels.ERROR) local err_msg = ''
for _, err in ipairs(errors) do
if err ~= "" and err ~= nil then
err_msg = err_msg .. err .. "\n"
end
end
vim.notify(err_msg, vim.log.levels.ERROR)
end end
}) })
end end
end
M.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
-- Builds the Go binary -- Builds the Go binary
M.build = function() M.build = function()
@@ -127,21 +129,15 @@ M.setPluginConfiguration = function(args)
local project_id = property["project_id"] local project_id = property["project_id"]
local gitlab_url = property["gitlab_url"] local gitlab_url = property["gitlab_url"]
local base_branch = property["base_branch"]
local auth_token = property["auth_token"] local auth_token = property["auth_token"]
state.PROJECT_ID = project_id state.PROJECT_ID = project_id
state.AUTH_TOKEN = auth_token or os.getenv("GITLAB_TOKEN") state.AUTH_TOKEN = auth_token or os.getenv("GITLAB_TOKEN")
state.GITLAB_URL = gitlab_url or "https://gitlab.com" state.GITLAB_URL = gitlab_url or "https://gitlab.com"
state.BASE_BRANCH = base_branch or "main"
local current_branch_raw = io.popen("git rev-parse --abbrev-ref HEAD"):read("*a") local current_branch_raw = io.popen("git rev-parse --abbrev-ref HEAD"):read("*a")
local current_branch = string.gsub(current_branch_raw, "\n", "") local current_branch = string.gsub(current_branch_raw, "\n", "")
if current_branch == state.BASE_BRANCH then
return false
end
if state.AUTH_TOKEN == nil then if state.AUTH_TOKEN == nil then
error("Missing authentication token for Gitlab") error("Missing authentication token for Gitlab")
end end
@@ -177,4 +173,20 @@ M.setPluginConfiguration = function(args)
return true return true
end end
-- Root Module Scope
M.summary = M.ensureState(summary.summary)
M.approve = M.ensureState(job.approve)
M.revoke = M.ensureState(job.revoke)
M.list_discussions = M.ensureState(discussions.list_discussions)
M.create_comment = M.ensureState(comment.create_comment)
M.edit_comment = M.ensureState(comment.edit_comment)
M.delete_comment = M.ensureState(comment.delete_comment)
M.toggle_resolved = M.ensureState(comment.toggle_resolved)
M.reply = M.ensureState(discussions.reply)
M.add_reviewer = M.ensureProjectMembers(M.ensureState(assignees_and_reviewers.add_reviewer))
M.delete_reviewer = M.ensureProjectMembers(M.ensureState(assignees_and_reviewers.delete_reviewer))
M.add_assignee = M.ensureProjectMembers(M.ensureState(assignees_and_reviewers.add_assignee))
M.delete_assignee = M.ensureProjectMembers(M.ensureState(assignees_and_reviewers.delete_assignee))
M.state = state
return M return M

View File

@@ -19,30 +19,4 @@ M.set_keymap_keys = function(keyTable)
state.keymaps = u.merge_tables(state.keymaps, keyTable) state.keymaps = u.merge_tables(state.keymaps, keyTable)
end end
M.set_keymaps = function()
local ok, _ = pcall(require, "diffview")
vim.keymap.set("n", state.keymaps.review.toggle, function()
if not ok then
require("notify")("You must have diffview.nvim installed to use this command!", "error")
return
end
local isDiff = vim.fn.getwinvar(nil, "&diff")
local bufName = vim.api.nvim_buf_get_name(0)
local has_develop = u.branch_exists("main") -- TODO: Write this function
if not has_develop then
require("notify")('No ' .. state.BASE_BRANCH .. ' branch, cannot review.', "error")
return
end
if isDiff ~= 0 or u.string_starts(bufName, "diff") then
vim.cmd.tabclose()
vim.cmd.tabprev()
else
vim.cmd.DiffviewOpen(state.BASE_BRANCH)
u.press_enter()
end
end)
end
return M return M

View File

@@ -5,7 +5,6 @@ M.BIN = nil
M.PROJECT_ID = nil M.PROJECT_ID = nil
M.ACTIVE_DISCUSSION = nil M.ACTIVE_DISCUSSION = nil
M.ACTIVE_NOTE = nil M.ACTIVE_NOTE = nil
M.BASE_BRANCH = "main"
M.keymaps = { M.keymaps = {
popup = { popup = {
exit = "<Esc>", exit = "<Esc>",

View File

@@ -7,7 +7,6 @@ local descriptionPopup = Popup(u.create_popup_state("Loading Description...", "8
local M = {} local M = {}
M.summary = function() M.summary = function()
if u.base_invalid() then return end
descriptionPopup:mount() descriptionPopup:mount()
local currentBuffer = vim.api.nvim_get_current_buf() local currentBuffer = vim.api.nvim_get_current_buf()
local title = state.INFO.title local title = state.INFO.title

View File

@@ -54,19 +54,6 @@ local function get_buffer_text(bufnr)
return text return text
end end
local feature_branch_exists = function(base_branch)
local is_git_branch = io.popen("git rev-parse --is-inside-work-tree 2>/dev/null"):read("*a")
if is_git_branch == "true\n" then
for line in io.popen("git branch 2>/dev/null"):lines() do
line = line:gsub("%s+", "")
if line == base_branch then
return true
end
end
end
return false
end
local string_starts = function(str, start) local string_starts = function(str, start)
return str:sub(1, #start) == start return str:sub(1, #start) == start
end end
@@ -75,25 +62,6 @@ local press_enter = function()
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", false, true, true), "n", false) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", false, true, true), "n", false)
end end
local base_invalid = function()
local current_branch_raw = io.popen("git rev-parse --abbrev-ref HEAD"):read("*a")
local current_branch = string.gsub(current_branch_raw, "\n", "")
if current_branch == "main" or current_branch == "master" then
vim.notify('On ' .. current_branch .. ' branch, no MRs available', vim.log.levels.ERROR)
return true
end
local base = state.BASE_BRANCH
local hasBaseBranch = feature_branch_exists(base)
if not hasBaseBranch then
vim.notify(
'Could not fetch feature branch. Please check that you have the correct base_branch value set in your .gitlab.nvim configuration file',
vim.log.levels.ERROR)
return true
end
end
local format_date = function(date_string) local format_date = function(date_string)
local date_table = os.date("!*t") local date_table = os.date("!*t")
local year, month, day, hour, min, sec = date_string:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)") local year, month, day, hour, min, sec = date_string:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)")
@@ -302,10 +270,8 @@ M.join_tables = join_tables
M.get_relative_file_path = get_relative_file_path M.get_relative_file_path = get_relative_file_path
M.get_current_line_number = get_current_line_number M.get_current_line_number = get_current_line_number
M.get_buffer_text = get_buffer_text M.get_buffer_text = get_buffer_text
M.feature_branch_exists = feature_branch_exists
M.press_enter = press_enter M.press_enter = press_enter
M.string_starts = string_starts M.string_starts = string_starts
M.base_invalid = base_invalid
M.format_date = format_date M.format_date = format_date
M.add_comment_sign = add_comment_sign M.add_comment_sign = add_comment_sign
M.jump_to_file = jump_to_file M.jump_to_file = jump_to_file