From 07f9b08ec5491f93ffd78d7eaf75c9bcfd08cc6e Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Mon, 17 Jul 2023 17:13:52 +0300 Subject: [PATCH 1/3] Add Launch UUID print options --- lib/commons/config.js | 20 +++++++ lib/constants/outputs.js | 8 +++ lib/report-portal-client.js | 3 ++ spec/report-portal-client.spec.js | 88 +++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 lib/constants/outputs.js diff --git a/lib/commons/config.js b/lib/commons/config.js index d41ad45..a09b468 100644 --- a/lib/commons/config.js +++ b/lib/commons/config.js @@ -1,4 +1,13 @@ const { ReportPortalRequiredOptionError, ReportPortalValidationError } = require('./errors'); +const { OUTPUT_TYPES } = require('../constants/outputs'); + +const getOption = (options, optionName, defaultValue) => { + if (!Object.prototype.hasOwnProperty.call(options, optionName) || !options[optionName]) { + return defaultValue; + } + + return options[optionName]; +}; const getRequiredOption = (options, optionName) => { if (!Object.prototype.hasOwnProperty.call(options, optionName) || !options[optionName]) { @@ -32,6 +41,15 @@ const getClientConfig = (options) => { const project = getRequiredOption(options, 'project'); const endpoint = getRequiredOption(options, 'endpoint'); + const launchUuidPrintOutputType = getOption(options, 'launchUuidPrintOutput', 'STDOUT') + .toString() + .toUpperCase(); + const launchUuidPrintOutput = getOption( + OUTPUT_TYPES, + launchUuidPrintOutputType, + OUTPUT_TYPES.STDOUT, + ); + calculatedOptions = { apiKey, project, @@ -45,6 +63,8 @@ const getClientConfig = (options) => { attributes: options.attributes, mode: options.mode, description: options.description, + launchUuidPrint: options.launchUuidPrint, + launchUuidPrintOutput, }; } catch (error) { // don't throw the error up to not break the entire process diff --git a/lib/constants/outputs.js b/lib/constants/outputs.js new file mode 100644 index 0000000..85f7857 --- /dev/null +++ b/lib/constants/outputs.js @@ -0,0 +1,8 @@ +const OUTPUT_TYPES = { + // eslint-disable-next-line no-console + STDOUT: console.log, + // eslint-disable-next-line no-console + STDERR: console.error, +}; + +module.exports = { OUTPUT_TYPES }; diff --git a/lib/report-portal-client.js b/lib/report-portal-client.js index bd7ffca..e95eb46 100644 --- a/lib/report-portal-client.js +++ b/lib/report-portal-client.js @@ -187,6 +187,9 @@ class RPClient { (response) => { this.map[tempId].realId = response.id; this.launchUuid = response.id; + if (this.config.launchUuidPrint) { + this.config.launchUuidPrintOutput(`Report Portal Launch UUID: ${this.launchUuid}`); + } if (this.isLaunchMergeRequired) { helpers.saveLaunchIdToFile(response.id); diff --git a/spec/report-portal-client.spec.js b/spec/report-portal-client.spec.js index 2a41e0c..54db15f 100644 --- a/spec/report-portal-client.spec.js +++ b/spec/report-portal-client.spec.js @@ -2,6 +2,7 @@ const process = require('process'); const RPClient = require('../lib/report-portal-client'); const RestClient = require('../lib/rest'); const helpers = require('../lib/helpers'); +const { OUTPUT_TYPES } = require('../lib/constants/outputs'); describe('ReportPortal javascript client', () => { describe('constructor', () => { @@ -316,6 +317,93 @@ describe('ReportPortal javascript client', () => { expect(client.restClient.create).not.toHaveBeenCalled(); expect(client.launchUuid).toEqual(id); }); + + it('should log Launch UUID if enabled', () => { + spyOn(OUTPUT_TYPES, 'STDOUT'); + const client = new RPClient({ + apiKey: 'startLaunchTest', + endpoint: 'https://rp.us/api/v1', + project: 'tst', + launchUuidPrint: true, + }); + const myPromise = Promise.resolve({ id: 'testidlaunch' }); + const time = 12345734; + spyOn(client.restClient, 'create').and.returnValue(myPromise); + return client + .startLaunch({ + startTime: time, + }) + .promise.then(function () { + expect(OUTPUT_TYPES.STDOUT).toHaveBeenCalledWith( + 'Report Portal Launch UUID: testidlaunch', + ); + }); + }); + + it('should log Launch UUID into STDERR if enabled', () => { + spyOn(OUTPUT_TYPES, 'STDERR'); + const client = new RPClient({ + apiKey: 'startLaunchTest', + endpoint: 'https://rp.us/api/v1', + project: 'tst', + launchUuidPrint: true, + launchUuidPrintOutput: 'stderr', + }); + const myPromise = Promise.resolve({ id: 'testidlaunch' }); + const time = 12345734; + spyOn(client.restClient, 'create').and.returnValue(myPromise); + return client + .startLaunch({ + startTime: time, + }) + .promise.then(function () { + expect(OUTPUT_TYPES.STDERR).toHaveBeenCalledWith( + 'Report Portal Launch UUID: testidlaunch', + ); + }); + }); + + it('should log Launch UUID into STDOUT if invalid output is set', () => { + spyOn(OUTPUT_TYPES, 'STDOUT'); + const client = new RPClient({ + apiKey: 'startLaunchTest', + endpoint: 'https://rp.us/api/v1', + project: 'tst', + launchUuidPrint: true, + launchUuidPrintOutput: 'asdfgh', + }); + const myPromise = Promise.resolve({ id: 'testidlaunch' }); + const time = 12345734; + spyOn(client.restClient, 'create').and.returnValue(myPromise); + return client + .startLaunch({ + startTime: time, + }) + .promise.then(function () { + expect(OUTPUT_TYPES.STDOUT).toHaveBeenCalledWith( + 'Report Portal Launch UUID: testidlaunch', + ); + }); + }); + + it('should not log Launch UUID if not enabled', () => { + spyOn(OUTPUT_TYPES, 'STDOUT'); + const client = new RPClient({ + apiKey: 'startLaunchTest', + endpoint: 'https://rp.us/api/v1', + project: 'tst', + }); + const myPromise = Promise.resolve({ id: 'testidlaunch' }); + const time = 12345734; + spyOn(client.restClient, 'create').and.returnValue(myPromise); + return client + .startLaunch({ + startTime: time, + }) + .promise.then(function () { + expect(OUTPUT_TYPES.STDOUT).not.toHaveBeenCalled(); + }); + }); }); describe('finishLaunch', () => { From 5cf1c6277afd824e122cfab475ffc376d622583d Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Mon, 17 Jul 2023 17:14:02 +0300 Subject: [PATCH 2/3] Some refactoring --- statistics/statistics.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/statistics/statistics.js b/statistics/statistics.js index 7b7a7e8..076d7e7 100644 --- a/statistics/statistics.js +++ b/statistics/statistics.js @@ -2,6 +2,10 @@ const axios = require('axios'); const { MEASUREMENT_ID, API_KEY, PJSON_NAME, PJSON_VERSION, INTERPRETER } = require('./constants'); const { getClientId } = require('./client-id'); +const hasOption = (options, optionName) => { + return Object.prototype.hasOwnProperty.call(options, optionName); +}; + class Statistics { constructor(eventName, agentParams) { this.eventName = eventName; @@ -14,18 +18,10 @@ class Statistics { client_name: PJSON_NAME, client_version: PJSON_VERSION, }; - if ( - agentParams && - Object.prototype.hasOwnProperty.call(agentParams, 'name') && - agentParams.name - ) { + if (agentParams && hasOption(agentParams, 'name') && agentParams.name) { params.agent_name = agentParams.name; } - if ( - agentParams && - Object.prototype.hasOwnProperty.call(agentParams, 'version') && - agentParams.version - ) { + if (agentParams && hasOption(agentParams, 'version') && agentParams.version) { params.agent_version = agentParams.version; } return params; From bd329ecf84853873c056afc0b70dec76be42a318 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Wed, 19 Jul 2023 11:41:23 +0300 Subject: [PATCH 3/3] Add CHANGELOG.md entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44ebd03..b78ca21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +### Added +- `launchUuidPrint` and `launchUuidPrintOutput` configuration options to ease integration with CI tools, by @HardNorth ## [5.0.12] - 2023-06-19 ### Changed