...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package predicate
16
17 import (
18 "context"
19 "strings"
20
21 "github.com/pkg/errors"
22
23 "edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
24
25 "edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
26 )
27
28 type HasSuccessfulStatus []string
29
30 var _ Predicate = HasSuccessfulStatus([]string{})
31
32 func (pred HasSuccessfulStatus) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
33 statuses, err := prctx.LatestStatuses()
34
35 predicateResult := common.PredicateResult{
36 ValuePhrase: "status checks",
37 ConditionPhrase: "exist and pass",
38 }
39
40 if err != nil {
41 return nil, errors.Wrap(err, "failed to list commit statuses")
42 }
43
44 var missingResults []string
45 var failingStatuses []string
46 for _, status := range pred {
47 result, ok := statuses[status]
48 if !ok {
49 missingResults = append(missingResults, status)
50 }
51 if result != "success" {
52 failingStatuses = append(failingStatuses, status)
53 }
54 }
55
56 if len(missingResults) > 0 {
57 predicateResult.Values = missingResults
58 predicateResult.Description = "One or more statuses is missing: " + strings.Join(missingResults, ", ")
59 predicateResult.Satisfied = false
60 return &predicateResult, nil
61 }
62
63 if len(failingStatuses) > 0 {
64 predicateResult.Values = failingStatuses
65 predicateResult.Description = "One or more statuses has not passed: " + strings.Join(failingStatuses, ",")
66 predicateResult.Satisfied = false
67 return &predicateResult, nil
68 }
69
70 predicateResult.Values = pred
71 predicateResult.Satisfied = true
72 return &predicateResult, nil
73 }
74
75 func (pred HasSuccessfulStatus) Trigger() common.Trigger {
76 return common.TriggerStatus
77 }
78
View as plain text