Skip to content

Commit 1f45b3a

Browse files
noerwjolheiser6543
authored
API: don't allow merged PRs to be reopened (#17192)
* api: dont open merged PRs * don't change base branch when already merged * don't allow any state change * also validate opening merged PRs in EditIssue Co-authored-by: John Olheiser <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 8f75a55 commit 1f45b3a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

routers/api/v1/repo/issue.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,15 @@ func EditIssue(ctx *context.APIContext) {
790790
}
791791
}
792792
if form.State != nil {
793+
if issue.IsPull {
794+
if pr, err := issue.GetPullRequest(); err != nil {
795+
ctx.Error(http.StatusInternalServerError, "GetPullRequest", err)
796+
return
797+
} else if pr.HasMerged {
798+
ctx.Error(http.StatusPreconditionFailed, "MergedPRState", "cannot change state of this pull request, it was already merged")
799+
return
800+
}
801+
}
793802
issue.IsClosed = api.StateClosed == api.StateType(*form.State)
794803
}
795804
statusChangeComment, titleChanged, err := models.UpdateIssueByAPI(issue, ctx.User)

routers/api/v1/repo/pull.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ func EditPullRequest(ctx *context.APIContext) {
564564
}
565565

566566
if form.State != nil {
567+
if pr.HasMerged {
568+
ctx.Error(http.StatusPreconditionFailed, "MergedPRState", "cannot change state of this pull request, it was already merged")
569+
return
570+
}
567571
issue.IsClosed = api.StateClosed == api.StateType(*form.State)
568572
}
569573
statusChangeComment, titleChanged, err := models.UpdateIssueByAPI(issue, ctx.User)
@@ -585,7 +589,7 @@ func EditPullRequest(ctx *context.APIContext) {
585589
}
586590

587591
// change pull target branch
588-
if len(form.Base) != 0 && form.Base != pr.BaseBranch {
592+
if !pr.HasMerged && len(form.Base) != 0 && form.Base != pr.BaseBranch {
589593
if !ctx.Repo.GitRepo.IsBranchExist(form.Base) {
590594
ctx.Error(http.StatusNotFound, "NewBaseBranchNotExist", fmt.Errorf("new base '%s' not exist", form.Base))
591595
return

0 commit comments

Comments
 (0)