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 #25 from jglick/context-step-JENKINS-28385
[JENKINS-28385] getContext & withContext steps
- Loading branch information
Showing
8 changed files
with
247 additions
and
2 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
74 changes: 74 additions & 0 deletions
74
src/main/java/org/jenkinsci/plugins/workflow/steps/GetContextStep.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,74 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 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 org.jenkinsci.plugins.workflow.steps; | ||
|
||
import com.google.inject.Inject; | ||
import hudson.Extension; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* Obtains a Jenkins API object from the current context. | ||
*/ | ||
public class GetContextStep extends AbstractStepImpl { | ||
|
||
public final Class<?> type; | ||
|
||
@DataBoundConstructor public GetContextStep(Class<?> type) { | ||
this.type = type; | ||
} | ||
|
||
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl { | ||
|
||
public DescriptorImpl() { | ||
super(Execution.class); | ||
} | ||
|
||
@Override public String getFunctionName() { | ||
return "getContext"; | ||
} | ||
|
||
@Override public String getDisplayName() { | ||
return "Get contextual object from internal APIs"; | ||
} | ||
|
||
@Override public boolean isAdvanced() { | ||
return true; | ||
} | ||
|
||
} | ||
|
||
public static class Execution extends AbstractSynchronousStepExecution<Object> { | ||
|
||
private static final long serialVersionUID = 1; | ||
|
||
@Inject(optional=true) private transient GetContextStep step; | ||
|
||
@Override protected Object run() throws Exception { | ||
return getContext().get(step.type); | ||
} | ||
|
||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/org/jenkinsci/plugins/workflow/steps/WithContextStep.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,94 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 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 org.jenkinsci.plugins.workflow.steps; | ||
|
||
import com.google.inject.Inject; | ||
import hudson.Extension; | ||
import hudson.LauncherDecorator; | ||
import hudson.console.ConsoleLogFilter; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* Supplies a contextual Jenkins API object to a block. | ||
*/ | ||
public class WithContextStep extends AbstractStepImpl { | ||
|
||
public final Object context; | ||
|
||
@DataBoundConstructor public WithContextStep(Object context) { | ||
this.context = context; | ||
} | ||
|
||
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl { | ||
|
||
public DescriptorImpl() { | ||
super(Execution.class); | ||
} | ||
|
||
@Override public String getFunctionName() { | ||
return "withContext"; | ||
} | ||
|
||
@Override public boolean takesImplicitBlockArgument() { | ||
return true; | ||
} | ||
|
||
@Override public String getDisplayName() { | ||
return "Use contextual object from internal APIs within a block"; | ||
} | ||
|
||
@Override public boolean isAdvanced() { | ||
return true; | ||
} | ||
|
||
} | ||
|
||
public static class Execution extends AbstractStepExecutionImpl { | ||
|
||
private static final long serialVersionUID = 1; | ||
|
||
@Inject(optional=true) private transient WithContextStep step; | ||
|
||
@Override public boolean start() throws Exception { | ||
Object obj = step.context; | ||
StepContext context = getContext(); | ||
if (obj instanceof ConsoleLogFilter) { | ||
obj = BodyInvoker.mergeConsoleLogFilters(context.get(ConsoleLogFilter.class), (ConsoleLogFilter) obj); | ||
} else if (obj instanceof LauncherDecorator) { | ||
obj = BodyInvoker.mergeLauncherDecorators(context.get(LauncherDecorator.class), (LauncherDecorator) obj); | ||
} else if (obj instanceof EnvironmentExpander) { | ||
obj = EnvironmentExpander.merge(context.get(EnvironmentExpander.class), (EnvironmentExpander) obj); | ||
} | ||
context.newBodyInvoker().withContext(obj).withCallback(BodyExecutionCallback.wrap(context)).start(); | ||
return false; | ||
} | ||
|
||
@Override public void stop(Throwable cause) throws Exception { | ||
getContext().onFailure(cause); | ||
} | ||
|
||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
src/main/resources/org/jenkinsci/plugins/workflow/steps/GetContextStep/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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form"> | ||
<f:block>No snippet generation supported. See inline help for usage.</f:block> | ||
</j:jelly> |
8 changes: 8 additions & 0 deletions
8
src/main/resources/org/jenkinsci/plugins/workflow/steps/GetContextStep/help.html
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,8 @@ | ||
<p> | ||
Obtains a contextual object as in <code>StepContext.get</code>; cf. <code>withContext</code>. | ||
Takes a single <code>type</code> argument. Example: | ||
</p> | ||
<pre><code>getContext hudson.FilePath</code></pre> | ||
<p> | ||
For use from trusted code, such as global libraries, which can manipulate internal Jenkins APIs. | ||
</p> |
5 changes: 5 additions & 0 deletions
5
src/main/resources/org/jenkinsci/plugins/workflow/steps/WithContextStep/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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form"> | ||
<f:block>No snippet generation supported. See inline help for usage.</f:block> | ||
</j:jelly> |
13 changes: 13 additions & 0 deletions
13
src/main/resources/org/jenkinsci/plugins/workflow/steps/WithContextStep/help.html
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,13 @@ | ||
<p> | ||
Wraps a block in a contextual object as in <code>BodyInvoker.withContext</code>; cf. <code>getContext</code>. | ||
Takes a single <code>context</code> argument plus a block. Example: | ||
</p> | ||
<pre><code>withContext(new MyConsoleLogFilter()) { | ||
sh 'process' | ||
}</code></pre> | ||
<p> | ||
Automatically merges its argument with contextual objects in the case of <code>ConsoleLogFilter</code>, <code>LauncherDecorator</code>, and <code>EnvironmentExpander</code>. | ||
</p> | ||
<p> | ||
For use from trusted code, such as global libraries, which can manipulate internal Jenkins APIs. | ||
</p> |
46 changes: 46 additions & 0 deletions
46
src/test/java/org/jenkinsci/plugins/workflow/steps/WithContextStepTest.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,46 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 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 org.jenkinsci.plugins.workflow.steps; | ||
|
||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.Rule; | ||
import org.jvnet.hudson.test.BuildWatcher; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
public class WithContextStepTest { | ||
|
||
@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher(); | ||
@Rule public JenkinsRule r = new JenkinsRule(); | ||
|
||
@Test public void pushd() throws Exception { | ||
WorkflowJob p = r.createProject(WorkflowJob.class, "p"); | ||
p.setDefinition(new CpsFlowDefinition("node {withContext(getContext(hudson.FilePath).child('subdir')) {echo(/simulating dir step in ${pwd()}/)}}", false)); | ||
r.assertLogContains("simulating dir step in " + r.jenkins.getWorkspaceFor(p).child("subdir").getRemote(), r.buildAndAssertSuccess(p)); | ||
} | ||
|
||
} |