Skip to content
Permalink
Browse files

Merge pull request #25 from jglick/context-step-JENKINS-28385

[JENKINS-28385] getContext & withContext steps
  • Loading branch information
jglick committed Nov 1, 2016
2 parents 7d6d996 + 6fe24d2 commit bf17f230386b6441e93807e92b155a2a93f219a3
@@ -95,13 +95,13 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.21</version>
<version>2.22-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.21</version>
<version>2.22-SNAPSHOT</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
@@ -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);
}

}

}
@@ -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);
}

}

}
@@ -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>
@@ -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>
@@ -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>
@@ -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>
@@ -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));
}

}

0 comments on commit bf17f23

Please sign in to comment.
You can’t perform that action at this time.