@@ -80,6 +80,7 @@ func testGitGeneral(t *testing.T, u *url.URL) {
80
80
mediaTest (t , & httpContext , pushedFilesStandard [0 ], pushedFilesStandard [1 ], pushedFilesLFS [0 ], pushedFilesLFS [1 ])
81
81
82
82
t .Run ("CreateAgitFlowPull" , doCreateAgitFlowPull (dstPath , & httpContext , "test/head" ))
83
+ t .Run ("CreateProtectedBranch" , doCreateProtectedBranch (& httpContext , dstPath ))
83
84
t .Run ("BranchProtectMerge" , doBranchProtectPRMerge (& httpContext , dstPath ))
84
85
t .Run ("AutoMerge" , doAutoPRMerge (& httpContext , dstPath ))
85
86
t .Run ("CreatePRAndSetManuallyMerged" , doCreatePRAndSetManuallyMerged (httpContext , httpContext , dstPath , "master" , "test-manually-merge" ))
@@ -121,6 +122,7 @@ func testGitGeneral(t *testing.T, u *url.URL) {
121
122
mediaTest (t , & sshContext , pushedFilesStandard [0 ], pushedFilesStandard [1 ], pushedFilesLFS [0 ], pushedFilesLFS [1 ])
122
123
123
124
t .Run ("CreateAgitFlowPull" , doCreateAgitFlowPull (dstPath , & sshContext , "test/head2" ))
125
+ t .Run ("CreateProtectedBranch" , doCreateProtectedBranch (& sshContext , dstPath ))
124
126
t .Run ("BranchProtectMerge" , doBranchProtectPRMerge (& sshContext , dstPath ))
125
127
t .Run ("MergeFork" , func (t * testing.T ) {
126
128
defer tests .PrintCurrentTest (t )()
@@ -325,6 +327,34 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
325
327
return filepath .Base (tmpFile .Name ()), err
326
328
}
327
329
330
+ func doCreateProtectedBranch (baseCtx * APITestContext , dstPath string ) func (t * testing.T ) {
331
+ return func (t * testing.T ) {
332
+ defer tests .PrintCurrentTest (t )()
333
+ ctx := NewAPITestContext (t , baseCtx .Username , baseCtx .Reponame , auth_model .AccessTokenScopeWriteRepository )
334
+
335
+ t .Run ("ProtectBranchWithFilePatterns" , doProtectBranch (ctx , "release-*" , baseCtx .Username , "" , "" , "config*" ))
336
+
337
+ // push a new branch without any new commits
338
+ t .Run ("CreateProtectedBranch-NoChanges" , doGitCreateBranch (dstPath , "release-v1.0" ))
339
+ t .Run ("PushProtectedBranch-NoChanges" , doGitPushTestRepository (dstPath , "origin" , "release-v1.0" ))
340
+ t .Run ("CheckoutMaster-NoChanges" , doGitCheckoutBranch (dstPath , "master" ))
341
+
342
+ // push a new branch with a new unprotected file
343
+ t .Run ("CreateProtectedBranch-UnprotectedFile" , doGitCreateBranch (dstPath , "release-v2.0" ))
344
+ _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"abc.txt" )
345
+ assert .NoError (t , err )
346
+ t .Run ("PushProtectedBranch-UnprotectedFile" , doGitPushTestRepository (dstPath , "origin" , "release-v2.0" ))
347
+ t .Run ("CheckoutMaster-UnprotectedFile" , doGitCheckoutBranch (dstPath , "master" ))
348
+
349
+ // push a new branch with a new protected file
350
+ t .Run ("CreateProtectedBranch-ProtectedFile" , doGitCreateBranch (dstPath , "release-v3.0" ))
351
+ _ ,
err = generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"config" )
352
+ assert .NoError (t , err )
353
+ t .Run ("PushProtectedBranch-ProtectedFile" , doGitPushTestRepositoryFail (dstPath , "origin" , "release-v3.0" ))
354
+ t .Run ("CheckoutMaster-ProtectedFile" , doGitCheckoutBranch (dstPath , "master" ))
355
+ }
356
+ }
357
+
328
358
func doBranchProtectPRMerge (baseCtx * APITestContext , dstPath string ) func (t * testing.T ) {
329
359
return func (t * testing.T ) {
330
360
defer tests .PrintCurrentTest (t )()
@@ -334,27 +364,23 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
334
364
ctx := NewAPITestContext (t , baseCtx .Username , baseCtx .Reponame , auth_model .AccessTokenScopeWriteRepository )
335
365
336
366
// Protect branch without any whitelisting
337
- t .Run ("ProtectBranchNoWhitelist" , func (t * testing.T ) {
338
- doProtectBranch (ctx , "protected" , "" , "" , "" )
339
- })
367
+ t .Run ("ProtectBranchNoWhitelist" , doProtectBranch (ctx , "protected" , "" , "" , "" , "" ))
340
368
341
369
// Try to push without permissions, which should fail
342
370
t .Run ("TryPushWithoutPermissions" , func (t * testing.T ) {
343
371
_ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
344
372
assert .NoError (t , err )
345
- doGitPushTestRepositoryFail (dstPath , "origin" , "protected" )
373
+ doGitPushTestRepositoryFail (dstPath , "origin" , "protected" )( t )
346
374
})
347
375
348
376
// Set up permissions for normal push but not force push
349
- t .Run ("SetupNormalPushPermissions" , func (t * testing.T ) {
350
- doProtectBranch (ctx , "protected" , baseCtx .Username , "" , "" )
351
- })
377
+ t .Run ("SetupNormalPushPermissions" , doProtectBranch (ctx , "protected" , baseCtx .Username , "" , "" , "" ))
352
378
353
379
// Normal push should work
354
380
t .Run ("NormalPushWithPermissions" , func (t * testing.T ) {
355
381
_ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
356
382
assert .NoError (t , err )
357
- doGitPushTestRepository (dstPath , "origin" , "protected" )
383
+ doGitPushTestRepository (dstPath , "origin" , "protected" )( t )
358
384
})
359
385
360
386
// Try to force push without force push permissions, which should fail
@@ -364,30 +390,22 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
364
390
_ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-new" )
365
391
assert .NoError (t , err )
366
392
})
367
- doGitPushTestRepositoryFail (dstPath , "-f" , "origin" , "protected" )
393
+ doGitPushTestRepositoryFail (dstPath , "-f" , "origin" , "protected" )( t )
368
394
})
369
395
370
396
// Set up permissions for force push but not normal push
371
- t .Run ("SetupForcePushPermissions" , func (t * testing.T ) {
372
- doProtectBranch (ctx , "protected" , "" , baseCtx .Username , "" )
373
- })
397
+ t .Run ("SetupForcePushPermissions" , doProtectBranch (ctx , "protected" , "" , baseCtx .Username , "" , "" ))
374
398
375
399
// Try to force push without normal push permissions, which should fail
376
- t .Run ("ForcePushWithoutNormalPermissions" , func (t * testing.T ) {
377
- doGitPushTestRepositoryFail (dstPath , "-f" , "origin" , "protected" )
378
- })
400
+ t .Run ("ForcePushWithoutNormalPermissions" , doGitPushTestRepositoryFail (dstPath , "-f" , "origin" , "protected" ))
379
401
380
402
// Set up permissions for normal and force push (both are required to force push)
381
- t .Run ("SetupNormalAndForcePushPermissions" , func (t * testing.T ) {
382
- doProtectBranch (ctx , "protected" , baseCtx .Username , baseCtx .Username , "" )
383
- })
403
+ t .Run ("SetupNormalAndForcePushPermissions" , doProtectBranch (ctx , "protected" , baseCtx .Username , baseCtx .Username , "" , "" ))
384
404
385
405
// Force push should now work
386
- t .Run ("ForcePushWithPermissions" , func (t * testing.T ) {
387
- doGitPushTestRepository (dstPath , "-f" , "origin" , "protected" )
388
- })
406
+ t .Run ("ForcePushWithPermissions" , doGitPushTestRepository (dstPath , "-f" , "origin" , "protected" ))
389
407
390
- t .Run ("ProtectProtectedBranchNoWhitelist" , doProtectBranch (ctx , "protected" , "" , "" , "" ))
408
+ t .Run ("ProtectProtectedBranchNoWhitelist" , doProtectBranch (ctx , "protected" , "" , "" , "" , "" ))
391
409
t .Run ("PushToUnprotectedBranch" , doGitPushTestRepository (dstPath , "origin" , "protected:unprotected" ))
392
410
var pr api.PullRequest
393
411
var err error
@@ -409,14 +427,14 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
409
427
t .Run ("MergePR" , doAPIMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
410
428
t .Run ("PullProtected" , doGitPull (dstPath , "origin" , "protected" ))
411
429
412
- t .Run ("ProtectProtectedBranchUnprotectedFilePaths" , doProtectBranch (ctx , "protected" , "" , "" , "unprotected-file-*" ))
430
+ t .Run ("ProtectProtectedBranchUnprotectedFilePaths" , doProtectBranch (ctx , "protected" , "" , "" , "unprotected-file-*" , "" ))
413
431
t .Run ("GenerateCommit" , func (t * testing.T ) {
414
432
_ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"unprotected-file-" )
415
433
assert .NoError (t , err )
416
434
})
417
435
t .Run ("PushUnprotectedFilesToProtectedBranch" , doGitPushTestRepository (dstPath , "origin" , "protected" ))
418
436
419
- t .Run ("ProtectProtectedBranchWhitelist" , doProtectBranch (ctx , "protected" , baseCtx .Username , "" , "" ))
437
+ t .Run ("ProtectProtectedBranchWhitelist" , doProtectBranch (ctx , "protected" , baseCtx .Username , "" , "" , "" ))
420
438
421
439
t .Run ("CheckoutMaster" , doGitCheckoutBranch (dstPath , "master" ))
422
440
t .Run ("CreateBranchForced" , doGitCreateBranch (dstPath , "toforce" ))
@@ -431,7 +449,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
431
449
}
432
450
}
433
451
434
- func doProtectBranch (ctx APITestContext , branch , userToWhitelistPush , userToWhitelistForcePush , unprotectedFilePatterns string ) func (t * testing.T ) {
452
+ func doProtectBranch (ctx APITestContext , branch , userToWhitelistPush , userToWhitelistForcePush , unprotectedFilePatterns , protectedFilePatterns string ) func (t * testing.T ) {
435
453
// We are going to just use the owner to set the protection.
436
454
return func (t * testing.T ) {
437
455
csrf := GetUserCSRFToken (t , ctx .Session )
@@ -440,6 +458,7 @@ func doProtectBranch(ctx APITestContext, branch, userToWhitelistPush, userToWhit
440
458
"_csrf" : csrf ,
441
459
"rule_name" : branch ,
442
460
"unprotected_file_patterns" : unprotectedFilePatterns ,
461
+ "protected_file_patterns" : protectedFilePatterns ,
443
462
}
444
463
445
464
if userToWhitelistPush != "" {
@@ -465,7 +484,7 @@ func doProtectBranch(ctx APITestContext, branch, userToWhitelistPush, userToWhit
465
484
// Check if master branch has been locked successfully
466
485
flashCookie := ctx .Session .GetCookie (gitea_context .CookieNameFlash )
467
486
assert .NotNil (t , flashCookie )
468
- assert .EqualValues (t , "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522" + url .QueryEscape (branch )+ "%2522%2Bhas%2Bbeen%2Bupdated." , flashCookie .Value )
487
+ assert .EqualValues (t , "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522" + url .QueryEscape (url . QueryEscape ( branch ) )+ "%2522%2Bhas%2Bbeen%2Bupdated." , flashCookie .Value )
469
488
}
470
489
}
471
490
0 commit comments