Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
- `instanceID` property for Google Analytics events.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Missing version header for the new changelog entry.

The new changelog entry is missing the standard version header format used throughout the file. Add the version number and date following the existing pattern.

📝 Proposed fix
+## [5.5.11] - 2026-03-26
 ### Added
 - `instanceID` property for Google Analytics events.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
### Added
- `instanceID` property for Google Analytics events.
## [5.5.11] - 2026-03-26
### Added
- `instanceID` property for Google Analytics events.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@CHANGELOG.md` around lines 1 - 2, The new changelog entry adding the
`instanceID` property under the "Added" section lacks the required version
header; add a version/date header above the "### Added" entry matching the
repository's existing changelog header pattern (same format used elsewhere in
the file), e.g. include the release version and date in the exact header style
used for other entries so the `instanceID` line is correctly grouped under that
release.

Comment thread
AmsterGet marked this conversation as resolved.
Outdated

## [5.5.10] - 2026-02-05
### Fixed
Expand Down
96 changes: 96 additions & 0 deletions __tests__/report-portal-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('ReportPortal javascript client', () => {
endpoint: 'https://rp.us/api/v1',
project: 'tst',
});
jest.spyOn(client, 'fetchServerInfo').mockResolvedValue({});
jest.spyOn(client.statistics, 'trackEvent').mockImplementation();

await client.triggerStatisticsEvent();
Expand Down Expand Up @@ -218,6 +219,101 @@ describe('ReportPortal javascript client', () => {
}),
);
});

it('should fetch server info and set instanceID before tracking event', async () => {
const client = new RPClient({
apiKey: 'startLaunchTest',
endpoint: 'https://rp.us/api/v1',
project: 'tst',
});
const serverInfoResponse = {
extensions: {
result: {
'server.details.instance': 'test-instance-123',
},
},
};
jest.spyOn(client, 'fetchServerInfo').mockResolvedValue(serverInfoResponse);
jest.spyOn(client.statistics, 'trackEvent').mockImplementation();
jest.spyOn(client.statistics, 'setInstanceID');

await client.triggerStatisticsEvent();

expect(client.fetchServerInfo).toHaveBeenCalled();
expect(client.statistics.setInstanceID).toHaveBeenCalledWith('test-instance-123');
expect(client.statistics.trackEvent).toHaveBeenCalled();
});

it('should still track event with not_set instanceID if fetchServerInfo fails', async () => {
const client = new RPClient({
apiKey: 'startLaunchTest',
endpoint: 'https://rp.us/api/v1',
project: 'tst',
});
jest.spyOn(client, 'fetchServerInfo').mockRejectedValue(new Error('Network error'));
jest.spyOn(client.statistics, 'trackEvent').mockImplementation();
jest.spyOn(client.statistics, 'setInstanceID');

await client.triggerStatisticsEvent();

expect(client.statistics.setInstanceID).toHaveBeenCalledWith('not_set');
expect(client.statistics.trackEvent).toHaveBeenCalled();
});

it('should not set instanceID if server info does not contain it', async () => {
const client = new RPClient({
apiKey: 'startLaunchTest',
endpoint: 'https://rp.us/api/v1',
project: 'tst',
});
jest.spyOn(client, 'fetchServerInfo').mockResolvedValue({});
jest.spyOn(client.statistics, 'trackEvent').mockImplementation();
jest.spyOn(client.statistics, 'setInstanceID');

await client.triggerStatisticsEvent();

expect(client.statistics.setInstanceID).not.toHaveBeenCalled();
expect(client.statistics.trackEvent).toHaveBeenCalled();
});
});

describe('getServerInfoUrl', () => {
it('should return correct info URL for v1 endpoint', () => {
const client = new RPClient({
apiKey: 'test',
project: 'test',
endpoint: 'https://rp.us/api/v1',
});

expect(client.getServerInfoUrl()).toBe('https://rp.us/api/info');
});

it('should return correct info URL for v2 endpoint', () => {
const client = new RPClient({
apiKey: 'test',
project: 'test',
endpoint: 'https://rp.us/api/v2',
});

expect(client.getServerInfoUrl()).toBe('https://rp.us/api/info');
});
});

describe('fetchServerInfo', () => {
it('should call restClient.request with correct URL', async () => {
const client = new RPClient({
apiKey: 'test',
project: 'test',
endpoint: 'https://rp.us/api/v1',
});
const serverInfo = { extensions: { result: {} } };
jest.spyOn(client.restClient, 'request').mockResolvedValue(serverInfo);

const result = await client.fetchServerInfo();

expect(client.restClient.request).toHaveBeenCalledWith('GET', 'https://rp.us/api/info', {});
expect(result).toEqual(serverInfo);
});
});

describe('startLaunch', () => {
Expand Down
39 changes: 39 additions & 0 deletions __tests__/statistics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,43 @@ describe('Statistics', () => {
expect(console.error).toHaveBeenCalledWith(errorMessage);
});
});

describe('setInstanceID', () => {
it('should set instanceID in event params', async () => {
jest.spyOn(axios, 'post').mockReturnValue({
send: () => {}, // eslint-disable-line
});

const statistics = new Statistics(eventName, agentParams);
statistics.setInstanceID('test-instance-id');
await statistics.trackEvent();

expect(axios.post).toHaveBeenCalledTimes(1);
expect(axios.post).toHaveBeenCalledWith(
url,
expect.objectContaining({
events: expect.arrayContaining([
expect.objectContaining({
params: expect.objectContaining({
instanceID: 'test-instance-id',
}),
}),
]),
}),
);
});

it('should not include instanceID if setInstanceID was not called', async () => {
jest.spyOn(axios, 'post').mockReturnValue({
send: () => {}, // eslint-disable-line
});

const statistics = new Statistics(eventName, agentParams);
await statistics.trackEvent();

expect(axios.post).toHaveBeenCalledTimes(1);
const callArgs = axios.post.mock.calls[0][1];
expect(callArgs.events[0].params).not.toHaveProperty('instanceID');
});
});
});
18 changes: 18 additions & 0 deletions lib/report-portal-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,28 @@ class RPClient {
return this.restClient.request('GET', url, {});
}

getServerInfoUrl() {
return this.config.endpoint.replace('/v1', '/info').replace('/v2', '/info');
}

async fetchServerInfo() {
const url = this.getServerInfoUrl();
return this.restClient.request('GET', url, {});
}

async triggerStatisticsEvent() {
if (process.env.REPORTPORTAL_CLIENT_JS_NO_ANALYTICS) {
return;
}
try {
const serverInfo = await this.fetchServerInfo();
const instanceID = serverInfo?.extensions?.result?.['server.details.instance'];
if (instanceID) {
this.statistics.setInstanceID(instanceID);
}
} catch (e) {
this.statistics.setInstanceID('not_set');
}
await this.statistics.trackEvent();
}

Expand Down
4 changes: 4 additions & 0 deletions statistics/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
return params;
}

setInstanceID(instanceID) {
this.eventParams.instanceID = instanceID;
}

async trackEvent() {
try {
const requestBody = {
Expand All @@ -44,7 +48,7 @@
requestBody,
);
} catch (error) {
console.error(error.message);

Check warning on line 51 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected console statement

Check warning on line 51 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (14)

Unexpected console statement

Check warning on line 51 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected console statement

Check warning on line 51 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (22)

Unexpected console statement

Check warning on line 51 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected console statement
}
}
}
Expand Down
Loading