-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (55 loc) · 1.58 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
const IncomingWebhook = require('@slack/client').IncomingWebhook;
const SlackWebhook = new IncomingWebhook(process.env.SLACK_WEBHOOK_URL);
exports.gcbNotifier = async (event, ctx, cb) => {
const build = eventToBuildAdapter(event.data);
const status = ['SUCCESS', 'FAILURE', 'INTERNAL_ERROR', 'TIMEOUT'];
if (status.indexOf(build.status) === -1) {
console.log('%s(%s): ignoring status %s',
ctx.eventType, ctx.eventId, build.status);
return cb();
}
try {
const message = buildSlackMessage(build);
const result = await SlackWebhook.send(message);
console.log('%s(%s): processed: %s',
ctx.eventType, ctx.eventId, result.body);
} catch (err) {
console.log('%s(%s): error: %s', ctx.eventType, ctx.eventId, err);
}
return cb();
};
const eventToBuildAdapter = (data) => {
const raw = Buffer.from(data, 'base64').toString();
console.log(raw);
return JSON.parse(raw);
};
const buildSlackMessage = (build) => {
return {
text: `${build.substitutions.REPO_NAME} — build ${build.id}`,
mrkdwn: true,
attachments: [
{
title: 'Build logs',
title_link: build.logUrl,
fields: [
{
title: 'Status',
value: build.status,
},
{
title: 'Repository',
value: build.substitutions.REPO_NAME,
},
{
title: 'Commit',
value: build.substitutions.COMMIT_SHA,
},
{
title: 'Branch',
value: build.substitutions.BRANCH_NAME,
},
],
},
],
};
};