Skip to content

Commit a85f7c5

Browse files
authored
Merge pull request #243 from abayer/jenkins-49226
[FIXED JENKINS-49226] Add equals condition.
2 parents 6cd7cb8 + 2a0cc3e commit a85f7c5

File tree

8 files changed

+258
-1
lines changed

8 files changed

+258
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2018, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.jenkinsci.plugins.pipeline.modeldefinition.when.impl;
26+
27+
import hudson.Extension;
28+
import org.codehaus.groovy.ast.expr.Expression;
29+
import org.jenkinsci.Symbol;
30+
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWhenContent;
31+
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.ASTParserUtils;
32+
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditional;
33+
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditionalDescriptor;
34+
import org.kohsuke.stapler.DataBoundConstructor;
35+
import org.kohsuke.stapler.DataBoundSetter;
36+
37+
import javax.annotation.CheckForNull;
38+
39+
import static org.apache.commons.lang.StringUtils.isEmpty;
40+
41+
/**
42+
* Stage condition based on object equality.
43+
*/
44+
public class EqualsConditional extends DeclarativeStageConditional<EqualsConditional> {
45+
private final Object expected;
46+
private final Object actual;
47+
48+
@DataBoundConstructor
49+
public EqualsConditional(Object expected, Object actual) {
50+
this.expected = expected;
51+
this.actual = actual;
52+
}
53+
54+
public Object getActual() {
55+
return actual;
56+
}
57+
58+
public Object getExpected() {
59+
return expected;
60+
}
61+
62+
@Extension
63+
@Symbol("equals")
64+
public static class DescriptorImpl extends DeclarativeStageConditionalDescriptor<EqualsConditional> {
65+
@Override
66+
public Expression transformToRuntimeAST(@CheckForNull ModelASTWhenContent original) {
67+
return ASTParserUtils.transformWhenContentToRuntimeAST(original);
68+
}
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!--
2+
~ The MIT License
3+
~
4+
~ Copyright (c) 2018, CloudBees, Inc.
5+
~
6+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
7+
~ of this software and associated documentation files (the "Software"), to deal
8+
~ in the Software without restriction, including without limitation the rights
9+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
~ copies of the Software, and to permit persons to whom the Software is
11+
~ furnished to do so, subject to the following conditions:
12+
~
13+
~ The above copyright notice and this permission notice shall be included in
14+
~ all copies or substantial portions of the Software.
15+
~
16+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
~ THE SOFTWARE.
23+
-->
24+
25+
<p>
26+
The actual value to compare against. Can be <code>variableName</code>, <code>env.ENV_VAR</code>,
27+
<code>"${variableName}"</code>, <code>"${env.ENV_VAR}"</code>, any other string, boolean, or number,
28+
or <code>null</code>.
29+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!--
2+
~ The MIT License
3+
~
4+
~ Copyright (c) 2018, CloudBees, Inc.
5+
~
6+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
7+
~ of this software and associated documentation files (the "Software"), to deal
8+
~ in the Software without restriction, including without limitation the rights
9+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
~ copies of the Software, and to permit persons to whom the Software is
11+
~ furnished to do so, subject to the following conditions:
12+
~
13+
~ The above copyright notice and this permission notice shall be included in
14+
~ all copies or substantial portions of the Software.
15+
~
16+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
~ THE SOFTWARE.
23+
-->
24+
25+
<p>
26+
The expected value to compare against. Can be a string, boolean, number, or <code>null</code>.
27+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!--
2+
~ The MIT License
3+
~
4+
~ Copyright (c) 2018, CloudBees, Inc.
5+
~
6+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
7+
~ of this software and associated documentation files (the "Software"), to deal
8+
~ in the Software without restriction, including without limitation the rights
9+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
~ copies of the Software, and to permit persons to whom the Software is
11+
~ furnished to do so, subject to the following conditions:
12+
~
13+
~ The above copyright notice and this permission notice shall be included in
14+
~ all copies or substantial portions of the Software.
15+
~
16+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
~ THE SOFTWARE.
23+
-->
24+
25+
<p>
26+
Execute the stage if the expected value equals the actual value given.
27+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2018, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
26+
package org.jenkinsci.plugins.pipeline.modeldefinition.when.impl
27+
28+
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditionalScript
29+
import org.jenkinsci.plugins.workflow.cps.CpsScript
30+
31+
32+
class EqualsConditionalScript extends DeclarativeStageConditionalScript<EqualsConditional> {
33+
EqualsConditionalScript(CpsScript s, EqualsConditional c) {
34+
super(s, c)
35+
}
36+
37+
@Override
38+
boolean evaluate() {
39+
return describable.expected == describable.actual
40+
}
41+
}

pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/AbstractModelDefTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public static Iterable<Object[]> configsWithErrors() {
225225
result.add(new Object[]{"agentUnknownParamForType", Messages.ModelValidatorImpl_InvalidAgentParameter("fruit", "otherField", "[label, otherField, nested]")});
226226
result.add(new Object[]{"notificationsSectionRemoved", "additional properties are not allowed"});
227227
result.add(new Object[]{"unknownWhenConditional", Messages.ModelValidatorImpl_UnknownWhenConditional("banana",
228-
"allOf, anyOf, branch, changelog, changeset, environment, expression, not")});
228+
"allOf, anyOf, branch, changelog, changeset, environment, equals, expression, not")});
229229
result.add(new Object[]{"whenInvalidParameterType", Messages.ModelValidatorImpl_InvalidUnnamedParameterType("class java.lang.String", 4, Integer.class)});
230230
result.add(new Object[]{"whenMissingRequiredParameter", Messages.ModelValidatorImpl_MissingRequiredStepParameter("value")});
231231
result.add(new Object[]{"whenUnknownParameter", Messages.ModelValidatorImpl_InvalidStepParameter("banana", "name")});

pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/WhenStageTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,16 @@ public void whenBeforeAgentUnspecified() throws Exception {
393393
.go();
394394
}
395395

396+
@Issue("JENKINS-49226")
397+
@Test
398+
public void whenEquals() throws Exception {
399+
env(s).put("SECOND_STAGE", "NOPE").set();
400+
ExpectationsBuilder expect = expect("when", "whenEquals").runFromRepo(false);
401+
expect.logContains("One", "Hello", "Two").logNotContains("World").go();
402+
env(s).put("SECOND_STAGE", "RUN").set();
403+
expect.resetForNewRun(Result.SUCCESS).logContains("One", "Hello", "Two", "World").go();
404+
}
405+
396406
private void waitFor(Queue.Item item) throws InterruptedException, ExecutionException {
397407
while (item != null && item.getFuture() == null) {
398408
Thread.sleep(200);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2018, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
pipeline {
26+
agent {
27+
label "here"
28+
}
29+
stages {
30+
stage("One") {
31+
steps {
32+
echo "Hello"
33+
}
34+
}
35+
stage("Two") {
36+
when {
37+
allOf {
38+
equals(expected: "RUN", actual: env.SECOND_STAGE)
39+
equals(expected: null, actual: currentBuild.result)
40+
equals(expected: 2, actual: currentBuild.number)
41+
equals(expected: "2", actual: "${currentBuild.number}")
42+
}
43+
}
44+
steps {
45+
script {
46+
echo "World"
47+
echo "Heal it"
48+
}
49+
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)