Simplify Go Endpoints + Add Tests (#120)

This MR represents a major refactor of the Go codebase, as well as introducing tests for the handlers. The MR also introduces an endpoint to shutdown or restart the Go server, which may be useful for clients who want to refresh the state of the plugin after checking out branches. Finally, this MR adds a contributing document for users who want to make feature changes.
This commit is contained in:
Harrison (Harry) Cramer
2023-12-04 10:15:07 -05:00
committed by GitHub
parent 10b0b596ae
commit 93fe3e8bd6
41 changed files with 1745 additions and 728 deletions

View File

@@ -2,75 +2,33 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"github.com/xanzy/go-gitlab"
)
const mrUrl = "%s/api/v4/projects/%s/merge_requests/%d"
type InfoResponse struct {
SuccessResponse
Info *gitlab.MergeRequest `json:"info"`
}
func (c *Client) Info() ([]byte, error) {
url := fmt.Sprintf(mrUrl, c.gitlabInstance, c.projectId, c.mergeId)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("Failed to build read request: %w", err)
}
req.Header.Set("PRIVATE-TOKEN", c.authToken)
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("Failed to make info request: %w", err)
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
return nil, fmt.Errorf("Recieved non-200 response: %d", res.StatusCode)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Failed to parse read response: %w", err)
}
/* This response is parsed into a table in our Lua code */
return body, nil
}
func InfoHandler(w http.ResponseWriter, r *http.Request) {
/* infoHandler fetches infomation about the current git project. The data returned here is used in many other API calls */
func (a *api) infoHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
c := r.Context().Value("client").(Client)
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
c.handleError(w, errors.New("Invalid request type"), "That request type is not allowed", http.StatusMethodNotAllowed)
w.Header().Set("Access-Control-Allow-Methods", http.MethodGet)
handleError(w, InvalidRequestError{}, "Expected GET", http.StatusMethodNotAllowed)
return
}
msg, err := c.Info()
mr, res, err := a.client.GetMergeRequest(a.projectInfo.ProjectId, a.projectInfo.MergeId, &gitlab.GetMergeRequestsOptions{})
if err != nil {
c.handleError(w, err, "Could not get project info and initialize gitlab.nvim plugin", http.StatusBadRequest)
handleError(w, err, "Could not get project info", http.StatusInternalServerError)
return
}
var mergeRequest *gitlab.MergeRequest
err = json.Unmarshal(msg, &mergeRequest)
if err != nil {
c.handleError(w, err, "Could not unmarshal data from merge requests", http.StatusBadRequest)
if res.StatusCode >= 300 {
handleError(w, GenericError{endpoint: "/info"}, "Could not get project info", res.StatusCode)
return
}
@@ -80,11 +38,11 @@ func InfoHandler(w http.ResponseWriter, r *http.Request) {
Message: "Merge requests retrieved",
Status: http.StatusOK,
},
Info: mergeRequest,
Info: mr,
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
c.handleError(w, err, "Could not encode response", http.StatusInternalServerError)
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
}
}