-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (59 loc) · 2.09 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
const IncomingWebhook = require('@slack/webhook').IncomingWebhook;
const SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/xxx/yyy/zzz';
const webhook = new IncomingWebhook(SLACK_WEBHOOK_URL);
exports.slackOnCodePushed = (pubSubEvent, callback) => {
const commit = eventToCommit(pubSubEvent.data);
// Send message to Slack.
const message = createSlackMessage(commit);
(async () => {
try {
const reply = await webhook.send(message);
// Use result
console.log(reply);
} catch (error) {
// Handle error
console.log(error);
}
})();
callback();
};
const eventToCommit = (data) => {
return JSON.parse(Buffer.from(data, 'base64').toString());
};
// createSlackMessage create a message from a build object.
const createSlackMessage = (commit) => {
const {refUpdates, email} = commit.refUpdateEvent;
const info = Object.keys(refUpdates).map(key => {
return refUpdates[key];
});
const updateType = info[0].updateType.toLowerCase().split('_')[0];
const branch = info[0].refName.split('refs/heads/')[1];
const commitId = info[0].newId;
let severityEmoji = ' :+1: ';
let commit_link = commitId ? `/+/${commitId}`:'';
let text, title;
if (commitId) {
// create , update
text = `\`${email}\` ${updateType} branch \`${branch}\``;
title = `${commit.name.split('/repos/')[1]} / ${branch} / ${commitId} ${severityEmoji}`;
} else {
text = `\`${email}\` ${updateType} branch \`${branch}\``;
title = `${commit.name.split('/repos/')[1]} / ${branch} ${severityEmoji}`;
}
return {
text,
mrkdwn: true,
username: commit.name.split('/repos/')[1],
attachments: [
{
title,
title_link: commit.url + commit_link,
color: 'good',
// fields: [{
// title: 'Status',
// value: typeof commit === "object" ? JSON.stringify(commit,null , 2) : commit
// }]
},
],
};
};