This repository has been archived by the owner on Apr 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (50 loc) · 1.66 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
try {
const github = require("@actions/github");
const githubContext = github.context;
const { state } = githubContext.payload;
if (state !== "failure" && state !== "error") {
console.log(`State is ${state}, no notification required.`);
return;
}
const { branches } = githubContext.payload;
if (!branches.some(branch => branch.name === "master")) {
console.log(`Not master branch status update, no notification required.`);
return;
}
const core = require("@actions/core");
const slackToken = core.getInput("slack-token");
const slackChannel = core.getInput("slack-channel");
const { WebClient } = require("@slack/web-api");
const { shortenString, firstLineString, reducedSha } = require("./utils");
const {
commit,
repository,
description,
context,
target_url: targetUrl,
avatar_url: avatarUrl
} = githubContext.payload;
const { html_url: commitUrl, commit: commitData } = commit;
const { html_url: repositoryUrl } = repository;
const commitHeader = shortenString(firstLineString(commitData.message), 50);
const reducedCommitSha = reducedSha(commit.sha);
const masterUrl = `${repositoryUrl}/commits/master`;
const slackbot = new WebClient(slackToken);
slackbot.chat.postMessage({
as_user: false,
channel: slackChannel,
text: `<!channel>\n${commitHeader} [<${commitUrl}|${reducedCommitSha}> on <${masterUrl}|master>]`,
attachments: [
{
fallback: `<${targetUrl}|${context}> - ${description}`,
color: "#d30515",
title: context,
title_link: targetUrl,
text: description,
thumb_url: avatarUrl
}
]
});
} catch (e) {
console.log(e);
}