Skip to content

Commit 5441e44

Browse files
authored
Merge pull request #834 from jglick/sickly-sweetest-injection
Fix deprecations, avoid `@Inject`
2 parents f6091ab + 12630eb commit 5441e44

File tree

14 files changed

+301
-368
lines changed

14 files changed

+301
-368
lines changed

plugin/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
<scope>import</scope>
5555
<type>pom</type>
5656
</dependency>
57+
<!-- TODO until in BOM: -->
58+
<dependency>
59+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
60+
<artifactId>workflow-step-api</artifactId>
61+
<version>657.v03b_e8115821b_</version>
62+
</dependency>
5763
</dependencies>
5864
</dependencyManagement>
5965
<dependencies>
@@ -104,12 +110,6 @@
104110
</exclusion>
105111
</exclusions>
106112
</dependency>
107-
<dependency>
108-
<groupId>org.jenkins-ci.plugins.workflow</groupId>
109-
<artifactId>workflow-step-api</artifactId>
110-
<classifier>tests</classifier>
111-
<scope>test</scope>
112-
</dependency>
113113
<dependency>
114114
<groupId>org.jenkins-ci.plugins.workflow</groupId>
115115
<artifactId>workflow-support</artifactId>

plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/steps/LoadStep.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package org.jenkinsci.plugins.workflow.cps.steps;
22

33
import hudson.Extension;
4-
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
5-
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
4+
import hudson.FilePath;
5+
import hudson.model.TaskListener;
6+
import java.util.Set;
7+
import org.jenkinsci.plugins.workflow.steps.Step;
8+
import org.jenkinsci.plugins.workflow.steps.StepContext;
9+
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
10+
import org.jenkinsci.plugins.workflow.steps.StepExecution;
611
import org.kohsuke.stapler.DataBoundConstructor;
712

