Feat: Adds Ability to Merge MR (#147)
This adds the ability to merge an MR from within `gitlab.nvim` directly. If the reviewer is open, it'll be closed automatically. Users may configure whether they'd like to squash commits on the merge, as well as whether they'd like to delete the original source branch on a merge. If squashing, users are prompted to provide an optional custom squash message for the squash commit.
This commit is contained in:
committed by
GitHub
parent
e254100a72
commit
64b36ac51d
71
cmd/merge.go
Normal file
71
cmd/merge.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type AcceptMergeRequestRequest struct {
|
||||
Squash bool `json:"squash"`
|
||||
SquashMessage string `json:"squash_message"`
|
||||
DeleteBranch bool `json:"delete_branch"`
|
||||
}
|
||||
|
||||
/* acceptAndMergeHandler merges a given merge request into the target branch */
|
||||
func (a *api) acceptAndMergeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Methods", http.MethodGet)
|
||||
if r.Method != 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
|
||||
}
|
||||
|
||||
var acceptAndMergeRequest AcceptMergeRequestRequest
|
||||
err = json.Unmarshal(body, &acceptAndMergeRequest)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not unmarshal request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
opts := gitlab.AcceptMergeRequestOptions{
|
||||
Squash: &acceptAndMergeRequest.Squash,
|
||||
ShouldRemoveSourceBranch: &acceptAndMergeRequest.DeleteBranch,
|
||||
}
|
||||
|
||||
if acceptAndMergeRequest.SquashMessage != "" {
|
||||
opts.SquashCommitMessage = &acceptAndMergeRequest.SquashMessage
|
||||
}
|
||||
|
||||
_, res, err := a.client.AcceptMergeRequest(a.projectInfo.ProjectId, a.projectInfo.MergeId, &opts)
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not merge MR", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
handleError(w, GenericError{endpoint: "/merge"}, "Could not merge MR", res.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
response := SuccessResponse{
|
||||
Status: http.StatusOK,
|
||||
Message: "MR merged successfully",
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user