forked from voxmedia/github-action-slack-notify-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (77 loc) · 2.8 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
89
90
91
92
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 messageId = core.getInput('message_id');
const text = core.getInput('text');
const token = process.env.SLACK_BOT_TOKEN;
const slack = new WebClient(token);
let status = core.getInput('status');
let color = core.getInput('color');
if (!channel && !core.getInput('channel_id')) {
core.setFailed(`You must provider either a 'channel' or a 'channel_id'.`);
return;
}
const channelId = core.getInput('channel_id') || (await lookUpChannelId({ slack, channel }));
if (!channelId) {
core.setFailed(`Slack channel ${channel} could not be found.`);
return;
}
if (!messageId && !status) {
core.setFailed(`You must provide an status when creating a new message`);
return;
}
if (status && !color) color = getStatusColor(status);
// if messageId is used (update), keep the same color and status if not modified
if (Boolean(messageId) && (!status || !color)) {
const result = await slack.conversations.history({
token: token,
channel: channelId,
latest: messageId,
inclusive: true,
limit: 1
});
if (!color) color = result.messages[0].attachments[0].color;
if (!status) status = result.messages[0].attachments[0].fields[2].value;
}
const attachments = buildSlackAttachments({ status, color, github, text });
const args = {
channel: channelId,
attachments,
};
if (messageId) {
args.ts = messageId;
}
const apiMethod = Boolean(messageId) ? 'update' : 'postMessage';
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', limit: 1000 })) {
// 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;
}
function getStatusColor(status) {
let color;
if (status === 'SUCCESS') color = 'good'
else if (status === 'STARTING') color = 'warning'
else if (status === 'FAILURE') color = 'danger'
else color = '#cccccc';
return color;
}