Skip to content

Commit

Permalink
Merge pull request #103 from jglick/lethal-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 0e341fa + e22b727 commit 0c6467c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
45 changes: 27 additions & 18 deletions src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());

Expand Down Expand Up @@ -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";
Expand All @@ -132,6 +132,10 @@ public DescriptorImpl() {
return "Load a library on the fly";
}

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

@Restricted(DoNotUse.class) // Jelly
public Collection<LibraryRetrieverDescriptor> getRetrieverDescriptors() {
return Jenkins.get().getDescriptorByType(LibraryConfiguration.DescriptorImpl.class).getRetrieverDescriptors();
Expand All @@ -157,15 +161,20 @@ public AutoCompletionCandidates doAutoCompleteIdentifier(@AncestorInPath ItemGro

}

public static class Execution extends AbstractSynchronousNonBlockingStepExecution<LoadedClasses> {
public static class Execution extends SynchronousNonBlockingStepExecution<LoadedClasses> {

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

Expand Down
33 changes: 22 additions & 11 deletions src/main/java/org/jenkinsci/plugins/workflow/libs/ResourceStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -81,13 +83,22 @@ public DescriptorImpl() {
return "libraryResource";
}

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

}

public static class Execution extends AbstractSynchronousStepExecution<String> {
public static class Execution extends SynchronousStepExecution<String> {

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;
Expand Down

0 comments on commit 0c6467c

Please sign in to comment.