-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (56 loc) · 2.1 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
const core = require('@actions/core');
const github = require('@actions/github');
const { WebClient } = require('@slack/web-api');
const { buildSlackBlocks, formatChannelName } = require('./src/utils');
(async() => {
try {
const channel = core.getInput('channel');
const message = core.getInput('message');
const colour = core.getInput('colour');
const messageId = core.getInput('message_id');
const token = process.env.SLACK_BOT_TOKEN;
const slack = new WebClient(token);
const apiMethod = Boolean(messageId) ? 'update' : 'postMessage';
const { owner, repo } = github.context.repo;
if (!channel) {
core.setFailed(`Channel is a required field`);
}
const channelId = (await lookupChannelId({ slack, channel }));
if (!channelId) {
core.setFailed(`Slack channel ${channel} could not be found`);
}
const blocks = buildSlackBlocks({ message, colour, github })
console.log(`The blocks payload:`);
console.log(blocks);
const args = {
channel: channelId,
blocks,
color: colour,
text: message,
footer_icon: 'https://github.githubassets.com/favicon.ico',
footer: `<https://github.com/${owner}/${repo} | ${owner}/${repo}>`,
ts: Math.floor(Date.now() / 1000),
};
if (messageId) {
args.ts = messageId;
}
console.log(`The full payload:`);
console.log(args);
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);
for await (const page of slack.paginate('conversations.list', { types: 'public_channel, private_channel' })) {
const match = page.channels.find(c => c.name === formattedChannel);
if (match) {
result = match.id;
break;
}
}
return result;
}