BREAKING CHANGE: Setup refactor and code cleanup

This MR makes several major tweaks to the codebase. Primarily it adjusts
the setup steps for the application so that rather than providing just
the project ID in the `.gitlab.nvim` file, users can also provide a
vareity of other settings, such as auth_token, base_branch, and so
forth. This is to make the project more extensible in the future.

This MR also fixes a variety of issues with error handling in the code,
primarily in the request/response model between the Lua jobs and the
Golang server.

BREAKING CHANGE: Modifies `.gitlab.nvim` and setup steps
This commit is contained in:
Harrison (Harry) Cramer
2023-08-06 11:21:39 -04:00
committed by Harrison Cramer
parent ade9f81426
commit 4f0d4b49ef
16 changed files with 281 additions and 293 deletions

View File

@@ -1,8 +1,10 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strconv"
@@ -10,10 +12,12 @@ import (
)
type Client struct {
command string
projectId string
mergeId int
git *gitlab.Client
command string
projectId string
mergeId int
gitlabInstance string
authToken string
git *gitlab.Client
}
type Logger struct {
@@ -22,7 +26,9 @@ type Logger struct {
func (l Logger) Printf(s string, args ...interface{}) {
logString := fmt.Sprintf(s+"\n", args...)
file, err := os.OpenFile("./logs", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
logPath := os.Args[len(os.Args)-1]
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
@@ -31,21 +37,36 @@ func (l Logger) Printf(s string, args ...interface{}) {
}
/* This will initialize the client with the token and check for the basic project ID and command arguments */
func (c *Client) Init(branchName string) error {
func (c *Client) init(branchName string) error {
if len(os.Args) < 2 {
return errors.New("Must provide project ID!")
if len(os.Args) < 5 {
return errors.New("Must provide project ID, gitlab instance, port, and auth token!")
}
projectId := os.Args[1]
c.projectId = projectId
gitlabInstance := os.Args[2]
authToken := os.Args[4]
if projectId == "" {
return errors.New("Project ID cannot be empty")
}
if gitlabInstance == "" {
return errors.New("GitLab instance URL cannot be empty")
}
if authToken == "" {
return errors.New("Auth token cannot be empty")
}
c.gitlabInstance = gitlabInstance
c.projectId = projectId
c.authToken = authToken
var l Logger
git, err := gitlab.NewClient(os.Getenv("GITLAB_TOKEN"), gitlab.WithCustomLogger(l))
var apiCustUrl = fmt.Sprintf(c.gitlabInstance + "/api/v4")
git, err := gitlab.NewClient(authToken, gitlab.WithBaseURL(apiCustUrl), gitlab.WithCustomLogger(l))
if err != nil {
return fmt.Errorf("Failed to create client: %v", err)
@@ -77,3 +98,12 @@ func (c *Client) Init(branchName string) error {
return nil
}
func (c *Client) handleError(w http.ResponseWriter, err error, message string, status int) {
w.WriteHeader(status)
response := ErrorResponse{
Message: message,
Details: err.Error(),
}
json.NewEncoder(w).Encode(response)
}