Skip to content

Commit

Permalink
Merge pull request #162 from reportportal/develop
Browse files Browse the repository at this point in the history
Release 5.0.12
  • Loading branch information
AmsterGet authored Jun 19, 2023
2 parents 01e3958 + e8904d7 commit 1a9a794
Show file tree
Hide file tree
Showing 12 changed files with 522 additions and 185 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@typescript-eslint/naming-convention": 0,
"@typescript-eslint/dot-notation": 0,
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-var-requires": 0
"@typescript-eslint/no-var-requires": 0,
"max-classes-per-file": 0
}
}
2 changes: 1 addition & 1 deletion .github/workflows/CI-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [10, 12, 14, 16, 18]
node: [10, 12, 14, 16, 18, 20]
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 18
- name: Clean install of node dependencies
run: npm ci
- name: Install of node dependencies
run: npm install
- name: Run lint
run: npm run lint
- name: Run tests
Expand All @@ -41,14 +41,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'
- name: Clean install of node dependencies
run: npm ci
- name: Install of node dependencies
run: npm install
- name: Publish to NPM
run: |
npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
Expand All @@ -57,7 +57,7 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_ACCESS_TOKEN }}
- name: Set up Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 18
registry-url: 'https://npm.pkg.github.com'
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
### Changed
- `token` configuration option was renamed to `apiKey` to maintain common convention.

## [5.0.11] - 2023-06-01
### Fixed
Expand Down
231 changes: 129 additions & 102 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.11
5.0.12-SNAPSHOT
61 changes: 61 additions & 0 deletions lib/commons/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { ReportPortalRequiredOptionError, ReportPortalValidationError } = require('./errors');

const getRequiredOption = (options, optionName) => {
if (!Object.prototype.hasOwnProperty.call(options, optionName) || !options[optionName]) {
throw new ReportPortalRequiredOptionError(optionName);
}

return options[optionName];
};

const getApiKey = ({ apiKey, token }) => {
let calculatedApiKey = apiKey;
if (!calculatedApiKey) {
calculatedApiKey = token;
if (!calculatedApiKey) {
throw new ReportPortalRequiredOptionError('apiKey');
} else {
console.warn(`Option 'token' is deprecated. Use 'apiKey' instead.`);
}
}

return calculatedApiKey;
};

const getClientConfig = (options) => {
let calculatedOptions = options;
try {
if (typeof options !== 'object') {
throw new ReportPortalValidationError('`options` must be an object.');
}
const apiKey = getApiKey(options);
const project = getRequiredOption(options, 'project');
const endpoint = getRequiredOption(options, 'endpoint');

calculatedOptions = {
apiKey,
project,
endpoint,
launch: options.launch,
debug: options.debug,
isLaunchMergeRequired:
options.isLaunchMergeRequired === undefined ? false : options.isLaunchMergeRequired,
headers: options.headers,
restClientConfig: options.restClientConfig,
attributes: options.attributes,
mode: options.mode,
description: options.description,
};
} catch (error) {
// don't throw the error up to not break the entire process
console.dir(error);
}

return calculatedOptions;
};

module.exports = {
getClientConfig,
getRequiredOption,
getApiKey,
};
29 changes: 29 additions & 0 deletions lib/commons/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ReportPortalError extends Error {
constructor(message) {
const basicMessage = `\nReportPortal client error: ${message}`;
super(basicMessage);
this.name = 'ReportPortalError';
}
}

class ReportPortalValidationError extends ReportPortalError {
constructor(message) {
const basicMessage = `\nValidation failed. Please, check the specified parameters: ${message}`;
super(basicMessage);
this.name = 'ReportPortalValidationError';
}
}

class ReportPortalRequiredOptionError extends ReportPortalValidationError {
constructor(propertyName) {
const basicMessage = `\nProperty '${propertyName}' must not be empty.`;
super(basicMessage);
this.name = 'ReportPortalRequiredOptionError';
}
}

