-
Notifications
You must be signed in to change notification settings - Fork 3
/
failed-builds-notification.js
252 lines (229 loc) · 7.26 KB
/
failed-builds-notification.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
'use latest';
import githubUrlFromGit from 'github-url-from-git';
import sendgrid from 'sendgrid';
import mandrill from 'mandrill-api/mandrill';
/**
* The org should really come as part of the webhook payload,
* but for some reason it is missing, so we extract it from an
* API url, such as
* https://api.buildkite.com/v2/organizations/my-org/pipelines/my-slug/builds/999
*/
function getBuildkiteOrgFromApiUrl(url) {
return url.match(/organizations\/(.*?)\//)[1];
}
function fullSlug({ org, slug }) {
return `${org}/${slug}`;
}
/**
* @param context {WebtaskContext}
*/
module.exports = (context, cb) => {
const { BUILDKITE_TOKEN } = context.secrets;
if (!BUILDKITE_TOKEN) {
cb(new Error(`BUILDKITE_TOKEN secret not set or empty`));
return;
}
if (
!('x-buildkite-token' in context.headers) ||
context.headers['x-buildkite-token'] !== BUILDKITE_TOKEN
) {
cb(new Error('Missing or incorrect Buildkite token'));
return;
}
if (!context.body) {
cb(new Error('Wrong Content-Type?'));
return;
}
const { event } = context.body;
switch (event) {
case 'ping':
cb(null, 'pong');
return;
case 'build.finished':
// this is the only other one we want to handle
break;
default:
cb(new Error(`Unknown Buildkite event '${event}'`));
return;
}
const {
build: {
state,
web_url: buildUrl,
number,
message,
commit: sha,
creator: { name, email },
},
pipeline: {
name: pipelineName,
url: pipelineUrl,
slug,
repository: repo,
},
} = context.body;
// we want to keep this data structure small,
// because we only have 500k in webtask
const currentCulprit = {
org: getBuildkiteOrgFromApiUrl(pipelineUrl),
slug,
name,
email,
sha,
message,
number,
repo,
};
switch (state) {
case 'passed':
// remove any stored culprits for the current pipeline
transformStorage(context, clearPipeline.bind(null, currentCulprit))
.then(() => cb())
.catch(cb);
break;
case 'failed':
// store culprits and send Email
transformStorage(context, storeCulprit.bind(null, currentCulprit))
.then(data => {
const { culprits } = data.pipelines[
fullSlug(currentCulprit)
];
return sendEmail(
context,
culprits,
currentCulprit,
buildUrl,
pipelineName
);
})
.then(() => cb())
.catch(cb);
break;
default:
// not interested in this state, but won't fail the hook
cb();
return;
}
};
function storeCulprit(culprit, data) {
const slug = fullSlug(culprit);
data = data || {};
data.pipelines = data.pipelines || {};
data.pipelines[slug] = data.pipelines[slug] || {};
data.pipelines[slug].culprits = data.pipelines[slug].culprits || [];
if (!data.pipelines[slug].culprits.some(({ sha }) => sha === culprit.sha)) {
// we only add a culprit to the list if we don't have it yet
// failing reruns of the same commit are not added to the list
data.pipelines[slug].culprits.unshift(culprit);
}
return data;
}
function clearPipeline(culprit, data) {
const slug = fullSlug(culprit);
data = data || {};
data.pipelines = data.pipelines || {};
delete data.pipelines[slug];
return data;
}
function sendEmail(context, culprits, currentCulprit, buildUrl, pipelineName) {
const { slug, name, email, number } = currentCulprit;
const subject = `🚨 Elves and dragons! ${pipelineName} (${slug}) failed (#${number})`;
const list = culprits
.map(({ repo, sha, message, name, number }) => {
const githubUrl = `${githubUrlFromGit(repo)}/commit/${sha}`;
const shortSha = sha.substring(0, 6);
return `* ${message} [${name}, ${shortSha}, ${githubUrl}, failing since #${number}]`;
})
.join('\n');
const content = `Greetings ${name}!
What in the Shire is this?
I hope that you’ll not take it amiss, but it seems you may have broken
${pipelineName} (${slug})
in build #${number}: ${buildUrl}
via:
${list}
Timely fixing will go down like lembas bread :)
Your good health!
Bilbo
`;
return send(context, email, subject, content);
}
function send(context, to, subject, content) {
const {
MANDRILL_API_KEY,
SENDGRID_API_KEY,
SENDER_EMAIL_ADDRESS: email,
} = context.secrets;
const name = 'Bilbo';
if (MANDRILL_API_KEY) {
const mandrillClient = new mandrill.Mandrill(MANDRILL_API_KEY);
const message = {
text: content,
subject,
from_email: email,
from_name: name,
to: [
{
email: to,
type: 'to',
},
],
track_opens: false,
track_clicks: false,
auto_html: false,
};
return new Promise((resolve, reject) => {
mandrillClient.messages.send(
{ message },
() => resolve(),
() => reject()
);
});
} else if (SENDGRID_API_KEY) {
const helper = sendgrid.mail;
const mail = new helper.Mail(
{ name, email },
subject,
new helper.Email(to),
new helper.Content('text/plain', content)
);
const sg = sendgrid(SENDGRID_API_KEY);
const request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON(),
});
return sg.API(request);
} else {
throw new Error('No valid API keys for either Mandrill or Sendgrid');
}
}
function transformStorage(ctx, transformFn) {
return new Promise((resolve, reject) => {
ctx.storage.get(function(error, data) {
if (error) {
reject(error);
return;
}
data = transformFn(data);
var attempts = 3;
ctx.storage.set(data, function set_cb(error) {
if (error) {
if (error.code === 409 && attempts--) {
// resolve conflict and re-attempt set
// unsure whether error.conflict contains the whole data
// object or just a fragment, the webtask docs don't give
// a clear indication there.
// TODO: potentially fix this after support q comes back
data = transformFn(error.conflict);
return ctx.storage.set(data, set_cb);
}
reject(error);
return;
}
resolve(data);
return;
});
});
});
}