-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support allOf in a when context
- Loading branch information
1 parent
ce22e78
commit e04e686
Showing
7 changed files
with
344 additions
and
20 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/main/groovy/com/lesfurets/jenkins/unit/declarative/AllOfDeclaration.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.lesfurets.jenkins.unit.declarative | ||
|
||
import org.springframework.util.AntPathMatcher | ||
|
||
import static groovy.lang.Closure.DELEGATE_FIRST | ||
|
||
class AllOfDeclaration extends WhenDeclaration { | ||
|
||
List<String> branches = [] | ||
List<Boolean> expressions = [] | ||
List<AnyOfDeclaration> anyOfs = [] | ||
|
||
def branch(String name) { | ||
this.branches.add(name) | ||
} | ||
|
||
def expression(Closure closure) { | ||
this.expressions.add(closure) | ||
} | ||
|
||
def anyOf(@DelegatesTo(strategy = DELEGATE_FIRST, value = AnyOfDeclaration) Closure closure) { | ||
this.anyOfs.add(createComponent(AnyOfDeclaration, closure)) | ||
} | ||
|
||
def expressions(Object delegate) { | ||
return this.expressions.collect {executeWith(delegate, it)}.every() | ||
} | ||
|
||
def anyOf(Object delegate) { | ||
return this.anyOfs.collect {it.execute(delegate)} | ||
} | ||
|
||
Boolean execute(Object delegate) { | ||
def results = [] | ||
|
||
AntPathMatcher antPathMatcher = new AntPathMatcher() | ||
|
||
if (this.branches.size() > 0) { | ||
branches.each { branch -> | ||
results.add(antPathMatcher.match(branch, delegate.env.BRANCH_NAME)) | ||
} | ||
} | ||
|
||
if (this.expressions.size() > 0) { | ||
results.add(expressions(delegate)) | ||
} | ||
|
||
if (this.anyOfs.size() > 0) { | ||
results.addAll(anyOf(delegate)) | ||
} | ||
|
||
return results.every() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
pipeline { | ||
agent any | ||
stages { | ||
stage('Example Build') { | ||
steps { | ||
echo 'Hello World' | ||
} | ||
} | ||
stage('Example allOf expression') { | ||
when { | ||
allOf { | ||
expression { | ||
return env.SHOULD_EXECUTE == 'true' | ||
} | ||
expression { | ||
return env.SHOULD_ALSO_EXECUTE == 'true' | ||
} | ||
} | ||
} | ||
steps { | ||
echo 'Executing allOf with expression' | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.