forked from galonge/GCB-Slack-Notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (69 loc) · 2.04 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
const { IncomingWebhook } = require('@slack/webhook');
const SLACK_WEBHOOK_URL = ''; // Enter Your Slack Webhook URL here
const webhook = new IncomingWebhook(SLACK_WEBHOOK_URL);
// subscribe is the main function called by Cloud Functions.
module.exports.subscribe = (event, callback) => {
const build = eventToBuild(event.data);
// 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',
'QUEUED',
'CANCELLED',
];
if (status.indexOf(build.status) === -1) {
return callback();
}
// Send message to Slack.
const message = createSlackMessage(build);
(async () => {
await webhook.send(message);
})();
};
// eventToBuild transforms pubsub event message to a build object.
const eventToBuild = (data) => {
return JSON.parse(Buffer.from(data, 'base64').toString());
};
// createSlackMessage create a message from a build object.
const createSlackMessage = (build) => {
let buildId = build.id || '';
let buildCommit = build.substitutions.COMMIT_SHA || '';
let branch = build.substitutions.BRANCH_NAME || '';
let repoName = build.source.repoSource.repoName.split('_').pop() || ''; //Get repository name
let message = {
text: `Build - \`${buildId}\``,
mrkdwn: true,
attachments: [
{
title: 'View Build Logs',
title_link: build.logUrl,
fields: [
{
title: 'Status',
value: build.status,
},
],
},
{
title: `Commit - ${buildCommit}`,
title_link: `https://bitbucket.org/<ORGANIZATION-NAME>/${repoName}/commits/${buildCommit}`, // Insert your Organization/Bitbucket/Github Url
fields: [
{
title: 'Branch',
value: branch,
},
{
title: 'Repository',
value: repoName,
},
],
},
],
};
return message;
};