Skip to content

Commit a5440fc

Browse files
zeripathlafriks
andauthored
Report error if API merge is not allowed (#12528)
#12496 demonstrated that the API merge needs to return some information as to why a merge has been disallowed with a status code 422. This PR ensures that a reason is always returned. Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv>
1 parent 1701d57 commit a5440fc

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

routers/api/v1/repo/pull.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,18 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
759759
return
760760
}
761761

762-
if !pr.CanAutoMerge() || pr.HasMerged || pr.IsWorkInProgress() {
763-
ctx.Status(http.StatusMethodNotAllowed)
762+
if !pr.CanAutoMerge() {
763+
ctx.Error(http.StatusMethodNotAllowed, "PR not in mergeable state", "Please try again later")
764+
return
765+
}
766+
767+
if pr.HasMerged {
768+
ctx.Error(http.StatusMethodNotAllowed, "PR already merged", "")
769+
return
770+
}
771+
772+
if pr.IsWorkInProgress() {
773+
ctx.Error(http.StatusMethodNotAllowed, "PR is a work in progress", "Work in progress PRs cannot be merged")
764774
return
765775
}
766776

@@ -812,7 +822,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
812822

813823
if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
814824
if models.IsErrInvalidMergeStyle(err) {
815-
ctx.Status(http.StatusMethodNotAllowed)
825+
ctx.Error(http.StatusMethodNotAllowed, "Invalid merge style", fmt.Errorf("%s is not allowed an allowed merge style for this repository", models.MergeStyle(form.Do)))
816826
return
817827
} else if models.IsErrMergeConflicts(err) {
818828
conflictError := err.(models.ErrMergeConflicts)

0 commit comments

Comments
 (0)