/
approve.go
553 lines (464 loc) · 14.5 KB
/
approve.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright 2018 Palantir Technologies, Inc.
//
// 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 approval
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/palantir/policy-bot/policy/common"
"github.com/palantir/policy-bot/policy/predicate"
"github.com/palantir/policy-bot/pull"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
type Rule struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Predicates predicate.Predicates `yaml:"if"`
Options Options `yaml:"options"`
Requires Requires `yaml:"requires"`
}
type Options struct {
AllowAuthor bool `yaml:"allow_author"`
AllowContributor bool `yaml:"allow_contributor"`
AllowNonAuthorContributor bool `yaml:"allow_non_author_contributor"`
InvalidateOnPush bool `yaml:"invalidate_on_push"`
IgnoreEditedComments bool `yaml:"ignore_edited_comments"`
IgnoreUpdateMerges bool `yaml:"ignore_update_merges"`
IgnoreCommitsBy common.Actors `yaml:"ignore_commits_by"`
RequestReview RequestReview `yaml:"request_review"`
Methods *common.Methods `yaml:"methods"`
}
type RequestReview struct {
Enabled bool `yaml:"enabled"`
Mode common.RequestMode `yaml:"mode"`
Count int `yaml:"count"`
}
func (opts *Options) GetMethods() *common.Methods {
methods := opts.Methods
if methods == nil {
methods = &common.Methods{}
}
if methods.Comments == nil {
methods.Comments = []string{
":+1:",
"👍",
}
}
if methods.GithubReview == nil {
defaultGithubReview := true
methods.GithubReview = &defaultGithubReview
}
methods.GithubReviewState = pull.ReviewApproved
return methods
}
type Requires struct {
Count int `yaml:"count"`
Actors common.Actors `yaml:",inline"`
Conditions predicate.Predicates `yaml:"conditions"`
}
func (r *Rule) Trigger() common.Trigger {
t := common.TriggerCommit
if r.Requires.Count > 0 {
m := r.Options.GetMethods()
if len(m.Comments) > 0 || len(m.CommentPatterns) > 0 {
t |= common.TriggerComment
}
if len(m.BodyPatterns) > 0 {
t |= common.TriggerPullRequest
}
if m.GithubReview != nil && *m.GithubReview || len(m.GithubReviewCommentPatterns) > 0 {
t |= common.TriggerReview
}
}
for _, c := range r.Requires.Conditions.Predicates() {
t |= c.Trigger()
}
for _, p := range r.Predicates.Predicates() {
t |= p.Trigger()
}
return t
}
func (r *Rule) Evaluate(ctx context.Context, prctx pull.Context) (res common.Result) {
log := zerolog.Ctx(ctx)
res.Name = r.Name
res.Description = r.Description
res.Status = common.StatusSkipped
res.Methods = r.Options.GetMethods()
var predicateResults []*common.PredicateResult
for _, p := range r.Predicates.Predicates() {
result, err := p.Evaluate(ctx, prctx)
if err != nil {
res.Error = errors.Wrap(err, "failed to evaluate predicate")
return
}
predicateResults = append(predicateResults, result)
if !result.Satisfied {
log.Debug().Msgf("skipping rule, predicate of type %T was not satisfied", p)
desc := result.Description
res.StatusDescription = desc
if desc == "" {
res.StatusDescription = "A precondition of this rule was not satisfied"
}
res.PredicateResults = []*common.PredicateResult{result}
return
}
}
res.PredicateResults = predicateResults
candidates, dismissals, err := r.FilteredCandidates(ctx, prctx)
if err != nil {
res.Error = errors.Wrap(err, "failed to filter candidates")
return
}
approved, result, err := r.IsApproved(ctx, prctx, candidates)
if err != nil {
res.Error = errors.Wrap(err, "failed to compute approval status")
return
}
res.Requires = result
res.Dismissals = dismissals
res.StatusDescription = statusDescription(approved, result, candidates)
if approved {
res.Status = common.StatusApproved
} else {
res.Status = common.StatusPending
res.ReviewRequestRule = r.getReviewRequestRule()
}
return
}
func (r *Rule) getReviewRequestRule() *common.ReviewRequestRule {
if !r.Options.RequestReview.Enabled {
return nil
}
mode := r.Options.RequestReview.Mode
if mode == "" {
mode = common.RequestModeRandomUsers
}
requestedCount := r.Options.RequestReview.Count
if requestedCount == 0 {
requestedCount = r.Requires.Count
}
return &common.ReviewRequestRule{
Users: r.Requires.Actors.Users,
Teams: r.Requires.Actors.Teams,
Organizations: r.Requires.Actors.Organizations,
Permissions: r.Requires.Actors.GetPermissions(),
RequiredCount: r.Requires.Count,
RequestedCount: requestedCount,
Mode: mode,
}
}
func (r *Rule) IsApproved(ctx context.Context, prctx pull.Context, candidates []*common.Candidate) (bool, common.RequiresResult, error) {
approvedByActors, approvers, err := r.isApprovedByActors(ctx, prctx, candidates)
if err != nil {
return false, common.RequiresResult{}, err
}
approvedByConditions, conditions, err := r.isApprovedByConditions(ctx, prctx)
if err != nil {
return false, common.RequiresResult{}, err
}
result := common.RequiresResult{
Count: r.Requires.Count,
Actors: r.Requires.Actors,
Approvers: approvers,
Conditions: conditions,
}
return approvedByActors && approvedByConditions, result, nil
}
func (r *Rule) isApprovedByActors(ctx context.Context, prctx pull.Context, candidates []*common.Candidate) (bool, []*common.Candidate, error) {
log := zerolog.Ctx(ctx)
if r.Requires.Count <= 0 {
log.Debug().Msg("rule requires no approvals")
return true, nil, nil
}
log.Debug().Msgf("found %d candidates for approval", len(candidates))
// collect users "banned" by approval options
banned := make(map[string]bool)
// "author" is the user who opened the PR
// if contributors are allowed, the author counts as a contributor
author := prctx.Author()
if !r.Options.AllowAuthor && !r.Options.AllowContributor {
banned[author] = true
}
// "contributor" is any user who added a commit to the PR
if !r.Options.AllowContributor && !r.Options.AllowNonAuthorContributor {
commits, err := r.filteredCommits(ctx, prctx)
if err != nil {
return false, nil, err
}
for _, c := range commits {
for _, u := range c.Users() {
if u != author {
banned[u] = true
}
}
}
}
// filter real approvers using banned status and required membership
var approvers []*common.Candidate
for _, c := range candidates {
if banned[c.User] {
log.Debug().Str("user", c.User).Msg("rejecting approval by banned user")
continue
}
isApprover, err := r.Requires.Actors.IsActor(ctx, prctx, c.User)
if err != nil {
return false, nil, errors.Wrap(err, "failed to check candidate status")
}
if !isApprover {
log.Debug().Str("user", c.User).Msg("ignoring approval by non-required user")
continue
}
approvers = append(approvers, c)
}
log.Debug().Msgf("found %d/%d required approvers", len(approvers), r.Requires.Count)
return len(approvers) >= r.Requires.Count, approvers, nil
}
func (r *Rule) isApprovedByConditions(ctx context.Context, prctx pull.Context) (bool, []*common.PredicateResult, error) {
log := zerolog.Ctx(ctx)
conditions := r.Requires.Conditions.Predicates()
if len(conditions) == 0 {
log.Debug().Msg("rule requires no conditions")
return true, nil, nil
}
var results []*common.PredicateResult
var approved int
for _, c := range conditions {
result, err := c.Evaluate(ctx, prctx)
if err != nil {
return false, nil, errors.Wrap(err, "failed to evaluate condition")
}
if result.Satisfied {
approved++
}
results = append(results, result)
}
log.Debug().Msgf("found %d/%d required conditions", approved, len(conditions))
return approved == len(conditions), results, nil
}
// FilteredCandidates returns the potential approval candidates and any
// candidates that should be dimissed due to rule options.
func (r *Rule) FilteredCandidates(ctx context.Context, prctx pull.Context) ([]*common.Candidate, []*common.Dismissal, error) {
candidates, err := r.Options.GetMethods().Candidates(ctx, prctx)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get approval candidates")
}
sort.Stable(common.CandidatesByCreationTime(candidates))
var editDismissals []*common.Dismissal
if r.Options.IgnoreEditedComments {
candidates, editDismissals, err = r.filterEditedCandidates(ctx, prctx, candidates)
if err != nil {
return nil, nil, err
}
}
var pushDismissals []*common.Dismissal
if r.Options.InvalidateOnPush {
candidates, pushDismissals, err = r.filterInvalidCandidates(ctx, prctx, candidates)
if err != nil {
return nil, nil, err
}
}
var dismissals []*common.Dismissal
dismissals = append(dismissals, editDismissals...)
dismissals = append(dismissals, pushDismissals...)
return candidates, dismissals, nil
}
func (r *Rule) filterEditedCandidates(ctx context.Context, prctx pull.Context, candidates []*common.Candidate) ([]*common.Candidate, []*common.Dismissal, error) {
log := zerolog.Ctx(ctx)
if !r.Options.IgnoreEditedComments {
return candidates, nil, nil
}
var allowed []*common.Candidate
var dismissed []*common.Dismissal
for _, c := range candidates {
if c.LastEditedAt.IsZero() {
allowed = append(allowed, c)
} else {
dismissed = append(dismissed, &common.Dismissal{
Candidate: c,
Reason: "Comment was edited",
})
}
}
log.Debug().Msgf("discarded %d candidates with edited comments", len(dismissed))
return allowed, dismissed, nil
}
func (r *Rule) filterInvalidCandidates(ctx context.Context, prctx pull.Context, candidates []*common.Candidate) ([]*common.Candidate, []*common.Dismissal, error) {
log := zerolog.Ctx(ctx)
commits, err := r.filteredCommits(ctx, prctx)
if err != nil {
return nil, nil, err
}
if len(commits) == 0 {
return candidates, nil, nil
}
sha := commits[0].SHA
lastPushedAt, err := prctx.PushedAt(sha)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get last push timestamp")
}
var allowed []*common.Candidate
var dismissed []*common.Dismissal
for _, c := range candidates {
if c.CreatedAt.After(lastPushedAt) {
allowed = append(allowed, c)
} else {
dismissed = append(dismissed, &common.Dismissal{
Candidate: c,
Reason: fmt.Sprintf("Invalidated by push of %.7s", sha),
})
}
}
log.Debug().Msgf(
"discarded %d candidates invalidated by push of %s on or before %s",
len(dismissed), sha, lastPushedAt.Format(time.RFC3339),
)
return allowed, dismissed, nil
}
// filteredCommits returns the relevant commits for the evaluation ordered in
// history order, from most to least recent.
func (r *Rule) filteredCommits(ctx context.Context, prctx pull.Context) ([]*pull.Commit, error) {
commits, err := prctx.Commits()
if err != nil {
return nil, errors.Wrap(err, "failed to list commits")
}
commits = sortCommits(commits, prctx.HeadSHA())
ignoreUpdates := r.Options.IgnoreUpdateMerges
ignoreCommits := !r.Options.IgnoreCommitsBy.IsEmpty()
if !ignoreUpdates && !ignoreCommits {
return commits, nil
}
var filtered []*pull.Commit
for _, c := range commits {
if ignoreUpdates {
if isUpdateMerge(commits, c) {
continue
}
}
if ignoreCommits {
ignore, err := isIgnoredCommit(ctx, prctx, &r.Options.IgnoreCommitsBy, c)
if err != nil {
return nil, err
}
if ignore {
continue
}
}
filtered = append(filtered, c)
}
return filtered, nil
}
func statusDescription(approved bool, result common.RequiresResult, candidates []*common.Candidate) string {
hasActors := result.Count > 0
hasConditions := len(result.Conditions) > 0
if approved {
if !hasActors && !hasConditions {
return "No approval required"
}
var desc strings.Builder
desc.WriteString("Approved by ")
for i, c := range result.Approvers {
if i > 0 {
desc.WriteString(", ")
}
desc.WriteString(c.User)
}
if hasConditions {
if hasActors {
desc.WriteString(" and ")
}
desc.WriteString("required conditions")
}
return desc.String()
}
var desc strings.Builder
if hasActors {
fmt.Fprintf(&desc, "%d/%d required approvals", len(result.Approvers), result.Count)
}
if hasConditions {
if hasActors {
desc.WriteString(" and ")
}
successful := 0
for _, c := range result.Conditions {
if c.Satisfied {
successful++
}
}
fmt.Fprintf(&desc, "%d/%d required conditions", successful, len(result.Conditions))
}
if disqualified := len(candidates) - len(result.Approvers); hasActors && disqualified > 0 {
fmt.Fprintf(&desc, ". Ignored %s from disqualified users", numberOfApprovals(disqualified))
}
return desc.String()
}
func isUpdateMerge(commits []*pull.Commit, c *pull.Commit) bool {
// must be a simple merge commit (exactly 2 parents)
if len(c.Parents) != 2 {
return false
}
// must be created via the UI or the API (no local merges)
if !c.CommittedViaWeb {
return false
}
shas := make(map[string]bool)
for _, c := range commits {
shas[c.SHA] = true
}
// first parent must exist: it is a commit on the head branch
// second parent must not exist: it is already in the base branch
return shas[c.Parents[0]] && !shas[c.Parents[1]]
}
func isIgnoredCommit(ctx context.Context, prctx pull.Context, actors *common.Actors, c *pull.Commit) (bool, error) {
for _, u := range c.Users() {
ignored, err := actors.IsActor(ctx, prctx, u)
if err != nil {
return false, err
}
if !ignored {
return false, nil
}
}
// either all users are ignored or the commit has no users; only ignore in the first case
return len(c.Users()) > 0, nil
}
func numberOfApprovals(count int) string {
if count == 1 {
return "1 approval"
}
return fmt.Sprintf("%d approvals", count)
}
// sortCommits orders commits in history order starting from head. It must be
// called on the unfiltered set of commits.
func sortCommits(commits []*pull.Commit, head string) []*pull.Commit {
commitsBySHA := make(map[string]*pull.Commit)
for _, c := range commits {
commitsBySHA[c.SHA] = c
}
ordered := make([]*pull.Commit, 0, len(commits))
for {
c, ok := commitsBySHA[head]
if !ok {
break
}
ordered = append(ordered, c)
if len(c.Parents) == 0 {
break
}
head = c.Parents[0]
}
return ordered
}