-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
308 lines (275 loc) · 11.1 KB
/
index.js
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
'use strict';
const Joi = require('joi');
const NotificationBase = require('screwdriver-notifications-base');
const schema = require('screwdriver-data-schema');
const hoek = require('@hapi/hoek');
const slacker = require('./slack');
// This should match what is in https://github.com/screwdriver-cd/data-schema/blob/master/models/build.js#L14
// https://github.com/screwdriver-cd/ui/blob/master/app/styles/screwdriver-colors.scss
const COLOR_MAP = {
ABORTED: 'danger',
CREATED: '#0b548c', // Using 'sd-info-fg' from https://github.com/screwdriver-cd/ui/blob/master/app/styles/screwdriver-colors.scss
FAILURE: 'danger',
QUEUED: '#0F69FF',
RUNNING: '#0F69FF',
SUCCESS: 'good',
BLOCKED: '#ccc', // Using 'sd-light-gray' from https://github.com/screwdriver-cd/ui/blob/master/app/styles/screwdriver-colors.scss
UNSTABLE: '#ffd333', // Using 'sd-unstable' from https://github.com/screwdriver-cd/ui/blob/master/app/styles/screwdriver-colors.scss
COLLAPSED: '#f2f2f2', // New color. Light grey.
FIXED: 'good',
FROZEN: '#acd9ff' // Using 'sd-frozen' from https://github.com/screwdriver-cd/ui/blob/master/app/styles/screwdriver-colors.scss
};
const STATUSES_MAP = {
ABORTED: ':cloud:',
CREATED: ':clock11:',
FAILURE: ':umbrella:',
QUEUED: ':cyclone:',
RUNNING: ':runner:',
SUCCESS: ':sunny:',
BLOCKED: ':lock:',
UNSTABLE: ':foggy:',
COLLAPSED: ':arrow_up:',
FIXED: ':sunny:',
FROZEN: ':snowman:'
};
const DEFAULT_STATUSES = ['FAILURE'];
const SCHEMA_STATUSES = Joi.array().items(schema.plugins.notifications.schemaStatus).min(0);
const SCHEMA_SLACK_CHANNEL = Joi.string().required();
const SCHEMA_SLACK_CHANNELS = Joi.array().items(SCHEMA_SLACK_CHANNEL).min(1);
const SCHEMA_SLACK_SETTINGS = Joi.object()
.keys({
slack: Joi.alternatives()
.try(
Joi.object().keys({
channels: SCHEMA_SLACK_CHANNELS,
statuses: SCHEMA_STATUSES,
message: Joi.string(),
minimized: Joi.boolean()
}),
SCHEMA_SLACK_CHANNELS,
SCHEMA_SLACK_CHANNEL
)
.required()
})
.unknown(true);
const SCHEMA_BUILD_DATA = Joi.object().keys({
...schema.plugins.notifications.schemaBuildData,
settings: SCHEMA_SLACK_SETTINGS.required()
});
const SCHEMA_JOB_DATA = Joi.object().keys({
...schema.plugins.notifications.schemaJobData,
settings: SCHEMA_SLACK_SETTINGS.required()
});
const SCHEMA_SLACK_CONFIG = Joi.object().keys({
defaultWorkspace: Joi.string().required(),
workspaces: Joi.object()
.unknown()
.pattern(
Joi.string(),
Joi.object().keys({
token: Joi.string().required()
})
)
.required()
});
/**
* Handle slack messaging for build status
* @method buildStatus
* @param {Object} buildData
* @param {String} buildData.status Build status
* @param {Object} buildData.pipeline Pipeline
* @param {String} buildData.jobName Job name
* @param {Object} buildData.build Build
* @param {Object} buildData.event Event
* @param {String} buildData.buildLink Build link
* @param {Object} buildData.settings Notification setting
* @param {Object} config Slack notifications config
*/
function buildStatus(buildData, config) {
try {
Joi.attempt(buildData, SCHEMA_BUILD_DATA, 'Invalid build data format');
} catch (e) {
return;
}
// Slack channel overwrite from meta data. Job specific only.
const metaReplaceVar = `build.meta.notification.slack.${buildData.jobName}.channels`;
const metaDataSlackChannel = hoek.reach(buildData, metaReplaceVar, { default: false });
let channelReplacement;
if (metaDataSlackChannel !== false) {
channelReplacement = metaDataSlackChannel.split(',');
// Remove empty/blank items.
channelReplacement = channelReplacement.filter(x => x.trim() !== '');
}
// Slack channels from configuration
if (typeof buildData.settings.slack === 'string' || Array.isArray(buildData.settings.slack)) {
buildData.settings.slack =
typeof buildData.settings.slack === 'string' ? [buildData.settings.slack] : buildData.settings.slack;
buildData.settings.slack = {
channels: buildData.settings.slack,
statuses: DEFAULT_STATUSES,
message: '',
minimized: false
};
}
if (channelReplacement) {
buildData.settings.slack.channels = channelReplacement;
}
if (buildData.settings.slack.statuses === undefined) {
buildData.settings.slack.statuses = DEFAULT_STATUSES;
}
// Add for fixed notification
if (buildData.isFixed) {
buildData.settings.slack.statuses.push('FIXED');
}
// Do not change the `buildData.status` directly
// It affects the behavior of other notification plugins
let notificationStatus = buildData.status;
if (buildData.settings.slack.statuses.includes('FAILURE')) {
if (notificationStatus === 'SUCCESS' && buildData.isFixed) {
notificationStatus = 'FIXED';
}
}
if (!buildData.settings.slack.statuses.includes(notificationStatus)) {
return;
}
const pipelineLink = /^PR-/.test(buildData.jobName)
? `${buildData.buildLink.split('/builds')[0]}/pulls`
: buildData.buildLink.split('/builds')[0];
const truncatedSha = buildData.event.sha.slice(0, 6);
const cutOff = 150;
const commitMessage =
buildData.event.commit.message.length > cutOff
? `${buildData.event.commit.message.substring(0, cutOff)}...`
: buildData.event.commit.message;
// Slack channel overwrite from meta data. Job specific only.
const metaMinimizedReplaceVar = `build.meta.notification.slack.${buildData.jobName}.minimized`;
const isMinimized = hoek.reach(buildData, metaMinimizedReplaceVar, {
default: buildData.settings.slack.minimized
});
let message = isMinimized
? // eslint-disable-next-line max-len
`<${pipelineLink}|${buildData.pipeline.scmRepo.name}#${buildData.jobName}> *${notificationStatus}*`
: // eslint-disable-next-line max-len
`*${notificationStatus}* ${STATUSES_MAP[notificationStatus]} <${pipelineLink}|${buildData.pipeline.scmRepo.name} ${buildData.jobName}>`;
const rootDir = hoek.reach(buildData, 'pipeline.scmRepo.rootDir', { default: false });
if (rootDir) {
message = `${message}\n*Source Directory:* ${rootDir}`;
}
const defaultMessage = buildData.settings.slack.message;
const metaMessage = hoek.reach(buildData, 'build.meta.notification.slack.message', { default: false });
const buildMessage = hoek.reach(buildData, `build.meta.notification.slack.${buildData.jobName}.message`, {
default: false
});
// Use job specific message over generic message.
const specificMessage = buildMessage || metaMessage || defaultMessage || '';
if (specificMessage) {
message = `${message}\n${specificMessage}`;
}
const attachments = isMinimized
? [
{
fallback: '',
color: COLOR_MAP[notificationStatus],
fields: [
{
title: 'Build',
value: `<${buildData.buildLink}|#${buildData.build.id}>`,
short: true
}
]
}
]
: [
{
fallback: '',
color: COLOR_MAP[notificationStatus],
title: `#${buildData.build.id}`,
title_link: `${buildData.buildLink}`,
// eslint-disable-next-line max-len
text:
`${commitMessage} (<${buildData.event.commit.url}|${truncatedSha}>)` +
`\n${buildData.event.causeMessage}`
}
];
const slackMessage = {
message,
attachments
};
slacker(config, buildData.settings.slack.channels, slackMessage);
}
/**
* Handle slack messaging for job status
* @method jobStatus
* @param {Object} jobData
* @param {String} jobData.status Status
* @param {Object} jobData.pipeline Pipeline
* @param {String} jobData.jobName Job name
* @param {String} jobData.pipelineLink Pipeline link
* @param {String} jobData.message Message
* @param {Object} jobData.settings Notification setting
* @param {Object} config Slack notifications config
*/
function jobStatus(jobData, config) {
try {
Joi.attempt(jobData, SCHEMA_JOB_DATA, 'Invalid job data format');
} catch (e) {
return;
}
// Slack channels from configuration
if (typeof jobData.settings.slack === 'string' || Array.isArray(jobData.settings.slack)) {
jobData.settings.slack =
typeof jobData.settings.slack === 'string' ? [jobData.settings.slack] : jobData.settings.slack;
jobData.settings.slack = {
channels: jobData.settings.slack,
statuses: DEFAULT_STATUSES,
minimized: false
};
}
const isMinimized = jobData.settings.slack.minimized;
const message = isMinimized
? // eslint-disable-next-line max-len
`<${jobData.pipelineLink}|${jobData.pipeline.scmRepo.name}#${jobData.jobName}> *${jobData.status}*\n${jobData.message}`
: // eslint-disable-next-line max-len
`*${jobData.status}* ${STATUSES_MAP[jobData.status]} <${jobData.pipelineLink}|${
jobData.pipeline.scmRepo.name
} ${jobData.jobName}>\n${jobData.message}`;
const slackMessage = {
message
};
slacker(config, jobData.settings.slack.channels, slackMessage);
}
class SlackNotifier extends NotificationBase {
/**
* Constructs an SlackNotifier
* @constructor
* @param {object} config - Screwdriver config object initialized in API
*/
constructor(config) {
super(...arguments);
this.config = Joi.attempt(config, SCHEMA_SLACK_CONFIG, 'Invalid config for slack notifications');
}
/**
* Sets listener on server event of name 'eventName' in Screwdriver
* @method _notify
* @param {String} event - Event emitted from Screwdriver
* @param {Object} payload - Data emitted with some event from Screwdriver
*/
_notify(event, payload) {
if (!payload || !payload.settings || Object.keys(payload.settings).length === 0) {
return;
}
switch (event) {
case 'build_status':
buildStatus(payload, this.config);
break;
case 'job_status':
jobStatus(payload, this.config);
break;
default:
}
}
static validateConfig(config) {
return SCHEMA_SLACK_SETTINGS.validate(config);
}
}
module.exports = SlackNotifier;