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:
committed by
GitHub
parent
10b0b596ae
commit
93fe3e8bd6
32
cmd/job.go
32
cmd/job.go
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
@@ -16,19 +15,19 @@ type JobTraceResponse struct {
|
||||
File string `json:"file"`
|
||||
}
|
||||
|
||||
func JobHandler(w http.ResponseWriter, r *http.Request) {
|
||||
/* jobHandler returns a string that shows the output of a specific job run in a Gitlab pipeline */
|
||||
func (a *api) jobHandler(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
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not read request body", http.StatusBadRequest)
|
||||
handleError(w, err, "Could not read request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
@@ -36,18 +35,27 @@ func JobHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var jobTraceRequest JobTraceRequest
|
||||
err = json.Unmarshal(body, &jobTraceRequest)
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not unmarshal data from request body", http.StatusBadRequest)
|
||||
handleError(w, err, "Could not unmarshal data from request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
reader, _, err := c.git.Jobs.GetTraceFile(c.projectId, jobTraceRequest.JobId)
|
||||
reader, res, err := a.client.GetTraceFile(a.projectInfo.ProjectId, jobTraceRequest.JobId)
|
||||
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not get trace file for job", http.StatusBadRequest)
|
||||
handleError(w, err, "Could not get trace file for job", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
handleError(w, GenericError{endpoint: "/job"}, "Could not get trace file for job", res.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := io.ReadAll(reader)
|
||||
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not read job trace file", http.StatusBadRequest)
|
||||
handleError(w, err, "Could not read job trace file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response := JobTraceResponse{
|
||||
@@ -60,6 +68,6 @@ func JobHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user