-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslack.js
187 lines (163 loc) · 3.48 KB
/
slack.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict'
const fetch = require('node-fetch'),
{
getOption,
raiseForStatus,
getEnvNs,
channelFormatter,
} = require('../util'),
{
SLACK_WEBHOOK_URL,
SLACK_MESSAGE_KEY,
} = require('../constants'),
{ createLogger, logTrace } = require('../logger')
const logger = createLogger('slack'),
MRKDWN_ESCAPE_REGEX = /(\\&)|(\\<)|(\\>)/gui,
MRKDWN_ESCAPE_REPLACEMENTS = ['&', '<', '>'],
MESSAGE = `
{{ REPORT_NAME }}
\`\`\`
{{ RESULTS_CSV_FORMATTED }}
\`\`\`
`
function escapeMrkdwn(str) {
return str.replace(
MRKDWN_ESCAPE_REGEX,
(s, ...args) => {
for (let index = 0; index < args.length; index += 1) {
if (args[index]) {
return MRKDWN_ESCAPE_REPLACEMENTS[index]
}
}
return s
},
)
}
function formatText(
context,
report,
publishConfig,
channelConfig,
output,
) {
const message = getOption(
channelConfig,
SLACK_MESSAGE_KEY,
null,
MESSAGE,
)
return channelFormatter(
context,
report,
publishConfig,
channelConfig,
output,
)(message)
}
function buildHeaders(headers = {}) {
return {
'Content-Type': 'application/json',
...headers,
}
}
async function post(webhookUrl, message, options = { headers: {} }) {
logTrace(logger, log => {
log({ message }, 'Slack Request:')
})
const response = await fetch(webhookUrl, {
headers: buildHeaders(options.headers),
method: 'POST',
body: message,
})
raiseForStatus(response)
const responseText = await response.text()
logTrace(logger, log => {
log({ response: responseText }, 'Slack Response:')
})
if (responseText !== 'ok') {
logger.error(
`Slack Webhook post error: ${responseText}`,
)
throw new Error(
`Slack Webhook post error: ${responseText}`,
message,
responseText,
)
}
return responseText
}
async function buildMessage(
context,
report,
publishConfig,
channelConfig,
output,
) {
if (channelConfig.passThrough) {
return await output.render(context, report, channelConfig)
}
/*
* Set the message text to the rendered output with any special mrkdwn
* characters escaped.
*/
return JSON.stringify({
text: escapeMrkdwn(
formatText(
context,
report,
publishConfig,
channelConfig,
await output.render(
context,
report,
channelConfig,
),
),
),
})
}
async function postToSlack(
context,
manifest,
report,
publishConfig,
channelConfig,
output,
) {
const reportName = report.name || report.id
/*
* This channel only supports Slack webhooks, not the API. Files can not be
* posted via the webhook so if this is a file, warn and exit.
*/
if (output.isFile()) {
logger.warn(
`Skipping output for report ${reportName} because Slack webhook does not support file attachments.`,
)
return
}
/*
* Check to ensure we have a Slack webhook URL. We only support getting this
* from the environment because it has a key in it.
*/
const webhookUrl = getEnvNs(context, SLACK_WEBHOOK_URL)
if (!webhookUrl) {
throw new Error(`Missing Slack webhook URL for report ${reportName}.`)
}
/*
* Post the message to the Slack webhook URL.
*/
await post(
webhookUrl,
await buildMessage(
context,
report,
publishConfig,
channelConfig,
output,
),
)
}
module.exports = {
publish: postToSlack,
getChannelDefaults: () => ({}),
}