forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch_protection.go
160 lines (138 loc) · 4.1 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"fmt"
"k8s.io/apimachinery/pkg/util/sets"
)
type Policy struct {
Protect *bool `json:"protect-by-default,omitempty"`
// TODO(fejta): add all protection options
Contexts []string `json:"require-contexts,omitempty"`
Pushers []string `json:"allow-push,omitempty"`
}
// selectBool returns the child argument if set, otherwise the parent
func selectBool(parent, child *bool) *bool {
if child != nil {
return child
}
return parent
}
// unionStrings merges the parent and child items together
func unionStrings(parent, child []string) []string {
if child == nil {
return parent
}
if parent == nil {
return child
}
s := sets.NewString(parent...)
s.Insert(child...)
return s.List()
}
// apply returns a policy that merges the child into the parent
func (parent Policy) Apply(child Policy) Policy {
return Policy{
Protect: selectBool(parent.Protect, child.Protect),
Contexts: unionStrings(parent.Contexts, child.Contexts),
Pushers: unionStrings(parent.Pushers, child.Pushers),
}
}
// BranchProtection specifies the global branch protection policy
type BranchProtection struct {
Policy
ProtectTested bool `json:"protect-tested-repos,omitempty"`
Orgs map[string]Org `json:"orgs,omitempty"`
}
type Org struct {
Policy
Repos map[string]Repo `json:"repos,omitempty"`
}
type Repo struct {
Policy
Branches map[string]Branch `json:"branches,omitempty"`
}
type Branch struct {
Policy
}
func (c *Config) GetBranchProtection(org, repo, branch string) (*Policy, error) {
policy := c.BranchProtection.Policy
if o, ok := c.BranchProtection.Orgs[org]; ok {
policy = policy.Apply(o.Policy)
if r, ok := o.Repos[repo]; ok {
policy = policy.Apply(r.Policy)
if b, ok := r.Branches[branch]; ok {
policy = policy.Apply(b.Policy)
if policy.Protect == nil {
return nil, fmt.Errorf("protect should not be nil")
}
}
}
} else {
return nil, nil
}
// Automatically require any required prow jobs
if prowContexts := branchRequirements(org, repo, branch, c.Presubmits); len(prowContexts) > 0 {
// Error if protection is disabled
if policy.Protect != nil && !*policy.Protect {
return nil, fmt.Errorf("required prow jobs require branch protection")
}
ps := Policy{
Contexts: prowContexts,
Protect: nil,
}
// Require protection by default if ProtectTested is true
if c.BranchProtection.ProtectTested {
yes := true
ps.Protect = &yes
}
policy = policy.Apply(ps)
}
if policy.Protect == nil {
return nil, nil
}
if policy.Protect != nil && !*policy.Protect {
if len(policy.Contexts) > 0 {
return nil, fmt.Errorf("required contexts requires branch protection")
}
if len(policy.Pushers) > 0 {
return nil, fmt.Errorf("push restrictions requires branch protection")
}
}
return &policy, nil
}
func jobRequirements(jobs []Presubmit, branch string, after bool) []string {
var required []string
for _, j := range jobs {
if !j.Brancher.RunsAgainstBranch(branch) {
continue
}
// Does this job require a context or have kids that might need one?
if !after && !j.AlwaysRun && j.RunIfChanged == "" {
continue // No
}
if !j.SkipReport && !j.Optional { // This job needs a context
required = append(required, j.Context)
}
// Check which children require contexts
required = append(required, jobRequirements(j.RunAfterSuccess, branch, true)...)
}
return required
}
func branchRequirements(org, repo, branch string, presubmits map[string][]Presubmit) []string {
p, ok := presubmits[org+"/"+repo]
if !ok {
return nil
}
return jobRequirements(p, branch, false)
}