module.exports = {
ReportPortalError,
ReportPortalValidationError,
ReportPortalRequiredOptionError,
};
29 changes: 16 additions & 13 deletions lib/report-portal-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const UniqId = require('uniqid');
const { URLSearchParams } = require('url');
const helpers = require('./helpers');
const RestClient = require('./rest');
const { getClientConfig } = require('./commons/config');
const Statistics = require('../statistics/statistics');
const { EVENT_NAME } = require('../statistics/constants');
const { RP_STATUSES } = require('./constants/statuses');
Expand All @@ -12,10 +13,10 @@ const MULTIPART_BOUNDARY = Math.floor(Math.random() * 10000000000).toString();
class RPClient {
/**
* Create a client for RP.
* @param {Object} params - config object.
* params should look like this
* @param {Object} options - config object.
* options should look like this
* {
* token: "00000000-0000-0000-0000-000000000000",
* apiKey: "reportportalApiKey",
* endpoint: "http://localhost:8080/api/v1",
* launch: "YOUR LAUNCH NAME",
* project: "PROJECT NAME",
Expand All @@ -28,24 +29,26 @@ class RPClient {
* version: "AGENT VERSION",
* }
*/
constructor(params, agentParams) {
this.debug = params.debug;
this.isLaunchMergeRequired =
params.isLaunchMergeRequired === undefined ? false : params.isLaunchMergeRequired;
constructor(options, agentParams) {
this.config = getClientConfig(options);
this.debug = this.config.debug;
this.isLaunchMergeRequired = this.config.isLaunchMergeRequired;
this.apiKey = this.config.apiKey;
// deprecated
this.token = this.apiKey;

this.map = {};
this.baseURL = [params.endpoint, params.project].join('/');
this.baseURL = [this.config.endpoint, this.config.project].join('/');
this.headers = {
'User-Agent': 'NodeJS',
Authorization: `bearer ${params.token}`,
...(params.headers || {}),
Authorization: `bearer ${this.apiKey}`,
...(this.config.headers || {}),
};
this.token = params.token;
this.config = params;
this.helpers = helpers;
this.restClient = new RestClient({
baseURL: this.baseURL,
headers: this.headers,
restClientConfig: params.restClientConfig,
restClientConfig: this.config.restClientConfig,
});
this.statistics = new Statistics(EVENT_NAME, agentParams);
this.launchUuid = '';
Expand Down
116 changes: 116 additions & 0 deletions spec/config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const { getClientConfig, getRequiredOption, getApiKey } = require('../lib/commons/config');
const {
ReportPortalRequiredOptionError,
ReportPortalValidationError,
} = require('../lib/commons/errors');

describe('Config commons test suite', () => {
describe('getRequiredOption', () => {
it('should return option if it presented in options and has value', () => {
const option = getRequiredOption({ project: 1 }, 'project');

expect(option).toBe(1);
});

it('should throw ReportPortalRequiredOptionError in case of empty option', () => {
let error;
try {
getRequiredOption({ project: undefined }, 'project');
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error).toBeInstanceOf(ReportPortalRequiredOptionError);
});

it(
'should throw ReportPortalRequiredOptionError in case of option ' + 'not present in options',
() => {
let error;
try {
getRequiredOption({ other: 1 }, 'project');
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error).toBeInstanceOf(ReportPortalRequiredOptionError);
},
);
});

describe('getApiKey', () => {
it('should return apiKey if it presented in options and has value', () => {
const apiKey = getApiKey({ apiKey: '1' });

expect(apiKey).toBe('1');
});

it('should return apiKey if it provided in options via deprecated token option', () => {
const apiKey = getApiKey({ token: '1' });

expect(apiKey).toBe('1');
});

it('should print warning to console if deprecated token option used', () => {
spyOn(console, 'warn');

getApiKey({ token: '1' });

expect(console.warn).toHaveBeenCalledWith(
`Option 'token' is deprecated. Use 'apiKey' instead.`,
);
});

it('should throw ReportPortalRequiredOptionError in case of no one option present', () => {
let error;
try {
getApiKey({});
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error).toBeInstanceOf(ReportPortalRequiredOptionError);
});
});

describe('getClientConfig', () => {
it('should print ReportPortalValidationError error to the console in case of options is not an object type', () => {
spyOn(console, 'dir');
getClientConfig('options');

expect(console.dir).toHaveBeenCalledWith(
new ReportPortalValidationError('`options` must be an object.'),
);
});

it('should print ReportPortalRequiredOptionError to the console in case of "endpoint" option missed', () => {
spyOn(console, 'dir');
getClientConfig({
apiKey: '123',
project: 'prj',
});

expect(console.dir).toHaveBeenCalledWith(new ReportPortalRequiredOptionError('endpoint'));
});

it('should print ReportPortalRequiredOptionError to the console in case of "project" option missed', () => {
spyOn(console, 'dir');
getClientConfig({
apiKey: '123',
endpoint: 'https://abc.com',
});

expect(console.dir).toHaveBeenCalledWith(new ReportPortalRequiredOptionError('project'));
});

it('should print ReportPortalRequiredOptionError to the console in case of "apiKey" option missed', () => {
spyOn(console, 'dir');
getClientConfig({
project: 'prj',
endpoint: 'https://abc.com',
});

expect(console.dir).toHaveBeenCalledWith(new ReportPortalRequiredOptionError('apiKey'));
});
});
});
Loading

0 comments on commit 1a9a794

Please sign in to comment.