-
Notifications
You must be signed in to change notification settings - Fork 411
/
Copy pathSlackNotifier.java
executable file
·674 lines (572 loc) · 23.4 KB
/
SlackNotifier.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
package jenkins.plugins.slack;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.domains.HostnameRequirement;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Item;
import hudson.model.Descriptor;
import hudson.model.listeners.ItemListener;
import hudson.model.Result;
import hudson.security.ACL;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import jenkins.plugins.slack.config.ItemConfigMigrator;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Logger;
import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials;
public class SlackNotifier extends Notifier {
private static final Logger logger = Logger.getLogger(SlackNotifier.class.getName());
private String teamDomain;
private String authToken;
private String authTokenCredentialId;
private boolean botUser;
private String room;
private String sendAs;
private boolean startNotification;
private boolean notifySuccess;
private boolean notifyAborted;
private boolean notifyNotBuilt;
private boolean notifyUnstable;
private boolean notifyFailure;
private boolean notifyBackToNormal;
private boolean notifyRepeatedFailure;
private boolean includeTestSummary;
private boolean includeFailedTests;
private CommitInfoChoice commitInfoChoice;
private boolean includeCustomMessage;
private String customMessage;
private Map<String, String> customMessageMap = new HashMap<String, String>();
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}
public String getTeamDomain() {
return teamDomain;
}
@DataBoundSetter
public void setTeamDomain(final String teamDomain) {
this.teamDomain = teamDomain;
}
public String getRoom() {
return room;
}
@DataBoundSetter
public void setRoom(String room) {
this.room = room;
}
public String getAuthToken() {
return authToken;
}
@DataBoundSetter
public void setAuthToken(String authToken) {
this.authToken = authToken;
}
public String getAuthTokenCredentialId() {
return authTokenCredentialId;
}
@DataBoundSetter
public void setAuthTokenCredentialId(String authTokenCredentialId) {
this.authTokenCredentialId = authTokenCredentialId;
}
public boolean getBotUser() {
return botUser;
}
@DataBoundSetter
public void setBotUser(boolean botUser) {
this.botUser = botUser;
}
public String getSendAs() {
return sendAs;
}
@DataBoundSetter
public void setSendAs(String sendAs) {
this.sendAs = sendAs;
}
public boolean getStartNotification() {
return startNotification;
}
public boolean getNotifySuccess() {
return notifySuccess;
}
public CommitInfoChoice getCommitInfoChoice() {
return commitInfoChoice;
}
public boolean getNotifyAborted() {
return notifyAborted;
}
public boolean getNotifyFailure() {
return notifyFailure;
}
public boolean getNotifyNotBuilt() {
return notifyNotBuilt;
}
public boolean getNotifyUnstable() {
return notifyUnstable;
}
public boolean getNotifyBackToNormal() {
return notifyBackToNormal;
}
public boolean includeTestSummary() {
return includeTestSummary;
}
public boolean includeFailedTests() {
return includeFailedTests;
}
public boolean getNotifyRepeatedFailure() {
return notifyRepeatedFailure;
}
public boolean includeCustomMessage() {
return includeCustomMessage;
}
public String getCustomMessage() {
return customMessage;
}
@DataBoundSetter
public void setStartNotification(boolean startNotification) {
this.startNotification = startNotification;
}
@DataBoundSetter
public void setNotifySuccess(boolean notifySuccess) {
this.notifySuccess = notifySuccess;
}
@DataBoundSetter
public void setCommitInfoChoice(CommitInfoChoice commitInfoChoice) {
this.commitInfoChoice = commitInfoChoice;
}
@DataBoundSetter
public void setNotifyAborted(boolean notifyAborted) {
this.notifyAborted = notifyAborted;
}
@DataBoundSetter
public void setNotifyFailure(boolean notifyFailure) {
this.notifyFailure = notifyFailure;
}
@DataBoundSetter
public void setNotifyNotBuilt(boolean notifyNotBuilt) {
this.notifyNotBuilt = notifyNotBuilt;
}
@DataBoundSetter
public void setNotifyUnstable(boolean notifyUnstable) {
this.notifyUnstable = notifyUnstable;
}
@DataBoundSetter
public void setNotifyBackToNormal(boolean notifyBackToNormal) {
this.notifyBackToNormal = notifyBackToNormal;
}
@DataBoundSetter
public void setIncludeTestSummary(boolean includeTestSummary) {
this.includeTestSummary = includeTestSummary;
}
@DataBoundSetter
public void setNotifyRepeatedFailure(boolean notifyRepeatedFailure) {
this.notifyRepeatedFailure = notifyRepeatedFailure;
}
@DataBoundSetter
public void setIncludeCustomMessage(boolean includeCustomMessage) {
this.includeCustomMessage = includeCustomMessage;
}
@DataBoundSetter
public void setCustomMessage(String customMessage) {
this.customMessage = customMessage;
}
/**
* Returns a custom message for a specific build type
* @param buildType
* @return String
*/
public String getCustomMessage(Result buildType) {
return this.getCustomMessage(buildType.toString());
}
/**
* Returns a custom message for a specific build type
* @param buildType
* @return String
*/
public String getCustomMessage(String buildType) {
String message = null;
for (Map.Entry pair : customMessageMap.entrySet()) {
if ( pair.getKey().toString().equals(buildType) )
{
message = pair.getValue().toString();
}
}
// If there's no specific message, use the generic custom message
if ( message == null || message.equals("") )
{
message = getCustomMessage();
}
return message;
}
@DataBoundConstructor
public SlackNotifier() {
super();
}
public SlackNotifier(final String teamDomain, final String authToken, final boolean botUser, final String room, final String authTokenCredentialId,
final String sendAs, final boolean startNotification, final boolean notifyAborted, final boolean notifyFailure,
final boolean notifyNotBuilt, final boolean notifySuccess, final boolean notifyUnstable, final boolean notifyBackToNormal,
final boolean notifyRepeatedFailure, final boolean includeTestSummary, final boolean includeFailedTests,
CommitInfoChoice commitInfoChoice, boolean includeCustomMessage, String customMessage, HashMap<String, String> customMessageMap) {
super();
this.teamDomain = teamDomain;
this.authToken = authToken;
this.authTokenCredentialId = StringUtils.trim(authTokenCredentialId);
this.botUser = botUser;
this.room = room;
this.sendAs = sendAs;
this.startNotification = startNotification;
this.notifyAborted = notifyAborted;
this.notifyFailure = notifyFailure;
this.notifyNotBuilt = notifyNotBuilt;
this.notifySuccess = notifySuccess;
this.notifyUnstable = notifyUnstable;
this.notifyBackToNormal = notifyBackToNormal;
this.notifyRepeatedFailure = notifyRepeatedFailure;
this.includeTestSummary = includeTestSummary;
this.includeFailedTests = includeFailedTests;
this.commitInfoChoice = commitInfoChoice;
this.includeCustomMessage = includeCustomMessage;
this.customMessage = customMessage;
this.customMessageMap = customMessageMap;
}
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
public SlackService newSlackService(AbstractBuild r, BuildListener listener) {
String teamDomain = this.teamDomain;
if (StringUtils.isEmpty(teamDomain)) {
teamDomain = getDescriptor().getTeamDomain();
}
String authToken = this.authToken;
boolean botUser = this.botUser;
if (StringUtils.isEmpty(authToken)) {
authToken = getDescriptor().getToken();
botUser = getDescriptor().getBotUser();
}
String authTokenCredentialId = this.authTokenCredentialId;
if (StringUtils.isEmpty(authTokenCredentialId)) {
authTokenCredentialId = getDescriptor().getTokenCredentialId();
}
String room = this.room;
if (StringUtils.isEmpty(room)) {
room = getDescriptor().getRoom();
}
EnvVars env = null;
try {
env = r.getEnvironment(listener);
} catch (Exception e) {
listener.getLogger().println("Error retrieving environment vars: " + e.getMessage());
env = new EnvVars();
}
teamDomain = env.expand(teamDomain);
authToken = env.expand(authToken);
authTokenCredentialId = env.expand(authTokenCredentialId);
room = env.expand(room);
return new StandardSlackService(teamDomain, authToken, authTokenCredentialId, botUser, room);
}
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
return true;
}
@Override
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
if (startNotification) {
Map<Descriptor<Publisher>, Publisher> map = build.getProject().getPublishersList().toMap();
for (Publisher publisher : map.values()) {
if (publisher instanceof SlackNotifier) {
logger.info("Invoking Started...");
new ActiveNotifier((SlackNotifier) publisher, listener).started(build);
}
}
}
return super.prebuild(build, listener);
}
@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
private String teamDomain;
private String token;
private String tokenCredentialId;
private boolean botUser;
private String room;
private String sendAs;
public static final CommitInfoChoice[] COMMIT_INFO_CHOICES = CommitInfoChoice.values();
public DescriptorImpl() {
load();
}
public String getTeamDomain() {
return teamDomain;
}
public String getToken() {
return token;
}
public String getTokenCredentialId() {
return tokenCredentialId;
}
public boolean getBotUser() {
return botUser;
}
public String getRoom() {
return room;
}
public String getSendAs() {
return sendAs;
}
public ListBoxModel doFillTokenCredentialIdItems() {
if (!Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER)) {
return new ListBoxModel();
}
return new StandardListBoxModel()
.withEmptySelection()
.withAll(lookupCredentials(
StringCredentials.class,
Jenkins.getInstance(),
ACL.SYSTEM,
new HostnameRequirement("*.slack.com"))
);
}
//WARN users that they should not use the plain/exposed token, but rather the token credential id
public FormValidation doCheckToken(@QueryParameter String value) {
//always show the warning - TODO investigate if there is a better way to handle this
return FormValidation.warning("Exposing your Integration Token is a security risk. Please use the Integration Token Credential ID");
}
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
return true;
}
@Override
public SlackNotifier newInstance(StaplerRequest sr, JSONObject json) {
String teamDomain = sr.getParameter("slackTeamDomain");
String token = sr.getParameter("slackToken");
String tokenCredentialId = json.getString("tokenCredentialId");
boolean botUser = "true".equals(sr.getParameter("slackBotUser"));
String room = sr.getParameter("slackRoom");
boolean startNotification = "true".equals(sr.getParameter("slackStartNotification"));
boolean notifySuccess = "true".equals(sr.getParameter("slackNotifySuccess"));
boolean notifyAborted = "true".equals(sr.getParameter("slackNotifyAborted"));
boolean notifyNotBuilt = "true".equals(sr.getParameter("slackNotifyNotBuilt"));
boolean notifyUnstable = "true".equals(sr.getParameter("slackNotifyUnstable"));
boolean notifyFailure = "true".equals(sr.getParameter("slackNotifyFailure"));
boolean notifyBackToNormal = "true".equals(sr.getParameter("slackNotifyBackToNormal"));
boolean notifyRepeatedFailure = "true".equals(sr.getParameter("slackNotifyRepeatedFailure"));
boolean includeTestSummary = "true".equals(sr.getParameter("includeTestSummary"));
boolean includeFailedTests = "true".equals(sr.getParameter("includeFailedTests"));
CommitInfoChoice commitInfoChoice = CommitInfoChoice.forDisplayName(sr.getParameter("slackCommitInfoChoice"));
boolean includeCustomMessage = "on".equals(sr.getParameter("includeCustomMessage"));
String customMessage = sr.getParameter("customMessage");
HashMap<String,String> customMessageMap = new HashMap<String,String>();
customMessageMap.put("SUCCESS", sr.getParameter("customMessageBuildSuccess"));
customMessageMap.put("FAILURE", sr.getParameter("customMessageBuildFailure"));
customMessageMap.put("ABORTED", sr.getParameter("customMessageBuildAborted"));
customMessageMap.put("NOT_BUILT", sr.getParameter("customMessageBuildNotBuilt"));
customMessageMap.put("UNSTABLE", sr.getParameter("customMessageBuildUnstable"));
return new SlackNotifier(teamDomain, token, botUser, room, tokenCredentialId, sendAs, startNotification, notifyAborted,
notifyFailure, notifyNotBuilt, notifySuccess, notifyUnstable, notifyBackToNormal, notifyRepeatedFailure,
includeTestSummary, includeFailedTests, commitInfoChoice, includeCustomMessage, customMessage, customMessageMap);
}
@Override
public boolean configure(StaplerRequest sr, JSONObject formData) throws FormException {
teamDomain = sr.getParameter("slackTeamDomain");
token = sr.getParameter("slackToken");
tokenCredentialId = formData.getJSONObject("slack").getString("tokenCredentialId");
botUser = "true".equals(sr.getParameter("slackBotUser"));
room = sr.getParameter("slackRoom");
sendAs = sr.getParameter("slackSendAs");
save();
return super.configure(sr, formData);
}
SlackService getSlackService(final String teamDomain, final String authToken, final String authTokenCredentialId, final boolean botUser, final String room) {
return new StandardSlackService(teamDomain, authToken, authTokenCredentialId, botUser, room);
}
@Override
public String getDisplayName() {
return "Slack Notifications";
}
public FormValidation doTestConnection(@QueryParameter("slackTeamDomain") final String teamDomain,
@QueryParameter("slackToken") final String authToken,
@QueryParameter("tokenCredentialId") final String authTokenCredentialId,
@QueryParameter("slackBotUser") final boolean botUser,
@QueryParameter("slackRoom") final String room) throws FormException {
try {
String targetDomain = teamDomain;
if (StringUtils.isEmpty(targetDomain)) {
targetDomain = this.teamDomain;
}
String targetToken = authToken;
boolean targetBotUser = botUser;
if (StringUtils.isEmpty(targetToken)) {
targetToken = this.token;
targetBotUser = this.botUser;
}
String targetTokenCredentialId = authTokenCredentialId;
if (StringUtils.isEmpty(targetTokenCredentialId)) {
targetTokenCredentialId = this.tokenCredentialId;
}
String targetRoom = room;
if (StringUtils.isEmpty(targetRoom)) {
targetRoom = this.room;
}
SlackService testSlackService = getSlackService(targetDomain, targetToken, targetTokenCredentialId, targetBotUser, targetRoom);
String message = "Slack/Jenkins plugin: you're all set on " + DisplayURLProvider.get().getRoot();
boolean success = testSlackService.publish(message, "good");
return success ? FormValidation.ok("Success") : FormValidation.error("Failure");
} catch (Exception e) {
return FormValidation.error("Client error : " + e.getMessage());
}
}
}
@Deprecated
public static class SlackJobProperty extends hudson.model.JobProperty<AbstractProject<?, ?>> {
private String teamDomain;
private String token;
private boolean botUser;
private String room;
private boolean startNotification;
private boolean notifySuccess;
private boolean notifyAborted;
private boolean notifyNotBuilt;
private boolean notifyUnstable;
private boolean notifyFailure;
private boolean notifyBackToNormal;
private boolean notifyRepeatedFailure;
private boolean includeTestSummary;
private boolean showCommitList;
private boolean includeCustomMessage;
private String customMessage;
@DataBoundConstructor
public SlackJobProperty(String teamDomain,
String token,
boolean botUser,
String room,
boolean startNotification,
boolean notifyAborted,
boolean notifyFailure,
boolean notifyNotBuilt,
boolean notifySuccess,
boolean notifyUnstable,
boolean notifyBackToNormal,
boolean notifyRepeatedFailure,
boolean includeTestSummary,
boolean showCommitList,
boolean includeCustomMessage,
String customMessage) {
this.teamDomain = teamDomain;
this.token = token;
this.botUser = botUser;
this.room = room;
this.startNotification = startNotification;
this.notifyAborted = notifyAborted;
this.notifyFailure = notifyFailure;
this.notifyNotBuilt = notifyNotBuilt;
this.notifySuccess = notifySuccess;
this.notifyUnstable = notifyUnstable;
this.notifyBackToNormal = notifyBackToNormal;
this.notifyRepeatedFailure = notifyRepeatedFailure;
this.includeTestSummary = includeTestSummary;
this.showCommitList = showCommitList;
this.includeCustomMessage = includeCustomMessage;
this.customMessage = customMessage;
}
@Exported
public String getTeamDomain() {
return teamDomain;
}
@Exported
public String getToken() {
return token;
}
@Exported
public boolean getBotUser() {
return botUser;
}
@Exported
public String getRoom() {
return room;
}
@Exported
public boolean getStartNotification() {
return startNotification;
}
@Exported
public boolean getNotifySuccess() {
return notifySuccess;
}
@Exported
public boolean getShowCommitList() {
return showCommitList;
}
@Override
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
return super.prebuild(build, listener);
}
@Exported
public boolean getNotifyAborted() {
return notifyAborted;
}
@Exported
public boolean getNotifyFailure() {
return notifyFailure;
}
@Exported
public boolean getNotifyNotBuilt() {
return notifyNotBuilt;
}
@Exported
public boolean getNotifyUnstable() {
return notifyUnstable;
}
@Exported
public boolean getNotifyBackToNormal() {
return notifyBackToNormal;
}
@Exported
public boolean includeTestSummary() {
return includeTestSummary;
}
@Exported
public boolean getNotifyRepeatedFailure() {
return notifyRepeatedFailure;
}
@Exported
public boolean includeCustomMessage() {
return includeCustomMessage;
}
@Exported
public String getCustomMessage() {
return customMessage;
}
}
@Extension(ordinal = 100) public static final class Migrator extends ItemListener {
@Override
public void onLoaded() {
logger.info("Starting Settings Migration Process");
ItemConfigMigrator migrator = new ItemConfigMigrator();
for (Item item : Jenkins.getInstance().getAllItems()) {
if (!migrator.migrate(item)) {
logger.info(String.format("Skipping job \"%s\" with type %s", item.getName(),
item.getClass().getName()));
continue;
}
}
logger.info("Completed Settings Migration Process");
}
}
}