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:
committed by
GitHub
parent
6274746d4b
commit
844e093294
70
cmd/assignee.go
Normal file
70
cmd/assignee.go
Normal 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)
|
||||
|
||||
}
|
||||
@@ -9,16 +9,16 @@ import (
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type UpdateRequest struct {
|
||||
type DescriptionUpdateRequest struct {
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type UpdateResponse struct {
|
||||
type DescriptionUpdateResponse struct {
|
||||
SuccessResponse
|
||||
MergeRequest *gitlab.MergeRequest `json:"mr"`
|
||||
}
|
||||
|
||||
func UpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
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 {
|
||||
@@ -33,31 +33,31 @@ func UpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
var updateRequest UpdateRequest
|
||||
err = json.Unmarshal(body, &updateRequest)
|
||||
var DescriptionUpdateRequest DescriptionUpdateRequest
|
||||
err = json.Unmarshal(body, &DescriptionUpdateRequest)
|
||||
|
||||
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{Description: &updateRequest.Description})
|
||||
mr, res, err := c.git.MergeRequests.UpdateMergeRequest(c.projectId, c.mergeId, &gitlab.UpdateMergeRequestOptions{Description: &DescriptionUpdateRequest.Description})
|
||||
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not edit merge request", http.StatusBadRequest)
|
||||
c.handleError(w, err, "Could not edit merge request description", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
c.handleError(w, err, "Could not edit merge request", http.StatusBadRequest)
|
||||
c.handleError(w, err, "Could not edit merge request description", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
response := UpdateResponse{
|
||||
response := DescriptionUpdateResponse{
|
||||
SuccessResponse: SuccessResponse{
|
||||
Message: "Merge request updated",
|
||||
Message: "Description updated",
|
||||
Status: http.StatusOK,
|
||||
},
|
||||
MergeRequest: mr,
|
||||
@@ -29,13 +29,16 @@ func main() {
|
||||
}
|
||||
|
||||
m := http.NewServeMux()
|
||||
m.Handle("/mr", withGitlabContext(http.HandlerFunc(UpdateHandler), c))
|
||||
m.Handle("/mr/description", withGitlabContext(http.HandlerFunc(DescriptionHandler), c))
|
||||
m.Handle("/mr/reviewer", withGitlabContext(http.HandlerFunc(ReviewersHandler), c))
|
||||
m.Handle("/mr/assignee", withGitlabContext(http.HandlerFunc(AssigneesHandler), c))
|
||||
m.Handle("/approve", withGitlabContext(http.HandlerFunc(ApproveHandler), c))
|
||||
m.Handle("/revoke", withGitlabContext(http.HandlerFunc(RevokeHandler), c))
|
||||
m.Handle("/info", withGitlabContext(http.HandlerFunc(InfoHandler), c))
|
||||
m.Handle("/discussions", withGitlabContext(http.HandlerFunc(ListDiscussionsHandler), c))
|
||||
m.Handle("/comment", withGitlabContext(http.HandlerFunc(CommentHandler), c))
|
||||
m.Handle("/reply", withGitlabContext(http.HandlerFunc(ReplyHandler), c))
|
||||
m.Handle("/members", withGitlabContext(http.HandlerFunc(ProjectMembersHandler), c))
|
||||
|
||||
port := fmt.Sprintf(":%s", os.Args[3])
|
||||
server := &http.Server{
|
||||
|
||||
43
cmd/members.go
Normal file
43
cmd/members.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type ProjectMembersResponse struct {
|
||||
SuccessResponse
|
||||
ProjectMembers []*gitlab.ProjectMember
|
||||
}
|
||||
|
||||
func ProjectMembersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
c := r.Context().Value("client").(Client)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
projectMemberOptions := gitlab.ListProjectMembersOptions{
|
||||
ListOptions: gitlab.ListOptions{
|
||||
PerPage: 100,
|
||||
},
|
||||
}
|
||||
|
||||
projectMembers, res, err := c.git.ProjectMembers.ListAllProjectMembers(c.projectId, &projectMemberOptions)
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not fetch project users", res.StatusCode)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
response := ProjectMembersResponse{
|
||||
SuccessResponse: SuccessResponse{
|
||||
Status: http.StatusOK,
|
||||
Message: "Project users fetched successfully",
|
||||
},
|
||||
ProjectMembers: projectMembers,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
|
||||
return
|
||||
}
|
||||
70
cmd/reviewer.go
Normal file
70
cmd/reviewer.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type ReviewerUpdateRequest struct {
|
||||
Ids []int `json:"ids"`
|
||||
}
|
||||
|
||||
type ReviewerUpdateResponse struct {
|
||||
SuccessResponse
|
||||
Reviewers []*gitlab.BasicUser `json:"reviewers"`
|
||||
}
|
||||
|
||||
type ReviewersRequestResponse struct {
|
||||
SuccessResponse
|
||||
Reviewers []int `json:"reviewers"`
|
||||
}
|
||||
|
||||
func ReviewersHandler(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 reviewerUpdateRequest ReviewerUpdateRequest
|
||||
err = json.Unmarshal(body, &reviewerUpdateRequest)
|
||||
|
||||
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{
|
||||
ReviewerIDs: &reviewerUpdateRequest.Ids,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not modify merge request reviewers", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
c.handleError(w, err, "Could not modify merge request reviewers", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
response := ReviewerUpdateResponse{
|
||||
SuccessResponse: SuccessResponse{
|
||||
Message: "Reviewers updated",
|
||||
Status: http.StatusOK,
|
||||
},
|
||||
Reviewers: mr.Reviewers,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user