diff --git a/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional.java b/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional.java index a6ddaf291..08553b80f 100644 --- a/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional.java +++ b/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional.java @@ -55,12 +55,14 @@ public class ChangeSetConditional extends DeclarativeStageConditional c.compare(pattern, path, caseSensitive)); + } else { + return change.getAffectedPaths().stream().anyMatch(path -> c.compare(pattern, path, caseSensitive)); + } + } - return change.getAffectedPaths().stream().anyMatch(path -> c.compare(pattern, path, caseSensitive)); + @Deprecated + public boolean changeSetMatches(ChangeLogSet.Entry change, String pattern, boolean caseSensitive) { + return changeSetMatches(change, pattern, caseSensitive, this.isShouldMatchAll()); } @Extension diff --git a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AbstractChangelogConditionalScript.groovy b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AbstractChangelogConditionalScript.groovy index 12f4b00d2..683ad6b2e 100644 --- a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AbstractChangelogConditionalScript.groovy +++ b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AbstractChangelogConditionalScript.groovy @@ -81,15 +81,20 @@ abstract class AbstractChangelogConditionalScript - return set.any { def change -> - return matches(change) - } - } + return changeSetsMatches(changeSets); } return false } abstract boolean matches(ChangeLogSet.Entry change) + + boolean changeSetsMatches(List> changeSets) { + return changeSets.any {def set -> + return set.any { def change -> + return matches(change) + } + } + } + void initializeEval() {} } diff --git a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional/config.jelly b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional/config.jelly index e0f295337..6b2812b0c 100644 --- a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional/config.jelly +++ b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional/config.jelly @@ -35,5 +35,8 @@ + + + diff --git a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional/help-shouldMatchAll.html b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional/help-shouldMatchAll.html new file mode 100644 index 000000000..93aefb890 --- /dev/null +++ b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional/help-shouldMatchAll.html @@ -0,0 +1,27 @@ + + +

+ If true, the comparison should match all the elements in the changeset. Defaults to false. +

