@@ -208,74 +208,53 @@ func TestAPIMergePull(t *testing.T) {
208208 return & prDTO
209209 }
210210
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 )
211+ performMerge := func (t * testing.T , prIndex int64 , params map [string ]any , optExpectedStatus ... int ) {
212+ req := NewRequestWithJSON (t , http .MethodPost , fmt .Sprintf ("/api/v1/repos/%s/%s/pulls/%d/merge" , owner .Name , repo .Name , prIndex ), params ).AddTokenAuth (apiCtx .Token )
216213 expectedStatus := util .OptionalArg (optExpectedStatus , http .StatusOK )
217214 MakeRequest (t , req , expectedStatus )
218215 }
219216
220217 t .Run ("Normal" , func (t * testing.T ) {
221218 newBranch := "test-pull-1"
222219 prDTO := createTestBranchPR (t , newBranch )
223- performMerge (t , prDTO .Index , nil )
220+ performMerge (t , prDTO .Index , map [ string ] any { "do" : "merge" } )
224221 checkBranchExists (t , newBranch , http .StatusOK )
225- performMerge (t , prDTO .Index , nil , http .StatusMethodNotAllowed ) // make sure we cannot perform a merge on the same PR
222+ // try to merge again, make sure we cannot perform a merge on the same PR
223+ performMerge (t , prDTO .Index , map [string ]any {"do" : "merge" }, http .StatusMethodNotAllowed )
226224 })
227225
228226 t .Run ("DeleteBranchAfterMergePassedByFormField" , func (t * testing.T ) {
229227 newBranch := "test-pull-2"
230228 prDTO := createTestBranchPR (t , newBranch )
231- performMerge (t , prDTO .Index , util . ToPointer ( true ) )
229+ performMerge (t , prDTO .Index , map [ string ] any { "do" : "merge" , "delete_branch_after_merge" : true } )
232230 checkBranchExists (t , newBranch , http .StatusNotFound )
233231 })
234232
235- t .Run ("DeleteBranchAfterMergePassedByRepoSettings" , func (t * testing.T ) {
236- newBranch := "test-pull-3"
237- prDTO := createTestBranchPR (t , newBranch )
238-
239- // set the default branch after merge setting at the repo level
233+ updateRepoUnitDefaultDeleteBranchAfterMerge := func (t * testing.T , repo * repo_model.Repository , value bool ) {
240234 prUnit , err := repo .GetUnit (t .Context (), unit_model .TypePullRequests )
241235 require .NoError (t , err )
242236
243- prConfig := prUnit .PullRequestsConfig ()
244- prConfig .DefaultDeleteBranchAfterMerge = true
245-
246- var units []repo_model.RepoUnit
247- units = append (units , repo_model.RepoUnit {
237+ prUnit .PullRequestsConfig ().DefaultDeleteBranchAfterMerge = value
238+ require .NoError (t , repo_service .UpdateRepositoryUnits (t .Context (), repo , []repo_model.RepoUnit {{
248239 RepoID : repo .ID ,
249240 Type : unit_model .TypePullRequests ,
250- Config : prConfig ,
251- })
252- require . NoError ( t , repo_service . UpdateRepositoryUnits ( t . Context (), repo , units , nil ))
241+ Config : prUnit . PullRequestsConfig () ,
242+ }}, nil ) )
243+ }
253244
254- performMerge (t , prDTO .Index , nil )
245+ t .Run ("DeleteBranchAfterMergePassedByRepoSettings" , func (t * testing.T ) {
246+ newBranch := "test-pull-3"
247+ prDTO := createTestBranchPR (t , newBranch )
248+ updateRepoUnitDefaultDeleteBranchAfterMerge (t , repo , true )
249+ performMerge (t , prDTO .Index , map [string ]any {"do" : "merge" })
255250 checkBranchExists (t , newBranch , http .StatusNotFound )
256251 })
257252
258253 t .Run ("DeleteBranchAfterMergeFormFieldIsSetButNotRepoSettings" , func (t * testing.T ) {
259254 newBranch := "test-pull-4"
260255 prDTO := createTestBranchPR (t , newBranch )
261-
262- // make sure the default branch after merge setting is unset at the repo level
263- prUnit , err := repo .GetUnit (t .Context (), unit_model .TypePullRequests )
264- require .NoError (t , err )
265-
266- prConfig := prUnit .PullRequestsConfig ()
267- prConfig .DefaultDeleteBranchAfterMerge = false
268-
269- var units []repo_model.RepoUnit
270- units = append (units , repo_model.RepoUnit {
271- RepoID : repo .ID ,
272- Type : unit_model .TypePullRequests ,
273- Config : prConfig ,
274- })
275- require .NoError (t , repo_service .UpdateRepositoryUnits (t .Context (), repo , units , nil ))
276-
277- // perform merge
278- performMerge (t , prDTO .Index , util .ToPointer (true ))
256+ updateRepoUnitDefaultDeleteBranchAfterMerge (t , repo , false )
257+ performMerge (t , prDTO .Index , map [string ]any {"do" : "merge" , "delete_branch_after_merge" : true })
279258 checkBranchExists (t , newBranch , http .StatusNotFound )
280259 })
281260 })
0 commit comments