@@ -22,6 +22,7 @@ import (
2222 user_model "code.gitea.io/gitea/models/user"
2323 "code.gitea.io/gitea/modules/setting"
2424 api "code.gitea.io/gitea/modules/structs"
25+ "code.gitea.io/gitea/modules/util"
2526 "code.gitea.io/gitea/services/convert"
2627 "code.gitea.io/gitea/services/forms"
2728 "code.gitea.io/gitea/services/gitdiff"
@@ -190,49 +191,50 @@ func TestAPIMergePullWIP(t *testing.T) {
190191}
191192
192193func TestAPIMergePull (t * testing.T ) {
193- doCheckBranchExists := func (t * testing.T , user * user_model.User , token , branchName string , repo * repo_model.Repository , status int ) {
194- req := NewRequest (t , "GET" , fmt .Sprintf ("/api/v1/repos/%s/%s/branches/%s" , user .Name , repo .Name , branchName )).AddTokenAuth (token )
195- MakeRequest (t , req , status )
196- }
197-
198194 onGiteaRun (t , func (t * testing.T , giteaURL * url.URL ) {
199195 repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
200196 owner := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : repo .OwnerID })
201- session := loginUser (t , owner .Name )
202- token := getTokenForLoggedInUser (t , session , auth_model .AccessTokenScopeWriteRepository )
197+ apiCtx := NewAPITestContext (t , repo .OwnerName , repo .Name , auth_model .AccessTokenScopeWriteRepository )
203198
204- t .Run ("Normal" , func (t * testing.T ) {
205- newBranch := "test-pull-1"
206- prDTO := createPullRequestWithCommit (t , owner , newBranch , repo )
199+ checkBranchExists := func (t * testing.T , branchName string , status int ) {
200+ req := NewRequest (t , "GET" , fmt .Sprintf ("/api/v1/repos/%s/%s/branches/%s" , owner .Name , repo .Name , branchName )).AddTokenAuth (apiCtx .Token )
201+ MakeRequest (t , req , status )
202+ }
207203
208- req := NewRequestWithJSON (t , http .MethodPost , fmt .Sprintf ("/api/v1/repos/%s/%s/pulls/%d/merge" , owner .Name , repo .Name , prDTO .Index ), & forms.MergePullRequestForm {
209- Do : "merge" ,
210- }).AddTokenAuth (token )
204+ createTestBranchPR := func (t * testing.T , branchName string ) * api.PullRequest {
205+ testCreateFileInBranch (t , owner , repo , createFileInBranchOptions {NewBranch : branchName }, map [string ]string {"a-new-file-" + branchName + ".txt" : "dummy content" })
206+ prDTO , err := doAPICreatePullRequest (apiCtx , repo .OwnerName , repo .Name , repo .DefaultBranch , branchName )(t )
207+ require .NoError (t , err )
208+ return & prDTO
209+ }
211210
212- MakeRequest (t , req , http .StatusOK )
213- doCheckBranchExists (t , owner , token , newBranch , repo , http .StatusOK )
214- // make sure we cannot perform a merge on the same PR
215- MakeRequest (t , req , http .StatusUnprocessableEntity )
216- doCheckBranchExists (t , owner , token , newBranch , repo , http .StatusOK )
211+ performMerge := func (t * testing.T , prIndex int64 , deleteBranchAfterMerge * bool , optExpectedStatus ... int ) {
212+ req := NewRequestWithJSON (t , http .MethodPost , fmt .Sprintf ("/api/v1/repos/%s/%s/pulls/%d/merge" , owner .Name , repo .Name , prIndex ), & forms.MergePullRequestForm {
213+ Do : "merge" ,
214+ DeleteBranchAfterMerge : deleteBranchAfterMerge ,
215+ }).AddTokenAuth (apiCtx .Token )
216+ expectedStatus := util .OptionalArg (optExpectedStatus , http .StatusOK )
217+ MakeRequest (t , req , expectedStatus )
218+ }
219+
220+ t .Run ("Normal" , func (t * testing.T ) {
221+ newBranch := "test-pull-1"
222+ prDTO := createTestBranchPR (t , newBranch )
223+ performMerge (t , prDTO .Index , nil )
224+ checkBranchExists (t , newBranch , http .StatusOK )
225+ performMerge (t , prDTO .Index , nil , http .StatusMethodNotAllowed ) // make sure we cannot perform a merge on the same PR
217226 })
218227
219228 t .Run ("DeleteBranchAfterMergePassedByFormField" , func (t * testing.T ) {
220229 newBranch := "test-pull-2"
221- prDTO := createPullRequestWithCommit (t , owner , newBranch , repo )
222- deleteBranch := true
223-
224- req := NewRequestWithJSON (t , http .MethodPost , fmt .Sprintf ("/api/v1/repos/%s/%s/pulls/%d/merge" , owner .Name , repo .Name , prDTO .Index ), & forms.MergePullRequestForm {
225- Do : "merge" ,
226- DeleteBranchAfterMerge : & deleteBranch ,
227- }).AddTokenAuth (token )
228-
229- MakeRequest (t , req , http .StatusOK )
230- doCheckBranchExists (t , owner , token , newBranch , repo , http .StatusNotFound )
230+ prDTO := createTestBranchPR (t , newBranch )
231+ performMerge (t , prDTO .Index , util .ToPointer (true ))
232+ checkBranchExists (t , newBranch , http .StatusNotFound )
231233 })
232234
233235 t .Run ("DeleteBranchAfterMergePassedByRepoSettings" , func (t * testing.T ) {
234236 newBranch := "test-pull-3"
235- prDTO := createPullRequestWithCommit (t , owner , newBranch , repo )
237+ prDTO := createTestBranchPR (t , newBranch )
236238
237239 // set the default branch after merge setting at the repo level
238240 prUnit , err := repo .GetUnit (t .Context (), unit_model .TypePullRequests )
@@ -249,18 +251,13 @@ func TestAPIMergePull(t *testing.T) {
249251 })
250252 require .NoError (t , repo_service .UpdateRepositoryUnits (t .Context (), repo , units , nil ))
251253
252- // perform merge
253- req := NewRequestWithJSON (t , http .MethodPost , fmt .Sprintf ("/api/v1/repos/%s/%s/pulls/%d/merge" , owner .Name , repo .Name , prDTO .Index ), & forms.MergePullRequestForm {
254- Do : "merge" ,
255- }).AddTokenAuth (token )
256-
257- MakeRequest (t , req , http .StatusOK )
258- doCheckBranchExists (t , owner , token , newBranch , repo , http .StatusNotFound )
254+ performMerge (t , prDTO .Index , nil )
255+ checkBranchExists (t , newBranch , http .StatusNotFound )
259256 })
260257
261258 t .Run ("DeleteBranchAfterMergeFormFieldIsSetButNotRepoSettings" , func (t * testing.T ) {
262259 newBranch := "test-pull-4"
263- prDTO := createPullRequestWithCommit (t , owner , newBranch , repo )
260+ prDTO := createTestBranchPR (t , newBranch )
264261
265262 // make sure the default branch after merge setting is unset at the repo level
266263 prUnit , err := repo .GetUnit (t .Context (), unit_model .TypePullRequests )
@@ -278,14 +275,8 @@ func TestAPIMergePull(t *testing.T) {
278275 require .NoError (t , repo_service .UpdateRepositoryUnits (t .Context (), repo , units , nil ))
279276
280277 // 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 , prDTO .Index ), & forms.MergePullRequestForm {
283- Do : "merge" ,
284- DeleteBranchAfterMerge : & deleteBranch ,
285- }).AddTokenAuth (token )
286-
287- MakeRequest (t , req , http .StatusOK )
288- doCheckBranchExists (t , owner , token , newBranch , repo , http .StatusNotFound )
278+ performMerge (t , prDTO .Index , util .ToPointer (true ))
279+ checkBranchExists (t , newBranch , http .StatusNotFound )
289280 })
290281 })
291282}
@@ -624,41 +615,3 @@ func TestAPIViewPullFilesWithHeadRepoDeleted(t *testing.T) {
624615 })(t )
625616 })
626617}
627-
628- func createPullRequestWithCommit (t * testing.T , user * user_model.User , newBranch string , repo * repo_model.Repository ) * api.PullRequest {
629- // create a new branch with a commit
630- filesResp , err := files_service .ChangeRepoFiles (t .Context (), repo , user , & files_service.ChangeRepoFilesOptions {
631- Files : []* files_service.ChangeRepoFile {
632- {
633- Operation : "create" ,
634- TreePath : strings .ReplaceAll (newBranch , " " , "_" ),
635- ContentReader : strings .NewReader ("This is a test." ),
636- },
637- },
638- Message : "Add test file using branch name as its name" ,
639- OldBranch : repo .DefaultBranch ,
640- NewBranch : newBranch ,
641- Author : & files_service.IdentityOptions {
642- GitUserName : user .Name ,
643- GitUserEmail : user .Email ,
644- },
645- Committer : & files_service.IdentityOptions {
646- GitUserName : user .Name ,
647- GitUserEmail : user .Email ,
648- },
649- Dates : & files_service.CommitDateOptions {
650- Author : time .Now (),
651- Committer : time .Now (),
652- },
653- })
654-
655- require .NoError (t , err )
656- require .NotEmpty (t , filesResp )
657-
658- // create a corresponding PR
659- apiCtx := NewAPITestContext (t , repo .OwnerName , repo .Name , auth_model .AccessTokenScopeWriteRepository )
660- prDTO , err := doAPICreatePullRequest (apiCtx , repo .OwnerName , repo .Name , repo .DefaultBranch , newBranch )(t )
661- require .NoError (t , err )
662-
663- return & prDTO
664- }
0 commit comments