Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-30357] Extends ParameterizedBuildSelector to also accepts im…
…mediate values (<Selector />) and variable expressions (${SELECTOR}).
  • Loading branch information
ikedam committed Sep 21, 2015
1 parent 4e6cb78 commit 43699c2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
Expand Up @@ -32,6 +32,7 @@
import hudson.model.Job;
import hudson.model.Run;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

/**
Expand All @@ -54,9 +55,8 @@ public String getParameterName() {

@Override
public Run<?,?> getBuild(Job<?,?> job, EnvVars env, BuildFilter filter, Run<?,?> parent) {
String xml = env.get(getParameterName());
String xml = resolveParameter(env);
if (xml == null) {
LOG.log(Level.WARNING, "{0} is not defined", getParameterName());
return null;
}
BuildSelector selector = null;
Expand All @@ -69,6 +69,39 @@ public Run<?,?> getBuild(Job<?,?> job, EnvVars env, BuildFilter filter, Run<?,?>
return selector.getBuild(job, env, filter, parent);
}

/**
* Expand the parameter and resolve it to a xstream expression.
* <ol>
* <li>Considers an immediate value if contains '&lt;'.
* This is expected to be used in especially in workflow jobs.</li>
* <li>Otherwise, considers a variable expression if contains '$'.
* This is to keep the compatibility of usage between workflow jobs and non-workflow jobs.</li>
* <li>Otherwise, considers a variable name.</li>
* </ol>
*
* @param env
* @return xstream expression.
*/
private String resolveParameter(EnvVars env) {
if (StringUtils.isBlank(getParameterName())) {
LOG.log(Level.WARNING, "Parameter name is not specified");
return null;
}
if (getParameterName().contains("<")) {
LOG.log(Level.FINEST, "{0} is considered a xstream expression", getParameterName());
return getParameterName();
}
if (getParameterName().contains("$")) {
LOG.log(Level.FINEST, "{0} is considered a variable expression", getParameterName());
return env.expand(getParameterName());
}
String xml = env.get(getParameterName());
if (xml == null) {
LOG.log(Level.WARNING, "{0} is not defined", getParameterName());
}
return xml;
}

@Extension(ordinal=-20)
public static final Descriptor<BuildSelector> DESCRIPTOR =
new SimpleBuildSelectorDescriptor(
Expand Down
Expand Up @@ -2,4 +2,7 @@
Name of the "build selector" parameter. A parameter with this name should be added
in the build parameters section above. There is a special parameter type for choosing
the build selector.
<p>
You can pass not only the parameter name, but also the parameter value itself.
This is useful especially used with workflow-plugin.
</div>
Expand Up @@ -208,4 +208,81 @@ public void testEmptySelector() throws Exception {
).get();
j.assertBuildStatusSuccess(b);
}

/**
* Also accepts immediate value.
*
* @throws Exception
*/
@Test
public void testImmediateValue() throws Exception {
// Prepare an artifact to be copied.
FreeStyleProject copiee = j.createFreeStyleProject("copiee");
copiee.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
copiee.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
j.assertBuildStatusSuccess(copiee.scheduleBuild2(0));

WorkflowJob copier = j.jenkins.createProject(WorkflowJob.class, "copier");
copier.setDefinition(new CpsFlowDefinition(
"node {"
+ "step([$class: 'CopyArtifact',"
+ "projectName: 'copiee',"
+ "filter: '**/*',"
+ "selector: [$class: 'ParameterizedBuildSelector', parameterName: '${SELECTOR}'],"
+ "]);"
+ "step([$class: 'ArtifactArchiver', artifacts: '**/*']);"
+ "}",
true
));

WorkflowRun b = j.assertBuildStatusSuccess(copier.scheduleBuild2(
0,
null,
new ParametersAction(new StringParameterValue(
"SELECTOR",
"<StatusBuildSelector><stable>true</stable></StatusBuildSelector>"
))
));

VirtualFile vf = b.getArtifactManager().root().child("artifact.txt");
assertEquals("foobar", IOUtils.toString(vf.open()));
}


/**
* Also accepts variable expression.
*
* @throws Exception
*/
@Test
public void testVariableExpression() throws Exception {
// Prepare an artifact to be copied.
FreeStyleProject copiee = j.createFreeStyleProject();
copiee.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
copiee.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
j.assertBuildStatusSuccess(copiee.scheduleBuild2(0));

FreeStyleProject copier = j.createFreeStyleProject();
ParameterizedBuildSelector pbs = new ParameterizedBuildSelector("${SELECTOR}");
copier.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(
copiee.getFullName(),
null, // parameters
pbs,
"**/*", // filter
"", // excludes
false, // flatten
false, // optional
false // finterprintArtifacts
));
FreeStyleBuild b = j.assertBuildStatusSuccess((FreeStyleBuild)copier.scheduleBuild2(
0,
new ParametersAction(new StringParameterValue(
"SELECTOR",
"<StatusBuildSelector><stable>true</stable></StatusBuildSelector>"
))
).get());

assertEquals("foobar", b.getWorkspace().child("artifact.txt").readToString());
}

}

0 comments on commit 43699c2

Please sign in to comment.