-
Notifications
You must be signed in to change notification settings - Fork 149
/
slack-send.js
165 lines (142 loc) · 6.35 KB
/
slack-send.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const { promises: fs } = require('fs');
const path = require('path');
const github = require('@actions/github');
const flatten = require('flat');
const axios = require('axios');
const markup = require('markup-js');
const { HttpsProxyAgent } = require('https-proxy-agent');
const { parseURL } = require('whatwg-url');
const { createWebClient } = require('./web-client');
const SLACK_WEBHOOK_TYPES = {
WORKFLOW_TRIGGER: 'WORKFLOW_TRIGGER',
INCOMING_WEBHOOK: 'INCOMING_WEBHOOK',
};
module.exports = async function slackSend(core) {
try {
const botToken = process.env.SLACK_BOT_TOKEN;
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
let webhookType = SLACK_WEBHOOK_TYPES.WORKFLOW_TRIGGER;
if (process.env.SLACK_WEBHOOK_TYPE) {
// The default type is for Workflow Builder triggers. If you want to use this action for Incoming Webhooks, use
// the corresponding type instead.
webhookType = process.env.SLACK_WEBHOOK_TYPE.toUpperCase();
}
if ((botToken === undefined || botToken.length <= 0) && (webhookUrl === undefined || webhookUrl.length <= 0)) {
throw new Error('Need to provide at least one botToken or webhookUrl');
}
let payload = core.getInput('payload');
const payloadFilePath = core.getInput('payload-file-path');
/** Option to replace templated context variables in the payload file JSON */
const payloadFilePathParsed = core.getBooleanInput('payload-file-path-parsed');
let webResponse;
if (payloadFilePath && !payload) {
try {
payload = await fs.readFile(path.resolve(payloadFilePath), 'utf-8');
if (payloadFilePathParsed) {
const context = { github: github.context, env: process.env };
const payloadString = payload.replaceAll('${{', '{{');
payload = markup.up(payloadString, context);
}
} catch (error) {
// passed in payload file path was invalid
console.error(error);
throw new Error(`The payload-file-path may be incorrect. Failed to load the file: ${payloadFilePath}`);
}
}
if (payload) {
try {
// confirm it is valid json
payload = JSON.parse(payload);
} catch (e) {
// passed in payload wasn't valid json
console.error('passed in payload was invalid JSON');
throw new Error('Need to provide valid JSON payload');
}
}
if (typeof botToken !== 'undefined' && botToken.length > 0) {
const message = core.getInput('slack-message') || '';
const channelIds = core.getInput('channel-id') || '';
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || '';
const web = createWebClient(botToken, httpsProxy);
if (channelIds.length <= 0) {
console.log('Channel ID is required to run this action. An empty one has been provided');
throw new Error('Channel ID is required to run this action. An empty one has been provided');
}
if (message.length > 0 || payload) {
const ts = core.getInput('update-ts');
await Promise.all(channelIds.split(',').map(async (channelId) => {
if (ts) {
// update message
webResponse = await web.chat.update({ ts, channel: channelId.trim(), text: message, ...(payload || {}) });
} else {
// post message
webResponse = await web.chat.postMessage({ channel: channelId.trim(), text: message, ...(payload || {}) });
}
}));
} else {
console.log('Missing slack-message or payload! Did not send a message via chat.postMessage with botToken', { channel: channelIds, text: message, ...(payload) });
throw new Error('Missing message content, please input a valid payload or message to send. No Message has been send.');
}
}
if (typeof webhookUrl !== 'undefined' && webhookUrl.length > 0) {
if (!payload) {
// No Payload was passed in
console.log('no custom payload was passed in, using default payload that triggered the GitHub Action');
// Get the JSON webhook payload for the event that triggered the workflow
payload = github.context.payload;
}
if (webhookType === SLACK_WEBHOOK_TYPES.WORKFLOW_TRIGGER) {
// flatten JSON payload (no nested attributes).
const payloadDelimiter = core.getInput('payload-delimiter') || '.';
const flatPayload = flatten(payload, { delimiter: payloadDelimiter });
// workflow builder requires values to be strings
// iterate over every value and convert it to string
Object.keys(flatPayload).forEach((key) => {
flatPayload[key] = `${flatPayload[key]}`;
});
payload = flatPayload;
}
const axiosOpts = {};
try {
if (parseURL(webhookUrl).scheme === 'https') {
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || '';
if (httpsProxy && parseURL(httpsProxy).scheme === 'http') {
const httpsProxyAgent = new HttpsProxyAgent(httpsProxy);
axiosOpts.httpsAgent = httpsProxyAgent;
// Use configured tunnel above instead of default axios proxy setup from env vars
axiosOpts.proxy = false;
}
}
} catch (err) {
console.log('failed to configure https proxy agent for http proxy. Using default axios configuration', err);
}
try {
await axios.post(webhookUrl, payload, axiosOpts);
} catch (err) {
console.log('axios post failed, double check the payload being sent includes the keys Slack expects');
if ('toJSON' in err) {
console.error(JSON.stringify(err.toJSON()));
}
console.error(`Attempted to POST payload: ${JSON.stringify(payload)}`);
if (err.response) {
core.setFailed(err.response.data);
} else {
core.setFailed(err.message);
}
return;
}
}
if (webResponse && webResponse.ok) {
core.setOutput('ts', webResponse.ts);
// return the thread_ts if it exists, if not return the ts
const thread_ts = webResponse.thread_ts ? webResponse.thread_ts : webResponse.ts;
core.setOutput('thread_ts', thread_ts);
// return id of the channel from the response
core.setOutput('channel_id', webResponse.channel);
}
const time = (new Date()).toTimeString();
core.setOutput('time', time);
} catch (error) {
core.setFailed(error);
}
};