forked from voxmedia/github-action-slack-notify-build
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
67 lines (55 loc) · 1.88 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
const core = require("@actions/core");
const github = require("@actions/github");
const { WebClient } = require("@slack/web-api");
const { buildSlackAttachments, formatChannelName } = require("./src/utils");
(async () => {
try {
const channel = core.getInput("channel");
const status = core.getInput("status");
const color = core.getInput("color");
const messageId = core.getInput("message_id");
const token = process.env.SLACK_BOT_TOKEN;
const slack = new WebClient(token);
if (!channel && !core.getInput("channel_id")) {
core.setFailed(`You must provider either a 'channel' or a 'channel_id'.`);
return;
}
const attachments = buildSlackAttachments({ status, color, github });
const channelId =
core.getInput("channel_id") ||
(await lookUpChannelId({ slack, channel }));
if (!channelId) {
core.setFailed(`Slack channel ${channel} could not be found.`);
return;
}
const apiMethod = Boolean(messageId) ? "update" : "postMessage";
const args = {
channel: channelId,
attachments,
};
if (messageId) {
args.ts = messageId;
}
const response = await slack.chat[apiMethod](args);
core.setOutput("message_id", response.ts);
} catch (error) {
core.setFailed(error);
}
})();
async function lookUpChannelId({ slack, channel }) {
let result;
const formattedChannel = formatChannelName(channel);
// Async iteration is similar to a simple for loop.
// Use only the first two parameters to get an async iterator.
for await (const page of slack.paginate("conversations.list", {
types: "public_channel, private_channel",
})) {
// You can inspect each page, find your result, and stop the loop with a `break` statement
const match = page.channels.find((c) => c.name === formattedChannel);
if (match) {
result = match.id;
break;
}
}
return result;
}