@@ -81,6 +81,7 @@ func testGitGeneral(t *testing.T, u *url.URL) {
8181 mediaTest (t , & httpContext , pushedFilesStandard [0 ], pushedFilesStandard [1 ], pushedFilesLFS [0 ], pushedFilesLFS [1 ])
8282
8383 t .Run ("CreateAgitFlowPull" , doCreateAgitFlowPull (dstPath , & httpContext , "test/head" ))
84+ t .Run ("CreateProtectedBranch" , doCreateProtectedBranch (& httpContext , dstPath ))
8485 t .Run ("BranchProtectMerge" , doBranchProtectPRMerge (& httpContext , dstPath ))
8586 t .Run ("AutoMerge" , doAutoPRMerge (& httpContext , dstPath ))
8687 t .Run ("CreatePRAndSetManuallyMerged" , doCreatePRAndSetManuallyMerged (httpContext , httpContext , dstPath , "master" , "test-manually-merge" ))
@@ -122,6 +123,7 @@ func testGitGeneral(t *testing.T, u *url.URL) {
122123 mediaTest (t , & sshContext , pushedFilesStandard [0 ], pushedFilesStandard [1 ], pushedFilesLFS [0 ], pushedFilesLFS [1 ])
123124
124125 t .Run ("CreateAgitFlowPull" , doCreateAgitFlowPull (dstPath , & sshContext , "test/head2" ))
126+ t .Run ("CreateProtectedBranch" , doCreateProtectedBranch (& sshContext , dstPath ))
125127 t .Run ("BranchProtectMerge" , doBranchProtectPRMerge (& sshContext , dstPath ))
126128 t .Run ("MergeFork" , func (t * testing.T ) {
127129 defer tests .PrintCurrentTest (t )()
@@ -326,6 +328,34 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
326328 return filepath .Base (tmpFile .Name ()), err
327329}
328330
331+ func doCreateProtectedBranch (baseCtx * APITestContext , dstPath string ) func (t * testing.T ) {
332+ return func (t * testing.T ) {
333+ defer tests .PrintCurrentTest (t )()
334+ ctx := NewAPITestContext (t , baseCtx .Username , baseCtx .Reponame , auth_model .AccessTokenScopeWriteRepository )
335+
336+ t .Run ("ProtectBranchWithFilePatterns" , doProtectBranch (ctx , "release-*" , baseCtx .Username , "" , "" , "config*" ))
337+
338+ // push a new branch without any new commits
339+ t .Run ("CreateProtectedBranch-NoChanges" , doGitCreateBranch (dstPath , "release-v1.0" ))
340+ t .Run ("PushProtectedBranch-NoChanges" , doGitPushTestRepository (dstPath , "origin" , "release-v1.0" ))
341+ t .Run ("CheckoutMaster-NoChanges" , doGitCheckoutBranch (dstPath , "master" ))
342+
343+ // push a new branch with a new unprotected file
344+ t .Run ("CreateProtectedBranch-UnprotectedFile" , doGitCreateBranch (dstPath , "release-v2.0" ))
345+ _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"abc.txt" )
346+ assert .NoError (t , err )
347+ t .Run ("PushProtectedBranch-UnprotectedFile" , doGitPushTestRepository (dstPath , "origin" , "release-v2.0" ))
348+ t .Run ("CheckoutMaster-UnprotectedFile" , doGitCheckoutBranch (dstPath , "master" ))
349+
350+ //push a new branch with a new protected file
351+ t .Run ("CreateProtectedBranch-ProtectedFile" , doGitCreateBranch (dstPath , "release-v3.0" ))
352+ _ ,
err = generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"config" )
353+ assert .NoError (t , err )
354+ t .Run ("PushProtectedBranch-ProtectedFile" , doGitPushTestRepositoryFail (dstPath , "origin" , "release-v3.0" ))
355+ t .Run ("CheckoutMaster-ProtectedFile" , doGitCheckoutBranch (dstPath , "master" ))
356+ }
357+ }
358+
329359func doBranchProtectPRMerge (baseCtx * APITestContext , dstPath string ) func (t * testing.T ) {
330360 return func (t * testing.T ) {
331361 defer tests .PrintCurrentTest (t )()
@@ -336,7 +366,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
336366
337367 // Protect branch without any whitelisting
338368 t .Run ("ProtectBranchNoWhitelist" , func (t * testing.T ) {
339- doProtectBranch (ctx , "protected" , "" , "" , "" )
369+ doProtectBranch (ctx , "protected" , "" , "" , "" , "" )
340370 })
341371
342372 // Try to push without permissions, which should fail
@@ -348,7 +378,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
348378
349379 // Set up permissions for normal push but not force push
350380 t .Run ("SetupNormalPushPermissions" , func (t * testing.T ) {
351- doProtectBranch (ctx , "protected" , baseCtx .Username , "" , "" )
381+ doProtectBranch (ctx , "protected" , baseCtx .Username , "" , "" , "" )
352382 })
353383
354384 // Normal push should work
@@ -370,7 +400,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
370400
371401 // Set up permissions for force push but not normal push
372402 t .Run ("SetupForcePushPermissions" , func (t * testing.T ) {
373- doProtectBranch (ctx , "protected" , "" , baseCtx .Username , "" )
403+ doProtectBranch (ctx , "protected" , "" , baseCtx .Username , "" , "" )
374404 })
375405
376406 // Try to force push without normal push permissions, which should fail
@@ -380,15 +410,15 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
380410
381411 // Set up permissions for normal and force push (both are required to force push)
382412 t .Run ("SetupNormalAndForcePushPermissions" , func (t * testing.T ) {
383- doProtectBranch (ctx , "protected" , baseCtx .Username , baseCtx .Username , "" )
413+ doProtectBranch (ctx , "protected" , baseCtx .Username , baseCtx .Username , "" , "" )
384414 })
385415
386416 // Force push should now work
387417 t .Run ("ForcePushWithPermissions" , func (t * testing.T ) {
388418 doGitPushTestRepository (dstPath , "-f" , "origin" , "protected" )
389419 })
390420
391- t .Run ("ProtectProtectedBranchNoWhitelist" , doProtectBranch (ctx , "protected" , "" , "" , "" ))
421+ t .Run ("ProtectProtectedBranchNoWhitelist" , doProtectBranch (ctx , "protected" , "" , "" , "" , "" ))
392422 t .Run ("PushToUnprotectedBranch" , doGitPushTestRepository (dstPath , "origin" , "protected:unprotected" ))
393423 var pr api.PullRequest
394424 var err error
@@ -410,14 +440,14 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
410440 t .Run ("MergePR" , doAPIMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
411441 t .Run ("PullProtected" , doGitPull (dstPath , "origin" , "protected" ))
412442
413- t .Run ("ProtectProtectedBranchUnprotectedFilePaths" , doProtectBranch (ctx , "protected" , "" , "" , "unprotected-file-*" ))
443+ t .Run ("ProtectProtectedBranchUnprotectedFilePaths" , doProtectBranch (ctx , "protected" , "" , "" , "unprotected-file-*" , "" ))
414444 t .Run ("GenerateCommit" , func (t * testing.T ) {
415445 _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"unprotected-file-" )
416446 assert .NoError (t , err )
417447 })
418448 t .Run ("PushUnprotectedFilesToProtectedBranch" , doGitPushTestRepository (dstPath , "origin" , "protected" ))
419449
420- t .Run ("ProtectProtectedBranchWhitelist" , doProtectBranch (ctx , "protected" , baseCtx .Username , "" , "" ))
450+ t .Run ("ProtectProtectedBranchWhitelist" , doProtectBranch (ctx , "protected" , baseCtx .Username , "" , "" , "" ))
421451
422452 t .Run ("CheckoutMaster" , doGitCheckoutBranch (dstPath , "master" ))
423453 t .Run ("CreateBranchForced" , doGitCreateBranch (dstPath , "toforce" ))
@@ -432,7 +462,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
432462 }
433463}
434464
435- func doProtectBranch (ctx APITestContext , branch , userToWhitelistPush , userToWhitelistForcePush , unprotectedFilePatterns string ) func (t * testing.T ) {
465+ func doProtectBranch (ctx APITestContext , branch , userToWhitelistPush , userToWhitelistForcePush , unprotectedFilePatterns , protectedFilePatterns string ) func (t * testing.T ) {
436466 // We are going to just use the owner to set the protection.
437467 return func (t * testing.T ) {
438468 csrf := GetUserCSRFToken (t , ctx .Session )
@@ -441,6 +471,7 @@ func doProtectBranch(ctx APITestContext, branch, userToWhitelistPush, userToWhit
441471 "_csrf" : csrf ,
442472 "rule_name" : branch ,
443473 "unprotected_file_patterns" : unprotectedFilePatterns ,
474+ "protected_file_patterns" : protectedFilePatterns ,
444475 }
445476
446477 if userToWhitelistPush != "" {
@@ -466,7 +497,7 @@ func doProtectBranch(ctx APITestContext, branch, userToWhitelistPush, userToWhit
466497 // Check if master branch has been locked successfully
467498 flashCookie := ctx .Session .GetCookie (gitea_context .CookieNameFlash )
468499 assert .NotNil (t , flashCookie )
469- assert .EqualValues (t , "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522" + url .QueryEscape (branch )+ "%2522%2Bhas%2Bbeen%2Bupdated." , flashCookie .Value )
500+ assert .EqualValues (t , "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522" + url .QueryEscape (url . QueryEscape ( branch ) )+ "%2522%2Bhas%2Bbeen%2Bupdated." , flashCookie .Value )
470501 }
471502}
472503
0 commit comments