Skip to content

Commit

Permalink
support multiple patterns as condition
Browse files Browse the repository at this point in the history
  • Loading branch information
bivas committed Jun 30, 2017
1 parent 592444d commit 6fda29c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ rules:
skip-if-labeled:
- label3
filter:
pattern: "docs/.*"
extension: ".go"
patterns:
- "docs/.*"
extension:
- ".go"
commenter:
comment: "We have a match!"
labeler:
Expand All @@ -107,8 +109,8 @@ The entire `condition` section is optional - you can run all rules all the time
- `if-labeled` - apply the rule if the issue has any of the provided labels
- `skip-if-labeled` - skip rule processing if issue has any of the provided labels
- `filter`
- `pattern` - [pattern](https://golang.org/s/re2syntax) matching the pull request file list
- `extension` - which file extension to match on pull request file list (must start with a dot [`.`])
- `patterns` - [pattern](https://golang.org/s/re2syntax) matching the pull request file list (any of the patterns)
- `extensions` - which file extension to match on pull request file list (must start with a dot [`.`])

### Available Actions
- [`autoassign`](bot/actions/autoassign/autoassign.md) - Automatic assignment of issue reviewers
Expand Down Expand Up @@ -153,7 +155,8 @@ rules:
docs:
condition:
filter:
pattern: "docs/.*"
patterns:
- "docs/.*"
labeler:
label: documentation
Expand Down
34 changes: 23 additions & 11 deletions bot/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type Condition struct {
IfLabeled []string `mapstructure:"if-labeled,omitempty"`
SkipIfLabeled []string `mapstructure:"skip-if-labeled,omitempty"`
Filter struct {
Pattern string `mapstructure:"pattern,omitempty"`
Extension string `mapstructure:"extension,omitempty"`
Patterns []string `mapstructure:"patterns,omitempty"`
Extensions []string `mapstructure:"extensions,omitempty"`
} `mapstructure:"filter,omitempty"`
}

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

func (c *Condition) checkPattern(meta EventData) bool {
if c.Filter.Pattern == "" {
if len(c.Filter.Patterns) == 0 {
return true
} else {
re, err := regexp.Compile(c.Filter.Pattern)
if err != nil {
util.Logger.Error("Unable to compile regex '%s'. %s", c.Filter.Pattern, err)
compiled := make([]*regexp.Regexp, 0)
for _, pattern := range c.Filter.Patterns {
re, err := regexp.Compile(pattern)
if err != nil {
util.Logger.Warning("Unable to compile regex '%s'. %s", pattern, err)
continue
}
compiled = append(compiled, re)
}
if len(compiled) == 0 {
util.Logger.Error("All configured patterns have failed to compile")
return false
}
for _, check := range meta.GetFileNames() {
if re.MatchString(check) {
return true
for _, reg := range compiled {
if reg.MatchString(check) {
return true
}
}
}
}
return false
}

func (c *Condition) checkExt(meta EventData) bool {
if c.Filter.Extension == "" {
if len(c.Filter.Extensions) == 0 {
return true
} else {
for _, check := range meta.GetFileExtensions() {
if c.Filter.Extension == check {
return true
for _, ext := range c.Filter.Extensions {
if ext == check {
return true
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions bot/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ rules:
rule3:
condition:
filter:
extension: ".go"
extensions:
- ".go"
rule4:
condition:
filter:
pattern: ".*/docs/.*"
patterns:
- ".*/docs/.*"

0 comments on commit 6fda29c

Please sign in to comment.