Files
gitlab.nvim/cmd/job.go
Harrison (Harry) Cramer 4ae623cd65 Add Filtering, HealthCheck, Better Tests (#350)
feat: add filtering when choosing merge requests (#346)
feat: Add healthcheck (#345)
refactor: Move to gomock (#349)
feat: Makes the remote of the plugin configurable (#348)

This is a #MINOR release.
2024-08-23 14:01:59 -04:00

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)
}
}