Skip to content

Commit 6fda29c

Browse files
committed
support multiple patterns as condition
1 parent 592444d commit 6fda29c

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ rules:
9494
skip-if-labeled:
9595
- label3
9696
filter:
97-
pattern: "docs/.*"
98-
extension: ".go"
97+
patterns:
98+
- "docs/.*"
99+
extension:
100+
- ".go"
99101
commenter:
100102
comment: "We have a match!"
101103
labeler:
@@ -107,8 +109,8 @@ The entire `condition` section is optional - you can run all rules all the time
107109
- `if-labeled` - apply the rule if the issue has any of the provided labels
108110
- `skip-if-labeled` - skip rule processing if issue has any of the provided labels
109111
- `filter`
110-
- `pattern` - [pattern](https://golang.org/s/re2syntax) matching the pull request file list
111-
- `extension` - which file extension to match on pull request file list (must start with a dot [`.`])
112+
- `patterns` - [pattern](https://golang.org/s/re2syntax) matching the pull request file list (any of the patterns)
113+
- `extensions` - which file extension to match on pull request file list (must start with a dot [`.`])
112114

113115
### Available Actions
114116
- [`autoassign`](bot/actions/autoassign/autoassign.md) - Automatic assignment of issue reviewers
@@ -153,7 +155,8 @@ rules:
153155
docs:
154156
condition:
155157
filter:
156-
pattern: "docs/.*"
158+
patterns:
159+
- "docs/.*"
157160
labeler:
158161
label: documentation
159162

bot/condition.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type Condition struct {
1010
IfLabeled []string `mapstructure:"if-labeled,omitempty"`
1111
SkipIfLabeled []string `mapstructure:"skip-if-labeled,omitempty"`
1212
Filter struct {
13-
Pattern string `mapstructure:"pattern,omitempty"`
14-
Extension string `mapstructure:"extension,omitempty"`
13+
Patterns []string `mapstructure:"patterns,omitempty"`
14+
Extensions []string `mapstructure:"extensions,omitempty"`
1515
} `mapstructure:"filter,omitempty"`
1616
}
1717

@@ -30,30 +30,42 @@ func (c *Condition) checkIfLabeled(meta EventData) bool {
3030
}
3131

3232
func (c *Condition) checkPattern(meta EventData) bool {
33-
if c.Filter.Pattern == "" {
33+
if len(c.Filter.Patterns) == 0 {
3434
return true
3535
} else {
36-
re, err := regexp.Compile(c.Filter.Pattern)
37-
if err != nil {
38-
util.Logger.Error("Unable to compile regex '%s'. %s", c.Filter.Pattern, err)
36+
compiled := make([]*regexp.Regexp, 0)
37+
for _, pattern := range c.Filter.Patterns {
38+
re, err := regexp.Compile(pattern)
39+
if err != nil {
40+
util.Logger.Warning("Unable to compile regex '%s'. %s", pattern, err)
41+
continue
42+
}
43+
compiled = append(compiled, re)
44+
}
45+
if len(compiled) == 0 {
46+
util.Logger.Error("All configured patterns have failed to compile")
3947
return false
4048
}
4149
for _, check := range meta.GetFileNames() {
42-
if re.MatchString(check) {
43-
return true
50+
for _, reg := range compiled {
51+
if reg.MatchString(check) {
52+
return true
53+
}
4454
}
4555
}
4656
}
4757
return false
4858
}
4959

5060
func (c *Condition) checkExt(meta EventData) bool {
51-
if c.Filter.Extension == "" {
61+
if len(c.Filter.Extensions) == 0 {
5262
return true
5363
} else {
5464
for _, check := range meta.GetFileExtensions() {
55-
if c.Filter.Extension == check {
56-
return true
65+
for _, ext := range c.Filter.Extensions {
66+
if ext == check {
67+
return true
68+
}
5769
}
5870
}
5971
}

bot/config_test.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ rules:
2727
rule3:
2828
condition:
2929
filter:
30-
extension: ".go"
30+
extensions:
31+
- ".go"
3132
rule4:
3233
condition:
3334
filter:
34-
pattern: ".*/docs/.*"
35+
patterns:
36+
- ".*/docs/.*"

0 commit comments

Comments
 (0)