@reportportal/client-javascript 5.0.5 → 5.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,12 +5,12 @@ const helpers = require('./helpers');
5
5
  const RestClient = require('./rest');
6
6
  const Analytics = require('../analytics/analytics');
7
7
  const { RP_STATUSES } = require('./constants/statuses');
8
- const { CLIENT_JAVASCRIPT_EVENTS, getAgentEventLabel } = require('./../analytics/events');
8
+ const { CLIENT_JAVASCRIPT_EVENTS, getAgentEventLabel } = require('../analytics/events');
9
9
 
10
10
  const MULTIPART_BOUNDARY = Math.floor(Math.random() * 10000000000).toString();
11
11
 
12
12
  class RPClient {
13
- /**
13
+ /**
14
14
  * Create a client for RP.
15
15
  * @param {Object} params - config object.
16
16
  * params should look like this
@@ -28,103 +28,104 @@ class RPClient {
28
28
  * version: "AGENT VERSION",
29
29
  * }
30
30
  */
31
- constructor(params, agentParams) {
32
- this.debug = params.debug;
33
- this.isLaunchMergeRequired = params.isLaunchMergeRequired === undefined ? false : params.isLaunchMergeRequired;
34
- this.map = {};
35
- this.baseURL = [params.endpoint, params.project].join('/');
36
- const headers = Object.assign({
37
- 'User-Agent': 'NodeJS',
38
- Authorization: `bearer ${params.token}`,
39
- }, params.headers || {});
40
- this.headers = headers;
41
- this.token = params.token;
42
- this.config = params;
43
- this.helpers = helpers;
44
- this.restClient = new RestClient({
45
- baseURL: this.baseURL,
46
- headers: this.headers,
47
- restClientConfig: params.restClientConfig,
48
- });
49
- this.analytics = new Analytics(agentParams);
50
- this.launchUuid = '';
51
- this.nonRetriedItemMap = new Map();
52
- }
53
-
54
- // eslint-disable-next-line valid-jsdoc
55
- /**
31
+ constructor(params, agentParams) {
32
+ this.debug = params.debug;
33
+ this.isLaunchMergeRequired = params.isLaunchMergeRequired === undefined ? false : params.isLaunchMergeRequired;
34
+ this.map = {};
35
+ this.baseURL = [params.endpoint, params.project].join('/');
36
+ const headers = {
37
+ 'User-Agent': 'NodeJS',
38
+ Authorization: `bearer ${params.token}`,
39
+ ...(params.headers || {}),
40
+ };
41
+ this.headers = headers;
42
+ this.token = params.token;
43
+ this.config = params;
44
+ this.helpers = helpers;
45
+ this.restClient = new RestClient({
46
+ baseURL: this.baseURL,
47
+ headers: this.headers,
48
+ restClientConfig: params.restClientConfig,
49
+ });
50
+ this.analytics = new Analytics(agentParams);
51
+ this.launchUuid = '';
52
+ this.nonRetriedItemMap = new Map();
53
+ }
54
+
55
+ // eslint-disable-next-line valid-jsdoc
56
+ /**
56
57
  *
57
58
  * @Private
58
59
  */
59
- logDebug(msg) {
60
- if (this.debug) {
61
- console.log(msg);
62
- }
63
- }
64
-
65
- calculateNonRetriedItemMapKey(launchId, parentId, name, itemId = '') {
66
- return `${launchId}__${parentId}__${name}__${itemId}`;
67
- }
68
-
69
- getUniqId() {
70
- return UniqId();
71
- }
72
-
73
- getRejectAnswer(tempId, error) {
74
- return {
75
- tempId,
76
- promise: Promise.reject(error),
77
- };
78
- }
79
-
80
- getNewItemObj(startPromiseFunc) {
81
- let resolveFinish;
82
- let rejectFinish;
83
- const obj = {
84
- promiseStart: (new Promise(startPromiseFunc)),
85
- realId: '',
86
- children: [],
87
- finishSend: false,
88
- promiseFinish: (new Promise((resolve, reject) => {
89
- resolveFinish = resolve;
90
- rejectFinish = reject;
91
- })),
92
- };
93
- obj.resolveFinish = resolveFinish;
94
- obj.rejectFinish = rejectFinish;
95
- return obj;
60
+ logDebug(msg) {
61
+ if (this.debug) {
62
+ console.log(msg);
96
63
  }
97
-
98
- // eslint-disable-next-line valid-jsdoc
99
- /**
64
+ }
65
+
66
+ calculateNonRetriedItemMapKey(launchId, parentId, name, itemId = '') {
67
+ return `${launchId}__${parentId}__${name}__${itemId}`;
68
+ }
69
+
70
+ getUniqId() {
71
+ return UniqId();
72
+ }
73
+
74
+ getRejectAnswer(tempId, error) {
75
+ return {
76
+ tempId,
77
+ promise: Promise.reject(error),
78
+ };
79
+ }
80
+
81
+ getNewItemObj(startPromiseFunc) {
82
+ let resolveFinish;
83
+ let rejectFinish;
84
+ const obj = {
85
+ promiseStart: (new Promise(startPromiseFunc)),
86
+ realId: '',
87
+ children: [],
88
+ finishSend: false,
89
+ promiseFinish: (new Promise((resolve, reject) => {
90
+ resolveFinish = resolve;
91
+ rejectFinish = reject;
92
+ })),
93
+ };
94
+ obj.resolveFinish = resolveFinish;
95
+ obj.rejectFinish = rejectFinish;
96
+ return obj;
97
+ }
98
+
99
+ // eslint-disable-next-line valid-jsdoc
100
+ /**
100
101
  *
101
102
  * @Private
102
103
  */
103
- cleanMap(ids) {
104
- ids.forEach((id) => {
105
- delete this.map[id];
106
- });
104
+ cleanMap(ids) {
105
+ ids.forEach((id) => {
106
+ delete this.map[id];
107
+ });
108
+ }
109
+
110
+ checkConnect() {
111
+ const url = [this.config.endpoint.replace('/v2', '/v1'), 'user'].join('/');
112
+ return RestClient.request('GET', url, {}, { headers: this.headers });
113
+ }
114
+
115
+ triggerAnalyticsEvent() {
116
+ if (process.env.REPORTPORTAL_CLIENT_JS_NO_ANALYTICS) {
117
+ return;
107
118
  }
108
119
 
109
- checkConnect() {
110
- const url = [this.config.endpoint.replace('/v2', '/v1'), 'user'].join('/');
111
- return RestClient.request('GET', url, {}, { headers: this.headers });
120
+ if (this.analytics.agentParams) {
121
+ const label = getAgentEventLabel(this.analytics.agentParams);
122
+ this.analytics.trackEvent(Object.assign(CLIENT_JAVASCRIPT_EVENTS.START_LAUNCH, { label }));
123
+ } else {
124
+ this.analytics.trackEvent(CLIENT_JAVASCRIPT_EVENTS.START_LAUNCH);
112
125
  }
126
+ }
113
127
 
114
- triggerAnalyticsEvent() {
115
- if (process.env.REPORTPORTAL_CLIENT_JS_NO_ANALYTICS) {
116
- return;
117
- }
118
-
119
- if (this.analytics.agentParams) {
120
- const label = getAgentEventLabel(this.analytics.agentParams);
121
- this.analytics.trackEvent(Object.assign(CLIENT_JAVASCRIPT_EVENTS.START_LAUNCH, { label }));
122
- } else {
123
- this.analytics.trackEvent(CLIENT_JAVASCRIPT_EVENTS.START_LAUNCH);
124
- }
125
- }
126
-
127
- /**
128
+ /**
128
129
  * Start launch and report it.
129
130
  * @param {Object} launchDataRQ - request object.
130
131
  * launchDataRQ should look like this
@@ -163,51 +164,52 @@ class RPClient {
163
164
  * or it would throw an error.
164
165
  * @Returns {Object} - an object which contains a tempID and a promise
165
166
  */
166
- startLaunch(launchDataRQ) {
167
- const tempId = this.getUniqId();
168
-
169
- if (launchDataRQ.id) {
170
- this.map[tempId] = this.getNewItemObj(resolve => resolve(launchDataRQ));
171
- this.map[tempId].realId = launchDataRQ.id;
172
- this.launchUuid = launchDataRQ.id;
173
- } else {
174
- const systemAttr = helpers.getSystemAttribute();
175
- const attributes = Array.isArray(launchDataRQ.attributes)
176
- ? launchDataRQ.attributes.concat(systemAttr)
177
- : systemAttr;
178
- const launchData = Object.assign(
179
- { name: this.config.launch || 'Test launch name', startTime: this.helpers.now() },
180
- launchDataRQ,
181
- { attributes },
182
- );
183
-
184
- this.map[tempId] = this.getNewItemObj((resolve, reject) => {
185
- const url = 'launch';
186
- this.logDebug(`Start launch ${tempId}`);
187
- this.restClient.create(url, launchData, { headers: this.headers })
188
- .then((response) => {
189
- this.map[tempId].realId = response.id;
190
- this.launchUuid = response.id;
191
-
192
- if (this.isLaunchMergeRequired) {
193
- helpers.saveLaunchIdToFile(response.id);
194
- }
195
-
196
- resolve(response);
197
- }, (error) => {
198
- console.dir(error);
199
- reject(error);
200
- });
201
- });
202
- }
203
- this.triggerAnalyticsEvent();
204
- return {
205
- tempId,
206
- promise: this.map[tempId].promiseStart,
207
- };
208
- }
167
+ startLaunch(launchDataRQ) {
168
+ const tempId = this.getUniqId();
169
+
170
+ if (launchDataRQ.id) {
171
+ this.map[tempId] = this.getNewItemObj((resolve) => resolve(launchDataRQ));
172
+ this.map[tempId].realId = launchDataRQ.id;
173
+ this.launchUuid = launchDataRQ.id;
174
+ } else {
175
+ const systemAttr = helpers.getSystemAttribute();
176
+ const attributes = Array.isArray(launchDataRQ.attributes)
177
+ ? launchDataRQ.attributes.concat(systemAttr)
178
+ : systemAttr;
179
+ const launchData = {
180
+ name: this.config.launch || 'Test launch name',
181
+ startTime: this.helpers.now(),
182
+ ...launchDataRQ,
183
+ attributes,
184
+ };
185
+
186
+ this.map[tempId] = this.getNewItemObj((resolve, reject) => {
187
+ const url = 'launch';
188
+ this.logDebug(`Start launch ${tempId}`);
189
+ this.restClient.create(url, launchData, { headers: this.headers })
190
+ .then((response) => {
191
+ this.map[tempId].realId = response.id;
192
+ this.launchUuid = response.id;
193
+
194
+ if (this.isLaunchMergeRequired) {
195
+ helpers.saveLaunchIdToFile(response.id);
196
+ }
209
197
 
210
- /**
198
+ resolve(response);
199
+ }, (error) => {
200
+ console.dir(error);
201
+ reject(error);
202
+ });
203
+ });
204
+ }
205
+ this.triggerAnalyticsEvent();
206
+ return {
207
+ tempId,
208
+ promise: this.map[tempId].promiseStart,
209
+ };
210
+ }
211
+
212
+ /**
211
213
  * Finish launch.
212
214
  * @param {string} launchTempId - temp launch id (returned in the query "startLaunch").
213
215
  * @param {Object} finishExecutionRQ - finish launch info should include time and status.
@@ -218,112 +220,111 @@ class RPClient {
218
220
  * }
219
221
  * @Returns {Object} - an object which contains a tempID and a promise
220
222
  */
221
- finishLaunch(launchTempId, finishExecutionRQ) {
222
- const launchObj = this.map[launchTempId];
223
- if (!launchObj) {
224
- return this.getRejectAnswer(launchTempId, new Error(`Launch "${launchTempId}" not found`));
225
- }
226
-
227
- const finishExecutionData = Object.assign({ endTime: this.helpers.now() }, finishExecutionRQ);
228
-
229
- launchObj.finishSend = true;
230
- Promise.all(launchObj.children.map(itemId => this.map[itemId].promiseFinish))
231
- .then(() => {
232
- launchObj.promiseStart
233
- .then(() => {
234
- this.logDebug(`Finish launch ${launchTempId}`);
235
- const url = ['launch', launchObj.realId, 'finish'].join('/');
236
- this.restClient.update(url, finishExecutionData, { headers: this.headers })
237
- .then((response) => {
238
- this.logDebug(`Success finish launch ${launchTempId}`);
239
- launchObj.resolveFinish(response);
240
- }, (error) => {
241
- this.logDebug(`Error finish launch ${launchTempId}`);
242
- console.dir(error);
243
- launchObj.rejectFinish(error);
244
- });
245
- }, (error) => {
246
- launchObj.rejectFinish(error);
247
- });
248
- }, (error) => {
249
- launchObj.rejectFinish(error);
250
- });
251
-
252
- return {
253
- tempId: launchTempId,
254
- promise: launchObj.promiseFinish,
255
- };
223
+ finishLaunch(launchTempId, finishExecutionRQ) {
224
+ const launchObj = this.map[launchTempId];
225
+ if (!launchObj) {
226
+ return this.getRejectAnswer(launchTempId, new Error(`Launch "${launchTempId}" not found`));
256
227
  }
257
228
 
258
- /*
229
+ const finishExecutionData = { endTime: this.helpers.now(), ...finishExecutionRQ };
230
+
231
+ launchObj.finishSend = true;
232
+ Promise.all(launchObj.children.map((itemId) => this.map[itemId].promiseFinish))
233
+ .then(() => {
234
+ launchObj.promiseStart
235
+ .then(() => {
236
+ this.logDebug(`Finish launch ${launchTempId}`);
237
+ const url = ['launch', launchObj.realId, 'finish'].join('/');
238
+ this.restClient.update(url, finishExecutionData, { headers: this.headers })
239
+ .then((response) => {
240
+ this.logDebug(`Success finish launch ${launchTempId}`);
241
+ launchObj.resolveFinish(response);
242
+ }, (error) => {
243
+ this.logDebug(`Error finish launch ${launchTempId}`);
244
+ console.dir(error);
245
+ launchObj.rejectFinish(error);
246
+ });
247
+ }, (error) => {
248
+ launchObj.rejectFinish(error);
249
+ });
250
+ }, (error) => {
251
+ launchObj.rejectFinish(error);
252
+ });
253
+
254
+ return {
255
+ tempId: launchTempId,
256
+ promise: launchObj.promiseFinish,
257
+ };
258
+ }
259
+
260
+ /*
259
261
  * This method is used to create data object for merge request to ReportPortal.
260
262
  *
261
263
  * @Returns {Object} - an object which contains a data for merge launches in ReportPortal.
262
264
  */
263
- getMergeLaunchesRequest(launchIds) {
264
- return {
265
- description: "Merged launch",
266
- endTime: this.helpers.now(),
267
- extendSuitesDescription: true,
268
- launches: launchIds,
269
- mergeType: "BASIC",
270
- mode: "DEFAULT",
271
- name: this.config.launch || 'Test launch name',
272
- attributes: this.config.attributes,
273
- };
274
- }
275
-
276
- /*
265
+ getMergeLaunchesRequest(launchIds) {
266
+ return {
267
+ description: "Merged launch",
268
+ endTime: this.helpers.now(),
269
+ extendSuitesDescription: true,
270
+ launches: launchIds,
271
+ mergeType: "BASIC",
272
+ mode: "DEFAULT",
273
+ name: this.config.launch || 'Test launch name',
274
+ attributes: this.config.attributes,
275
+ };
276
+ }
277
+
278
+ /*
277
279
  * This method is used for merge launches in ReportPortal.
278
280
  *
279
281
  * Please, keep in mind that this method is work only in case
280
282
  * the option isLaunchMergeRequired is true.
281
283
  */
282
- mergeLaunches() {
283
- if (this.isLaunchMergeRequired) {
284
- const launchUUIds = helpers.readLaunchesFromFile();
285
- const params = new URLSearchParams({
286
- 'filter.in.uuid': launchUUIds,
287
- });
288
- const launchSearchUrl = `launch?${params.toString()}`;
289
- return this.restClient.retrieveSyncAPI(launchSearchUrl, { headers: this.headers })
290
- .then((response) => {
291
- const launchIds = response.content.map(launch => launch.id);
292
- this.logDebug(`Found launches: ${launchIds}`);
293
- return launchIds;
294
- }, (error) => {
295
- console.dir(error);
296
- })
297
- .then((launchIds) => {
298
- const request = this.getMergeLaunchesRequest(launchIds);
299
- const mergeURL = 'launch/merge';
300
- return this.restClient.create(mergeURL, request, { headers: this.headers });
301
- })
302
- .then(() => {
303
- this.logDebug(`Launches successfully merged!`);
304
- })
305
- .catch((error) => {
306
- this.logDebug('ERROR');
307
- this.logDebug(error);
308
- });
309
- }
310
- this.logDebug('Option isLaunchMergeRequired is false');
284
+ mergeLaunches() {
285
+ if (this.isLaunchMergeRequired) {
286
+ const launchUUIds = helpers.readLaunchesFromFile();
287
+ const params = new URLSearchParams({
288
+ 'filter.in.uuid': launchUUIds,
289
+ });
290
+ const launchSearchUrl = `launch?${params.toString()}`;
291
+ return this.restClient.retrieveSyncAPI(launchSearchUrl, { headers: this.headers })
292
+ .then((response) => {
293
+ const launchIds = response.content.map((launch) => launch.id);
294
+ this.logDebug(`Found launches: ${launchIds}`);
295
+ return launchIds;
296
+ }, (error) => {
297
+ console.dir(error);
298
+ })
299
+ .then((launchIds) => {
300
+ const request = this.getMergeLaunchesRequest(launchIds);
301
+ const mergeURL = 'launch/merge';
302
+ return this.restClient.create(mergeURL, request, { headers: this.headers });
303
+ })
304
+ .then(() => {
305
+ this.logDebug(`Launches successfully merged!`);
306
+ })
307
+ .catch((error) => {
308
+ this.logDebug('ERROR');
309
+ this.logDebug(error);
310
+ });
311
311
  }
312
+ this.logDebug('Option isLaunchMergeRequired is false');
313
+ }
312
314
 
313
- /*
315
+ /*
314
316
  * This method is used for frameworks as Jasmine. There is problem when
315
317
  * it doesn't wait for promise resolve and stop the process. So it better to call
316
318
  * this method at the spec's function as @afterAll() and manually resolve this promise.
317
319
  *
318
320
  * @return Promise
319
321
  */
320
- getPromiseFinishAllItems(launchTempId) {
321
- const launchObj = this.map[launchTempId];
322
- return Promise.all(launchObj.children.map(itemId => this.map[itemId].promiseFinish));
323
- }
324
-
322
+ getPromiseFinishAllItems(launchTempId) {
323
+ const launchObj = this.map[launchTempId];
324
+ return Promise.all(launchObj.children.map((itemId) => this.map[itemId].promiseFinish));
325
+ }
325
326
 
326
- /**
327
+ /**
327
328
  * Update launch.
328
329
  * @param {string} launchTempId - temp launch id (returned in the query "startLaunch").
329
330
  * @param {Object} launchData - new launch data
@@ -343,37 +344,36 @@ class RPClient {
343
344
  }
344
345
  * @Returns {Object} - an object which contains a tempId and a promise
345
346
  */
346
- updateLaunch(launchTempId, launchData) {
347
- const launchObj = this.map[launchTempId];
348
- if (!launchObj) {
349
- return this.getRejectAnswer(launchTempId, new Error(`Launch "${launchTempId}" not found`));
350
- }
351
- let resolvePromise;
352
- let rejectPromise;
353
- const promise = new Promise((resolve, reject) => {
354
- resolvePromise = resolve;
355
- rejectPromise = reject;
356
- });
357
-
358
- launchObj.promiseFinish.then(() => {
359
- const url = ['launch', launchObj.realId, 'update'].join('/');
360
- this.restClient.update(url, launchData, { headers: this.headers })
361
- .then((response) => {
362
- resolvePromise(response);
363
- }, (error) => {
364
- rejectPromise(error);
365
- });
347
+ updateLaunch(launchTempId, launchData) {
348
+ const launchObj = this.map[launchTempId];
349
+ if (!launchObj) {
350
+ return this.getRejectAnswer(launchTempId, new Error(`Launch "${launchTempId}" not found`));
351
+ }
352
+ let resolvePromise;
353
+ let rejectPromise;
354
+ const promise = new Promise((resolve, reject) => {
355
+ resolvePromise = resolve;
356
+ rejectPromise = reject;
357
+ });
358
+
359
+ launchObj.promiseFinish.then(() => {
360
+ const url = ['launch', launchObj.realId, 'update'].join('/');
361
+ this.restClient.update(url, launchData, { headers: this.headers })
362
+ .then((response) => {
363
+ resolvePromise(response);
366
364
  }, (error) => {
367
- rejectPromise(error);
365
+ rejectPromise(error);
368
366
  });
369
- return {
370
- tempId: launchTempId,
371
- promise,
372
- };
373
- }
374
-
375
-
376
- /**
367
+ }, (error) => {
368
+ rejectPromise(error);
369
+ });
370
+ return {
371
+ tempId: launchTempId,
372
+ promise,
373
+ };
374
+ }
375
+
376
+ /**
377
377
  * If there is no parentItemId starts Suite, else starts test or item.
378
378
  * @param {Object} testItemDataRQ - object with item parameters
379
379
  * testItemDataRQ should look like this
@@ -400,83 +400,81 @@ class RPClient {
400
400
  * @param {string} parentTempId (optional) - temp item id (returned in the query "startTestItem").
401
401
  * @Returns {Object} - an object which contains a tempId and a promise
402
402
  */
403
- startTestItem(testItemDataRQ, launchTempId, parentTempId) {
404
- let parentMapId = launchTempId;
405
- const launchObj = this.map[launchTempId];
406
- if (!launchObj) {
407
- return this.getRejectAnswer(launchTempId, new Error(`Launch "${launchTempId}" not found`));
408
- }
409
- if (launchObj.finishSend) {
410
- const err = new Error(`Launch "${launchTempId}" is already finished, you can not add an item to it`);
411
- return this.getRejectAnswer(launchTempId, err);
412
- }
403
+ startTestItem(testItemDataRQ, launchTempId, parentTempId) {
404
+ let parentMapId = launchTempId;
405
+ const launchObj = this.map[launchTempId];
406
+ if (!launchObj) {
407
+ return this.getRejectAnswer(launchTempId, new Error(`Launch "${launchTempId}" not found`));
408
+ }
409
+ if (launchObj.finishSend) {
410
+ const err = new Error(`Launch "${launchTempId}" is already finished, you can not add an item to it`);
411
+ return this.getRejectAnswer(launchTempId, err);
412
+ }
413
413
 
414
- const testCaseId = testItemDataRQ.testCaseId
414
+ const testCaseId = testItemDataRQ.testCaseId
415
415
  || helpers.generateTestCaseId(testItemDataRQ.codeRef, testItemDataRQ.parameters);
416
- const testItemData = Object.assign({
417
- startTime: this.helpers.now(),
418
- }, testItemDataRQ, testCaseId && { testCaseId });
419
-
420
- let parentPromise = launchObj.promiseStart;
421
- if (parentTempId) {
422
- parentMapId = parentTempId;
423
- const parentObj = this.map[parentTempId];
424
- if (!parentObj) {
425
- return this.getRejectAnswer(launchTempId, new Error(`Item "${parentTempId}" not found`));
426
- }
427
- parentPromise = parentObj.promiseStart;
428
- }
429
-
430
- const itemKey = this.calculateNonRetriedItemMapKey(
431
- launchTempId,
432
- parentTempId,
433
- testItemDataRQ.name,
434
- testItemDataRQ.uniqueId,
435
- );
436
- const firstNonRetriedItemPromise = testItemDataRQ.retry && this.nonRetriedItemMap.get(itemKey);
437
- if (firstNonRetriedItemPromise) {
438
- parentPromise = Promise.all([parentPromise, firstNonRetriedItemPromise]);
439
- }
416
+ const testItemData = { startTime: this.helpers.now(), ...testItemDataRQ, ...(testCaseId && { testCaseId }) };
417
+
418
+ let parentPromise = launchObj.promiseStart;
419
+ if (parentTempId) {
420
+ parentMapId = parentTempId;
421
+ const parentObj = this.map[parentTempId];
422
+ if (!parentObj) {
423
+ return this.getRejectAnswer(launchTempId, new Error(`Item "${parentTempId}" not found`));
424
+ }
425
+ parentPromise = parentObj.promiseStart;
426
+ }
440
427
 
441
- const tempId = this.getUniqId();
442
- this.map[tempId] = this.getNewItemObj((resolve, reject) => {
443
- parentPromise.then(() => {
444
- const realLaunchId = this.map[launchTempId].realId;
445
- let url = 'item/';
446
- if (parentTempId) {
447
- const realParentId = this.map[parentTempId].realId;
448
- url += `${realParentId}`;
449
- }
450
- testItemData.launchUuid = realLaunchId;
451
- this.logDebug(`Start test item ${tempId}`);
452
- this.restClient.create(url, testItemData, { headers: this.headers })
453
- .then((response) => {
454
- this.logDebug(`Success start item ${tempId}`);
455
- this.map[tempId].realId = response.id;
456
- this.nonRetriedItemMap.delete(itemKey);
457
- resolve(response);
458
- }, (error) => {
459
- this.logDebug(`Error start item ${tempId}:`);
460
- console.dir(error);
461
- reject(error);
462
- });
463
- }, (error) => {
464
- reject(error);
465
- });
466
- });
467
- this.map[parentMapId].children.push(tempId);
428
+ const itemKey = this.calculateNonRetriedItemMapKey(
429
+ launchTempId,
430
+ parentTempId,
431
+ testItemDataRQ.name,
432
+ testItemDataRQ.uniqueId,
433
+ );
434
+ const firstNonRetriedItemPromise = testItemDataRQ.retry && this.nonRetriedItemMap.get(itemKey);
435
+ if (firstNonRetriedItemPromise) {
436
+ parentPromise = Promise.all([parentPromise, firstNonRetriedItemPromise]);
437
+ }
468
438
 
469
- if (!testItemDataRQ.retry) {
470
- this.nonRetriedItemMap.set(itemKey, this.map[tempId].promiseStart);
439
+ const tempId = this.getUniqId();
440
+ this.map[tempId] = this.getNewItemObj((resolve, reject) => {
441
+ parentPromise.then(() => {
442
+ const realLaunchId = this.map[launchTempId].realId;
443
+ let url = 'item/';
444
+ if (parentTempId) {
445
+ const realParentId = this.map[parentTempId].realId;
446
+ url += `${realParentId}`;
471
447
  }
472
-
473
- return {
474
- tempId,
475
- promise: this.map[tempId].promiseStart,
476
- };
448
+ testItemData.launchUuid = realLaunchId;
449
+ this.logDebug(`Start test item ${tempId}`);
450
+ this.restClient.create(url, testItemData, { headers: this.headers })
451
+ .then((response) => {
452
+ this.logDebug(`Success start item ${tempId}`);
453
+ this.map[tempId].realId = response.id;
454
+ this.nonRetriedItemMap.delete(itemKey);
455
+ resolve(response);
456
+ }, (error) => {
457
+ this.logDebug(`Error start item ${tempId}:`);
458
+ console.dir(error);
459
+ reject(error);
460
+ });
461
+ }, (error) => {
462
+ reject(error);
463
+ });
464
+ });
465
+ this.map[parentMapId].children.push(tempId);
466
+
467
+ if (!testItemDataRQ.retry) {
468
+ this.nonRetriedItemMap.set(itemKey, this.map[tempId].promiseStart);
477
469
  }
478
470
 
479
- /**
471
+ return {
472
+ tempId,
473
+ promise: this.map[tempId].promiseStart,
474
+ };
475
+ }
476
+
477
+ /**
480
478
  * Finish Suite or Step level.
481
479
  * @param {string} itemTempId - temp item id (returned in the query "startTestItem").
482
480
  * @param {Object} finishTestItemRQ - object with item parameters.
@@ -500,80 +498,87 @@ class RPClient {
500
498
  }
501
499
  * @Returns {Object} - an object which contains a tempId and a promise
502
500
  */
503
- finishTestItem(itemTempId, finishTestItemRQ) {
504
- const itemObj = this.map[itemTempId];
505
- if (!itemObj) {
506
- return this.getRejectAnswer(itemTempId, new Error(`Item "${itemTempId}" not found`));
507
- }
508
-
509
- const finishTestItemData = Object.assign({
510
- endTime: this.helpers.now(),
511
- }, itemObj.children.length ? {} : { status: RP_STATUSES.PASSED }, finishTestItemRQ);
512
-
513
- itemObj.finishSend = true;
514
- Promise.all(itemObj.children.map(itemId => this.map[itemId] && this.map[itemId].promiseFinish))
515
- .then(() => {
516
- this.cleanMap(itemObj.children);
517
- this.finishTestItemPromiseStart(itemObj,
518
- itemTempId,
519
- Object.assign(finishTestItemData, { launchUuid: this.launchUuid }));
520
- }, () => {
521
- this.cleanMap(itemObj.children);
522
- this.logDebug(`Error finish children of test item ${itemTempId}`);
523
- this.logDebug(`Finish test item ${itemTempId}`);
524
- this.finishTestItemPromiseStart(itemObj,
525
- itemTempId,
526
- Object.assign(finishTestItemData, { launchUuid: this.launchUuid }));
527
- });
528
-
529
- return {
530
- tempId: itemTempId,
531
- promise: itemObj.promiseFinish,
532
- };
501
+ finishTestItem(itemTempId, finishTestItemRQ) {
502
+ const itemObj = this.map[itemTempId];
503
+ if (!itemObj) {
504
+ return this.getRejectAnswer(itemTempId, new Error(`Item "${itemTempId}" not found`));
533
505
  }
534
506
 
535
- saveLog(itemObj, requestPromiseFunc) {
536
- const tempId = this.getUniqId();
537
- this.map[tempId] = this.getNewItemObj((resolve, reject) => {
538
- itemObj.promiseStart.then(() => {
539
- this.logDebug(`Save log ${tempId}`);
540
- requestPromiseFunc(itemObj.realId, this.launchUuid).then((response) => {
541
- this.logDebug(`Successfully save log ${tempId}`);
542
- resolve(response);
543
- }, (error) => {
544
- this.logDebug(`Error finish log: ${error}`);
545
- reject(error);
546
- });
547
- }, (error) => {
548
- reject(error);
549
- });
507
+ const finishTestItemData = {
508
+ endTime: this.helpers.now(),
509
+ ...(itemObj.children.length ? {} : { status: RP_STATUSES.PASSED }),
510
+ ...finishTestItemRQ,
511
+ };
512
+
513
+ itemObj.finishSend = true;
514
+ Promise.all(itemObj.children.map((itemId) => this.map[itemId] && this.map[itemId].promiseFinish))
515
+ .then(() => {
516
+ this.cleanMap(itemObj.children);
517
+ this.finishTestItemPromiseStart(
518
+ itemObj,
519
+ itemTempId,
520
+ Object.assign(finishTestItemData, { launchUuid: this.launchUuid }),
521
+ );
522
+ }, () => {
523
+ this.cleanMap(itemObj.children);
524
+ this.logDebug(`Error finish children of test item ${itemTempId}`);
525
+ this.logDebug(`Finish test item ${itemTempId}`);
526
+ this.finishTestItemPromiseStart(
527
+ itemObj,
528
+ itemTempId,
529
+ Object.assign(finishTestItemData, { launchUuid: this.launchUuid }),
530
+ );
531
+ });
532
+
533
+ return {
534
+ tempId: itemTempId,
535
+ promise: itemObj.promiseFinish,
536
+ };
537
+ }
538
+
539
+ saveLog(itemObj, requestPromiseFunc) {
540
+ const tempId = this.getUniqId();
541
+ this.map[tempId] = this.getNewItemObj((resolve, reject) => {
542
+ itemObj.promiseStart.then(() => {
543
+ this.logDebug(`Save log ${tempId}`);
544
+ requestPromiseFunc(itemObj.realId, this.launchUuid).then((response) => {
545
+ this.logDebug(`Successfully save log ${tempId}`);
546
+ resolve(response);
547
+ }, (error) => {
548
+ this.logDebug(`Error finish log: ${error}`);
549
+ reject(error);
550
550
  });
551
- itemObj.children.push(tempId);
552
-
553
- const logObj = this.map[tempId];
554
- logObj.finishSend = true;
555
- logObj.promiseStart.then(response => logObj.resolveFinish(response), error => logObj.rejectFinish(error));
556
-
557
- return {
558
- tempId,
559
- promise: this.map[tempId].promiseFinish,
560
- };
551
+ }, (error) => {
552
+ reject(error);
553
+ });
554
+ });
555
+ itemObj.children.push(tempId);
556
+
557
+ const logObj = this.map[tempId];
558
+ logObj.finishSend = true;
559
+ logObj.promiseStart.then((response) => logObj.resolveFinish(response), (error) => logObj.rejectFinish(error));
560
+
561
+ return {
562
+ tempId,
563
+ promise: this.map[tempId].promiseFinish,
564
+ };
565
+ }
566
+
567
+ sendLog(itemTempId, saveLogRQ, fileObj) {
568
+ const saveLogData = {
569
+ time: this.helpers.now(),
570
+ message: '',
571
+ level: '',
572
+ ...saveLogRQ,
573
+ };
574
+
575
+ if (fileObj) {
576
+ return this.sendLogWithFile(itemTempId, saveLogData, fileObj);
561
577
  }
578
+ return this.sendLogWithoutFile(itemTempId, saveLogData);
579
+ }
562
580
 
563
- sendLog(itemTempId, saveLogRQ, fileObj) {
564
- const saveLogData = Object.assign({
565
- time: this.helpers.now(),
566
- message: '',
567
- level: '',
568
- }, saveLogRQ);
569
-
570
- if (fileObj) {
571
- return this.sendLogWithFile(itemTempId, saveLogData, fileObj);
572
- }
573
- return this.sendLogWithoutFile(itemTempId, saveLogData);
574
- }
575
-
576
- /**
581
+ /**
577
582
  * Send log of test results.
578
583
  * @param {string} itemTempId - temp item id (returned in the query "startTestItem").
579
584
  * @param {Object} saveLogRQ - object with data of test result.
@@ -585,27 +590,29 @@ class RPClient {
585
590
  * }
586
591
  * @Returns {Object} - an object which contains a tempId and a promise
587
592
  */
588
- sendLogWithoutFile(itemTempId, saveLogRQ) {
589
- const itemObj = this.map[itemTempId];
590
- if (!itemObj) {
591
- return this.getRejectAnswer(itemTempId, new Error(`Item "${itemTempId}" not found`));
592
- }
593
-
594
- const requestPromise = (itemUuid, launchUuid) => {
595
- const url = 'log';
596
- const isItemUuid = itemUuid !== launchUuid;
597
- return this.restClient.create(
598
- url,
599
- Object.assign(saveLogRQ,
600
- { launchUuid },
601
- isItemUuid && { itemUuid }),
602
- { headers: this.headers },
603
- );
604
- };
605
- return this.saveLog(itemObj, requestPromise);
593
+ sendLogWithoutFile(itemTempId, saveLogRQ) {
594
+ const itemObj = this.map[itemTempId];
595
+ if (!itemObj) {
596
+ return this.getRejectAnswer(itemTempId, new Error(`Item "${itemTempId}" not found`));
606
597
  }
607
598
 
608
- /**
599
+ const requestPromise = (itemUuid, launchUuid) => {
600
+ const url = 'log';
601
+ const isItemUuid = itemUuid !== launchUuid;
602
+ return this.restClient.create(
603
+ url,
604
+ Object.assign(
605
+ saveLogRQ,
606
+ { launchUuid },
607
+ isItemUuid && { itemUuid },
608
+ ),
609
+ { headers: this.headers },
610
+ );
611
+ };
612
+ return this.saveLog(itemObj, requestPromise);
613
+ }
614
+
615
+ /**
609
616
  * Send log of test results with file.
610
617
  * @param {string} itemTempId - temp item id (returned in the query "startTestItem").
611
618
  * @param {Object} saveLogRQ - object with data of test result.
@@ -626,89 +633,87 @@ class RPClient {
626
633
  * }
627
634
  * @Returns {Object} - an object which contains a tempId and a promise
628
635
  */
629
- sendLogWithFile(itemTempId, saveLogRQ, fileObj) {
630
- const itemObj = this.map[itemTempId];
631
- if (!itemObj) {
632
- return this.getRejectAnswer(itemTempId, new Error(`Item "${itemTempId}" not found`));
633
- }
634
- // eslint-disable-next-line max-len
635
- const requestPromise = (itemUuid, launchUuid) => {
636
- const isItemUuid = itemUuid !== launchUuid;
637
-
638
- // eslint-disable-next-line max-len
639
- return this.getRequestLogWithFile(Object.assign(saveLogRQ, { launchUuid }, isItemUuid && { itemUuid }), fileObj);
640
- };
641
-
642
- return this.saveLog(itemObj, requestPromise);
643
- }
644
-
645
- getRequestLogWithFile(saveLogRQ, fileObj) {
646
- const url = 'log';
647
- // eslint-disable-next-line no-param-reassign
648
- saveLogRQ.file = { name: fileObj.name };
649
- return this.restClient.create(
650
- url,
651
- this.buildMultiPartStream([saveLogRQ], fileObj, MULTIPART_BOUNDARY),
652
- {
653
- headers: Object.assign({}, this.headers, {
654
- 'Content-Type': `multipart/form-data; boundary=${MULTIPART_BOUNDARY}`,
655
- }),
656
- },
657
- )
658
- .then(response => response)
659
- .catch((error) => {
660
- this.logDebug('ERROR');
661
- this.logDebug(error);
662
- });
636
+ sendLogWithFile(itemTempId, saveLogRQ, fileObj) {
637
+ const itemObj = this.map[itemTempId];
638
+ if (!itemObj) {
639
+ return this.getRejectAnswer(itemTempId, new Error(`Item "${itemTempId}" not found`));
663
640
  }
664
-
665
- // eslint-disable-next-line valid-jsdoc
666
- /**
641
+ // eslint-disable-next-line max-len
642
+ const requestPromise = (itemUuid, launchUuid) => {
643
+ const isItemUuid = itemUuid !== launchUuid;
644
+
645
+ // eslint-disable-next-line max-len
646
+ return this.getRequestLogWithFile(Object.assign(saveLogRQ, { launchUuid }, isItemUuid && { itemUuid }), fileObj);
647
+ };
648
+
649
+ return this.saveLog(itemObj, requestPromise);
650
+ }
651
+
652
+ getRequestLogWithFile(saveLogRQ, fileObj) {
653
+ const url = 'log';
654
+ // eslint-disable-next-line no-param-reassign
655
+ saveLogRQ.file = { name: fileObj.name };
656
+ return this.restClient.create(
657
+ url,
658
+ this.buildMultiPartStream([saveLogRQ], fileObj, MULTIPART_BOUNDARY),
659
+ {
660
+ headers: { ...this.headers, 'Content-Type': `multipart/form-data; boundary=${MULTIPART_BOUNDARY}` },
661
+ },
662
+ )
663
+ .then((response) => response)
664
+ .catch((error) => {
665
+ this.logDebug('ERROR');
666
+ this.logDebug(error);
667
+ });
668
+ }
669
+
670
+ // eslint-disable-next-line valid-jsdoc
671
+ /**
667
672
  *
668
673
  * @Private
669
674
  */
670
- buildMultiPartStream(jsonPart, filePart, boundary) {
671
- const eol = "\r\n";
672
- const bx = `--${boundary}`;
673
- const buffers = [
674
- // eslint-disable-next-line function-paren-newline
675
- Buffer.from(
676
- // eslint-disable-next-line prefer-template
677
- bx + eol + "Content-Disposition: form-data; name=\"json_request_part\""
675
+ buildMultiPartStream(jsonPart, filePart, boundary) {
676
+ const eol = "\r\n";
677
+ const bx = `--${boundary}`;
678
+ const buffers = [
679
+ // eslint-disable-next-line function-paren-newline
680
+ Buffer.from(
681
+ // eslint-disable-next-line prefer-template
682
+ bx + eol + "Content-Disposition: form-data; name=\"json_request_part\""
678
683
  + eol + "Content-Type: application/json" + eol
679
684
  + eol + eol + JSON.stringify(jsonPart) + eol,
680
- ),
681
- // eslint-disable-next-line function-paren-newline
682
- Buffer.from(
683
- // eslint-disable-next-line prefer-template
684
- bx + eol + "Content-Disposition: form-data; name=\"file\"; filename=\"" + filePart.name + "\"" + eol
685
+ ),
686
+ // eslint-disable-next-line function-paren-newline
687
+ Buffer.from(
688
+ // eslint-disable-next-line prefer-template
689
+ bx + eol + "Content-Disposition: form-data; name=\"file\"; filename=\"" + filePart.name + "\"" + eol
685
690
  + "Content-Type: " + filePart.type + eol + eol,
686
- ),
687
- Buffer.from(filePart.content, 'base64'),
688
- Buffer.from(`${eol + bx}--${eol}`),
689
- ];
690
- return Buffer.concat(buffers);
691
- }
692
-
693
- finishTestItemPromiseStart(itemObj, itemTempId, finishTestItemData) {
694
- itemObj.promiseStart
695
- .then(() => {
696
- const url = ['item', itemObj.realId].join('/');
697
- this.logDebug(`Finish test item ${itemTempId}`);
698
- // eslint-disable-next-line max-len
699
- this.restClient.update(url, Object.assign(finishTestItemData, { launchUuid: this.launchUuid }), { headers: this.headers })
700
- .then((response) => {
701
- this.logDebug(`Success finish item ${itemTempId}`);
702
- itemObj.resolveFinish(response);
703
- }, (error) => {
704
- this.logDebug(`Error finish test item ${itemTempId}`);
705
- console.dir(error);
706
- itemObj.rejectFinish(error);
707
- });
708
- }, (error) => {
709
- itemObj.rejectFinish(error);
710
- });
711
- }
691
+ ),
692
+ Buffer.from(filePart.content, 'base64'),
693
+ Buffer.from(`${eol + bx}--${eol}`),
694
+ ];
695
+ return Buffer.concat(buffers);
696
+ }
697
+
698
+ finishTestItemPromiseStart(itemObj, itemTempId, finishTestItemData) {
699
+ itemObj.promiseStart
700
+ .then(() => {
701
+ const url = ['item', itemObj.realId].join('/');
702
+ this.logDebug(`Finish test item ${itemTempId}`);
703
+ // eslint-disable-next-line max-len
704
+ this.restClient.update(url, Object.assign(finishTestItemData, { launchUuid: this.launchUuid }), { headers: this.headers })
705
+ .then((response) => {
706
+ this.logDebug(`Success finish item ${itemTempId}`);
707
+ itemObj.resolveFinish(response);
708
+ }, (error) => {
709
+ this.logDebug(`Error finish test item ${itemTempId}`);
710
+ console.dir(error);
711
+ itemObj.rejectFinish(error);
712
+ });
713
+ }, (error) => {
714
+ itemObj.rejectFinish(error);
715
+ });
716
+ }
712
717
  }
713
718
 
714
719
  module.exports = RPClient;