diff --git a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditionalScript.groovy b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditionalScript.groovy index 2edb2b9b8..d037ce1d0 100644 --- a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditionalScript.groovy +++ b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditionalScript.groovy @@ -35,8 +35,21 @@ class ChangeSetConditionalScript extends AbstractChangelogConditionalScript> changeSets) { + if (describable.shouldMatchAll) { + return changeSets.every { def set -> + return set.every { def change -> + return matches(change) + } + } + } else { + super.changeSetsMatches(changeSets); + } + } + @Override boolean matches(ChangeLogSet.Entry change) { - return describable.changeSetMatches(change, describable.pattern, describable.caseSensitive) + return describable.changeSetMatches(change, describable.pattern, describable.caseSensitive, describable.shouldMatchAll) } } diff --git a/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/WhenStageTest.java b/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/WhenStageTest.java index 93e37354b..c5215d502 100644 --- a/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/WhenStageTest.java +++ b/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/WhenStageTest.java @@ -156,8 +156,9 @@ public void whenChangeset() throws Exception { "Hello", "Stage \"Two\" skipped due to when conditional", "Stage \"Three\" skipped due to when conditional", + "Stage \"Four\" skipped due to when conditional", "Warning, empty changelog. Probably because this is the first build.") - .logNotContains("JS World", "With regexp"); + .logNotContains("JS World", "With regexp", "With shouldMatchAll"); builder.go(); builder.resetForNewRun(Result.SUCCESS); @@ -168,10 +169,11 @@ public void whenChangeset() throws Exception { sampleRepo.git("add", "somecode.js"); sampleRepo.git("commit", "--message=files"); - builder.logContains("Hello", "JS World", "With regexp") + builder.logContains("Hello", "JS World", "With regexp", "With shouldMatchAll") .logNotContains( "Stage \"Two\" skipped due to when conditional", "Stage \"Three\" skipped due to when conditional", + "Stage \"Four\" skipped due to when conditional", "Warning, empty changelog.", "Examining changelog from all builds of this change request.") .go(); @@ -185,8 +187,9 @@ public void whenChangesetMoreCommits() throws Exception { "Hello", "Stage \"Two\" skipped due to when conditional", "Stage \"Three\" skipped due to when conditional", + "Stage \"Four\" skipped due to when conditional", "Warning, empty changelog. Probably because this is the first build.") - .logNotContains("JS World", "With regexp"); + .logNotContains("JS World", "With regexp", "With shouldMatchAll"); builder.go(); builder.resetForNewRun(Result.SUCCESS); @@ -210,10 +213,12 @@ public void whenChangesetMoreCommits() throws Exception { sampleRepo.git("add", "somefile.js"); sampleRepo.git("commit", "--message=same"); - builder.logContains("Hello", "JS World", "With regexp") + builder.logContains("Hello", "JS World", "With regexp", + "Stage \"Four\" skipped due to when conditional") .logNotContains( "Stage \"Two\" skipped due to when conditional", "Stage \"Three\" skipped due to when conditional", + "With shouldMatchAll", "Warning, empty changelog.", "Examining changelog from all builds of this change request.") .go(); @@ -243,8 +248,10 @@ public void whenChangesetPR() throws Exception { j.assertLogContains("Hello", build); j.assertLogContains("Stage \"Two\" skipped due to when conditional", build); j.assertLogContains("Stage \"Three\" skipped due to when conditional", build); + j.assertLogContains("With shouldMatchAll", build); j.assertLogNotContains("JS World", build); j.assertLogNotContains("With regexp", build); + j.assertLogNotContains("Stage \"Four\" skipped due to when conditional", build); controller.addFile("repoX", crNum, "files", @@ -258,8 +265,10 @@ public void whenChangesetPR() throws Exception { j.assertLogContains("Hello", build2); j.assertLogContains("JS World", build2); j.assertLogContains("With regexp", build2); + j.assertLogContains("With shouldMatchAll", build2); j.assertLogNotContains("Stage \"Two\" skipped due to when conditional", build2); j.assertLogNotContains("Stage \"Three\" skipped due to when conditional", build2); + j.assertLogNotContains("Stage \"Four\" skipped due to when conditional", build2); j.assertLogNotContains("Warning, empty changelog", build2); controller.addFile("repoX", crNum, @@ -274,9 +283,11 @@ public void whenChangesetPR() throws Exception { j.assertLogContains("Hello", build3); j.assertLogContains("JS World", build3); j.assertLogContains("With regexp", build3); + j.assertLogContains("Stage \"Four\" skipped due to when conditional", build3); j.assertLogContains("Examining changelog from all builds of this change request", build3); j.assertLogNotContains("Stage \"Two\" skipped due to when conditional", build3); j.assertLogNotContains("Stage \"Three\" skipped due to when conditional", build3); + j.assertLogNotContains("With shouldMatchAll", build3); j.assertLogNotContains("Warning, empty changelog", build3); } diff --git a/pipeline-model-definition/src/test/resources/json/when/conditions/changelog/changeset.json b/pipeline-model-definition/src/test/resources/json/when/conditions/changelog/changeset.json index d4ed34538..852cb9337 100644 --- a/pipeline-model-definition/src/test/resources/json/when/conditions/changelog/changeset.json +++ b/pipeline-model-definition/src/test/resources/json/when/conditions/changelog/changeset.json @@ -61,6 +61,48 @@ "value": ".*\\.js" } }]} + }, + { + "name": "Four", + "branches": [ { + "name": "default", + "steps": [ { + "name": "echo", + "arguments": [ { + "key": "message", + "value": { + "isLiteral": true, + "value": "With shouldMatchAll" + } + }] + }] + }], + "when": {"conditions": [ { + "name": "changeset", + "arguments": [ + { + "key": "pattern", + "value": { + "isLiteral": true, + "value": "(.*\\.js|Jenkinsfile)" + } + }, + { + "key": "comparator", + "value": { + "isLiteral": true, + "value": "regexp" + } + }, + { + "key": "shouldMatchAll", + "value": { + "isLiteral": true, + "value": "true" + } + } + ] + }]} } ], "agent": { diff --git a/pipeline-model-definition/src/test/resources/when/conditions/changelog/changeset.groovy b/pipeline-model-definition/src/test/resources/when/conditions/changelog/changeset.groovy index d29f621d4..90f40aac5 100644 --- a/pipeline-model-definition/src/test/resources/when/conditions/changelog/changeset.groovy +++ b/pipeline-model-definition/src/test/resources/when/conditions/changelog/changeset.groovy @@ -51,5 +51,13 @@ pipeline { echo "With regexp" } } + stage("Four") { + when { + changeset pattern: '(.*\\.js|Jenkinsfile)', comparator: 'regexp', shouldMatchAll: true + } + steps { + echo "With shouldMatchAll" + } + } } }