-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
59 lines (52 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
const { IncomingWebhook } = require('@slack/client');
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
const webhook = new IncomingWebhook(SLACK_WEBHOOK_URL);
// subscribe is the main function called by Cloud Functions.
exports.subscribe = (pubSubEvent, context) => {
const build = pubSubEvent.data
? JSON.parse(Buffer.from(pubSubEvent.data, 'base64').toString())
: null;
if (build == null) {
return;
}
// Skip if the current status is not in the status list.
// Add additional statues to list if you'd like:
// QUEUED, WORKING, SUCCESS, FAILURE,
// INTERNAL_ERROR, TIMEOUT, CANCELLED
const status = ['SUCCESS', 'FAILURE', 'INTERNAL_ERROR', 'TIMEOUT'];
if (status.indexOf(build.status) === -1) {
return;
}
// Send message to Slack.
const message = createSlackMessage(build);
webhook.send(message);
};
// dockerImage returns the last part of the provided URL.
// For example, gcr.io/tidal-1529434400027/cast-highlight:latest => cast-highlight:latest
const dockerImage = (url) => {
return url.split('/').slice(-1)[0];
}
// createSlackMessage create a message from a build object.
const createSlackMessage = (build) => {
let message = {
text: `Build \`${build.id}\``,
mrkdwn: true,
attachments: [
{
title: 'Build logs',
title_link: build.logUrl,
fields: [{
title: 'Status',
value: build.status
}]
}
]
};
if (build.images) {
message.attachments[0].fields.push({
title: 'Image',
value: build.images.map(dockerImage).join(', ')
});
}
return message;
}