Skip to content

Commit 20b5d9d

Browse files
committed
Give form field higher precedence then repo setting equivalent
1 parent 6d5d154 commit 20b5d9d

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

routers/api/v1/repo/pull.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"code.gitea.io/gitea/modules/gitrepo"
2626
"code.gitea.io/gitea/modules/graceful"
2727
"code.gitea.io/gitea/modules/log"
28+
"code.gitea.io/gitea/modules/optional"
2829
"code.gitea.io/gitea/modules/setting"
2930
api "code.gitea.io/gitea/modules/structs"
3031
"code.gitea.io/gitea/modules/timeutil"
@@ -990,7 +991,7 @@ func MergePullRequest(ctx *context.APIContext) {
990991
}
991992

992993
if form.MergeWhenChecksSucceed {
993-
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message, form.DeleteBranchAfterMerge)
994+
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message, optional.FromPtr(form.DeleteBranchAfterMerge).ValueOrDefault(false))
994995
if err != nil {
995996
if pull_model.IsErrAlreadyScheduledToAutoMerge(err) {
996997
ctx.APIError(http.StatusConflict, err)
@@ -1042,8 +1043,14 @@ func MergePullRequest(ctx *context.APIContext) {
10421043
}
10431044

10441045
// for agit flow, we should not delete the agit reference after merge
1045-
shouldDeleteBranch := (prUnit.PullRequestsConfig().DefaultDeleteBranchAfterMerge || form.DeleteBranchAfterMerge) &&
1046-
pr.Flow == issues_model.PullRequestFlowGithub
1046+
shouldDeleteBranch := pr.Flow == issues_model.PullRequestFlowGithub
1047+
if form.DeleteBranchAfterMerge != nil {
1048+
// if the form field is defined, it takes precedence over the repo setting equivalent
1049+
shouldDeleteBranch = shouldDeleteBranch && *form.DeleteBranchAfterMerge
1050+
} else {
1051+
// otherwise, we look at the repo setting to make the determination
1052+
shouldDeleteBranch = shouldDeleteBranch && prUnit.PullRequestsConfig().DefaultDeleteBranchAfterMerge
1053+
}
10471054

10481055
if shouldDeleteBranch {
10491056
// check permission even it has been checked in repo_service.DeleteBranch so that we don't need to

routers/web/repo/pull.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"code.gitea.io/gitea/modules/graceful"
3131
issue_template "code.gitea.io/gitea/modules/issue/template"
3232
"code.gitea.io/gitea/modules/log"
33+
"code.gitea.io/gitea/modules/optional"
3334
"code.gitea.io/gitea/modules/setting"
3435
"code.gitea.io/gitea/modules/templates"
3536
"code.gitea.io/gitea/modules/util"
@@ -1099,7 +1100,7 @@ func MergePullRequest(ctx *context.Context) {
10991100
// delete all scheduled auto merges
11001101
_ = pull_model.DeleteScheduledAutoMerge(ctx, pr.ID)
11011102
// schedule auto merge
1102-
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message, form.DeleteBranchAfterMerge)
1103+
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message, optional.FromPtr(form.DeleteBranchAfterMerge).ValueOrDefault(false))
11031104
if err != nil {
11041105
ctx.ServerError("ScheduleAutoMerge", err)
11051106
return
@@ -1185,7 +1186,7 @@ func MergePullRequest(ctx *context.Context) {
11851186

11861187
log.Trace("Pull request merged: %d", pr.ID)
11871188

1188-
if !form.DeleteBranchAfterMerge {
1189+
if !optional.FromPtr(form.DeleteBranchAfterMerge).ValueOrDefault(false) {
11891190
ctx.JSONRedirect(issue.Link())
11901191
return
11911192
}

services/forms/repo_form.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ type MergePullRequestForm struct {
540540
HeadCommitID string `json:"head_commit_id,omitempty"`
541541
ForceMerge bool `json:"force_merge,omitempty"`
542542
MergeWhenChecksSucceed bool `json:"merge_when_checks_succeed,omitempty"`
543-
DeleteBranchAfterMerge bool `json:"delete_branch_after_merge,omitempty"`
543+
DeleteBranchAfterMerge *bool `json:"delete_branch_after_merge,omitempty"`
544544
}
545545

546546
// Validate validates the fields

tests/integration/api_pull_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,11 @@ func TestAPIMergePull(t *testing.T) {
219219
t.Run("DeleteBranchAfterMergePassedByFormField", func(t *testing.T) {
220220
newBranch := "test-pull-2"
221221
prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo)
222+
deleteBranch := true
222223

223224
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{
224225
Do: "merge",
225-
DeleteBranchAfterMerge: true,
226+
DeleteBranchAfterMerge: &deleteBranch,
226227
}).AddTokenAuth(token)
227228

228229
MakeRequest(t, req, http.StatusOK)
@@ -256,6 +257,36 @@ func TestAPIMergePull(t *testing.T) {
256257
MakeRequest(t, req, http.StatusOK)
257258
doCheckBranchExists(owner, token, newBranch, repo, http.StatusNotFound)
258259
})
260+
261+
t.Run("DeleteBranchAfterMergeFormFieldIsSetButNotRepoSettings", func(t *testing.T) {
262+
newBranch := "test-pull-4"
263+
prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo)
264+
265+
// make sure the default branch after merge setting is unset at the repo level
266+
prUnit, err := repo.GetUnit(t.Context(), unit_model.TypePullRequests)
267+
require.NoError(t, err)
268+
269+
prConfig := prUnit.PullRequestsConfig()
270+
prConfig.DefaultDeleteBranchAfterMerge = false
271+
272+
var units []repo_model.RepoUnit
273+
units = append(units, repo_model.RepoUnit{
274+
RepoID: repo.ID,
275+
Type: unit_model.TypePullRequests,
276+
Config: prConfig,
277+
})
278+
require.NoError(t, repo_service.UpdateRepositoryUnits(t.Context(), repo, units, nil))
279+
280+
// perform merge
281+
deleteBranch := true
282+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{
283+
Do: "merge",
284+
DeleteBranchAfterMerge: &deleteBranch,
285+
}).AddTokenAuth(token)
286+
287+
MakeRequest(t, req, http.StatusOK)
288+
doCheckBranchExists(owner, token, newBranch, repo, http.StatusNotFound)
289+
})
259290
})
260291
}
261292

0 commit comments

Comments
 (0)