Feat: Edit PR Description (#22)

This MR makes it possible to edit the description. Non-breaking, will happen within the normal description popup.
This commit is contained in:
Harrison (Harry) Cramer
2023-08-14 20:37:36 -04:00
committed by GitHub
parent 35d5b619ce
commit 2028be2154
6 changed files with 107 additions and 20 deletions

View File

@@ -29,6 +29,7 @@ func main() {
}
m := http.NewServeMux()
m.Handle("/mr", withGitlabContext(http.HandlerFunc(UpdateHandler), 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))

74
cmd/update.go Normal file
View File

@@ -0,0 +1,74 @@
package main
import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"github.com/xanzy/go-gitlab"
)
type UpdateRequest struct {
Description string `json:"description"`
}
type UpdateResponse struct {
SuccessResponse
MergeRequest *gitlab.MergeRequest `json:"mr"`
}
func UpdateHandler(w http.ResponseWriter, r *http.Request) {
c := r.Context().Value("client").(Client)
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPut {
c.handleError(w, errors.New("Invalid request type"), "That request type is not allowed", http.StatusMethodNotAllowed)
return
}
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 updateRequest UpdateRequest
err = json.Unmarshal(body, &updateRequest)
if err != nil {
c.handleError(w, err, "Could not read JSON from request", http.StatusBadRequest)
return
}
git, err := gitlab.NewClient(c.authToken)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
mr, res, err := git.MergeRequests.UpdateMergeRequest(c.projectId, c.mergeId, &gitlab.UpdateMergeRequestOptions{Description: &updateRequest.Description})
if err != nil {
c.handleError(w, err, "Could not edit merge request", http.StatusBadRequest)
return
}
if res.StatusCode != http.StatusOK {
c.handleError(w, err, "Could not edit merge request", http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
response := UpdateResponse{
SuccessResponse: SuccessResponse{
Message: "Merge request updated",
Status: http.StatusOK,
},
MergeRequest: mr,
}
json.NewEncoder(w).Encode(response)
}