Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launch UUID print config #164

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 20 additions & 0 deletions lib/commons/config.js
Original file line number Diff line number Diff line change
@@ -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]) {
Expand All @@ -15,7 +24,7 @@
if (!calculatedApiKey) {
throw new ReportPortalRequiredOptionError('apiKey');
} else {
console.warn(`Option 'token' is deprecated. Use 'apiKey' instead.`);

Check warning on line 27 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (10)

Unexpected console statement

Check warning on line 27 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (12)

Unexpected console statement

Check warning on line 27 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (14)

Unexpected console statement

Check warning on line 27 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected console statement

Check warning on line 27 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected console statement

Check warning on line 27 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected console statement
}
}

Expand All @@ -32,6 +41,15 @@
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,
Expand All @@ -45,10 +63,12 @@
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
console.dir(error);

Check warning on line 71 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (10)

Unexpected console statement

Check warning on line 71 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (12)

Unexpected console statement

Check warning on line 71 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (14)

Unexpected console statement

Check warning on line 71 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected console statement

Check warning on line 71 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected console statement

Check warning on line 71 in lib/commons/config.js

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected console statement
}

return calculatedOptions;
Expand Down
8 changes: 8 additions & 0 deletions lib/constants/outputs.js
Original file line number Diff line number Diff line change
@@ -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 };
3 changes: 3 additions & 0 deletions lib/report-portal-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
88 changes: 88 additions & 0 deletions spec/report-portal-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
16 changes: 6 additions & 10 deletions statistics/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down