Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable comparator in the environment when #368

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,31 @@

package org.jenkinsci.plugins.pipeline.modeldefinition.when.impl;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import org.codehaus.groovy.ast.expr.Expression;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWhenContent;
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.ASTParserUtils;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditional;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditionalDescriptor;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.utils.Comparator;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import static org.apache.commons.lang.StringUtils.isEmpty;
import java.io.IOException;

/**
* Stage condition based on environment variable equality.
*/
@SuppressFBWarnings(value = "SE_NO_SERIALVERSIONID")
public class EnvironmentConditional extends DeclarativeStageConditional<EnvironmentConditional> {
private final String name;
private final String value;
private String value;
v1v marked this conversation as resolved.
Show resolved Hide resolved
private boolean ignoreCase = false;
private String comparator;

@DataBoundConstructor
public EnvironmentConditional(String name, String value) {
Expand All @@ -71,25 +74,37 @@ public void setIgnoreCase(boolean ignoreCase) {
this.ignoreCase = ignoreCase;
}

public boolean environmentMatches(String v, String var) {
if (isEmpty(var) && isEmpty(v)) {
return true;
} else if (isEmpty(var)) {
return false;
} else if (ignoreCase) {
return var.equalsIgnoreCase(v);
/**
* The {@link Comparator} to use.
* Default is {@link Comparator#EQUALS}
* @return the name of the comparator or null if default.
*/
public String getComparator() {
return comparator;
}

@DataBoundSetter
public void setComparator(String comparator) {
final Comparator c = Comparator.get(comparator, null);
if (c != null) {
this.comparator = c.name();
} else {
return var.equals(v);
this.comparator = null;
}
}

public boolean environmentMatches(String v, String var) {
Comparator c = Comparator.get(comparator, Comparator.EQUALS);
return c.compare(v, var, !this.isIgnoreCase());
}

@Extension
@Symbol("environment")
public static class DescriptorImpl extends DeclarativeStageConditionalDescriptor<EnvironmentConditional> {
@Override
@Nonnull
public String getDisplayName() {
return "Execute the stage if an environment variable exists and equals a value";
return "Execute the stage if an environment variable exists and matches a pattern";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ public boolean compare(@Nonnull String pattern, String actual, boolean caseSensi
EQUALS(Messages._Comparator_EQUALS_DisplayName()) {
@Override
public boolean compare(@Nonnull String pattern, String actual) {
actual = defaultIfBlank(actual, "");
return actual.equals(pattern);
return compare(pattern, actual, true);
}
@Override
public boolean compare(@Nonnull String pattern, String actual, boolean caseSensitive) {
return compare(pattern, actual);
actual = defaultIfBlank(actual, "");
if (caseSensitive) {
return actual.equals(pattern);
} else {
return actual.equalsIgnoreCase(pattern);
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<f:entry field="value" title="Expected value for environment variable">
<f:textbox/>
</f:entry>
<f:advanced>
<f:entry field="comparator" title="Comparator">
<f:select/>
</f:entry>
</f:advanced>
<f:entry field="ignoreCase">
<f:checkbox title="Ignore case when comparing" checked="false"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--
~ The MIT License
~
~ Copyright (c) 2019, CloudBees, Inc., Elasticsearch B.V.
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in
~ all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
~ THE SOFTWARE.
~
-->

<p>
What way to compare the pattern specified to the actual value.
<!-- TODO Update when new Comparators are added-->
Possible values are:
<ul>
<li><strong>EQUALS</strong> - A plain string comparison</li>
<li><strong>GLOB</strong> - <a href="https://ant.apache.org/manual/dirtasks.html#patterns">ANT style pattern</a></li>
<li><strong>REGEXP</strong> - <a href="https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Regular Expression</a></li>
</ul>
If not specified <strong>GLOB</strong> will be used as default.
v1v marked this conversation as resolved.
Show resolved Hide resolved
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
-->

<p>
If true, ignore case when comparing the environment variable and given value.
</p>
If true, ignore case when comparing the environment variable and given pattern onfly for
the EQUALS and GLOB comparators. Defaults to false.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
~ THE SOFTWARE.
-->

<p>
The value to compare the environment variable to.
</p>
<p>
The pattern to compare the environment variable with.
</p>

<p>
If <code>comparator</code> is not specified it will be evaluated as a plain string comparison.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
-->

<p>
Execute the stage if the specified environment variable's value matches the given value.
</p>
Execute the stage if the specified environment variable's value matches the given pattern.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,9 @@ public void whenNestedCombinations() throws Exception {
@Test
public void whenEnv() throws Exception {
expect("when/whenEnv")
.logContains("[Pipeline] { (One)", "[Pipeline] { (Two)", "World", "Ignore case worked")
.logNotContains("Should never be reached")
.logContains("[Pipeline] { (One)", "[Pipeline] { (Two)", "World",
"Ignore case worked", "Regexp case worked", "Glob with ignore case worked")
.logNotContains("Should never be reached", "Glob should never be reached")
.go();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ public void testEqualsComparator() {
assertFalse(comparator.compare("foo", null));
assertFalse(comparator.compare("foo", ""));
assertTrue(comparator.compare("foo", "foo"));
assertTrue(comparator.compare("foo", "FoO", false));
}
}
26 changes: 24 additions & 2 deletions pipeline-model-definition/src/test/resources/when/whenEnv.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pipeline {
echo "World"
echo "Heal it"
}

}
}
stage("Three") {
Expand All @@ -62,6 +61,29 @@ pipeline {
echo "Ignore case worked"
}
}

stage("Five") {
when {
environment name: "FOO", value: "(?i).*bar", comparator: 'REGEXP'
}
steps {
echo "Regexp case worked"
}
}
stage("Six") {
when {
environment name: "FOO", value: "*ar", comparator: 'GLOB'
}
steps {
echo "Glob should never be reached"
}
}
stage("Seven") {
when {
environment name: "FOO", value: "*ar", comparator: 'GLOB', ignoreCase: true
}
steps {
echo "Glob with ignore case worked"
}
}
}
}