Feat: View + Manage Pipeline (#53)

This MR adds the `.pipeline()` command which opens up the pipeline popup. This popup shows information about the current pipeline and it's jobs, and gives users the ability to re-trigger failed jobs.
This commit is contained in:
Harrison (Harry) Cramer
2023-09-03 18:01:54 -04:00
committed by GitHub
parent 4c6dcacfcd
commit e6e0bf4093
15 changed files with 267 additions and 8 deletions

109
cmd/pipeline.go Normal file
View File

@@ -0,0 +1,109 @@
package main
import (
"encoding/json"
"io"
"net/http"
"github.com/xanzy/go-gitlab"
)
type PipelineRequest struct {
PipelineId int `json:"pipeline_id"`
}
type RetriggerPipelineResponse struct {
SuccessResponse
Pipeline *gitlab.Pipeline
}
type GetJobsResponse struct {
SuccessResponse
Jobs []*gitlab.Job
}
func PipelineHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
GetJobs(w, r)
case http.MethodPost:
RetriggerPipeline(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func GetJobs(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
c := r.Context().Value("client").(Client)
body, err := io.ReadAll(r.Body)
if err != nil {
c.handleError(w, err, "Could not read request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
var pipelineRequest PipelineRequest
err = json.Unmarshal(body, &pipelineRequest)
if err != nil {
c.handleError(w, err, "Could not read JSON", http.StatusBadRequest)
}
jobs, res, err := c.git.Jobs.ListPipelineJobs(c.projectId, pipelineRequest.PipelineId, &gitlab.ListJobsOptions{})
if err != nil {
c.handleError(w, err, "Could not get pipeline jobs", res.StatusCode)
}
w.WriteHeader(http.StatusOK)
response := GetJobsResponse{
SuccessResponse: SuccessResponse{
Status: http.StatusOK,
Message: "Jobs fetched successfully",
},
Jobs: jobs,
}
json.NewEncoder(w).Encode(response)
}
func RetriggerPipeline(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
c := r.Context().Value("client").(Client)
body, err := io.ReadAll(r.Body)
if err != nil {
c.handleError(w, err, "Could not read request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
var pipelineRequest PipelineRequest
err = json.Unmarshal(body, &pipelineRequest)
if err != nil {
c.handleError(w, err, "Could not read JSON", http.StatusBadRequest)
}
pipeline, res, err := c.git.Pipelines.RetryPipelineBuild(c.projectId, pipelineRequest.PipelineId)
if err != nil {
c.handleError(w, err, "Could not retrigger pipeline", res.StatusCode)
}
w.WriteHeader(http.StatusOK)
response := RetriggerPipelineResponse{
SuccessResponse: SuccessResponse{
Message: "Pipeline retriggered",
Status: http.StatusOK,
},
Pipeline: pipeline,
}
json.NewEncoder(w).Encode(response)
}