From c8258dcce597f126839f85faa01af976ee50060e Mon Sep 17 00:00:00 2001 From: Norwin Date: Thu, 30 Sep 2021 14:38:03 +0200 Subject: [PATCH 1/4] api: dont open merged PRs --- routers/api/v1/repo/pull.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 3974069ab4ecd..2d356bed2e0d6 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -564,6 +564,10 @@ func EditPullRequest(ctx *context.APIContext) { } if form.State != nil { + if pr.HasMerged && api.StateType(*form.State) == api.StateOpen { + ctx.Error(http.StatusPreconditionFailed, "OpenMergedPR", "cannot reopen this pull request, it was already merged") + return + } issue.IsClosed = api.StateClosed == api.StateType(*form.State) } statusChangeComment, titleChanged, err := models.UpdateIssueByAPI(issue, ctx.User) From 335433e9f1f32787d09248689275d3d0f4b9fa41 Mon Sep 17 00:00:00 2001 From: Norwin Date: Thu, 30 Sep 2021 14:49:14 +0200 Subject: [PATCH 2/4] don't change base branch when already merged --- routers/api/v1/repo/pull.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 2d356bed2e0d6..13714d865cdf7 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -589,7 +589,7 @@ func EditPullRequest(ctx *context.APIContext) { } // change pull target branch - if len(form.Base) != 0 && form.Base != pr.BaseBranch { + if !pr.HasMerged && len(form.Base) != 0 && form.Base != pr.BaseBranch { if !ctx.Repo.GitRepo.IsBranchExist(form.Base) { ctx.Error(http.StatusNotFound, "NewBaseBranchNotExist", fmt.Errorf("new base '%s' not exist", form.Base)) return From 484be91f9247789678861de26b4854717be8ce5a Mon Sep 17 00:00:00 2001 From: Norwin Date: Thu, 30 Sep 2021 23:35:01 +0200 Subject: [PATCH 3/4] don't allow any state change --- routers/api/v1/repo/pull.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 13714d865cdf7..6718224d7dff1 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -564,8 +564,8 @@ func EditPullRequest(ctx *context.APIContext) { } if form.State != nil { - if pr.HasMerged && api.StateType(*form.State) == api.StateOpen { - ctx.Error(http.StatusPreconditionFailed, "OpenMergedPR", "cannot reopen this pull request, it was already merged") + if pr.HasMerged { + ctx.Error(http.StatusPreconditionFailed, "MergedPRState", "cannot change state of this pull request, it was already merged") return } issue.IsClosed = api.StateClosed == api.StateType(*form.State) From a282684c163364b6698706b87cfd9291e2b7b564 Mon Sep 17 00:00:00 2001 From: Norwin Date: Thu, 30 Sep 2021 23:35:20 +0200 Subject: [PATCH 4/4] also validate opening merged PRs in EditIssue --- routers/api/v1/repo/issue.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 7b97a5683a36b..17a3becd5bbb5 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -790,6 +790,15 @@ func EditIssue(ctx *context.APIContext) { } } if form.State != nil { + if issue.IsPull { + if pr, err := issue.GetPullRequest(); err != nil { + ctx.Error(http.StatusInternalServerError, "GetPullRequest", err) + return + } else if pr.HasMerged { + ctx.Error(http.StatusPreconditionFailed, "MergedPRState", "cannot change state of this pull request, it was already merged") + return + } + } issue.IsClosed = api.StateClosed == api.StateType(*form.State) } statusChangeComment, titleChanged, err := models.UpdateIssueByAPI(issue, ctx.User)