Feat: Add and Remove Labels from an MR (#159)
This MR adds the ability to add or remove labels to a merge request. These labels are visible in the summary panel and are colored the same way as they would be in the Gitlab UI. This is a MINOR release.
This commit is contained in:
committed by
GitHub
parent
67f09e559a
commit
50e06ceff6
@@ -32,6 +32,7 @@ type Client struct {
|
||||
*gitlab.ProjectMembersService
|
||||
*gitlab.JobsService
|
||||
*gitlab.PipelinesService
|
||||
*gitlab.LabelsService
|
||||
}
|
||||
|
||||
/* initGitlabClient parses and validates the project settings and initializes the Gitlab client. */
|
||||
@@ -87,6 +88,7 @@ func initGitlabClient() (error, *Client) {
|
||||
ProjectMembersService: client.ProjectMembers,
|
||||
JobsService: client.Jobs,
|
||||
PipelinesService: client.Pipelines,
|
||||
LabelsService: client.Labels,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
130
cmd/label.go
Normal file
130
cmd/label.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type LabelUpdateRequest struct {
|
||||
Labels []string `json:"labels"`
|
||||
}
|
||||
|
||||
type Label struct {
|
||||
Name string
|
||||
Color string
|
||||
}
|
||||
|
||||
type LabelUpdateResponse struct {
|
||||
SuccessResponse
|
||||
Labels gitlab.Labels `json:"labels"`
|
||||
}
|
||||
|
||||
type LabelsRequestResponse struct {
|
||||
SuccessResponse
|
||||
Labels []Label `json:"labels"`
|
||||
}
|
||||
|
||||
/* labelsHandler adds or removes labels from a merge request, and returns all labels for the current project */
|
||||
func (a *api) labelHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
a.getLabels(w, r)
|
||||
case http.MethodPut:
|
||||
a.updateLabels(w, r)
|
||||
default:
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Methods", fmt.Sprintf("%s, %s", http.MethodPut, http.MethodGet))
|
||||
handleError(w, InvalidRequestError{}, "Expected GET or PUT", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *api) getLabels(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
labels, res, err := a.client.ListLabels(a.projectInfo.ProjectId, &gitlab.ListLabelsOptions{})
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not modify merge request labels", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
handleError(w, GenericError{endpoint: "/mr/label"}, "Could not modify merge request labels", res.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
/* Hacky, but convert them to the correct response */
|
||||
convertedLabels := make([]Label, len(labels))
|
||||
for i, labelPtr := range labels {
|
||||
convertedLabels[i] = Label{
|
||||
Name: labelPtr.Name,
|
||||
Color: labelPtr.Color,
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response := LabelsRequestResponse{
|
||||
SuccessResponse: SuccessResponse{
|
||||
Message: "Labels updated",
|
||||
Status: http.StatusOK,
|
||||
},
|
||||
Labels: convertedLabels,
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (a *api) updateLabels(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
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 labelUpdateRequest LabelUpdateRequest
|
||||
err = json.Unmarshal(body, &labelUpdateRequest)
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not read JSON from request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var labels = gitlab.Labels(labelUpdateRequest.Labels)
|
||||
mr, res, err := a.client.UpdateMergeRequest(a.projectInfo.ProjectId, a.projectInfo.MergeId, &gitlab.UpdateMergeRequestOptions{
|
||||
Labels: &labels,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not modify merge request labels", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
handleError(w, GenericError{endpoint: "/mr/label"}, "Could not modify merge request labels", res.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response := LabelUpdateResponse{
|
||||
SuccessResponse: SuccessResponse{
|
||||
Message: "Labels updated",
|
||||
Status: http.StatusOK,
|
||||
},
|
||||
Labels: mr.Labels,
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -122,6 +122,7 @@ func createRouterAndApi(client ClientInterface, optFuncs ...optFunc) (*http.Serv
|
||||
m.HandleFunc("/mr/reviewer", a.withMr(a.reviewersHandler))
|
||||
m.HandleFunc("/mr/revisions", a.withMr(a.revisionsHandler))
|
||||
m.HandleFunc("/mr/reply", a.withMr(a.replyHandler))
|
||||
m.HandleFunc("/mr/label", a.withMr(a.labelHandler))
|
||||
m.HandleFunc("/mr/revoke", a.withMr(a.revokeHandler))
|
||||
|
||||
m.HandleFunc("/attachment", a.attachmentHandler)
|
||||
|
||||
@@ -36,6 +36,7 @@ type fakeClient struct {
|
||||
retryPipelineBuild func(pid interface{}, pipeline int, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error)
|
||||
listPipelineJobs func(pid interface{}, pipelineID int, opts *gitlab.ListJobsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Job, *gitlab.Response, error)
|
||||
getTraceFile func(pid interface{}, jobID int, options ...gitlab.RequestOptionFunc) (*bytes.Reader, *gitlab.Response, error)
|
||||
listLabels func(pid interface{}, opt *gitlab.ListLabelsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Label, *gitlab.Response, error)
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
@@ -120,6 +121,10 @@ func (f fakeClient) GetTraceFile(pid interface{}, jobID int, options ...gitlab.R
|
||||
return f.getTraceFile(pid, jobID, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) ListLabels(pid interface{}, opt *gitlab.ListLabelsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Label, *gitlab.Response, error) {
|
||||
return f.listLabels(pid, opt, options...)
|
||||
}
|
||||
|
||||
/* This middleware function needs to return an ID for the rest of the handlers */
|
||||
func (f fakeClient) ListProjectMergeRequests(pid interface{}, opt *gitlab.ListProjectMergeRequestsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error) {
|
||||
return []*gitlab.MergeRequest{{ID: 1}}, &gitlab.Response{}, nil
|
||||
|
||||
@@ -54,4 +54,5 @@ type ClientInterface interface {
|
||||
RetryPipelineBuild(pid interface{}, pipeline int, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error)
|
||||
ListPipelineJobs(pid interface{}, pipelineID int, opts *gitlab.ListJobsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Job, *gitlab.Response, error)
|
||||
GetTraceFile(pid interface{}, jobID int, options ...gitlab.RequestOptionFunc) (*bytes.Reader, *gitlab.Response, error)
|
||||
ListLabels(pid interface{}, opt *gitlab.ListLabelsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Label, *gitlab.Response, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user