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 #31 from amuniz/JENKINS-34853
[JENKINS-34853] Inject required resources environment variable
- Loading branch information
Showing
9 changed files
with
117 additions
and
11 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
43 changes: 43 additions & 0 deletions
43
src/main/java/org/jenkins/plugins/lockableresources/actions/ResourceVariableNameAction.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,43 @@ | ||
package org.jenkins.plugins.lockableresources.actions; | ||
|
||
import java.io.IOException; | ||
|
||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
||
import hudson.EnvVars; | ||
import hudson.Extension; | ||
import hudson.model.EnvironmentContributor; | ||
import hudson.model.InvisibleAction; | ||
import hudson.model.Run; | ||
import hudson.model.StringParameterValue; | ||
import hudson.model.TaskListener; | ||
|
||
@Restricted(NoExternalUse.class) | ||
public class ResourceVariableNameAction extends InvisibleAction { | ||
|
||
private final StringParameterValue resourceNameParameter; | ||
|
||
public ResourceVariableNameAction(StringParameterValue r) { | ||
this.resourceNameParameter = r; | ||
} | ||
|
||
StringParameterValue getParameter() { | ||
return resourceNameParameter; | ||
} | ||
|
||
@Extension | ||
public static final class ResourceVariableNameActionEnvironmentContributor extends EnvironmentContributor { | ||
|
||
@Override | ||
public void buildEnvironmentFor(Run r, EnvVars envs, TaskListener listener) | ||
throws IOException, InterruptedException { | ||
ResourceVariableNameAction a = r.getAction(ResourceVariableNameAction.class); | ||
if (a != null && a.getParameter() != null && a.getParameter().getValue() != null) { | ||
envs.put(a.getParameter().getName(), String.valueOf(a.getParameter().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
1 change: 1 addition & 0 deletions
1
src/main/resources/org/jenkins/plugins/lockableresources/LockStep/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
1 change: 1 addition & 0 deletions
1
src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/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
1 change: 1 addition & 0 deletions
1
...ain/resources/org/jenkins/plugins/lockableresources/LockableResourcesManager/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
1 change: 1 addition & 0 deletions
1
...in/resources/org/jenkins/plugins/lockableresources/RequiredResourcesProperty/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
54 changes: 54 additions & 0 deletions
54
src/test/java/org/jenkins/plugins/lockableresources/BasicIntegrationTest.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,54 @@ | ||
package org.jenkins.plugins.lockableresources; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Semaphore; | ||
|
||
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 org.jvnet.hudson.test.TestExtension; | ||
|
||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.BuildListener; | ||
import hudson.model.FreeStyleBuild; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.Result; | ||
|
||
public class BasicIntegrationTest { | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
@Issue("JENKINS-34853") | ||
public void security170fix() throws Exception { | ||
LockableResourcesManager.get().createResource("resource1"); | ||
FreeStyleProject p = j.createFreeStyleProject("p"); | ||
p.addProperty(new RequiredResourcesProperty("resource1", "resourceNameVar", null, null)); | ||
p.getBuildersList().add(new PrinterBuilder()); | ||
|
||
FreeStyleBuild b1 = p.scheduleBuild2(0).get(); | ||
j.assertLogContains("resourceNameVar: resource1", b1); | ||
j.assertBuildStatus(Result.SUCCESS, b1); | ||
} | ||
|
||
@TestExtension | ||
public static class PrinterBuilder extends MockBuilder { | ||
|
||
public PrinterBuilder() { | ||
super(Result.SUCCESS); | ||
} | ||
|
||
@Override | ||
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { | ||
listener.getLogger().println("resourceNameVar: " + build.getEnvironment(listener).get("resourceNameVar")); | ||
return true; | ||
} | ||
|
||
} | ||
|
||
} |