Skip to content

Commit

Permalink
Add allowBuild args on FilterByContext
Browse files Browse the repository at this point in the history
  • Loading branch information
samrocketman committed Jul 27, 2023
1 parent 5be49f7 commit f2a53a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/groovy/net/gleske/jervis/tools/FilterByContext.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,20 @@ class FilterByContext {
Boolean getAllowBuild() {
checkFilter(this.filters)
}

/**
Evaluate the full list of filters provided as an argument instead of
relying on the <tt>{@link #filters}</tt> property.
@param filters A list of user-provided filters where a filter can be a
String, Map, or a List (containing a list of objects of
type List, String, or Map therein).
@return True if the current environment <tt>{@link #context}</tt>
evaluates against the provided filters.
*/
Boolean allowBuild(def filters) {
List providedFilters = (filters in List) ? filters : [filters]
validateFilters(providedFilters)
checkFilter(providedFilters)
}
}
31 changes: 31 additions & 0 deletions src/test/groovy/net/gleske/jervis/tools/FilterByContextTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1153,4 +1153,35 @@ class FilterByContextTest extends GroovyTestCase {
shouldFilter.filters = [pr: 3]
}
}
@Test public void test_FilterByContext_allowBuild_with_filters_arg() {
Map context = [
trigger: 'pr_comment',
context: 'pr',
metadata: [
pr: true,
branch: 'main',
tag: '',
push: false,
cron: false,
manually: '',
pr_comment: 'retest this please'
]
]

// Build on push, tag, or manually triggered build. OR if in a PR and
// the user commented a specific comment.
List filter = ['push', 'tag', 'manually', [pr_comment: '/.*test this please.*/']]
shouldFilter = new FilterByContext(context, filter)
assert shouldFilter.allowBuild == true
assert shouldFilter.allowBuild(filter) == true
assert shouldFilter.allowBuild('tag') == false
assert shouldFilter.allowBuild('manually') == false
assert shouldFilter.allowBuild('push') == false
assert shouldFilter.allowBuild('pr_comment') == true
assert shouldFilter.allowBuild('pr') == true
assert shouldFilter.allowBuild(pr_comment: '/some other comment/') == false
shouldFail(FilterByContextException) {
shouldFilter.allowBuild('invalid')
}
}
}

0 comments on commit f2a53a6

Please sign in to comment.