Skip to content

Commit 148c4bd

Browse files
authored
Add testhub build run ID to PercyEnv and PercyClient (#2013)
* Add testhub build run ID to PercyEnv and PercyClient * Add tests for TH_BUILD_RUN_ID handling in PercyEnv * Add testhub-build-run-id to client environment attributes in PercyClient * Update test to use TH_BUILD_RUN_ID for creating a new build in PercyClient
1 parent e338d1f commit 148c4bd

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

packages/client/src/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ export class PercyClient {
206206
'cli-start-time': cliStartTime,
207207
source: source,
208208
'skip-base-build': this.config.percy?.skipBaseBuild,
209-
'testhub-build-uuid': this.env.testhubBuildUuid
209+
'testhub-build-uuid': this.env.testhubBuildUuid,
210+
'testhub-build-run-id': this.env.testhubBuildRunId
210211
},
211212
relationships: {
212213
resources: {

packages/client/test/client.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ describe('PercyClient', () => {
229229
'parallel-total-shards': client.env.parallel.total,
230230
'cli-start-time': null,
231231
'testhub-build-uuid': client.env.testhubBuildUuid,
232+
'testhub-build-run-id': client.env.testhubBuildRunId,
232233
source: 'user_created',
233234
partial: client.env.partial,
234235
tags: []
@@ -266,6 +267,7 @@ describe('PercyClient', () => {
266267
'parallel-total-shards': client.env.parallel.total,
267268
'cli-start-time': null,
268269
'testhub-build-uuid': client.env.testhubBuildUuid,
270+
'testhub-build-run-id': client.env.testhubBuildRunId,
269271
source: 'user_created',
270272
partial: client.env.partial,
271273
tags: []
@@ -350,6 +352,7 @@ describe('PercyClient', () => {
350352
'parallel-total-shards': client.env.parallel.total,
351353
'cli-start-time': null,
352354
'testhub-build-uuid': client.env.testhubBuildUuid,
355+
'testhub-build-run-id': client.env.testhubBuildRunId,
353356
source: 'user_created',
354357
partial: client.env.partial,
355358
tags: []
@@ -391,6 +394,7 @@ describe('PercyClient', () => {
391394
'parallel-total-shards': client.env.parallel.total,
392395
'cli-start-time': null,
393396
'testhub-build-uuid': client.env.testhubBuildUuid,
397+
'testhub-build-run-id': client.env.testhubBuildRunId,
394398
source: 'user_created',
395399
partial: client.env.partial,
396400
tags: [{ id: null, name: 'tag1' }, { id: null, name: 'tag2' }]
@@ -433,6 +437,7 @@ describe('PercyClient', () => {
433437
'parallel-total-shards': client.env.parallel.total,
434438
'cli-start-time': cliStartTime,
435439
'testhub-build-uuid': client.env.testhubBuildUuid,
440+
'testhub-build-run-id': client.env.testhubBuildRunId,
436441
source: 'auto_enabled_group',
437442
partial: client.env.partial,
438443
tags: [{ id: null, name: 'tag1' }, { id: null, name: 'tag2' }]
@@ -474,6 +479,7 @@ describe('PercyClient', () => {
474479
'parallel-total-shards': client.env.parallel.total,
475480
'cli-start-time': null,
476481
'testhub-build-uuid': client.env.testhubBuildUuid,
482+
'testhub-build-run-id': client.env.testhubBuildRunId,
477483
source: 'user_created',
478484
partial: client.env.partial,
479485
'skip-base-build': true,
@@ -513,6 +519,46 @@ describe('PercyClient', () => {
513519
'parallel-total-shards': client.env.parallel.total,
514520
'cli-start-time': null,
515521
'testhub-build-uuid': 'test-uuid-123',
522+
'testhub-build-run-id': client.env.testhubBuildRunId,
523+
source: 'user_created',
524+
partial: client.env.partial,
525+
tags: []
526+
}
527+
}));
528+
});
529+
530+
it('creates a new build with testhub-build-run-id', async () => {
531+
process.env.TH_BUILD_RUN_ID = 'test-run-id-123';
532+
await expectAsync(client.createBuild({ projectType: 'web' })).toBeResolvedTo({
533+
data: {
534+
id: '123',
535+
attributes: {
536+
'build-number': 1,
537+
'web-url': 'https://percy.io/test/test/123'
538+
}
539+
}
540+
});
541+
542+
expect(api.requests['/builds'][0].body.data)
543+
.toEqual(jasmine.objectContaining({
544+
attributes: {
545+
branch: client.env.git.branch,
546+
type: 'web',
547+
'target-branch': client.env.target.branch,
548+
'target-commit-sha': client.env.target.commit,
549+
'commit-sha': client.env.git.sha,
550+
'commit-committed-at': client.env.git.committedAt,
551+
'commit-author-name': client.env.git.authorName,
552+
'commit-author-email': client.env.git.authorEmail,
553+
'commit-committer-name': client.env.git.committerName,
554+
'commit-committer-email': client.env.git.committerEmail,
555+
'commit-message': client.env.git.message,
556+
'pull-request-number': client.env.pullRequest,
557+
'parallel-nonce': client.env.parallel.nonce,
558+
'parallel-total-shards': client.env.parallel.total,
559+
'cli-start-time': null,
560+
'testhub-build-uuid': client.env.testhubBuildUuid,
561+
'testhub-build-run-id': 'test-run-id-123',
516562
source: 'user_created',
517563
partial: client.env.partial,
518564
tags: []
@@ -551,6 +597,7 @@ describe('PercyClient', () => {
551597
'parallel-total-shards': client.env.parallel.total,
552598
'cli-start-time': null,
553599
'testhub-build-uuid': client.env.testhubBuildUuid,
600+
'testhub-build-run-id': client.env.testhubBuildRunId,
554601
source: 'bstack_sdk_created',
555602
partial: client.env.partial,
556603
tags: []

packages/env/src/environment.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ export class PercyEnv {
303303
return this.vars.TH_BUILD_UUID || this.vars.BROWSERSTACK_TESTHUB_UUID || null;
304304
}
305305

306+
// th build run id
307+
get testhubBuildRunId() {
308+
return this.vars.TH_BUILD_RUN_ID || this.vars.BROWSERSTACK_TESTHUB_RUN_ID || null;
309+
}
310+
306311
// PERCY_FORCE_PKG_VALUE for forcing package.json values
307312
// to be used as the current environment values in client
308313
get forcedPkgValue() {

packages/env/test/environment.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,37 @@ describe('PercyEnv', () => {
5555
expect(env.testhubBuildUuid).toBeNull();
5656
});
5757
});
58+
59+
describe('testhubBuildRunId', () => {
60+
it('should return TH_BUILD_RUN_ID when it is set', () => {
61+
let env = new PercyEnv({ TH_BUILD_RUN_ID: 'test_run_id' });
62+
expect(env.testhubBuildRunId).toEqual('test_run_id');
63+
});
64+
65+
it('should return BROWSERSTACK_TESTHUB_RUN_ID when TH_BUILD_RUN_ID is not set', () => {
66+
let env = new PercyEnv({ BROWSERSTACK_TESTHUB_RUN_ID: 'browserstack_run_id' });
67+
expect(env.testhubBuildRunId).toEqual('browserstack_run_id');
68+
});
69+
70+
it('should prioritize TH_BUILD_RUN_ID over BROWSERSTACK_TESTHUB_RUN_ID when both are set', () => {
71+
let env = new PercyEnv({
72+
TH_BUILD_RUN_ID: 'test_run_id',
73+
BROWSERSTACK_TESTHUB_RUN_ID: 'browserstack_run_id'
74+
});
75+
expect(env.testhubBuildRunId).toEqual('test_run_id');
76+
});
77+
78+
it('should return null if neither TH_BUILD_RUN_ID nor BROWSERSTACK_TESTHUB_RUN_ID are set', () => {
79+
let env = new PercyEnv({});
80+
expect(env.testhubBuildRunId).toBeNull();
81+
});
82+
83+
it('should return null if both values are null', () => {
84+
let env = new PercyEnv({
85+
TH_BUILD_RUN_ID: null,
86+
BROWSERSTACK_TESTHUB_RUN_ID: null
87+
});
88+
expect(env.testhubBuildRunId).toBeNull();
89+
});
90+
});
5891
});

0 commit comments

Comments
 (0)