Skip to content
Permalink
Browse files

[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 43699c2f605172202f228d627a4cb329dc95f984
@@ -32,6 +32,7 @@
import hudson.model.Job;
import hudson.model.Run;

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

/**
@@ -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;
@@ -69,6 +69,39 @@ public String getParameterName() {
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(
@@ -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>
@@ -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.
You can’t perform that action at this time.