813
/**
914
* Evaluate arbitrary script file.
1015
*
1116
* @author Kohsuke Kawaguchi
1217
*/
13-
public class LoadStep extends AbstractStepImpl {
18+
public class LoadStep extends Step {
1419
/**
1520
* Relative path of the script within the current workspace.
1621
*/
@@ -25,11 +30,13 @@ public String getPath() {
2530
return path;
2631
}
2732

33+
@Override
34+
public StepExecution start(StepContext context) throws Exception {
35+
return new LoadStepExecution(this, context);
36+
}
37+
2838
@Extension
29-
public static class DescriptorImpl extends AbstractStepDescriptorImpl {
30-
public DescriptorImpl() {
31-
super(LoadStepExecution.class);
32-
}
39+
public static class DescriptorImpl extends StepDescriptor {
3340

3441
@Override
3542
public String getFunctionName() {
@@ -40,6 +47,11 @@ public String getFunctionName() {
4047
public String getDisplayName() {
4148
return "Evaluate a Groovy source file into the Pipeline script";
4249
}
50+
51+
@Override
52+
public Set<? extends Class<?>> getRequiredContext() {
53+
return Set.of(FilePath.class, TaskListener.class);
54+
}
4355
}
4456

4557
}

plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/steps/LoadStepExecution.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.jenkinsci.plugins.workflow.cps.steps;
22

3-
import com.google.inject.Inject;
43
import groovy.lang.Script;
54
import hudson.FilePath;
65
import hudson.model.TaskListener;
@@ -12,26 +11,27 @@
1211
import org.jenkinsci.plugins.workflow.cps.replay.ReplayAction;
1312
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
1413
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
15-
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
14+
import org.jenkinsci.plugins.workflow.steps.StepContext;
1615

1716
/**
1817
* Loads another Groovy script file and executes it.
1918
*
2019
* @author Kohsuke Kawaguchi
2120
*/
2221
public class LoadStepExecution extends AbstractStepExecutionImpl {
23-
@StepContextParameter
24-
private transient FilePath cwd;
2522

26-
@Inject(optional=true)
2723
private transient LoadStep step;
2824

29-
@StepContextParameter
30-
private transient TaskListener listener;
25+
LoadStepExecution(LoadStep step, StepContext context) {
26+
super(context);
27+
this.step = step;
28+
}
3129

3230
@Override
3331
public boolean start() throws Exception {
3432
CpsStepContext cps = (CpsStepContext) getContext();
33+
FilePath cwd = cps.get(FilePath.class);
34+
TaskListener listener = cps.get(TaskListener.class);
3535
CpsThread t = CpsThread.current();
3636

3737
CpsFlowExecution execution = t.getExecution();
@@ -62,6 +62,11 @@ public boolean start() throws Exception {
6262
return false;
6363
}
6464

65+
@Override
66+
public void onResume() {
67+
// do nothing
68+
}
69+
6570
private static final long serialVersionUID = 1L;
6671

6772
}

plugin/src/test/java/org/jenkinsci/plugins/workflow/cps/ContextVariableSetTest.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
4040
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
4141
import org.jenkinsci.plugins.workflow.log.TaskListenerDecorator;
42-
import org.jenkinsci.plugins.workflow.steps.BodyInvoker;
4342
import org.jenkinsci.plugins.workflow.steps.DynamicContext;
4443
import org.jenkinsci.plugins.workflow.steps.Step;
4544
import org.jenkinsci.plugins.workflow.steps.StepContext;
@@ -155,21 +154,20 @@ private static final class DecoratorImpl extends TaskListenerDecorator {
155154
}
156155
}
157156
private static final class YesPleaseDecorate implements Serializable {}
158-
public static final class DecoratorStep extends Step implements Serializable {
157+
public static final class DecoratorStep extends Step {
159158
@DataBoundConstructor public DecoratorStep() {}
160159
@DataBoundSetter public @CheckForNull String message;
161160
@Override public StepExecution start(StepContext context) throws Exception {
162-
return StepExecutions.block(context, this::start);
163-
}
164-
private void start(StepContext context, BodyInvoker invoker) throws Exception {
165-
if (message != null) {
166-
TaskListenerDecorator original = context.get(TaskListenerDecorator.class);
167-
DecoratorImpl subsequent = new DecoratorImpl(message);
168-
LOGGER.log(Level.INFO, "merging {0} with {1}", new Object[] {original, subsequent});
169-
invoker.withContext(TaskListenerDecorator.merge(original, subsequent));
170-
} else {
171-
invoker.withContext(new YesPleaseDecorate());
172-
}
161+
return StepExecutions.block(context, (c, invoker) -> {
162+
if (message != null) {
163+
TaskListenerDecorator original = c.get(TaskListenerDecorator.class);
164+
DecoratorImpl subsequent = new DecoratorImpl(message);
165+
LOGGER.log(Level.INFO, "merging {0} with {1}", new Object[] {original, subsequent});
166+
invoker.withContext(TaskListenerDecorator.merge(original, subsequent));
167+
} else {
168+
invoker.withContext(new YesPleaseDecorate());
169+
}
170+
});
173171
}
174172
@TestExtension("smokes") public static final class DescriptorImpl extends StepDescriptor {
175173
@Override public String getFunctionName() {
@@ -213,11 +211,10 @@ private static final class Message implements Serializable {
213211
public static final class GetMessageStep extends Step {
214212
@DataBoundConstructor public GetMessageStep() {}
215213
@Override public StepExecution start(StepContext context) throws Exception {
216-
return StepExecutions.synchronous(context, GetMessageStep::run);
217-
}
218-
private static Object run(StepContext context) throws Exception {
219-
Message message = context.get(Message.class);
220-
return message != null ? message.text : null;
214+
return StepExecutions.synchronous(context, c -> {
215+
Message message = c.get(Message.class);
216+
return message != null ? message.text : null;
217+
});
221218
}
222219
@TestExtension("dynamicVsStatic") public static final class DescriptorImpl extends StepDescriptor {
223220
@Override public String getFunctionName() {
@@ -228,7 +225,7 @@ private static Object run(StepContext context) throws Exception {
228225
}
229226
}
230227
}
231-
public static final class WithStaticMessageStep extends Step implements Serializable {
228+
public static final class WithStaticMessageStep extends Step {
232229
public final String text;
233230
@DataBoundConstructor public WithStaticMessageStep(String text) {
234231
this.text = text;
@@ -266,7 +263,7 @@ private static final class DynamicMessage implements Serializable {
266263
return dynamicMessage != null ? new Message(dynamicMessage.text) : null;
267264
}
268265
}
269-
public static final class WithDynamicMessageStep extends Step implements Serializable {
266+
public static final class WithDynamicMessageStep extends Step {
270267
public final String text;
271268
@DataBoundConstructor public WithDynamicMessageStep(String text) {
272269
this.text = text;

plugin/src/test/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition2Test.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import hudson.model.Describable;
3131
import hudson.model.Executor;
3232
import hudson.model.Result;
33-
import java.io.Serializable;
3433
import java.util.Collections;
3534
import java.util.Set;
3635
import java.util.logging.Level;
@@ -927,17 +926,14 @@ public void scriptInitializerCallsCpsTransformedMethod() throws Exception {
927926
jenkins.assertLogContains("Scripts not permitted to use field Test metaClass", b);
928927
}
929928

930-
public static class UnsafeParameterStep extends Step implements Serializable {
929+
public static class UnsafeParameterStep extends Step {
931930
private final UnsafeDescribable val;
932931
@DataBoundConstructor
933932
public UnsafeParameterStep(UnsafeDescribable val) {
934933
this.val = val;
935934
}
936-
public StepExecution start(StepContext context) throws Exception {
937-
return StepExecutions.synchronousNonBlocking(context, c -> {
938-
val.doSomething();
939-
return null;
940-
});
935+
@Override public StepExecution start(StepContext context) throws Exception {
936+
return StepExecutions.synchronousNonBlockingVoid(context, c -> val.doSomething());
941937
}
942938
@TestExtension
943939
public static class DescriptorImpl extends StepDescriptor {

plugin/src/test/java/org/jenkinsci/plugins/workflow/cps/actions/ArgumentsActionImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ public static class NopStep extends Step {
700700
public NopStep(Object value) {}
701701
@Override
702702
public StepExecution start(StepContext context) throws Exception {
703-
return StepExecutions.synchronous(context, unused -> null);
703+
return StepExecutions.synchronousVoid(context, c -> {});
704704
}
705705
@TestExtension
706706
public static class DescriptorImpl extends StepDescriptor {

0 commit comments

Comments
 (0)