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.
74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
type JobTraceRequest struct {
|
|
JobId int `json:"job_id"`
|
|
}
|
|
|
|
type JobTraceResponse struct {
|
|
SuccessResponse
|
|
File string `json:"file"`
|
|
}
|
|
|
|
/* 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")
|
|
if r.Method != http.MethodGet {
|
|
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 {
|
|
handleError(w, err, "Could not read request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
var jobTraceRequest JobTraceRequest
|
|
err = json.Unmarshal(body, &jobTraceRequest)
|
|
if err != nil {
|
|
handleError(w, err, "Could not unmarshal data from request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
reader, res, err := a.client.GetTraceFile(a.projectInfo.ProjectId, jobTraceRequest.JobId)
|
|
|
|
if err != nil {
|
|
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 {
|
|
handleError(w, err, "Could not read job trace file", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
response := JobTraceResponse{
|
|
SuccessResponse: SuccessResponse{
|
|
Status: http.StatusOK,
|
|
Message: "Log file read",
|
|
},
|
|
File: string(file),
|
|
}
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
if err != nil {
|
|
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
|
|
}
|
|
}
|