-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (33 loc) · 1.24 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
const core = require("@actions/core");
const github = require('@actions/github');
const fetch = require("node-fetch");
const { buildSlackAttachments, formatChannelName } = require('./src/utils');
(async () => {
try {
const jobStatus = core.getInput("job-status");
const slackChannel = core.getInput("slack-channel");
const slackBotToken = core.getInput("slack-bot-token");
const status = jobStatus === "success" ? "Success" : jobStatus === "failure" ? "Failure" : "Cancelled";
const color = jobStatus === "success" ? "#2e993e" : jobStatus === "failure" ? "#bd0f26" : "#d29d0c";
const attachments = buildSlackAttachments({ status, color, github });
const payload = {
channel: slackChannel,
attachments: attachments
};
const result = await fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": payload.length,
Authorization: `Bearer ${slackBotToken}`,
Accept: "application/json",
},
});
if (!result.ok) {
throw new Error(`Server error ${result.status}`);
}
} catch (error) {
core.setFailed(error.message);
}
})();