Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from amuniz/JENKINS-34996
[JENKINS-34996] Release parameters visibility
- Loading branch information
Showing
17 changed files
with
204 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/hudson/plugins/release/SafeParametersAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package hudson.plugins.release; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
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; | ||
|
||
@Restricted(NoExternalUse.class) | ||
public class SafeParametersAction extends ParametersAction { | ||
|
||
private final List<ParameterValue> parameters; | ||
|
||
/** | ||
* At this point the list of parameter values is guaranteed to be safe, which is | ||
* parameter defined either at top level or release wrapper level. | ||
*/ | ||
SafeParametersAction(List<ParameterValue> parameters) { | ||
this.parameters = parameters; | ||
} | ||
|
||
/** | ||
* Returns all parameters allowed by the job (defined as regular job parameters) and | ||
* the parameters allowed by release-specific parameters definition. | ||
*/ | ||
@Override | ||
public List<ParameterValue> getParameters() { | ||
return Collections.unmodifiableList(parameters); | ||
} | ||
|
||
/** | ||
* Returns the parameter if defined as a regular parameters or it is a release-specific parameter defined | ||
* by the release wrapper. | ||
*/ | ||
@Override | ||
public ParameterValue getParameter(String name) { | ||
for (ParameterValue p : parameters) { | ||
if (p == null) continue; | ||
if (p.getName().equals(name)) { | ||
return p; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Extension | ||
public static final class SafeParametersActionEnvironmentContributor extends EnvironmentContributor { | ||
|
||
@Override | ||
public void buildEnvironmentFor(Run r, EnvVars envs, TaskListener listener) throws IOException, InterruptedException { | ||
SafeParametersAction action = r.getAction(SafeParametersAction.class); | ||
if (action != null) { | ||
for(ParameterValue p : action.getParameters()) { | ||
envs.put(p.getName(), String.valueOf(p.getValue())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...in/resources/hudson/plugins/release/promotion/ReleasePromotionCondition/Badge/index.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core"> | ||
<div> | ||
Build was a release build | ||
|
1 change: 1 addition & 0 deletions
1
src/main/resources/hudson/plugins/release/promotion/ReleasePromotionCondition/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core"> | ||
</j:jelly> |
1 change: 1 addition & 0 deletions
1
src/main/resources/hudson/plugins/release/promotion/ReleasePromotionCondition/index.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core"> | ||
<div> | ||
Not a release build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/test/java/hudson/plugins/release/TestReleasePluginParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package hudson.plugins.release; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.MockBuilder; | ||
|
||
import com.gargoylesoftware.htmlunit.html.HtmlForm; | ||
|
||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.BuildListener; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.Job; | ||
import hudson.model.ParameterDefinition; | ||
import hudson.model.ParametersDefinitionProperty; | ||
import hudson.model.Result; | ||
import hudson.model.StringParameterDefinition; | ||
import hudson.tasks.BuildStep; | ||
|
||
public class TestReleasePluginParameters { | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
@Issue("JENKINS-34996") | ||
public void testReleaseParameters() throws Exception { | ||
FreeStyleProject prj = j.createProject(FreeStyleProject.class, "foo"); | ||
ReleaseWrapper releaseWrapper = new ReleaseWrapper(); | ||
// Parameter in release specific builder | ||
releaseWrapper.setParameterDefinitions(Arrays.asList(new ParameterDefinition [] { | ||
new StringParameterDefinition("TEST", "test value") })); | ||
|
||
CheckerBuildStep checkerBuildStep = new CheckerBuildStep(); | ||
releaseWrapper.setPostSuccessfulBuildSteps(Arrays.asList(new BuildStep [] { checkerBuildStep })); | ||
prj.getBuildWrappersList().add(releaseWrapper); | ||
|
||
scheduleReleaseBuild(prj); | ||
|
||
j.waitUntilNoActivity(); | ||
assertTrue("Build finishes", prj.getLastBuild() != null); | ||
|
||
assertTrue("The parameter must be visible as environment variable", checkerBuildStep.value != null); | ||
assertTrue("The parameter value must match", checkerBuildStep.value.equals("test value")); | ||
j.assertBuildStatus(Result.SUCCESS, prj.getLastBuild()); | ||
} | ||
|
||
@Test | ||
@Issue("JENKINS-34996") // This test worked before the fix, added just as a verification | ||
public void testJobParameters() throws Exception { | ||
FreeStyleProject prj = j.createProject(FreeStyleProject.class, "foo"); | ||
// Parameter at top level (job) | ||
prj.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("TEST","test value"))); | ||
|
||
ReleaseWrapper releaseWrapper = new ReleaseWrapper(); | ||
CheckerBuildStep checkerBuildStep = new CheckerBuildStep(); | ||
releaseWrapper.setPostSuccessfulBuildSteps(Arrays.asList(new BuildStep [] { checkerBuildStep })); | ||
prj.getBuildWrappersList().add(releaseWrapper); | ||
|
||
scheduleReleaseBuild(prj); | ||
|
||
j.waitUntilNoActivity(); | ||
|
||
assertTrue("The parameter must be visible as environment variable", checkerBuildStep.value != null); | ||
assertTrue("The parameter value must match", checkerBuildStep.value.equals("test value")); | ||
j.assertBuildStatus(Result.SUCCESS, prj.getLastBuild()); | ||
} | ||
|
||
public static class CheckerBuildStep extends MockBuilder { | ||
|
||
public String value; | ||
|
||
public CheckerBuildStep() { | ||
super(Result.SUCCESS); | ||
} | ||
|
||
@Override | ||
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) | ||
throws InterruptedException, IOException { | ||
value = build.getEnvironment(listener).get("TEST"); | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* The release build must be scheduled from UI as all the parameters logic is tied | ||
* to {@link ReleaseWrapper.ReleaseAction#doSubmit(org.kohsuke.stapler.StaplerRequest, org.kohsuke.stapler.StaplerResponse)}. | ||
*/ | ||
private void scheduleReleaseBuild(Job<?, ?> job) throws Exception { | ||
HtmlForm releaseForm = j.createWebClient().getPage(job, "release").getFormByName("release-action-form"); | ||
j.submit(releaseForm); | ||
} | ||
|
||
} |