Add/Remove Reviewers and Assignees (#38)

Adds APIs for the ability to add or remove reviewers and assignees to a merge request. The eligible reviewers and assignees are pulled from the current members of a project.
This commit is contained in:
Harrison (Harry) Cramer
2023-08-16 21:42:53 -04:00
committed by GitHub
parent 6274746d4b
commit 844e093294
10 changed files with 368 additions and 44 deletions

70
cmd/assignee.go Normal file
View File

@@ -0,0 +1,70 @@
package main
import (
"encoding/json"
"io"
"net/http"
"github.com/xanzy/go-gitlab"
)
type AssigneeUpdateRequest struct {
Ids []int `json:"ids"`
}
type AssigneeUpdateResponse struct {
SuccessResponse
Assignees []*gitlab.BasicUser `json:"assignees"`
}
type AssigneesRequestResponse struct {
SuccessResponse
Assignees []int `json:"assignees"`
}
func AssigneesHandler(w http.ResponseWriter, r *http.Request) {
c := r.Context().Value("client").(Client)
w.Header().Set("Content-Type", "application/json")
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 assigneeUpdateRequest AssigneeUpdateRequest
err = json.Unmarshal(body, &assigneeUpdateRequest)
if err != nil {
c.handleError(w, err, "Could not read JSON from request", http.StatusBadRequest)
return
}
mr, res, err := c.git.MergeRequests.UpdateMergeRequest(c.projectId, c.mergeId, &gitlab.UpdateMergeRequestOptions{
AssigneeIDs: &assigneeUpdateRequest.Ids,
})
if err != nil {
c.handleError(w, err, "Could not modify merge request assignees", http.StatusBadRequest)
return
}
if res.StatusCode != http.StatusOK {
c.handleError(w, err, "Could not modify merge request assignees", http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
response := AssigneeUpdateResponse{
SuccessResponse: SuccessResponse{
Message: "Assignees updated",
Status: http.StatusOK,
},
Assignees: mr.Assignees,
}
json.NewEncoder(w).Encode(response)
}