forked from harrisoncramer/gitlab.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
68 lines (53 loc) · 1.53 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"encoding/json"
"errors"
"io"
"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
}
mr, res, err := c.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)
}