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 diff --git a/README.md b/README.md index 9524125..2c59d52 100644 --- a/README.md +++ b/README.md @@ -52,17 +52,19 @@ rpClient.checkConnect().then((response) => { When creating a client instance, you need to specify the following options: -| Option | Necessity | Default | Description | -|-----------------------|------------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| apiKey | Required | | User's reportportal token from which you want to send requests. It can be found on the profile page of this user. | -| endpoint | Required | | URL of your server. For example, if you visit the page at 'https://server:8080/ui', then endpoint will be equal to 'https://server:8080/api/v1'. | -| launch | Required | | Name of launch at creation. | -| project | Required | | The name of the project in which the launches will be created. | -| headers | Optional | {} | The object with custom headers for internal http client. | -| debug | Optional | false | This flag allows seeing the logs of the client. Useful for debugging. | -| isLaunchMergeRequired | Optional | false | Allows client to merge launches into one at the end of the run via saving their UUIDs to the file system. At the end of the run launches can be merged using `mergeLaunches` method. | -| restClientConfig | Optional | Not set | The object with `agent` property for configure [http(s)](https://nodejs.org/api/https.html#https_https_request_url_options_callback) client, may contain other client options eg. `timeout`. | -| token | Deprecated | Not set | Use `apiKey` instead. | +| Option | Necessity | Default | Description | +|-----------------------|------------|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| apiKey | Required | | User's reportportal token from which you want to send requests. It can be found on the profile page of this user. | +| endpoint | Required | | URL of your server. For example, if you visit the page at 'https://server:8080/ui', then endpoint will be equal to 'https://server:8080/api/v1'. | +| launch | Required | | Name of launch at creation. | +| project | Required | | The name of the project in which the launches will be created. | +| headers | Optional | {} | The object with custom headers for internal http client. | +| debug | Optional | false | This flag allows seeing the logs of the client. Useful for debugging. | +| isLaunchMergeRequired | Optional | false | Allows client to merge launches into one at the end of the run via saving their UUIDs to the file system. At the end of the run launches can be merged using `mergeLaunches` method. | +| restClientConfig | Optional | Not set | The object with `agent` property for configure [http(s)](https://nodejs.org/api/https.html#https_https_request_url_options_callback) client, may contain other client options eg. `timeout`. | +| launchUuidPrint | Optional | false | Whether to print the current launch UUID. | +| launchUuidPrintOutput | Optional | 'STDOUT' | Launch UUID printing output. Possible values: 'STDOUT', 'STDERR'. Works only if `launchUuidPrint` set to `true`. | +| token | Deprecated | Not set | Use `apiKey` instead. | ## Asynchronous reporting diff --git a/VERSION b/VERSION index 718db1c..3a13884 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -5.0.12 +5.0.13-SNAPSHOT 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/package-lock.json b/package-lock.json index 31cfd2a..17a2b5b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,13 +6,13 @@ "packages": { "": { "name": "@reportportal/client-javascript", - "version": "5.0.9", + "version": "5.0.12", "license": "Apache-2.0", "dependencies": { "axios": "^0.27.2", "axios-retry": "^3.4.0", "glob": "^7.2.3", - "ini": "2.0.0", + "ini": "^2.0.0", "uniqid": "^5.4.0", "uuid": "^9.0.0" }, @@ -4644,9 +4644,9 @@ } }, "node_modules/word-wrap": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", - "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.4.tgz", + "integrity": "sha512-2V81OA4ugVo5pRo46hAoD2ivUJx8jXmWXfUkY4KFNw0hEptvN0QfH3K4nHiwzGeKl5rFKedV48QVoqYavy4YpA==", "dev": true, "engines": { "node": ">=0.10.0" 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', () => { 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;