forked from jackcoded/newman-reporter-slackmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (45 loc) · 1.7 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
const {
slackUtils
} = require('./slackUtils');
function SlackNewmanReporter(emitter, reporterOptions) {
if (missingReporterOptions(reporterOptions)) {
return;
}
const webhookUrl = reporterOptions.webhookurl;
const messageSize = reporterOptions.messageSize || 100;
const collection = reporterOptions.collection || '';
const environment = reporterOptions.environment || '';
const token = reporterOptions.token || '';
const reportingUrl = reporterOptions.reportingurl || '';
let channel = reporterOptions.channel || '';
emitter.on('done', (error, summary) => {
if (error) {
console.error('error in done')
return;
}
let run = summary.run;
if (run.failures.length > 0 && reporterOptions.failuresChannel) {
channel = reporterOptions.failuresChannel;
}
slackUtils.send(webhookUrl, slackUtils.slackMessage(run.stats, run.timings, run.failures, run.executions, messageSize, collection, environment, channel, reportingUrl), token);
});
function missingReporterOptions(reporterOptions) {
let missing = false;
if (!reporterOptions.webhookurl) {
console.error('Missing Slack Webhook Url');
missing = true;
}
if (reporterOptions.webhookurl === 'https://slack.com/api/chat.postMessage') {
if (!reporterOptions.token) {
console.error('Missing Bearer Token');
missing = true;
}
if (!reporterOptions.channel) {
console.error('Missing channel');
missing = true;
}
}
return missing;
}
}
module.exports = SlackNewmanReporter