-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Honor delete branch on merge repo setting when using merge API #35488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lunny
merged 19 commits into
go-gitea:main
from
kemzeb:fix/inherit-merge-api-delete-branch-repo-settings
Oct 22, 2025
Merged
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3b468a7
Honor delete branch on merge repo setting when using merge API
kemzeb 61c4e69
PR linter feedback
kemzeb 01dce97
Add tests
kemzeb 6d5d154
CI feedback
kemzeb 20b5d9d
Give form field higher precedence then repo setting equivalent
kemzeb 62fba4f
Merge branch 'main' into fix/inherit-merge-api-delete-branch-repo-set…
kemzeb da4bb40
PR feedback + some touchups
kemzeb 64db368
fix
wxiaoguang 4df4d45
fix test
wxiaoguang 6998025
fine tune
wxiaoguang 2a57ea3
make ScheduleAutoMerge share the same "delete branch after merge" logic
wxiaoguang a68474a
rewrite
wxiaoguang 8882022
fix
wxiaoguang 4fddb54
fix test
wxiaoguang 7059275
fine tune
wxiaoguang b7bd7be
Merge branch 'main' into fix-inherit-merge-api-delete-branch-repo-set…
wxiaoguang c0924f9
Merge branch 'main' into fix/inherit-merge-api-delete-branch-repo-set…
GiteaBot f2f6a9f
Merge branch 'main' into fix/inherit-merge-api-delete-branch-repo-set…
GiteaBot 9bb30fa
Merge branch 'main' into fix/inherit-merge-api-delete-branch-repo-set…
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import ( | |
| issues_model "code.gitea.io/gitea/models/issues" | ||
| "code.gitea.io/gitea/models/perm" | ||
| repo_model "code.gitea.io/gitea/models/repo" | ||
| unit_model "code.gitea.io/gitea/models/unit" | ||
| "code.gitea.io/gitea/models/unittest" | ||
| user_model "code.gitea.io/gitea/models/user" | ||
| "code.gitea.io/gitea/modules/setting" | ||
|
|
@@ -26,10 +27,12 @@ import ( | |
| "code.gitea.io/gitea/services/gitdiff" | ||
| issue_service "code.gitea.io/gitea/services/issue" | ||
| pull_service "code.gitea.io/gitea/services/pull" | ||
| repo_service "code.gitea.io/gitea/services/repository" | ||
| files_service "code.gitea.io/gitea/services/repository/files" | ||
| "code.gitea.io/gitea/tests" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestAPIViewPulls(t *testing.T) { | ||
|
|
@@ -186,6 +189,107 @@ func TestAPIMergePullWIP(t *testing.T) { | |
| MakeRequest(t, req, http.StatusMethodNotAllowed) | ||
| } | ||
|
|
||
| func TestAPIMergePull(t *testing.T) { | ||
| doCheckBranchExists := func(t *testing.T, user *user_model.User, token, branchName string, repo *repo_model.Repository, status int) { | ||
| req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/branches/%s", user.Name, repo.Name, branchName)).AddTokenAuth(token) | ||
| MakeRequest(t, req, status) | ||
| } | ||
|
|
||
| onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { | ||
| repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) | ||
| owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) | ||
| session := loginUser(t, owner.Name) | ||
| token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||
|
|
||
| t.Run("Normal", func(t *testing.T) { | ||
| newBranch := "test-pull-1" | ||
| prDTO := createPullRequestWithCommit(t, owner, newBranch, repo) | ||
|
|
||
| req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prDTO.Index), &forms.MergePullRequestForm{ | ||
| Do: "merge", | ||
| }).AddTokenAuth(token) | ||
|
|
||
| MakeRequest(t, req, http.StatusOK) | ||
| doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusOK) | ||
| // make sure we cannot perform a merge on the same PR | ||
| MakeRequest(t, req, http.StatusUnprocessableEntity) | ||
|
||
| doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusOK) | ||
| }) | ||
|
|
||
| t.Run("DeleteBranchAfterMergePassedByFormField", func(t *testing.T) { | ||
| newBranch := "test-pull-2" | ||
| prDTO := createPullRequestWithCommit(t, owner, newBranch, repo) | ||
| deleteBranch := true | ||
|
|
||
| req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prDTO.Index), &forms.MergePullRequestForm{ | ||
| Do: "merge", | ||
| DeleteBranchAfterMerge: &deleteBranch, | ||
| }).AddTokenAuth(token) | ||
|
|
||
| MakeRequest(t, req, http.StatusOK) | ||
| doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusNotFound) | ||
| }) | ||
|
|
||
| t.Run("DeleteBranchAfterMergePassedByRepoSettings", func(t *testing.T) { | ||
| newBranch := "test-pull-3" | ||
| prDTO := createPullRequestWithCommit(t, owner, newBranch, repo) | ||
|
|
||
| // set the default branch after merge setting at the repo level | ||
| prUnit, err := repo.GetUnit(t.Context(), unit_model.TypePullRequests) | ||
| require.NoError(t, err) | ||
|
|
||
| prConfig := prUnit.PullRequestsConfig() | ||
| prConfig.DefaultDeleteBranchAfterMerge = true | ||
|
|
||
| var units []repo_model.RepoUnit | ||
| units = append(units, repo_model.RepoUnit{ | ||
| RepoID: repo.ID, | ||
| Type: unit_model.TypePullRequests, | ||
| Config: prConfig, | ||
| }) | ||
| require.NoError(t, repo_service.UpdateRepositoryUnits(t.Context(), repo, units, nil)) | ||
|
|
||
| // perform merge | ||
| req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prDTO.Index), &forms.MergePullRequestForm{ | ||
| Do: "merge", | ||
| }).AddTokenAuth(token) | ||
|
|
||
| MakeRequest(t, req, http.StatusOK) | ||
| doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusNotFound) | ||
| }) | ||
|
|
||
| t.Run("DeleteBranchAfterMergeFormFieldIsSetButNotRepoSettings", func(t *testing.T) { | ||
| newBranch := "test-pull-4" | ||
| prDTO := createPullRequestWithCommit(t, owner, newBranch, repo) | ||
|
|
||
| // make sure the default branch after merge setting is unset at the repo level | ||
| prUnit, err := repo.GetUnit(t.Context(), unit_model.TypePullRequests) | ||
| require.NoError(t, err) | ||
|
|
||
| prConfig := prUnit.PullRequestsConfig() | ||
| prConfig.DefaultDeleteBranchAfterMerge = false | ||
|
|
||
| var units []repo_model.RepoUnit | ||
| units = append(units, repo_model.RepoUnit{ | ||
| RepoID: repo.ID, | ||
| Type: unit_model.TypePullRequests, | ||
| Config: prConfig, | ||
| }) | ||
| require.NoError(t, repo_service.UpdateRepositoryUnits(t.Context(), repo, units, nil)) | ||
|
|
||
| // perform merge | ||
| deleteBranch := true | ||
| req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prDTO.Index), &forms.MergePullRequestForm{ | ||
| Do: "merge", | ||
| DeleteBranchAfterMerge: &deleteBranch, | ||
| }).AddTokenAuth(token) | ||
|
|
||
| MakeRequest(t, req, http.StatusOK) | ||
| doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusNotFound) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func TestAPICreatePullSuccess(t *testing.T) { | ||
| defer tests.PrepareTestEnv(t)() | ||
| repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}) | ||
|
|
@@ -520,3 +624,41 @@ func TestAPIViewPullFilesWithHeadRepoDeleted(t *testing.T) { | |
| })(t) | ||
| }) | ||
| } | ||
|
|
||
| func createPullRequestWithCommit(t *testing.T, user *user_model.User, newBranch string, repo *repo_model.Repository) *api.PullRequest { | ||
| // create a new branch with a commit | ||
| filesResp, err := files_service.ChangeRepoFiles(t.Context(), repo, user, &files_service.ChangeRepoFilesOptions{ | ||
| Files: []*files_service.ChangeRepoFile{ | ||
| { | ||
| Operation: "create", | ||
| TreePath: strings.ReplaceAll(newBranch, " ", "_"), | ||
| ContentReader: strings.NewReader("This is a test."), | ||
| }, | ||
| }, | ||
| Message: "Add test file using branch name as its name", | ||
| OldBranch: repo.DefaultBranch, | ||
| NewBranch: newBranch, | ||
| Author: &files_service.IdentityOptions{ | ||
| GitUserName: user.Name, | ||
| GitUserEmail: user.Email, | ||
| }, | ||
| Committer: &files_service.IdentityOptions{ | ||
| GitUserName: user.Name, | ||
| GitUserEmail: user.Email, | ||
| }, | ||
| Dates: &files_service.CommitDateOptions{ | ||
| Author: time.Now(), | ||
| Committer: time.Now(), | ||
| }, | ||
| }) | ||
|
|
||
| require.NoError(t, err) | ||
| require.NotEmpty(t, filesResp) | ||
|
|
||
| // create a corresponding PR | ||
| apiCtx := NewAPITestContext(t, repo.OwnerName, repo.Name, auth_model.AccessTokenScopeWriteRepository) | ||
| prDTO, err := doAPICreatePullRequest(apiCtx, repo.OwnerName, repo.Name, repo.DefaultBranch, newBranch)(t) | ||
| require.NoError(t, err) | ||
|
|
||
| return &prDTO | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.