...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package predicate
16
17 import (
18 "context"
19 "fmt"
20
21 "edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
22
23 "edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
24 )
25
26 type TargetsBranch struct {
27 Pattern common.Regexp `yaml:"pattern"`
28 }
29
30 var _ Predicate = &TargetsBranch{}
31
32 func (pred *TargetsBranch) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
33 targetName, _ := prctx.Branches()
34 matches := pred.Pattern.Matches(targetName)
35
36 desc := ""
37 if !matches {
38 desc = fmt.Sprintf("Target branch %q does not match required pattern %q", targetName, pred.Pattern)
39 }
40
41 predicateResult := common.PredicateResult{
42 Satisfied: matches,
43 Description: desc,
44 Values: []string{targetName},
45 ValuePhrase: "target branches",
46 ConditionPhrase: "match the required pattern",
47 ConditionValues: []string{pred.Pattern.String()},
48 }
49
50 return &predicateResult, nil
51 }
52
53 func (pred *TargetsBranch) Trigger() common.Trigger {
54 return common.TriggerPullRequest
55 }
56
57 type FromBranch struct {
58 Pattern common.Regexp `yaml:"pattern"`
59 }
60
61 var _ Predicate = &FromBranch{}
62
63 func (pred *FromBranch) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
64 _, sourceBranchName := prctx.Branches()
65 matches := pred.Pattern.Matches(sourceBranchName)
66
67 desc := ""
68 if !matches {
69 desc = fmt.Sprintf("Source branch %q does not match specified from_branch pattern %q", sourceBranchName, pred.Pattern)
70 }
71
72 predicateResult := common.PredicateResult{
73 Satisfied: matches,
74 Description: desc,
75 Values: []string{sourceBranchName},
76 ValuePhrase: "source branches",
77 ConditionPhrase: "match the required pattern",
78 ConditionValues: []string{pred.Pattern.String()},
79 }
80
81 return &predicateResult, nil
82 }
83
84 func (pred *FromBranch) Trigger() common.Trigger {
85 return common.TriggerStatic
86 }
87
View as plain text