@@ -16,6 +16,7 @@ import (
1616 "path/filepath"
1717 "slices"
1818 "strconv"
19+ "strings"
1920 "testing"
2021 "time"
2122
@@ -489,40 +490,60 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
489490}
490491
491492func doProtectBranch (ctx APITestContext , branch , userToWhitelistPush , userToWhitelistForcePush , unprotectedFilePatterns , protectedFilePatterns string ) func (t * testing.T ) {
493+ return doProtectBranchExt (ctx , branch , doProtectBranchOptions {
494+ UserToWhitelistPush : userToWhitelistPush ,
495+ UserToWhitelistForcePush : userToWhitelistForcePush ,
496+ UnprotectedFilePatterns : unprotectedFilePatterns ,
497+ ProtectedFilePatterns : protectedFilePatterns ,
498+ })
499+ }
500+
501+ type doProtectBranchOptions struct {
502+ UserToWhitelistPush , UserToWhitelistForcePush , UnprotectedFilePatterns , ProtectedFilePatterns string
503+
504+ StatusCheckPatterns []string
505+ }
506+
507+ func doProtectBranchExt (ctx APITestContext , ruleName string , opts doProtectBranchOptions ) func (t * testing.T ) {
492508 // We are going to just use the owner to set the protection.
493509 return func (t * testing.T ) {
494510 csrf := GetUserCSRFToken (t , ctx .Session )
495511
496512 formData := map [string ]string {
497513 "_csrf" : csrf ,
498- "rule_name" : branch ,
499- "unprotected_file_patterns" : unprotectedFilePatterns ,
500- "protected_file_patterns" : protectedFilePatterns ,
514+ "rule_name" : ruleName ,
515+ "unprotected_file_patterns" : opts . UnprotectedFilePatterns ,
516+ "protected_file_patterns" : opts . ProtectedFilePatterns ,
501517 }
502518
503- if userToWhitelistPush != "" {
504- user , err := user_model .GetUserByName (db .DefaultContext , userToWhitelistPush )
519+ if opts . UserToWhitelistPush != "" {
520+ user , err := user_model .GetUserByName (db .DefaultContext , opts . UserToWhitelistPush )
505521 assert .NoError (t , err )
506522 formData ["whitelist_users" ] = strconv .FormatInt (user .ID , 10 )
507523 formData ["enable_push" ] = "whitelist"
508524 formData ["enable_whitelist" ] = "on"
509525 }
510526
511- if userToWhitelistForcePush != "" {
512- user , err := user_model .GetUserByName (db .DefaultContext , userToWhitelistForcePush )
527+ if opts . UserToWhitelistForcePush != "" {
528+ user , err := user_model .GetUserByName (db .DefaultContext , opts . UserToWhitelistForcePush )
513529 assert .NoError (t , err )
514530 formData ["force_push_allowlist_users" ] = strconv .FormatInt (user .ID , 10 )
515531 formData ["enable_force_push" ] = "whitelist"
516532 formData ["enable_force_push_allowlist" ] = "on"
517533 }
518534
535+ if len (opts .StatusCheckPatterns ) > 0 {
536+ formData ["enable_status_check" ] = "on"
537+ formData ["status_check_contexts" ] = strings .Join (opts .StatusCheckPatterns , "\n " )
538+ }
539+
519540 // Send the request to update branch protection settings
520541 req := NewRequestWithValues (t , "POST" , fmt .Sprintf ("/%s/%s/settings/branches/edit" , url .PathEscape (ctx .Username ), url .PathEscape (ctx .Reponame )), formData )
521542 ctx .Session .MakeRequest (t , req , http .StatusSeeOther )
522543
523- // Check if master branch has been locked successfully
544+ // Check if the " master" branch has been locked successfully
524545 flashMsg := ctx .Session .GetCookieFlashMessage ()
525- assert .Equal (t , `Branch protection for rule "` + branch + `" has been updated.` , flashMsg .SuccessMsg )
546+ assert .Equal (t , `Branch protection for rule "` + ruleName + `" has been updated.` , flashMsg .SuccessMsg )
526547 }
527548}
528549
@@ -688,6 +709,10 @@ func doAutoPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) {
688709
689710 ctx := NewAPITestContext (t , baseCtx .Username , baseCtx .Reponame , auth_model .AccessTokenScopeWriteRepository )
690711
712+ // automerge will merge immediately if the PR is mergeable and there is no "status check" because no status check also means "all checks passed"
713+ // so we must set up a status check to test the auto merge feature
714+ doProtectBranchExt (ctx , "protected" , doProtectBranchOptions {StatusCheckPatterns : []string {"*" }})(t )
715+
691716 t .Run ("CheckoutProtected" , doGitCheckoutBranch (dstPath , "protected" ))
692717 t .Run ("PullProtected" , doGitPull (dstPath , "origin" , "protected" ))
693718 t .Run ("GenerateCommit" , func (t * testing.T ) {
@@ -728,13 +753,13 @@ func doAutoPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) {
728753
729754 // Cancel not existing auto merge
730755 ctx .ExpectedCode = http .StatusNotFound
731- t .Run ("CancelAutoMergePR " , doAPICancelAutoMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
756+ t .Run ("CancelAutoMergePRNotExist " , doAPICancelAutoMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
732757
733758 // Add auto merge request
734759 ctx .ExpectedCode = http .StatusCreated
735760 t .Run ("AutoMergePR" , doAPIAutoMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
736761
737- // Can not create schedule twice
762+ // Cannot create schedule twice
738763 ctx .ExpectedCode = http .StatusConflict
739764 t .Run ("AutoMergePRTwice" , doAPIAutoMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
740765
0 commit comments