...
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 Repository struct {
26 Matches []common.Regexp `yaml:"matches"`
27 NotMatches []common.Regexp `yaml:"not_matches"`
28 }
29
30 var _ Predicate = Repository{}
31
32 func (pred Repository) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
33 owner := prctx.RepositoryOwner()
34 repo := prctx.RepositoryName()
35 repoFullName := owner + "/" + repo
36
37 predicateResult := common.PredicateResult{
38 ValuePhrase: "repositories",
39 Values: []string{repoFullName},
40 ConditionPhrase: "meet the pattern requirement",
41 }
42
43 var matchPatterns, notMatchPatterns []string
44
45 for _, reg := range pred.Matches {
46 matchPatterns = append(matchPatterns, reg.String())
47 }
48
49 for _, reg := range pred.NotMatches {
50 notMatchPatterns = append(notMatchPatterns, reg.String())
51 }
52
53 if len(pred.Matches) > 0 {
54 if anyMatches(pred.Matches, repoFullName) {
55 predicateResult.ConditionsMap = map[string][]string{"match": matchPatterns}
56 predicateResult.Description = "PR Repository matches a Match pattern"
57 predicateResult.Satisfied = true
58 return &predicateResult, nil
59 }
60 }
61
62 if len(pred.NotMatches) > 0 {
63 if !anyMatches(pred.NotMatches, repoFullName) {
64 predicateResult.ConditionsMap = map[string][]string{"not match": notMatchPatterns}
65 predicateResult.Description = "PR Repository doesn't match a NotMatch pattern"
66 predicateResult.Satisfied = true
67 return &predicateResult, nil
68 }
69 }
70 predicateResult.Satisfied = false
71 predicateResult.ConditionsMap = map[string][]string{"match": matchPatterns, "not match": notMatchPatterns}
72 return &predicateResult, nil
73 }
74
75 func (pred Repository) Trigger() common.Trigger {
76 return common.TriggerPullRequest
77 }
78
View as plain text