generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
88 lines (73 loc) · 2.13 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
80
81
82
83
84
85
86
87
88
const core = require('@actions/core');
const github = require('@actions/github');
const { IncomingWebhook } = require('@slack/webhook');
const context = github.context;
function toJSON(value, pretty=true) {
return pretty
? JSON.stringify(value, null, 4)
: JSON.stringify(value);
}
function formatLogMessage(msg, obj = null) {
return obj ? `${msg}: ${toJSON(obj)}` : msg;
}
function info(msg, obj = null) {
core.info(formatLogMessage(msg, obj));
}
function debug(msg, obj = null) {
core.debug(formatLogMessage(msg, obj));
}
function notifySlack(commits) {
const slack_webhook_url = core.getInput('slack_webhook_url');
const slack_message = core.getInput('slack_message');
const webhook = new IncomingWebhook(slack_webhook_url);
const attachments = commits.map((commit) => {
return {
fallback: commit['message'],
author_name: commit['author']['name'],
author_link: `https://github.com/${commit['author']['username']}`,
title: commit['message'],
title_link: commit['url'],
footer: 'Commit Hawk',
footer_icon: 'https://platform.slack-edge.com/img/default_application_icon.png',
ts: commit['timestamp']
}
});
info(`Ping: ${slack_webhook_url}`, attachments);
return webhook.send({
text: slack_message,
attachments: attachments,
icon_emoji: ':eagle:'
});
}
async function getCommits() {
let commits;
info('Getting commits...');
switch(context.eventName) {
case 'push':
commits = context.payload.commits;
break;
default:
info(`${context.eventName} event is not supported by this action.`);
commits = [];
break;
}
return commits;
}
async function run() {
try {
getCommits().then(commits => {
// Exclude merge commits
commits = commits.filter(c => !c.parents || 1 === c.parents.length);
if ('push' === context.eventName) {
commits = commits.filter(c => c.distinct);
}
core.setOutput('commits', commits);
notifySlack(commits)
.then(() => process.exitCode = 0)
.catch(err => core.error(err) && (process.exitCode = 1));
});
} catch (error) {
core.setFailed(error.message);
}
}
run();