forked from DannyDainton/basic-newman-slack-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
311 lines (286 loc) · 11.6 KB
/
app.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const axios = require('axios')
const express = require('express')
const bodyParser = require('body-parser')
const prettyMs = require('pretty-ms')
const newman = require('newman')
const fs = require('fs')
const path = require('path')
const app = express()
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json({ limit: "50mb" }));
app.use(express.static('reports'));
const apikey = process.env.API_KEY
class TestRunContext {
constructor(newmanResult) {
this.collection = newmanResult.collection.name;
this.environment = newmanResult.environment.name
this.iterationCount = newmanResult.run.stats.iterations.total
this.start = new Date(newmanResult.run.timings.started)
this.end = new Date(newmanResult.run.timings.completed)
this.responseAverage = newmanResult.run.timings.responseAverage
this.totalRequests = newmanResult.run.stats.tests.total
this.testResultTotal = newmanResult.run.stats.assertions.total
this.testResultFailed = newmanResult.run.stats.assertions.failed
this.failures = newmanResult.run.failures
this.skipped = newmanResult.skippedTests
}
get percentagePassed() {
return (this.testResultTotal * 100 / (this.testResultTotal + this.testResultFailed)).toFixed(2)
}
get environmentName() {
return this.environment === undefined ? "No Environment specified for the Newman Run" : this.environment;
}
get collectionName() {
return this.collection === undefined ? "No Collection for the Newman Run" : this.collection;
}
get skippedList() {
if(this.skipped === undefined) {
return "No Skipped Tests"
}
else {
return this.skipped.reduce((accumulator, current) => `${accumulator} *${current.item.name}:* _${current.assertion}_\n\n`, '')
}
}
get failsList() {
return this.failures.length > 0
? this.failures.reduce((accumulator, current) => `${accumulator} *${current.error.name}:* ${current.source.name} - ${current.error.test} - _${current.error.message}_\n\n`, '')
: "No Test Failures"
}
get totalAssertions() {
if(this.skipped === undefined) {
return this.testResultTotal
}
else {
return this.testResultTotal - this.skipped.length
}
}
get result() {
return this.testResultFailed > 0 ? "Failed" : "Passed"
}
get runDuration() {
return prettyMs(this.end - this.start)
}
get colour() {
return this.testResultFailed > 0 ? "danger" : "good"
}
get averageResponseTime() {
return prettyMs(this.responseAverage, {msDecimalDigits: 2})
}
get slackData() {
return {
"response_type": "in_channel",
"attachments": [
{
"fallback": "Newman Run Summary",
"color": `${this.colour}`,
"title": "Summary Test Result",
"title_link": "http://newman-app.serverless.social/htmlResults.html",
"text": `Collection : *${this.collectionName}* \nEnvironment : *${this.environmentName}*\nTotal Run Duration: *${this.runDuration}*`,
"mrkdwn": true,
"fields": [
{
"title": "No. Of Iterations ",
"value": `${this.iterationCount}`,
"short": true
},
{
"title": "No. Of Requests",
"value": `${this.totalRequests}`,
"short": true
},
{
"title": "No. Of Assertions",
"value": `${this.totalAssertions}`,
"short": true
},
{
"title": "No. Of Failures",
"value": `${this.testResultFailed}`,
"short": true
},
{
"title": "Av. Response Time",
"value": `${this.averageResponseTime}`,
"short": true
},
{
"title": "Result",
"value": `${this.result}`,
"short": true
},
{
"title": "Skipped Tests",
"value": `${this.skippedList}`
},
{
"title": "Test Failures",
"value": `${this.failsList}`
}
]
}
]
}
}
}
let getCollectionUid = collectionName => {
const errorMessage = 'The Collection name provided is not found in your workspace. Please try again';
return new Promise((resolve, reject) => {
axios({
method: "GET",
url: `https://api.getpostman.com/collections/?apikey=${apikey}`
})
.then(response => {
collection = response.data.collections.find(collection => collection.name === collectionName);
collection != undefined ? resolve(collection.uid) : reject(errorMessage);
})
.catch(error => {
reject(error);
});
});
};
let getEnvironmentUid = environmentName => {
const errorMessage = 'The Environment name provided is not found in your workspace. Please try again';
return new Promise((resolve, reject) => {
axios({
method: "get",
url:
`https://api.getpostman.com/environments/?apikey=${apikey}`
})
.then(response => {
environment = response.data.environments.find( environment => environment.name === environmentName);
environment != undefined ? resolve(environment.uid) : reject(errorMessage);
})
.catch(error => {
reject(error);
});
});
};
let executeNewman = (collectionUid, environmentUid, iterationCount) => {
return new Promise((resolve, reject) => {
newman.run({
collection: `https://api.getpostman.com/collections/${collectionUid}?apikey=${apikey}`,
environment: `https://api.getpostman.com/environments/${environmentUid}?apikey=${apikey}`,
iterationCount: iterationCount,
reporters: ['cli', 'htmlextra'],
reporter: {
htmlextra: {
export: './reports/htmlResults.html'
}
}
}, (err, summary) => {
if (err) {
return reject(err)
}
resolve(summary)
})
})
}
function InvalidArgument(responseURL, argName, message, res) {
axios({
method: 'post',
url: `${responseURL}`,
headers: { 'Content-Type': 'application/json' },
data: {
'response_type': 'in_channel',
'attachments': [
{
'color': 'danger',
'title': `Invalid ${argName}`,
'mrkdwn': true,
'fields': [
{
'value': `${message}`
}
]
}
]
}
})
.then(res.status(202).end());
return;
}
app.post("/newmanRun", (req, res) => {
const responseURL = req.body.response_url
const channelText = req.body.text
const collectionName = channelText.split(" & ")[0];
const environmentName = channelText.split(" & ")[1];
const iterationCount = parseInt(channelText.split(" & ")[2]);
if (channelText.length === 0) {
message = "Please enter a valid *Command* .";
return InvalidArgument(responseURL,'Slack command', message, res);
} else if (collectionName === undefined || environmentName === undefined) {
message = `Could not find the Collection or the Environment in your command. Please try again.`;
return InvalidArgument(responseURL, 'Slack command', message, res);
}
getCollectionUid(collectionName)
.then(collectionUid => {
getEnvironmentUid(environmentName)
.then(environmentUid => {
axios({
'method': "post",
'url': `${responseURL}`,
'headers': { "Content-Type": "application/json" },
'data': {
'response_type': "in_channel",
'attachments': [
{
'color': "good",
'title': "Newman Test Run Started",
'mrkdwn': true,
'fields': [
{
'value': `Your Summary Report for the collection *${collectionName}* in *${environmentName}* environment will be with you _very_ soon`
}
]
}
]
}
})
.then(res.status(202).end())
.then(() => {
executeNewman(collectionUid, environmentUid, iterationCount)
.then(newmanResult => {
return new TestRunContext(newmanResult);
})
.then(context => {
return axios({
'method': "post",
'url': `${responseURL}`,
'headers': { "Content-Type": "application/json" },
'data': context.slackData
});
})
.catch(err => {
axios({
method: "post",
url: `${responseURL}`,
headers: { "Content-Type": "application/json" },
data: {
response_type: "in_channel",
attachments: [
{
color: "danger",
title: "Newman Run Error",
fields: [
{
value: `${err}`
}
]
}
]
}
});
});
});
})
.catch(error => {
InvalidArgument(responseURL, "Environment", error, res);
});
})
.catch(error => {
InvalidArgument(responseURL, "Collection", error, res);
});
})
const port = Number(process.env.PORT || 3000)
app.listen(port)
console.log(`Server Started on port: ${port}`)