- 
          
- 
                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
Changes from 6 commits
3b468a7
              61c4e69
              01dce97
              6d5d154
              20b5d9d
              62fba4f
              da4bb40
              64db368
              4df4d45
              6998025
              2a57ea3
              a68474a
              8882022
              4fddb54
              7059275
              b7bd7be
              c0924f9
              f2f6a9f
              9bb30fa
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(user *user_model.User, token, branchName string, repo *repo_model.Repository, status int) { | ||
|         
                  wxiaoguang marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| 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" | ||
| prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo) | ||
|  | ||
| req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{ | ||
| Do: "merge", | ||
| }).AddTokenAuth(token) | ||
|  | ||
| MakeRequest(t, req, http.StatusOK) | ||
| doCheckBranchExists(owner, token, newBranch, repo, http.StatusOK) | ||
| // make sure we cannot perform a merge on the same PR | ||
| MakeRequest(t, req, http.StatusUnprocessableEntity) | ||
|          | ||
| doCheckBranchExists(owner, token, newBranch, repo, http.StatusOK) | ||
| }) | ||
|  | ||
| t.Run("DeleteBranchAfterMergePassedByFormField", func(t *testing.T) { | ||
| newBranch := "test-pull-2" | ||
| prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo) | ||
| deleteBranch := true | ||
|  | ||
| req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{ | ||
| Do: "merge", | ||
| DeleteBranchAfterMerge: &deleteBranch, | ||
| }).AddTokenAuth(token) | ||
|  | ||
| MakeRequest(t, req, http.StatusOK) | ||
| doCheckBranchExists(owner, token, newBranch, repo, http.StatusNotFound) | ||
| }) | ||
|  | ||
| t.Run("DeleteBranchAfterMergePassedByRepoSettings", func(t *testing.T) { | ||
| newBranch := "test-pull-3" | ||
| prResp := creatPullRequestWithCommit(t, owner, token, 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, prResp.Index), &forms.MergePullRequestForm{ | ||
| Do: "merge", | ||
| }).AddTokenAuth(token) | ||
|  | ||
| MakeRequest(t, req, http.StatusOK) | ||
| doCheckBranchExists(owner, token, newBranch, repo, http.StatusNotFound) | ||
| }) | ||
|  | ||
| t.Run("DeleteBranchAfterMergeFormFieldIsSetButNotRepoSettings", func(t *testing.T) { | ||
| newBranch := "test-pull-4" | ||
| prResp := creatPullRequestWithCommit(t, owner, token, 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, prResp.Index), &forms.MergePullRequestForm{ | ||
| Do: "merge", | ||
| DeleteBranchAfterMerge: &deleteBranch, | ||
| }).AddTokenAuth(token) | ||
|  | ||
| MakeRequest(t, req, http.StatusOK) | ||
| doCheckBranchExists(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,46 @@ func TestAPIViewPullFilesWithHeadRepoDeleted(t *testing.T) { | |
| })(t) | ||
| }) | ||
| } | ||
|  | ||
| func creatPullRequestWithCommit(t *testing.T, user *user_model.User, token, 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 | ||
| req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", user.Name, repo.Name), &api.CreatePullRequestOption{ | ||
| Head: newBranch, | ||
| Base: repo.DefaultBranch, | ||
| Title: "Add a test file", | ||
| }).AddTokenAuth(token) | ||
| resp := MakeRequest(t, req, http.StatusCreated) | ||
| pull := new(api.PullRequest) | ||
| DecodeJSON(t, resp, pull) | ||
|  | ||
| return pull | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.