diff --git a/src/main/java/hudson/plugins/emailext/EmailExtRecipientStep.java b/src/main/java/hudson/plugins/emailext/EmailExtRecipientStep.java index 460f7993b..e74949850 100644 --- a/src/main/java/hudson/plugins/emailext/EmailExtRecipientStep.java +++ b/src/main/java/hudson/plugins/emailext/EmailExtRecipientStep.java @@ -1,5 +1,6 @@ package hudson.plugins.emailext; +import com.google.common.collect.ImmutableSet; import hudson.EnvVars; import hudson.Extension; import hudson.model.Run; @@ -7,20 +8,21 @@ import hudson.plugins.emailext.plugins.RecipientProvider; import hudson.plugins.emailext.plugins.RecipientProviderDescriptor; import org.jenkinsci.plugins.workflow.job.WorkflowJob; -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.stapler.DataBoundConstructor; -import javax.inject.Inject; +import javax.annotation.Nonnull; import javax.mail.internet.InternetAddress; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; -public class EmailExtRecipientStep extends AbstractStepImpl { +public class EmailExtRecipientStep extends Step { private List recipientProviders; @DataBoundConstructor @@ -32,21 +34,20 @@ public List getRecipientProviders() { return recipientProviders; } - @SuppressWarnings("WeakerAccess") - public static class Executor extends AbstractSynchronousNonBlockingStepExecution { - private static final long serialVersionUID = 1L; - - @Inject - private transient EmailExtRecipientStep step; + @Override + public StepExecution start(StepContext context) throws Exception { + return new Executor(this, context); + } - @StepContextParameter - private transient Run run; + public static class Executor extends SynchronousNonBlockingStepExecution { + private static final long serialVersionUID = 1L; - @StepContextParameter - private transient TaskListener listener; + private final transient EmailExtRecipientStep step; - @StepContextParameter - private transient EnvVars env; + protected Executor(EmailExtRecipientStep step, @Nonnull StepContext context) { + super(context); + this.step = step; + } @Override protected String run() throws Exception { @@ -54,11 +55,18 @@ protected String run() throws Exception { throw new IllegalArgumentException("You must provide at least one recipient provider"); } ExtendedEmailPublisher publisher = new ExtendedEmailPublisher(); - ExtendedEmailPublisherContext context = new ExtendedEmailPublisherContext(publisher, run, null, null, listener); + ExtendedEmailPublisherContext context = + new ExtendedEmailPublisherContext( + publisher, + getContext().get(Run.class), + null, + null, + getContext().get(TaskListener.class)); Set to = new HashSet<>(); - RecipientProvider.checkAllSupport(step.recipientProviders, run.getParent().getClass()); + RecipientProvider.checkAllSupport( + step.recipientProviders, getContext().get(Run.class).getParent().getClass()); for (RecipientProvider provider : step.recipientProviders) { - provider.addRecipients(context, env, to, to, to); + provider.addRecipients(context, getContext().get(EnvVars.class), to, to, to); } StringBuilder rt = new StringBuilder(); @@ -76,9 +84,11 @@ protected String run() throws Exception { } @Extension(optional = true) - public static final class DescriptorImpl extends AbstractStepDescriptorImpl { - public DescriptorImpl() { - super(Executor.class); + public static final class DescriptorImpl extends StepDescriptor { + + @Override + public Set> getRequiredContext() { + return ImmutableSet.of(Run.class, TaskListener.class, EnvVars.class); } @Override diff --git a/src/main/java/hudson/plugins/emailext/EmailExtStep.java b/src/main/java/hudson/plugins/emailext/EmailExtStep.java index cd7fb531d..52ec035b4 100644 --- a/src/main/java/hudson/plugins/emailext/EmailExtStep.java +++ b/src/main/java/hudson/plugins/emailext/EmailExtStep.java @@ -1,8 +1,8 @@ package hudson.plugins.emailext; import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; -import com.google.inject.Inject; import hudson.Extension; import hudson.FilePath; import hudson.Launcher; @@ -17,21 +17,24 @@ import jenkins.model.Jenkins; import org.apache.commons.lang.StringUtils; import org.jenkinsci.plugins.workflow.job.WorkflowJob; -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.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import java.util.List; +import java.util.Set; /** * Created by acearl on 9/14/2015. */ -public class EmailExtStep extends AbstractStepImpl { +public class EmailExtStep extends Step { public final String subject; @@ -171,18 +174,21 @@ public void setSaveOutput(boolean saveOutput) { this.saveOutput = saveOutput; } - public static class EmailExtStepExecution extends AbstractSynchronousNonBlockingStepExecution { + @Override + public StepExecution start(StepContext context) throws Exception { + return new EmailExtStepExecution(this, context); + } - private static final long serialVersionUID = 1L; + public static class EmailExtStepExecution extends SynchronousNonBlockingStepExecution { - @Inject - private transient EmailExtStep step; + private static final long serialVersionUID = 1L; - @StepContextParameter - private transient TaskListener listener; + private final transient EmailExtStep step; - @StepContextParameter - private transient Run run; + protected EmailExtStepExecution(EmailExtStep step, @Nonnull StepContext context) { + super(context); + this.step = step; + } @Override protected Void run() throws Exception { @@ -194,7 +200,9 @@ protected Void run() throws Exception { trigger.getEmail().getRecipientProviders().clear(); trigger.getEmail().addRecipientProvider(new ListRecipientProvider()); if (step.recipientProviders != null) { - RecipientProvider.checkAllSupport(step.recipientProviders, run.getParent().getClass()); + RecipientProvider.checkAllSupport( + step.recipientProviders, + getContext().get(Run.class).getParent().getClass()); trigger.getEmail().addRecipientProviders(step.recipientProviders); } publisher.configuredTriggers.add(trigger); @@ -227,8 +235,13 @@ protected Void run() throws Exception { publisher.contentType = step.mimeType; } - final ExtendedEmailPublisherContext ctx = new ExtendedEmailPublisherContext(publisher, run, - getContext().get(FilePath.class), getContext().get(Launcher.class), listener); + final ExtendedEmailPublisherContext ctx = + new ExtendedEmailPublisherContext( + publisher, + getContext().get(Run.class), + getContext().get(FilePath.class), + getContext().get(Launcher.class), + getContext().get(TaskListener.class)); final Multimap triggered = ArrayListMultimap.create(); triggered.put(AlwaysTrigger.TRIGGER_NAME, publisher.configuredTriggers.get(0)); ctx.setTrigger(publisher.configuredTriggers.get(0)); @@ -240,12 +253,13 @@ protected Void run() throws Exception { @Extension(optional=true) - public static final class DescriptorImpl extends AbstractStepDescriptorImpl { + public static final class DescriptorImpl extends StepDescriptor { public static final String defaultMimeType = "text/plain"; - public DescriptorImpl() { - super(EmailExtStepExecution.class); + @Override + public Set> getRequiredContext() { + return ImmutableSet.of(Run.class, TaskListener.class); } @Override diff --git a/src/test/java/hudson/plugins/emailext/EmailExtStepTest.java b/src/test/java/hudson/plugins/emailext/EmailExtStepTest.java index 6b046d815..028787c26 100644 --- a/src/test/java/hudson/plugins/emailext/EmailExtStepTest.java +++ b/src/test/java/hudson/plugins/emailext/EmailExtStepTest.java @@ -1,15 +1,17 @@ package hudson.plugins.emailext; +import com.google.common.collect.ImmutableSet; import hudson.FilePath; import hudson.model.Run; import org.apache.commons.lang.StringEscapeUtils; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; -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.Step; import org.jenkinsci.plugins.workflow.steps.StepConfigTester; -import org.jenkinsci.plugins.workflow.steps.StepContextParameter; +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.junit.After; import org.junit.Rule; import org.junit.Test; @@ -18,12 +20,14 @@ import org.jvnet.mock_javamail.Mailbox; import org.kohsuke.stapler.DataBoundConstructor; +import javax.annotation.Nonnull; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.io.File; import java.net.URL; +import java.util.Set; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -130,7 +134,7 @@ public void saveOutput() throws Exception { j.assertLogContains("Archiving artifacts", run); } - public static class FileCopyStep extends AbstractStepImpl { + public static class FileCopyStep extends Step { private final String file; @@ -143,11 +147,17 @@ public String getFile() { return file; } + @Override + public StepExecution start(StepContext context) throws Exception { + return new Execution(this, context); + } + @TestExtension("attachment") - public static class DescriptorImpl extends AbstractStepDescriptorImpl { + public static class DescriptorImpl extends StepDescriptor { - public DescriptorImpl() { - super(Execution.class); + @Override + public Set> getRequiredContext() { + return ImmutableSet.of(FilePath.class); } @Override public String getFunctionName() { @@ -160,16 +170,18 @@ public DescriptorImpl() { } - public static class Execution extends AbstractSynchronousNonBlockingStepExecution { + public static class Execution extends SynchronousNonBlockingStepExecution { - @javax.inject.Inject - private transient FileCopyStep step; - @StepContextParameter - private transient FilePath workspace; + private final transient FileCopyStep step; + + protected Execution(FileCopyStep step, @Nonnull StepContext context) { + super(context); + this.step = step; + } @Override protected Boolean run() throws Exception { FilePath file = new FilePath(new File(step.file)); - workspace.child(file.getName()).copyFrom(file); + getContext().get(FilePath.class).child(file.getName()).copyFrom(file); return true; }