forked from jlewallen/jenkins-hipchat-plugin
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathHipChatSendStep.java
223 lines (186 loc) · 8.41 KB
/
HipChatSendStep.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package jenkins.plugins.hipchat.workflow;
import hudson.AbortException;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.model.Item;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import java.io.IOException;
import jenkins.model.Jenkins;
import jenkins.plugins.hipchat.CardProvider;
import jenkins.plugins.hipchat.HipChatNotifier;
import jenkins.plugins.hipchat.HipChatService;
import jenkins.plugins.hipchat.Messages;
import jenkins.plugins.hipchat.exceptions.NotificationException;
import jenkins.plugins.hipchat.impl.NoopCardProvider;
import jenkins.plugins.hipchat.model.notifications.Icon;
import jenkins.plugins.hipchat.model.notifications.Notification;
import jenkins.plugins.hipchat.model.notifications.Notification.Color;
import jenkins.plugins.hipchat.model.notifications.Notification.MessageFormat;
import jenkins.plugins.hipchat.utils.BuildUtils;
import jenkins.plugins.hipchat.utils.CredentialUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
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.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
/**
* Workflow step to send a HipChat room notification.
*/
public class HipChatSendStep extends AbstractStepImpl {
private static final Logger logger = Logger.getLogger(HipChatSendStep.class.getName());
public final String message;
@DataBoundSetter
public Color color;
@DataBoundSetter
public String icon;
/**
* @deprecated Use {@link #credentialId instead}.
*/
@Deprecated
@DataBoundSetter
public String token;
@DataBoundSetter
public String credentialId;
@DataBoundSetter
public String room;
@DataBoundSetter
public String server;
@DataBoundSetter
public boolean notify;
@DataBoundSetter
public boolean textFormat;
@DataBoundSetter
public Boolean v2enabled;
@DataBoundSetter
public String sendAs;
@DataBoundSetter
public boolean failOnError;
@DataBoundConstructor
public HipChatSendStep(@Nonnull String message) {
this.message = message;
}
@Extension
public static class DescriptorImpl extends AbstractStepDescriptorImpl {
public DescriptorImpl() {
super(HipChatSendStepExecution.class);
}
public ListBoxModel doFillCredentialIdItems(@AncestorInPath Item context, @QueryParameter String server) {
return Jenkins.getInstance().getDescriptorByType(HipChatNotifier.DescriptorImpl.class)
.doFillCredentialIdItems(context, server);
}
@Override
public String getFunctionName() {
return "hipchatSend";
}
@Override
public String getDisplayName() {
return Messages.HipChatSendStepDisplayName();
}
}
public static class HipChatSendStepExecution extends AbstractSynchronousNonBlockingStepExecution<Void> {
private static final long serialVersionUID = 1L;
@Inject
private transient BuildUtils buildUtils;
@Inject
private transient CredentialUtils credentialUtils;
@Inject
private transient HipChatSendStep step;
@StepContextParameter
private transient TaskListener listener;
@StepContextParameter
private transient Run<?, ?> run;
@Override
protected Void run() throws Exception {
if (StringUtils.isBlank(step.message)) {
//allow entire run to fail based on failOnError field
if (step.failOnError) {
throw new AbortException(Messages.MessageRequiredError());
} else {
listener.error(Messages.MessageRequiredError());
}
return null;
}
//default to global config values if not set in step, but allow step to override all global settings
HipChatNotifier.DescriptorImpl hipChatDesc =
Jenkins.getInstance().getDescriptorByType(HipChatNotifier.DescriptorImpl.class);
String room = firstNonEmpty(step.room, hipChatDesc.getRoom());
String server = firstNonEmpty(step.server, hipChatDesc.getServer());
String sendAs = firstNonEmpty(step.sendAs, hipChatDesc.getSendAs());
String credentialId = step.credentialId;
String token = null;
if (StringUtils.isEmpty(credentialId)) {
if (StringUtils.isEmpty(step.token)) {
credentialId = hipChatDesc.getCredentialId();
} else {
token = step.token;
}
}
if (StringUtils.isNotEmpty(credentialId)) {
StringCredentials creds = credentialUtils.resolveCredential(run.getParent(), credentialId, server);
if (creds != null) {
token = Secret.toString(creds.getSecret());
}
}
//default to gray if not set in step
Color color = step.color != null ? step.color : Color.GRAY;
boolean v2enabled = step.v2enabled != null ? step.v2enabled : hipChatDesc.isV2Enabled();
HipChatService hipChatService = HipChatNotifier.getHipChatService(server, token, v2enabled, room, sendAs);
logger.log(Level.FINER, "HipChat publish settings: api v2 - {0} server - {1} token - {2} room - {3}",
new Object[]{v2enabled, server, token, room});
//attempt to publish message, log NotificationException, will allow run to continue
try {
FilePath workspace = null;
try {
workspace = getContext().get(FilePath.class);
} catch (IOException | InterruptedException ex) {
//workspace is not always available, ignore these exceptions
}
String message = TokenMacro.expandAll(run, workspace, listener,
HipChatNotifier.migrateMessageTemplate(step.message), false, null);
CardProvider cardProvider = ExtensionList.lookup(CardProvider.class).getDynamic(Jenkins.getInstance()
.getDescriptorByType(jenkins.plugins.hipchat.HipChatNotifier.DescriptorImpl.class)
.getCardProvider());
if (cardProvider == null) {
cardProvider = new NoopCardProvider();
}
hipChatService.publish(new Notification()
.withColor(color)
.withMessage(message)
.withCard(cardProvider.getCard(run, listener,
StringUtils.isEmpty(step.icon) ? null : new Icon().withUrl(step.icon), message))
.withNotify(step.notify)
.withMessageFormat(step.textFormat ? MessageFormat.TEXT : MessageFormat.HTML));
listener.getLogger().println(Messages.NotificationSuccessful(room));
} catch (MacroEvaluationException | IOException | NotificationException ex) {
listener.getLogger().println(Messages.NotificationFailed(ex.getMessage()));
//allow entire run to fail based on failOnError field
if (step.failOnError) {
throw new AbortException(Messages.NotificationFailed(ex.getMessage()));
} else {
listener.error(Messages.NotificationFailed(ex.getMessage()));
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
return null;
}
private String firstNonEmpty(String value, String defaultValue) {
return StringUtils.isNotEmpty(value) ? value : defaultValue;
}
}
}