github.com/jenkins-x/jx/v2@v2.1.155/pkg/prow/config/branch_protection.go (about) 1 package config 2 3 import ( 4 "fmt" 5 6 "github.com/jenkins-x/jx/v2/pkg/gits" 7 "github.com/jenkins-x/jx/v2/pkg/util" 8 "github.com/jenkins-x/lighthouse/pkg/config" 9 "github.com/jenkins-x/lighthouse/pkg/config/branchprotection" 10 "github.com/pkg/errors" 11 ) 12 13 // AddRepoToBranchProtection adds a repository to the Branch Protection section of a prow config 14 func AddRepoToBranchProtection(bp *branchprotection.Config, repoSpec string, context string, kind Kind) error { 15 bp.ProtectTested = true 16 if bp.Orgs == nil { 17 bp.Orgs = make(map[string]branchprotection.Org, 0) 18 } 19 url, err := gits.ParseGitURL(repoSpec) 20 if err != nil { 21 return err 22 } 23 requiredOrg, requiredRepo := url.Organisation, url.Name 24 if _, ok := bp.Orgs[requiredOrg]; !ok { 25 bp.Orgs[requiredOrg] = branchprotection.Org{} 26 } 27 if bp.Orgs[requiredOrg].Repos == nil { 28 org := bp.Orgs[requiredOrg] 29 org.Repos = make(map[string]branchprotection.Repo, 0) 30 bp.Orgs[requiredOrg] = org 31 } 32 if _, ok := bp.Orgs[requiredOrg].Repos[requiredRepo]; !ok { 33 bp.Orgs[requiredOrg].Repos[requiredRepo] = branchprotection.Repo{ 34 Policy: branchprotection.Policy{ 35 RequiredStatusChecks: &branchprotection.ContextPolicy{}, 36 }, 37 } 38 39 } 40 if bp.Orgs[requiredOrg].Repos[requiredRepo].Policy.RequiredStatusChecks.Contexts == nil { 41 bp.Orgs[requiredOrg].Repos[requiredRepo].Policy.RequiredStatusChecks.Contexts = make([]string, 0) 42 } 43 contexts := bp.Orgs[requiredOrg].Repos[requiredRepo].Policy.RequiredStatusChecks.Contexts 44 switch kind { 45 case Application: 46 if !util.Contains(contexts, ServerlessJenkins) { 47 contexts = append(contexts, ServerlessJenkins) 48 } 49 case Environment, RemoteEnvironment: 50 if !util.Contains(contexts, PromotionBuild) { 51 contexts = append(contexts, PromotionBuild) 52 } 53 case Protection: 54 if !util.Contains(contexts, context) { 55 contexts = append(contexts, context) 56 } 57 default: 58 return fmt.Errorf("unknown Prow config kind %s", kind) 59 } 60 bp.Orgs[requiredOrg].Repos[requiredRepo].Policy.RequiredStatusChecks.Contexts = contexts 61 return nil 62 } 63 64 // RemoveRepoFromBranchProtection removes a repository to the Branch Protection section of a prow config 65 func RemoveRepoFromBranchProtection(bp *branchprotection.Config, repoSpec string) error { 66 if bp.Orgs == nil { 67 return errors.New("no orgs in BranchProtection object") 68 } 69 url, err := gits.ParseGitURL(repoSpec) 70 if err != nil { 71 return err 72 } 73 requiredOrg, requiredRepo := url.Organisation, url.Name 74 75 repos := bp.Orgs[requiredOrg].Repos 76 if repos == nil { 77 return errors.New("no repos found for org " + requiredOrg) 78 } 79 if _, ok := repos[requiredRepo]; !ok { 80 return errors.New(fmt.Sprintf("repo %s not found in org %s", requiredRepo, requiredOrg)) 81 } 82 delete(repos, requiredRepo) 83 return nil 84 } 85 86 // GetAllBranchProtectionContexts gets all the contexts that have branch protection for a repo 87 func GetAllBranchProtectionContexts(org string, repo string, prowConfig *config.Config) ([]string, error) { 88 prowOrg, ok := prowConfig.BranchProtection.Orgs[org] 89 if !ok { 90 prowOrg = branchprotection.Org{} 91 } 92 if prowOrg.Repos == nil { 93 prowOrg.Repos = make(map[string]branchprotection.Repo, 0) 94 } 95 prowRepo, ok := prowOrg.Repos[repo] 96 if !ok { 97 prowRepo = branchprotection.Repo{} 98 } 99 if prowRepo.RequiredStatusChecks == nil { 100 prowRepo.RequiredStatusChecks = &branchprotection.ContextPolicy{} 101 } 102 return prowRepo.RequiredStatusChecks.Contexts, nil 103 } 104 105 // GetBranchProtectionContexts gets the branch protection contexts for a repo 106 func GetBranchProtectionContexts(org string, repo string, prowConfig *config.Config) ([]string, error) { 107 result := make([]string, 0) 108 contexts, err := GetAllBranchProtectionContexts(org, repo, prowConfig) 109 if err != nil { 110 return result, errors.Wrap(err, "getting branch protection contexts") 111 } 112 for _, c := range contexts { 113 if c != ServerlessJenkins && c != PromotionBuild { 114 result = append(result, c) 115 } 116 } 117 return result, nil 118 }