Skip to content

Commit

Permalink
Merge pull request #834 from jglick/sickly-sweetest-injection
Browse files Browse the repository at this point in the history
Fix deprecations, avoid `@Inject`
  • Loading branch information
jglick authored Jan 18, 2024
2 parents f6091ab + 12630eb commit 5441e44
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 368 deletions.
12 changes: 6 additions & 6 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- TODO until in BOM: -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>657.v03b_e8115821b_</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -104,12 +110,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.jenkinsci.plugins.workflow.cps.steps;

import hudson.Extension;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import hudson.FilePath;
import hudson.model.TaskListener;
import java.util.Set;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Evaluate arbitrary script file.
*
* @author Kohsuke Kawaguchi
*/
public class LoadStep extends AbstractStepImpl {
public class LoadStep extends Step {
/**
* Relative path of the script within the current workspace.
*/
Expand All @@ -25,11 +30,13 @@ public String getPath() {
return path;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new LoadStepExecution(this, context);
}

@Extension
public static class DescriptorImpl extends AbstractStepDescriptorImpl {
public DescriptorImpl() {
super(LoadStepExecution.class);
}
public static class DescriptorImpl extends StepDescriptor {

@Override
public String getFunctionName() {
Expand All @@ -40,6 +47,11 @@ public String getFunctionName() {
public String getDisplayName() {
return "Evaluate a Groovy source file into the Pipeline script";
}

@Override
public Set<? extends Class<?>> getRequiredContext() {
return Set.of(FilePath.class, TaskListener.class);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jenkinsci.plugins.workflow.cps.steps;

import com.google.inject.Inject;
import groovy.lang.Script;
import hudson.FilePath;
import hudson.model.TaskListener;
Expand All @@ -12,26 +11,27 @@
import org.jenkinsci.plugins.workflow.cps.replay.ReplayAction;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.jenkinsci.plugins.workflow.steps.StepContext;

/**
* Loads another Groovy script file and executes it.
*
* @author Kohsuke Kawaguchi
*/
public class LoadStepExecution extends AbstractStepExecutionImpl {
@StepContextParameter
private transient FilePath cwd;

@Inject(optional=true)
private transient LoadStep step;

@StepContextParameter
private transient TaskListener listener;
LoadStepExecution(LoadStep step, StepContext context) {
super(context);
this.step = step;
}

@Override
public boolean start() throws Exception {
CpsStepContext cps = (CpsStepContext) getContext();
FilePath cwd = cps.get(FilePath.class);
TaskListener listener = cps.get(TaskListener.class);
CpsThread t = CpsThread.current();

CpsFlowExecution execution = t.getExecution();
Expand Down Expand Up @@ -62,6 +62,11 @@ public boolean start() throws Exception {
return false;
}

@Override
public void onResume() {
// do nothing
}

private static final long serialVersionUID = 1L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.log.TaskListenerDecorator;
import org.jenkinsci.plugins.workflow.steps.BodyInvoker;
import org.jenkinsci.plugins.workflow.steps.DynamicContext;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
Expand Down Expand Up @@ -155,21 +154,20 @@ private static final class DecoratorImpl extends TaskListenerDecorator {
}
}
private static final class YesPleaseDecorate implements Serializable {}
public static final class DecoratorStep extends Step implements Serializable {
public static final class DecoratorStep extends Step {
@DataBoundConstructor public DecoratorStep() {}
@DataBoundSetter public @CheckForNull String message;
@Override public StepExecution start(StepContext context) throws Exception {
return StepExecutions.block(context, this::start);
}
private void start(StepContext context, BodyInvoker invoker) throws Exception {
if (message != null) {
TaskListenerDecorator original = context.get(TaskListenerDecorator.class);
DecoratorImpl subsequent = new DecoratorImpl(message);
LOGGER.log(Level.INFO, "merging {0} with {1}", new Object[] {original, subsequent});
invoker.withContext(TaskListenerDecorator.merge(original, subsequent));
} else {
invoker.withContext(new YesPleaseDecorate());
}
return StepExecutions.block(context, (c, invoker) -> {
if (message != null) {
TaskListenerDecorator original = c.get(TaskListenerDecorator.class);
DecoratorImpl subsequent = new DecoratorImpl(message);
LOGGER.log(Level.INFO, "merging {0} with {1}", new Object[] {original, subsequent});
invoker.withContext(TaskListenerDecorator.merge(original, subsequent));
} else {
invoker.withContext(new YesPleaseDecorate());
}
});
}
@TestExtension("smokes") public static final class DescriptorImpl extends StepDescriptor {
@Override public String getFunctionName() {
Expand Down Expand Up @@ -213,11 +211,10 @@ private static final class Message implements Serializable {
public static final class GetMessageStep extends Step {
@DataBoundConstructor public GetMessageStep() {}
@Override public StepExecution start(StepContext context) throws Exception {
return StepExecutions.synchronous(context, GetMessageStep::run);
}
private static Object run(StepContext context) throws Exception {
Message message = context.get(Message.class);
return message != null ? message.text : null;
return StepExecutions.synchronous(context, c -> {
Message message = c.get(Message.class);
return message != null ? message.text : null;
});
}
@TestExtension("dynamicVsStatic") public static final class DescriptorImpl extends StepDescriptor {
@Override public String getFunctionName() {
Expand All @@ -228,7 +225,7 @@ private static Object run(StepContext context) throws Exception {
}
}
}
public static final class WithStaticMessageStep extends Step implements Serializable {
public static final class WithStaticMessageStep extends Step {
public final String text;
@DataBoundConstructor public WithStaticMessageStep(String text) {
this.text = text;
Expand Down Expand Up @@ -266,7 +263,7 @@ private static final class DynamicMessage implements Serializable {
return dynamicMessage != null ? new Message(dynamicMessage.text) : null;
}
}
public static final class WithDynamicMessageStep extends Step implements Serializable {
public static final class WithDynamicMessageStep extends Step {
public final String text;
@DataBoundConstructor public WithDynamicMessageStep(String text) {
this.text = text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import hudson.model.Describable;
import hudson.model.Executor;
import hudson.model.Result;
import java.io.Serializable;
import java.util.Collections;
import java.util.Set;
import java.util.logging.Level;
Expand Down Expand Up @@ -927,17 +926,14 @@ public void scriptInitializerCallsCpsTransformedMethod() throws Exception {
jenkins.assertLogContains("Scripts not permitted to use field Test metaClass", b);
}

public static class UnsafeParameterStep extends Step implements Serializable {
public static class UnsafeParameterStep extends Step {
private final UnsafeDescribable val;
@DataBoundConstructor
public UnsafeParameterStep(UnsafeDescribable val) {
this.val = val;
}
public StepExecution start(StepContext context) throws Exception {
return StepExecutions.synchronousNonBlocking(context, c -> {
val.doSomething();
return null;
});
@Override public StepExecution start(StepContext context) throws Exception {
return StepExecutions.synchronousNonBlockingVoid(context, c -> val.doSomething());
}
@TestExtension
public static class DescriptorImpl extends StepDescriptor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ public static class NopStep extends Step {
public NopStep(Object value) {}
@Override
public StepExecution start(StepContext context) throws Exception {
return StepExecutions.synchronous(context, unused -> null);
return StepExecutions.synchronousVoid(context, c -> {});
}
@TestExtension
public static class DescriptorImpl extends StepDescriptor {
Expand Down
Loading

0 comments on commit 5441e44

Please sign in to comment.