Skip to content

Commit

Permalink
Showing 3 changed files with 94 additions and 58 deletions.
58 changes: 34 additions & 24 deletions src/main/java/hudson/plugins/emailext/EmailExtRecipientStep.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package hudson.plugins.emailext;

import com.google.common.collect.ImmutableSet;
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
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<RecipientProvider> recipientProviders;

@DataBoundConstructor
@@ -32,33 +34,39 @@ public List<RecipientProvider> getRecipientProviders() {
return recipientProviders;
}

@SuppressWarnings("WeakerAccess")
public static class Executor extends AbstractSynchronousNonBlockingStepExecution<String> {
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<String> {
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 {
if (step.recipientProviders == null || step.recipientProviders.isEmpty()) {
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<InternetAddress> 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<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(Run.class, TaskListener.class, EnvVars.class);
}

@Override
54 changes: 34 additions & 20 deletions src/main/java/hudson/plugins/emailext/EmailExtStep.java
Original file line number Diff line number Diff line change
@@ -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<Void> {
@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<Void> {

@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<String, EmailTrigger> 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<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(Run.class, TaskListener.class);
}

@Override
40 changes: 26 additions & 14 deletions src/test/java/hudson/plugins/emailext/EmailExtStepTest.java
Original file line number Diff line number Diff line change
@@ -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<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(FilePath.class);
}

@Override public String getFunctionName() {
@@ -160,16 +170,18 @@ public DescriptorImpl() {

}

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

@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;
}

0 comments on commit d26a492

Please sign in to comment.