Skip to content

Commit

Permalink
Merge pull request #36 from amuniz/JENKINS-34758-fix
Browse files Browse the repository at this point in the history
[JENKINS-34758] Parameters visibility in child builds
  • Loading branch information
olivergondza committed May 24, 2016
2 parents 510aa05 + 7fc0953 commit d9e5c84
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
106 changes: 106 additions & 0 deletions src/main/java/hudson/matrix/MatrixChildParametersAction.java
@@ -0,0 +1,106 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* 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.
*/
package hudson.matrix;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.EnvironmentContributor;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.model.TaskListener;

/**
* This class is used to pass parameter actions from a {@link MatrixBuild} to a {@link MatrixRun}.
* This wrapper action is needed since SECURITY-170 is blocking undefined parameters in the child job (thus builds).
*
* It's intended for internal use only, that's why constructor and methods are packaged visible.
* The class itself is public to be visible to core so it can pick up the {@link MatrixChildParametersActionEnvironmentContributor}.
*/
@Restricted(NoExternalUse.class)
public class MatrixChildParametersAction extends ParametersAction implements MatrixChildAction {

private final List<ParameterValue> parameters;

MatrixChildParametersAction(List<ParameterValue> parameters) {
this.parameters = parameters;
}

@Override
public List<ParameterValue> getParameters() {
return parameters;
}

@Override
public ParameterValue getParameter(String name) {
for (ParameterValue p : parameters) {
if (p != null && p.getName().equals(name)) {
return p;
}
}
return null;
}

static MatrixChildParametersAction create(ParametersAction action) {
List<ParameterValue> p = new ArrayList<ParameterValue>();
if (action != null) {
p.addAll(action.getParameters());
}
return new MatrixChildParametersAction(p);
}

@Extension
public static final class MatrixChildParametersActionEnvironmentContributor extends EnvironmentContributor {

@Override
public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener)
throws IOException, InterruptedException {
if (r instanceof MatrixRun) {
MatrixChildParametersAction childParameters = r.getAction(MatrixChildParametersAction.class);
if (childParameters != null) {
for(ParameterValue p : childParameters.getParameters()) {
putEnvVar(envs, p.getName(), String.valueOf(p.getValue()));
}
}
}
}

private static void putEnvVar(@Nonnull EnvVars envs, String name, String value){
if (value != null) {
envs.put(name, value);
} else {
envs.put(name, "");
}
}
}
}
10 changes: 8 additions & 2 deletions src/main/java/hudson/matrix/MatrixConfiguration.java
Expand Up @@ -476,10 +476,16 @@ public boolean scheduleBuild(List<? extends Action> actions, Cause c) {
LOGGER.log(Level.WARNING, "Cannot schedule the build {0}. Jenkins is not ready", this);
return false;
}

List<Action> allActions = new ArrayList<Action>();
if(actions != null) {
allActions.addAll(actions);
for (Action a : actions) { // SECURITY-170
if (a instanceof ParametersAction) {
allActions.add(MatrixChildParametersAction.create((ParametersAction) a));
} else {
allActions.add(a);
}
}
}
allActions.add(new ParentBuildAction());
allActions.add(new CauseAction(c));
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/hudson/matrix/MatrixProjectTest.java
Expand Up @@ -70,6 +70,7 @@
import hudson.model.ParametersAction;
import hudson.model.FileParameterValue;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;

import java.util.List;
import java.util.ArrayList;
Expand Down Expand Up @@ -412,6 +413,24 @@ public void testTwoFileParams() throws Exception {
j.assertBuildStatusSuccess(f.get(10,TimeUnit.SECONDS));
}

@Issue("JENKINS-34758")
@Test
public void testParametersAsEnvOnChildren() throws Exception {
MatrixProject p = createMatrixProject();
p.setAxes(new AxisList(new TextAxis("foo","1")));
p.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("MY_PARAM","")
));
// must fail if $MY_PARAM or $foo are not defined in children
p.getBuildersList().add(new Shell("set -eux; echo $MY_PARAM; echo $foo"));

List<ParameterValue> params = new ArrayList<ParameterValue>();
params.add(new StringParameterValue("MY_PARAM", "value1"));

QueueTaskFuture<MatrixBuild> f = p.scheduleBuild2(0, new LegacyCodeCause(), new ParametersAction(params));
j.assertBuildStatusSuccess(f.get());
}

/**
* Verifies that the concurrent build feature works, and makes sure
* that each gets its own unique workspace.
Expand Down

0 comments on commit d9e5c84

Please sign in to comment.