...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package predicate
16
17 import (
18 "context"
19
20 "edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
21
22 "edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
23 )
24
25 type Title struct {
26 Matches []common.Regexp `yaml:"matches"`
27 NotMatches []common.Regexp `yaml:"not_matches"`
28 }
29
30 var _ Predicate = Title{}
31
32 func (pred Title) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
33 title := prctx.Title()
34
35 predicateResult := common.PredicateResult{
36 ValuePhrase: "titles",
37 Values: []string{title},
38 ConditionPhrase: "meet the pattern requirement",
39 }
40
41 var matchPatterns, notMatchPatterns []string
42
43 for _, reg := range pred.Matches {
44 matchPatterns = append(matchPatterns, reg.String())
45 }
46
47 for _, reg := range pred.NotMatches {
48 notMatchPatterns = append(notMatchPatterns, reg.String())
49 }
50
51 if len(pred.Matches) > 0 {
52 if anyMatches(pred.Matches, title) {
53 predicateResult.ConditionsMap = map[string][]string{"match": matchPatterns}
54 predicateResult.Description = "PR Title matches a Match pattern"
55 predicateResult.Satisfied = true
56 return &predicateResult, nil
57 }
58 }
59
60 if len(pred.NotMatches) > 0 {
61 if !anyMatches(pred.NotMatches, title) {
62 predicateResult.ConditionsMap = map[string][]string{"not match": notMatchPatterns}
63 predicateResult.Description = "PR Title doesn't match a NotMatch pattern"
64 predicateResult.Satisfied = true
65 return &predicateResult, nil
66 }
67 }
68 predicateResult.Satisfied = false
69 predicateResult.ConditionsMap = map[string][]string{"match": matchPatterns, "not match": notMatchPatterns}
70 return &predicateResult, nil
71 }
72
73 func (pred Title) Trigger() common.Trigger {
74 return common.TriggerPullRequest
75 }
76
View as plain text