/
EmailExtRecipientStep.java
109 lines (91 loc) · 3.86 KB
/
EmailExtRecipientStep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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.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 edu.umd.cs.findbugs.annotations.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 Step {
private List<RecipientProvider> recipientProviders;
@DataBoundConstructor
public EmailExtRecipientStep(List<RecipientProvider> recipientProviders) {
this.recipientProviders = recipientProviders;
}
public List<RecipientProvider> getRecipientProviders() {
return recipientProviders;
}
@Override
public StepExecution start(StepContext context) throws Exception {
return new Executor(this, context);
}
public static class Executor extends SynchronousNonBlockingStepExecution<String> {
private static final long serialVersionUID = 1L;
private final transient EmailExtRecipientStep step;
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,
getContext().get(Run.class),
null,
null,
getContext().get(TaskListener.class));
Set<InternetAddress> to = new HashSet<>();
RecipientProvider.checkAllSupport(
step.recipientProviders, getContext().get(Run.class).getParent().getClass());
for (RecipientProvider provider : step.recipientProviders) {
provider.addRecipients(context, getContext().get(EnvVars.class), to, to, to);
}
StringBuilder rt = new StringBuilder();
Iterator<InternetAddress> iterator = to.iterator();
while (iterator.hasNext()) {
rt.append(iterator.next().toString());
if (iterator.hasNext()) {
rt.append(" ");
}
}
return rt.toString();
}
}
@Extension(optional = true)
public static final class DescriptorImpl extends StepDescriptor {
@Override
public Set<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(Run.class, TaskListener.class, EnvVars.class);
}
@Override
public String getFunctionName() {
return "emailextrecipients";
}
@Override
public String getDisplayName() {
return "Extended Email Recipients";
}
@SuppressWarnings("unused")
public List<RecipientProviderDescriptor> getRecipientProvidersDescriptors() {
return RecipientProvider.allSupporting(WorkflowJob.class);
}
}
}