Skip to content

Commit

Permalink
[JENKINS-28385] Initial implementation of getContext & withContext st…
Browse files Browse the repository at this point in the history
…eps.
  • Loading branch information
jglick committed Oct 20, 2016
1 parent f541cd2 commit 72f7e2a
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -28,8 +28,8 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.14</version>
<relativePath />
<version>2.17</version>
<relativePath/>
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
Expand Down
@@ -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 comments on commit 72f7e2a

Please sign in to comment.