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