Files
gitlab.nvim/cmd/app/reply.go
Harrison (Harry) Cramer ea2b2b2f5c Fix MR Selection, Go Code Refactor (#358)
refactor: Refactors the Go codebase into a more modular and idiomatic approach
fix: require selection of specific MR when there are multiple targets for a given source branch
feat: Allows for the passing of Gitlab's filter options when choosing an MR, improves MR selection
feat: API to choose an MR from a list based on the provided username's involvement as an assignee/reviewer/author

This is a #MINOR release
2024-09-08 16:45:09 -04:00

88 lines
2.1 KiB
Go

package app
import (
"encoding/json"
"io"
"net/http"
"time"
"github.com/xanzy/go-gitlab"
)
type ReplyRequest struct {
DiscussionId string `json:"discussion_id"`
Reply string `json:"reply"`
IsDraft bool `json:"is_draft"`
}
type ReplyResponse struct {
SuccessResponse
Note *gitlab.Note `json:"note"`
}
type ReplyManager interface {
AddMergeRequestDiscussionNote(interface{}, int, string, *gitlab.AddMergeRequestDiscussionNoteOptions, ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
}
type replyService struct {
data
client ReplyManager
}
/* replyHandler sends a reply to a note or comment */
func (a replyService) handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPost {
w.Header().Set("Access-Control-Allow-Methods", http.MethodPost)
handleError(w, InvalidRequestError{}, "Expected POST", http.StatusMethodNotAllowed)
return
}
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 replyRequest ReplyRequest
err = json.Unmarshal(body, &replyRequest)
if err != nil {
handleError(w, err, "Could not read JSON from request", http.StatusBadRequest)
return
}
now := time.Now()
options := gitlab.AddMergeRequestDiscussionNoteOptions{
Body: gitlab.Ptr(replyRequest.Reply),
CreatedAt: &now,
}
note, res, err := a.client.AddMergeRequestDiscussionNote(a.projectInfo.ProjectId, a.projectInfo.MergeId, replyRequest.DiscussionId, &options)
if err != nil {
handleError(w, err, "Could not leave reply", http.StatusInternalServerError)
return
}
if res.StatusCode >= 300 {
handleError(w, GenericError{endpoint: "/mr/reply"}, "Could not leave reply", res.StatusCode)
return
}
w.WriteHeader(http.StatusOK)
response := ReplyResponse{
SuccessResponse: SuccessResponse{
Message: "Replied to comment",
Status: http.StatusOK,
},
Note: note,
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
}
}