Post MR cleanup (better error handling, comments, etc)
This commit is contained in:
24
cmd/main.go
24
cmd/main.go
@@ -11,16 +11,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
branchName, err := getCurrentBranch()
|
branchName, err := getCurrentBranch()
|
||||||
errCheck(err)
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failure: Failed to get current branch: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
if branchName == "main" || branchName == "master" {
|
if branchName == "main" || branchName == "master" {
|
||||||
log.Fatalf("Cannot run on %s branch", branchName)
|
log.Fatalf("Cannot run on %s branch", branchName)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize Gitlab client */
|
/* Initialize Gitlab client */
|
||||||
var c Client
|
var c Client
|
||||||
errCheck(c.Init(branchName))
|
|
||||||
|
if err := c.Init(branchName); err != nil {
|
||||||
|
log.Fatalf("Failure: Failed to iniialize client: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
m := http.NewServeMux()
|
m := http.NewServeMux()
|
||||||
m.Handle("/approve", withGitlabContext(http.HandlerFunc(ApproveHandler), c))
|
m.Handle("/approve", withGitlabContext(http.HandlerFunc(ApproveHandler), c))
|
||||||
@@ -45,10 +51,6 @@ func main() {
|
|||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseError struct {
|
|
||||||
message string
|
|
||||||
}
|
|
||||||
|
|
||||||
func withGitlabContext(next http.HandlerFunc, c Client) http.Handler {
|
func withGitlabContext(next http.HandlerFunc, c Client) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := context.WithValue(context.Background(), "client", c)
|
ctx := context.WithValue(context.Background(), "client", c)
|
||||||
@@ -56,13 +58,6 @@ func withGitlabContext(next http.HandlerFunc, c Client) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func errCheck(err error) {
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failure: %s", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Gets the current branch */
|
/* Gets the current branch */
|
||||||
func getCurrentBranch() (res string, e error) {
|
func getCurrentBranch() (res string, e error) {
|
||||||
gitCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
|
gitCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
|
||||||
@@ -73,5 +68,4 @@ func getCurrentBranch() (res string, e error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return strings.TrimSpace(string(output)), nil
|
return strings.TrimSpace(string(output)), nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,85 +19,70 @@ M.edit_comment = comment.edit_comment
|
|||||||
M.delete_comment = comment.delete_comment
|
M.delete_comment = comment.delete_comment
|
||||||
M.reply = discussions.reply
|
M.reply = discussions.reply
|
||||||
|
|
||||||
-- Builds the Go binary, initializes the plugin, fetches MR info
|
-- Builds the Go binary
|
||||||
M.build = function(args)
|
local function build_binary()
|
||||||
if args == nil then args = {} end
|
|
||||||
local command = string.format("cd %s && make", state.BIN_PATH)
|
local command = string.format("cd %s && make", state.BIN_PATH)
|
||||||
local installCode = os.execute(command .. "> /dev/null")
|
local installCode = os.execute(command .. "> /dev/null")
|
||||||
if installCode ~= 0 then
|
if installCode ~= 0 then
|
||||||
notify("Could not install gitlab.nvim!", "error")
|
notify("Could not install gitlab.nvim!", "error")
|
||||||
return
|
return false
|
||||||
else
|
|
||||||
M.setup(args, true)
|
|
||||||
end
|
end
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
M.setup = function(args, build_only)
|
|
||||||
local file_path = M.current_file_path()
|
-- Setups up the binary (if not built), starts the Go server, and calls the /info endpoint,
|
||||||
local parent_dir = vim.fn.fnamemodify(file_path, ":h:h:h")
|
-- which sets the Gitlab project's information in gitlab.nvim's state module
|
||||||
|
M.setup = function(args)
|
||||||
|
local file_path = u.current_file_path()
|
||||||
|
local parent_dir = vim.fn.fnamemodify(file_path, ":h:h:h:h")
|
||||||
state.BIN_PATH = parent_dir
|
state.BIN_PATH = parent_dir
|
||||||
state.BIN = parent_dir .. "/bin"
|
state.BIN = parent_dir .. "/bin"
|
||||||
|
|
||||||
if args == nil then args = {} end
|
if args == nil then args = {} end
|
||||||
if args.dev == true then
|
|
||||||
M.build(args)
|
|
||||||
end
|
|
||||||
|
|
||||||
local binExists = io.open(state.BIN, "r")
|
|
||||||
if not binExists or args.dev == true then
|
|
||||||
local command = string.format("cd %s && make", state.BIN_PATH)
|
|
||||||
local installCode = os.execute(command .. "> /dev/null")
|
|
||||||
if installCode ~= 0 then
|
|
||||||
require("notify")("Could not install gitlab.nvim! Do you have Go installed?", "error")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local binary_exists = vim.loop.fs_stat(state.BIN)
|
local binary_exists = vim.loop.fs_stat(state.BIN)
|
||||||
if binary_exists == nil then
|
if binary_exists == nil then
|
||||||
return -- Ensure build function completes before initializing plugin
|
build_binary()
|
||||||
end
|
end
|
||||||
|
|
||||||
if build_only then return end
|
|
||||||
|
|
||||||
local config_file_path = vim.fn.getcwd() .. "/.gitlab.nvim"
|
local config_file_path = vim.fn.getcwd() .. "/.gitlab.nvim"
|
||||||
local config_file_content = u.read_file(config_file_path)
|
local config_file_content = u.read_file(config_file_path)
|
||||||
if config_file_content == nil then
|
if config_file_content == nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
args.project_id = config_file_content
|
state.PROJECT_ID = config_file_content
|
||||||
|
if state.PROJECT_ID == nil then
|
||||||
if args.project_id == nil then
|
|
||||||
args.project_id = u.read_file(state.BIN_PATH .. "/.gitlab/project_id")
|
|
||||||
error("No project ID provided!")
|
error("No project ID provided!")
|
||||||
end
|
end
|
||||||
|
|
||||||
state.PROJECT_ID = args.project_id
|
if type(tonumber(state.PROJECT_ID)) ~= 'number' then
|
||||||
|
error("The .gitlab.nvim project file may only contain a project number")
|
||||||
|
end
|
||||||
|
|
||||||
if args.base_branch ~= nil then
|
if args.base_branch ~= nil then
|
||||||
state.BASE_BRANCH = args.base_branch
|
state.BASE_BRANCH = args.base_branch
|
||||||
end
|
end
|
||||||
|
|
||||||
local error_message = "Failed to set up gitlab.nvim, could not get project information."
|
|
||||||
if u.is_gitlab_repo() then
|
if u.is_gitlab_repo() then
|
||||||
state.PORT = args.port or 21036
|
state.PORT = args.port or 21036
|
||||||
vim.fn.jobstart(state.BIN .. " " .. state.PROJECT_ID .. " " .. state.PORT, {
|
vim.fn.jobstart(state.BIN .. " " .. state.PROJECT_ID .. " " .. state.PORT, {
|
||||||
on_stdout = function(job_id)
|
on_stdout = function(job_id)
|
||||||
if job_id <= 0 then
|
if job_id <= 0 then
|
||||||
notify(error_message, "error")
|
notify("Could not start gitlab.nvim binary", "error")
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local response_ok, response = pcall(curl.get, "localhost:" .. state.PORT .. "/info",
|
local response_ok, response = pcall(curl.get, "localhost:" .. state.PORT .. "/info",
|
||||||
{ timeout = 750 })
|
{ timeout = 750 })
|
||||||
if response == nil or not response_ok then
|
if response == nil or not response_ok then
|
||||||
notify(error_message, "error")
|
notify("The gitlab.nvim server did not respond", "error")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local body = response.body
|
local body = response.body
|
||||||
local parsed_ok, data = pcall(vim.json.decode, body)
|
local parsed_ok, data = pcall(vim.json.decode, body)
|
||||||
if parsed_ok ~= true then
|
if parsed_ok ~= true then
|
||||||
notify(error_message, "error")
|
notify("The gitlab.nvim server returned an invalid response to the /info endpoint", "error")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
state.INFO = data
|
state.INFO = data
|
||||||
@@ -109,9 +94,4 @@ M.setup = function(args, build_only)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
M.current_file_path = function()
|
|
||||||
local path = debug.getinfo(1, 'S').source:sub(2)
|
|
||||||
return vim.fn.fnamemodify(path, ':p')
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -239,6 +239,12 @@ local split_diff_view_filename = function(filename)
|
|||||||
return hash, path
|
return hash, path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local current_file_path = function()
|
||||||
|
local path = debug.getinfo(1, 'S').source:sub(2)
|
||||||
|
return vim.fn.fnamemodify(path, ':p')
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
@@ -259,5 +265,6 @@ M.exit = exit
|
|||||||
M.read_file = read_file
|
M.read_file = read_file
|
||||||
M.split_diff_view_filename = split_diff_view_filename
|
M.split_diff_view_filename = split_diff_view_filename
|
||||||
M.branch_exists = branch_exists
|
M.branch_exists = branch_exists
|
||||||
|
M.current_file_path = current_file_path
|
||||||
M.P = P
|
M.P = P
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user