/
ExtEmailSlicer.java
128 lines (115 loc) · 3.97 KB
/
ExtEmailSlicer.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package configurationslicing.email;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.plugins.emailext.EmailType;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.trigger.FailureTrigger;
import hudson.tasks.Publisher;
import hudson.util.DescribableList;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import configurationslicing.UnorderedStringSlicer;
@Extension
public class ExtEmailSlicer extends UnorderedStringSlicer<AbstractProject<?, ?>> {
public ExtEmailSlicer() {
super(new ExtEmailSliceSpec());
}
public boolean isLoaded() {
try {
new EmailType();
return true;
} catch (Throwable t) {
return false;
}
}
@SuppressWarnings("unchecked")
public static class ExtEmailSliceSpec extends AbstractEmailSliceSpec implements ProjectHandler {
public ExtEmailSliceSpec() {
super(",", "Editable Email Notification", "emailext");
}
@Override
protected ProjectHandler getProjectHandler(AbstractProject<?,?> project) {
return this;
}
@Override
public List<String> getCommonValueStrings() {
List<String> values = new ArrayList<String>();
values.add("$DEFAULT_RECIPIENTS");
return values;
}
public String getRecipients(AbstractProject<?,?> project) {
ExtendedEmailPublisher mailer = getMailer(project);
if (mailer != null) {
return mailer.recipientList;
} else {
return null;
}
}
private ExtendedEmailPublisher getMailer(AbstractProject<?,?> project) {
DescribableList<Publisher,Descriptor<Publisher>> publishers = project.getPublishersList();
Descriptor<Publisher> descriptor = Hudson.getInstance().getDescriptor(ExtendedEmailPublisher.class);
Publisher emailPublisher = publishers.get(descriptor);
return (ExtendedEmailPublisher) emailPublisher;
}
public boolean setRecipients(AbstractProject<?,?> project, String value) {
ExtendedEmailPublisher mailer = getMailer(project);
if (!StringUtils.equals(value, mailer.recipientList)) {
mailer.recipientList = value;
return true;
} else {
return false;
}
}
public boolean addMailer(AbstractProject<?,?> project) throws IOException {
ExtendedEmailPublisher mailer = getMailer(project);
if (mailer == null) {
DescribableList<Publisher,Descriptor<Publisher>> publishers = project.getPublishersList();
ExtendedEmailPublisher publisher = new ExtendedEmailPublisher();
FailureTrigger trigger = FailureTrigger.createDefault();
EmailType email = new EmailType();
email.setSendToDevelopers(true);
email.setSendToRecipientList(true);
trigger.setEmail(email);
publisher.getConfiguredTriggers().add(trigger);
// there is no way to get this text from the plugin itself
publisher.defaultContent = "$DEFAULT_CONTENT";
publisher.defaultSubject = "$DEFAULT_SUBJECT";
publishers.add(publisher);
return true;
} else {
return false;
}
}
public boolean removeMailer(AbstractProject<?,?> project) throws IOException {
ExtendedEmailPublisher mailer = getMailer(project);
if (mailer != null) {
DescribableList<Publisher,Descriptor<Publisher>> publishers = project.getPublishersList();
publishers.remove(mailer);
return true;
} else {
return false;
}
}
/**
* not yet implemented for ExtendedEmailPublisher
*/
public boolean sendToIndividuals(AbstractProject<?,?> project) {
boolean result = false;
ExtendedEmailPublisher mailer = getMailer(project);
if (mailer != null) {
for (EmailTrigger trigger : mailer.getConfiguredTriggers()) {
if (trigger.getEmail().getSendToDevelopers()) {
result = true;
break;
}
}
}
return result;
}
}
}