From e22b7274a0881f5bdbae02f8eb94b4da6df8069c Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Tue, 16 Jan 2024 17:00:47 -0500 Subject: [PATCH] Fix deprecations, avoid `@Inject` --- .../plugins/workflow/libs/LibraryStep.java | 45 +++++++++++-------- .../plugins/workflow/libs/ResourceStep.java | 33 +++++++++----- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java b/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java index 82e9d957..2169429a 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java @@ -60,7 +60,6 @@ import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import groovy.lang.MissingPropertyException; -import jakarta.inject.Inject; import jenkins.model.Jenkins; import jenkins.scm.impl.SingleSCMSource; import org.codehaus.groovy.control.MultipleCompilationErrorsException; @@ -70,10 +69,11 @@ import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution; import org.jenkinsci.plugins.workflow.cps.CpsThread; import org.jenkinsci.plugins.workflow.flow.FlowExecution; -import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl; -import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl; -import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution; -import org.jenkinsci.plugins.workflow.steps.StepContextParameter; +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.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.DoNotUse; import org.kohsuke.groovy.sandbox.GroovyInterceptor; @@ -86,7 +86,7 @@ /** * Dynamically injects a library into the running build. */ -public class LibraryStep extends AbstractStepImpl { +public class LibraryStep extends Step { private static final Logger LOGGER = Logger.getLogger(LibraryStep.class.getName()); @@ -118,11 +118,11 @@ public Boolean getChangelog() { this.changelog = changelog; } - @Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl { + @Override public StepExecution start(StepContext context) throws Exception { + return new Execution(this, context); + } - public DescriptorImpl() { - super(Execution.class); - } + @Extension public static class DescriptorImpl extends StepDescriptor { @Override public String getFunctionName() { return "library"; @@ -132,6 +132,10 @@ public DescriptorImpl() { return "Load a library on the fly"; } + @Override public Set> getRequiredContext() { + return Set.of(Run.class, TaskListener.class, FlowExecution.class); + } + @Restricted(DoNotUse.class) // Jelly public Collection getRetrieverDescriptors() { return Jenkins.get().getDescriptorByType(LibraryConfiguration.DescriptorImpl.class).getRetrieverDescriptors(); @@ -157,15 +161,20 @@ public AutoCompletionCandidates doAutoCompleteIdentifier(@AncestorInPath ItemGro } - public static class Execution extends AbstractSynchronousNonBlockingStepExecution { + public static class Execution extends SynchronousNonBlockingStepExecution { private static final long serialVersionUID = 1L; - @Inject private transient LibraryStep step; - @StepContextParameter private transient Run run; - @StepContextParameter private transient TaskListener listener; + private transient final LibraryStep step; + + Execution(LibraryStep step, StepContext context) { + super(context); + this.step = step; + } @Override protected LoadedClasses run() throws Exception { + Run run = getContext().get(Run.class); + TaskListener listener = getContext().get(TaskListener.class); String[] parsed = LibraryAdder.parse(step.identifier); String name = parsed[0], version = parsed[1]; boolean trusted = false; @@ -204,9 +213,9 @@ public static class Execution extends AbstractSynchronousNonBlockingStepExecutio // uses the library step with a non-null retriever to check out a static version of the library. // Fixing this would require us being able to detect usage of SCMVar precisely, which is not currently possible. else if (retriever instanceof SCMRetriever) { - verifyRevision(((SCMRetriever) retriever).getScm(), name); + verifyRevision(((SCMRetriever) retriever).getScm(), name, run, listener); } else if (retriever instanceof SCMSourceRetriever && ((SCMSourceRetriever) retriever).getScm() instanceof SingleSCMSource) { - verifyRevision(((SingleSCMSource) ((SCMSourceRetriever) retriever).getScm()).getScm(), name); + verifyRevision(((SingleSCMSource) ((SCMSourceRetriever) retriever).getScm()).getScm(), name, run, listener); } LibraryRecord record = new LibraryRecord(name, version, trusted, changelog, cachingConfiguration, source); @@ -236,9 +245,9 @@ else if (retriever instanceof SCMRetriever) { return new LoadedClasses(name, record.getDirectoryName(), trusted, changelog, run); } - private void verifyRevision(SCM scm, String name) throws IOException, InterruptedException { + private void verifyRevision(SCM scm, String name, Run run, TaskListener listener) throws IOException, InterruptedException { for (LibraryStepRetrieverVerifier revisionVerifier : LibraryStepRetrieverVerifier.all()) { - revisionVerifier.verify(this.run, listener, scm, name); + revisionVerifier.verify(run, listener, scm, name); } } diff --git a/src/main/java/org/jenkinsci/plugins/workflow/libs/ResourceStep.java b/src/main/java/org/jenkinsci/plugins/workflow/libs/ResourceStep.java index 3adec256..2f49b229 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/libs/ResourceStep.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/libs/ResourceStep.java @@ -29,19 +29,21 @@ import hudson.Util; import java.util.Map; import edu.umd.cs.findbugs.annotations.CheckForNull; -import jakarta.inject.Inject; +import java.util.Set; import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution; import org.jenkinsci.plugins.workflow.flow.FlowExecution; -import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl; -import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl; -import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousStepExecution; +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.jenkinsci.plugins.workflow.steps.SynchronousStepExecution; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; /** * Step to load a resource from a library. */ -public class ResourceStep extends AbstractStepImpl { +public class ResourceStep extends Step { private final String resource; private String encoding; @@ -67,11 +69,11 @@ public String getResource() { this.encoding = Util.fixEmptyAndTrim(encoding); } - @Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl { + @Override public StepExecution start(StepContext context) throws Exception { + return new Execution(this, context); + } - public DescriptorImpl() { - super(Execution.class); - } + @Extension public static class DescriptorImpl extends StepDescriptor { @Override public String getDisplayName() { return "Load a resource file from a library"; @@ -81,13 +83,22 @@ public DescriptorImpl() { return "libraryResource"; } + @Override public Set> getRequiredContext() { + return Set.of(FlowExecution.class); + } + } - public static class Execution extends AbstractSynchronousStepExecution { + public static class Execution extends SynchronousStepExecution { private static final long serialVersionUID = 1L; - @Inject private transient ResourceStep step; + private transient final ResourceStep step; + + public Execution(ResourceStep step, StepContext context) { + super(context); + this.step = step; + } @Override protected String run() throws Exception { String resource = step.resource;