Skip to content
Permalink
Browse files
[FIXED JENKINS-9327] match filters against all EnvVars, not just buil…
…d parameters
  • Loading branch information
alanharder committed Apr 16, 2011
1 parent a8a0840 commit d88baa76cc716584848f2f9a3c71d2b9628e3c0e
@@ -249,9 +249,12 @@ private static class JobResolver {
int i = projectName.indexOf('/');
if (i > 0) {
Job<?,?> candidate = hudson.getItemByFullName(projectName.substring(0, i), Job.class);
if (candidate != null && candidate.getProperty(ParametersDefinitionProperty.class) != null) {
job = candidate;
filter = new ParametersBuildFilter(projectName.substring(i + 1));
if (candidate != null) {
ParametersBuildFilter pFilter = new ParametersBuildFilter(projectName.substring(i + 1));
if (pFilter.isValid(candidate)) {
job = candidate;
filter = pFilter;
}
}
}
}
@@ -24,9 +24,8 @@
package hudson.plugins.copyartifact;

import hudson.EnvVars;
import hudson.model.BooleanParameterValue;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.TaskListener;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.StringParameterValue;
import java.util.ArrayList;
@@ -35,61 +34,55 @@
import java.util.regex.Pattern;

/**
* Additional filter used by BuildSelector.
* Filter to find builds matching particular parameters.
* @author Alan Harder
*/
public class ParametersBuildFilter extends BuildFilter {
private String paramsToMatch;
private List<StringParameterValue> stringMatches;
private List<BooleanParameterValue> booleanMatches;
private List<StringParameterValue> filters;

private static final Pattern PARAMVAL_PATTERN = Pattern.compile("(.*?)=([^,]*)(,|$)");

public ParametersBuildFilter(String paramsToMatch) {
this.paramsToMatch = paramsToMatch;
// Initialize.. parse out the given parameters/values.
filters = new ArrayList<StringParameterValue>(5);
Matcher m = PARAMVAL_PATTERN.matcher(paramsToMatch);
while (m.find()) {
filters.add(new StringParameterValue(m.group(1), m.group(2)));
}
}

public boolean isValid(Job<?,?> job) {
if (filters.isEmpty()) return false; // Unable to parse text after /
// Consider the filter valid for this job if any build for this job has all the filter params
outer:
for (Run<?,?> run = job.getLastCompletedBuild(); run != null; run = run.getPreviousCompletedBuild()) try {
EnvVars env = run.getEnvironment(TaskListener.NULL);
for (StringParameterValue spv : filters) {
if (!env.containsKey(spv.getName())) {
continue outer;
}
}
return true;
} catch (Exception ignore) {
}
return false;
}

/**
* {@inheritDoc}
*/
@Override
public boolean isSelectable(Run<?,?> run, EnvVars env) {
if (stringMatches == null) {
// Initialize.. parse out the given parameters/values.
Matcher m = PARAMVAL_PATTERN.matcher(paramsToMatch);
stringMatches = new ArrayList<StringParameterValue>(5);
booleanMatches = new ArrayList<BooleanParameterValue>(5);
while (m.find()) {
String name = m.group(1), value = m.group(2);
stringMatches.add(new StringParameterValue(name, value));
// Try Boolean if parameter value looks boolean
if ("true".equalsIgnoreCase(value) || "1".equals(value)
|| "yes".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value)) {
booleanMatches.add(new BooleanParameterValue(name, true));
}
else if ("false".equalsIgnoreCase(value) || "0".equals(value)
|| "no".equalsIgnoreCase(value) || "off".equalsIgnoreCase(value)) {
booleanMatches.add(new BooleanParameterValue(name, false));
}
else booleanMatches.add(null);
}
EnvVars otherEnv;
try {
otherEnv = run.getEnvironment(TaskListener.NULL);
} catch (Exception ex) {
return false;
}
if (stringMatches.isEmpty()) return false; // Unable to parse text after /

ParametersAction pa = run.getAction(ParametersAction.class);
if (pa == null) return false;
int i = 0;
// All parameters must match (either as string or boolean):
for (StringParameterValue spv : stringMatches) {
BooleanParameterValue bpv = booleanMatches.get(i++);
boolean ok = false;
for (ParameterValue pv : pa.getParameters()) {
if (spv.equals(pv) || (bpv != null && bpv.equals(pv))) {
ok = true;
break;
}
for (StringParameterValue spv : filters) {
if (!spv.value.equals(otherEnv.get(spv.getName()))) {
return false;
}
if (!ok) return false; // No match for this parameter
}
return true;
}
@@ -22,13 +22,11 @@
select the matching configuration in the source project.
Example: <tt>OtherMatrixJob/jdk=$jdk</tt>
<p/>
Parameterized jobs may be filtered to select only builds matching particular
parameters. Use <tt>JOBNAME/PARAM=VALUE,...</tt> in the project name box to list
the parameter filter; this is the same syntax as described above for
Jobs may be filtered to select only builds matching particular parameters or
other build variables. Use <tt>JOBNAME/PARAM=VALUE,...</tt> in the project name
box to list the parameter filter; this is the same syntax as described above for
multiconfiguration jobs, except with parameters instead of axis values.
For example, <tt>MyParamJob/FOO=bar,BAZ=true</tt> examines only builds from
<tt>MyParamJob</tt> that ran with parameter <tt>FOO</tt> set to <tt>bar</tt>
and the checkbox for <tt>BAZ</tt> was checked. Checkbox (boolean) parameters
may be specified in any of several common ways: true, 1, yes, on for true
and false, 0, no, off for false; all are case insensitive.
and the checkbox for <tt>BAZ</tt> was checked.
</div>
@@ -619,13 +619,13 @@ public void testFilterByParameters() throws Exception {
assertBuildStatusSuccess(b);
assertEquals("2", envStep.getEnvVars().get("COPYARTIFACT_BUILD_NUMBER_FOO_JOB"));

p = createProject(other.getName() + "/BAR=0", "*.txt", "", true, false, false);
p = createProject(other.getName() + "/BAR=false", "*.txt", "", true, false, false);
p.getBuildersList().add(envStep);
b = p.scheduleBuild2(0, new UserCause()).get();
assertBuildStatusSuccess(b);
assertEquals("1", envStep.getEnvVars().get("COPYARTIFACT_BUILD_NUMBER_FOO_JOB"));

p = createProject(other.getName() + "/BAZ=foo,BAR=yes", "*.txt", "", true, false, false);
p = createProject(other.getName() + "/BAZ=foo,BAR=true", "*.txt", "", true, false, false);
p.getBuildersList().add(envStep);
b = p.scheduleBuild2(0, new UserCause()).get();
assertBuildStatusSuccess(b);
@@ -640,6 +640,13 @@ public void testFilterByParameters() throws Exception {
p = createProject(other.getName() + "/BAZ=bar,FOO=bogus", "*.txt", "", true, false, false);
b = p.scheduleBuild2(0, new UserCause()).get();
assertBuildStatus(Result.FAILURE, b);

// Test matching other build variables besides parameters
p = createProject(other.getName() + "/BUILD_NUMBER=2", "*.txt", "", true, false, false);
p.getBuildersList().add(envStep);
b = p.scheduleBuild2(0, new UserCause()).get();
assertBuildStatusSuccess(b);
assertEquals("2", envStep.getEnvVars().get("COPYARTIFACT_BUILD_NUMBER_FOO_JOB"));
}

public void testSavedBuildSelectorWithParameterFilter() throws Exception {

0 comments on commit d88baa7

Please sign in to comment.