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

View File

@@ -11,6 +11,7 @@ func ApproveHandler(w http.ResponseWriter, r *http.Request) {
c := r.Context().Value("client").(Client)
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
c.handleError(w, errors.New("Invalid request type"), "That request type is not allowed", http.StatusMethodNotAllowed)
return
}

View File

@@ -22,6 +22,7 @@ func DescriptionHandler(w http.ResponseWriter, r *http.Request) {
c := r.Context().Value("client").(Client)
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPut {
w.Header().Set("Allow", http.MethodPut)
c.handleError(w, errors.New("Invalid request type"), "That request type is not allowed", http.StatusMethodNotAllowed)
return
}

View File

@@ -56,6 +56,7 @@ func InfoHandler(w http.ResponseWriter, r *http.Request) {
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)
return
}

View File

@@ -83,6 +83,7 @@ func ListDiscussionsHandler(w http.ResponseWriter, r *http.Request) {
c := r.Context().Value("client").(Client)
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
c.handleError(w, errors.New("Invalid request type"), "That request type is not allowed", http.StatusMethodNotAllowed)
return
}

View File

@@ -40,6 +40,7 @@ func main() {
m.Handle("/comment", withGitlabContext(http.HandlerFunc(CommentHandler), c))
m.Handle("/reply", withGitlabContext(http.HandlerFunc(ReplyHandler), c))
m.Handle("/members", withGitlabContext(http.HandlerFunc(ProjectMembersHandler), c))
m.Handle("/pipeline", withGitlabContext(http.HandlerFunc(PipelineHandler), c))
port := fmt.Sprintf(":%s", os.Args[3])
server := &http.Server{

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

View File

@@ -43,6 +43,7 @@ func ReplyHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
c.handleError(w, errors.New("Invalid request type"), "That request type is not allowed", http.StatusMethodNotAllowed)
return
}

View File

@@ -18,6 +18,7 @@ func RevisionsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
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.WriteHeader(http.StatusMethodNotAllowed)
return

View File

@@ -11,6 +11,7 @@ func RevokeHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
c.handleError(w, errors.New("Invalid request type"), "That request type is not allowed", http.StatusMethodNotAllowed)
w.WriteHeader(http.StatusMethodNotAllowed)
return