-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch_protection.go
325 lines (292 loc) · 10.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
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 (
"errors"
"fmt"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
)
// Policy for the config/org/repo/branch.
// When merging policies, a nil value results in inheriting the parent policy.
type Policy struct {
// Protect overrides whether branch protection is enabled if set.
Protect *bool `json:"protect,omitempty"`
// RequiredStatusChecks configures github contexts
RequiredStatusChecks *ContextPolicy `json:"required_status_checks,omitempty"`
// Admins overrides whether protections apply to admins if set.
Admins *bool `json:"enforce_admins,omitempty"`
// Restrictions limits who can merge
Restrictions *Restrictions `json:"restrictions,omitempty"`
// RequiredPullRequestReviews specifies github approval/review criteria.
RequiredPullRequestReviews *ReviewPolicy `json:"required_pull_request_reviews,omitempty"`
}
func (p Policy) defined() bool {
return p.Protect != nil || p.RequiredStatusChecks != nil || p.Admins != nil || p.Restrictions != nil || p.RequiredPullRequestReviews != nil
}
// ContextPolicy configures required github contexts.
// When merging policies, contexts are appended to context list from parent.
// Strict determines whether merging to the branch invalidates existing contexts.
type ContextPolicy struct {
// Contexts appends required contexts that must be green to merge
Contexts []string `json:"contexts,omitempty"`
// Strict overrides whether new commits in the base branch require updating the PR if set
Strict *bool `json:"strict,omitempty"`
}
// ReviewPolicy specifies github approval/review criteria.
// Any nil values inherit the policy from the parent, otherwise bool/ints are overridden.
// Non-empty lists are appended to parent lists.
type ReviewPolicy struct {
// Restrictions appends users/teams that are allowed to merge
DismissalRestrictions *Restrictions `json:"dismissal_restrictions,omitempty"`
// DismissStale overrides whether new commits automatically dismiss old reviews if set
DismissStale *bool `json:"dismiss_stale_reviews,omitempty"`
// RequireOwners overrides whether CODEOWNERS must approve PRs if set
RequireOwners *bool `json:"require_code_owner_reviews,omitempty"`
// Approvals overrides the number of approvals required if set (set to 0 to disable)
Approvals *int `json:"required_approving_review_count,omitempty"`
}
// Restrictions limits who can merge
// Users and Teams items are appended to parent lists.
type Restrictions struct {
Users []string `json:"users"`
Teams []string `json:"teams"`
}
// selectInt returns the child if set, else parent
func selectInt(parent, child *int) *int {
if child != nil {
return child
}
return parent
}
// 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()
}
func mergeContextPolicy(parent, child *ContextPolicy) *ContextPolicy {
if child == nil {
return parent
}
if parent == nil {
return child
}
return &ContextPolicy{
Contexts: unionStrings(parent.Contexts, child.Contexts),
Strict: selectBool(parent.Strict, child.Strict),
}
}
func mergeReviewPolicy(parent, child *ReviewPolicy) *ReviewPolicy {
if child == nil {
return parent
}
if parent == nil {
return child
}
return &ReviewPolicy{
DismissalRestrictions: mergeRestrictions(parent.DismissalRestrictions, child.DismissalRestrictions),
DismissStale: selectBool(parent.DismissStale, child.DismissStale),
RequireOwners: selectBool(parent.RequireOwners, child.RequireOwners),
Approvals: selectInt(parent.Approvals, child.Approvals),
}
}
func mergeRestrictions(parent, child *Restrictions) *Restrictions {
if child == nil {
return parent
}
if parent == nil {
return child
}
return &Restrictions{
Users: unionStrings(parent.Users, child.Users),
Teams: unionStrings(parent.Teams, child.Teams),
}
}
// Apply returns a policy that merges the child into the parent
func (p Policy) Apply(child Policy) (Policy, error) {
return Policy{
Protect: selectBool(p.Protect, child.Protect),
RequiredStatusChecks: mergeContextPolicy(p.RequiredStatusChecks, child.RequiredStatusChecks),
Admins: selectBool(p.Admins, child.Admins),
Restrictions: mergeRestrictions(p.Restrictions, child.Restrictions),
RequiredPullRequestReviews: mergeReviewPolicy(p.RequiredPullRequestReviews, child.RequiredPullRequestReviews),
}, nil
}
// 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"`
AllowDisabledPolicies bool `json:"allow_disabled_policies,omitempty"`
}
// GetOrg returns the org config after merging in any global policies.
func (bp BranchProtection) GetOrg(name string) (*Org, error) {
o, ok := bp.Orgs[name]
if ok {
var err error
if o.Policy, err = bp.Apply(o.Policy); err != nil {
return nil, err
}
} else {
o.Policy = bp.Policy
}
return &o, nil
}
// Org holds the default protection policy for an entire org, as well as any repo overrides.
type Org struct {
Policy
Repos map[string]Repo `json:"repos,omitempty"`
}
// GetRepo returns the repo config after merging in any org policies.
func (o Org) GetRepo(name string) (*Repo, error) {
r, ok := o.Repos[name]
if ok {
var err error
if r.Policy, err = o.Apply(r.Policy); err != nil {
return nil, err
}
} else {
r.Policy = o.Policy
}
return &r, nil
}
// Repo holds protection policy overrides for all branches in a repo, as well as specific branch overrides.
type Repo struct {
Policy
Branches map[string]Branch `json:"branches,omitempty"`
}
// GetBranch returns the branch config after merging in any repo policies.
func (r Repo) GetBranch(name string) (*Branch, error) {
b, ok := r.Branches[name]
if ok {
var err error
if b.Policy, err = r.Apply(b.Policy); err != nil {
return nil, err
}
if b.Protect == nil {
return nil, errors.New("defined branch policies must set protect")
}
} else {
b.Policy = r.Policy
}
return &b, nil
}
// Branch holds protection policy overrides for a particular branch.
type Branch struct {
Policy
}
// GetBranchProtection returns the policy for a given branch.
//
// Handles merging any policies defined at repo/org/global levels into the branch policy.
func (c *Config) GetBranchProtection(org, repo, branch string) (*Policy, error) {
if _, present := c.BranchProtection.Orgs[org]; !present {
return nil, nil // only consider branches in configured orgs
}
var b *Branch
if o, err := c.BranchProtection.GetOrg(org); err != nil {
return nil, fmt.Errorf("org: %v", err)
} else if r, err := o.GetRepo(repo); err != nil {
return nil, fmt.Errorf("repo: %v", err)
} else if b, err = r.GetBranch(branch); err != nil {
return nil, fmt.Errorf("branch: %v", err)
}
return c.GetPolicy(org, repo, branch, *b)
}
// GetPolicy returns the protection policy for the branch, after merging in presubmits.
func (c *Config) GetPolicy(org, repo, branch string, b Branch) (*Policy, error) {
policy := b.Policy
// 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{
RequiredStatusChecks: &ContextPolicy{
Contexts: prowContexts,
},
}
// Require protection by default if ProtectTested is true
if c.BranchProtection.ProtectTested {
yes := true
ps.Protect = &yes
}
var err error
if policy, err = policy.Apply(ps); err != nil {
return nil, err
}
}
if policy.Protect != nil && !*policy.Protect {
// Ensure that protection is false => no protection settings
var old *bool
old, policy.Protect = policy.Protect, old
switch {
case policy.defined() && c.BranchProtection.AllowDisabledPolicies:
logrus.Warnf("%s/%s=%s defines a policy but has protect: false", org, repo, branch)
policy = Policy{
Protect: policy.Protect,
}
case policy.defined():
return nil, fmt.Errorf("%s/%s=%s defines a policy, which requires protect: true", org, repo, branch)
}
policy.Protect = old
}
if !policy.defined() {
return nil, nil
}
return &policy, nil
}
func jobRequirements(jobs []Presubmit, branch string, after bool) ([]string, []string) {
var required, optional []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.ContextRequired() { // This job needs a context
required = append(required, j.Context)
} else {
optional = append(optional, j.Context)
}
// Check which children require contexts
r, o := jobRequirements(j.RunAfterSuccess, branch, true)
required = append(required, r...)
optional = append(optional, o...)
}
return required, optional
}
// BranchRequirements returns required and optional presubmits prow jobs for a given org, repo branch.
func BranchRequirements(org, repo, branch string, presubmits map[string][]Presubmit) ([]string, []string) {
p, ok := presubmits[org+"/"+repo]
if !ok {
return nil, nil
}
return jobRequirements(p, branch, false)
}