Skip to content

Commit

Permalink
PPLT_3233: Added CLI Start Time (#1686)
Browse files Browse the repository at this point in the history
* feat: added cli-start-time in CLI

* test: test fix

* lint fix

* chore: Removing console.log

* fix: instead of using Epoch Time using exact date

* test: test fix
  • Loading branch information
this-is-shivamsingh committed Aug 21, 2024
1 parent 5e9b413 commit 21c64d9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/cli-build/src/finalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const finalize = command('finalize', {
log.info('Finalizing parallel build...');

// rely on the parallel nonce to cause the API to return the current running build for the nonce
let { data: build } = await percy.client.createBuild();
let { data: build } = await percy.client.createBuild({ cliStartTime: percy.cliStartTime });
await percy.client.finalizeBuild(build.id, { all: true });

let { 'build-number': number, 'web-url': url } = build.attributes;
Expand Down
5 changes: 3 additions & 2 deletions packages/client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class PercyClient {
// Creates a build with optional build resources. Only one build can be
// created at a time per instance so snapshots and build finalization can be
// done more seemlessly without manually tracking build ids
async createBuild({ resources = [], projectType } = {}) {
async createBuild({ resources = [], projectType, cliStartTime = null } = {}) {
this.log.debug('Creating a new build...');

let tagsArr = tagsList(this.labels);
Expand All @@ -158,7 +158,8 @@ export class PercyClient {
'parallel-nonce': this.env.parallel.nonce,
'parallel-total-shards': this.env.parallel.total,
partial: this.env.partial,
tags: tagsArr
tags: tagsArr,
'cli-start-time': cliStartTime
},
relationships: {
resources: {
Expand Down
44 changes: 44 additions & 0 deletions packages/client/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ describe('PercyClient', () => {
});

describe('#createBuild()', () => {
let cliStartTime = new Date().toISOString();
it('creates a new build', async () => {
await expectAsync(client.createBuild()).toBeResolvedTo({
data: {
Expand Down Expand Up @@ -164,6 +165,7 @@ describe('PercyClient', () => {
'pull-request-number': client.env.pullRequest,
'parallel-nonce': client.env.parallel.nonce,
'parallel-total-shards': client.env.parallel.total,
'cli-start-time': null,
partial: client.env.partial,
tags: []
}
Expand Down Expand Up @@ -198,6 +200,7 @@ describe('PercyClient', () => {
'pull-request-number': client.env.pullRequest,
'parallel-nonce': client.env.parallel.nonce,
'parallel-total-shards': client.env.parallel.total,
'cli-start-time': null,
partial: client.env.partial,
tags: []
}
Expand Down Expand Up @@ -279,6 +282,7 @@ describe('PercyClient', () => {
'pull-request-number': client.env.pullRequest,
'parallel-nonce': client.env.parallel.nonce,
'parallel-total-shards': client.env.parallel.total,
'cli-start-time': null,
partial: client.env.partial,
tags: []
}
Expand Down Expand Up @@ -317,6 +321,46 @@ describe('PercyClient', () => {
'pull-request-number': client.env.pullRequest,
'parallel-nonce': client.env.parallel.nonce,
'parallel-total-shards': client.env.parallel.total,
'cli-start-time': null,
partial: client.env.partial,
tags: [{ id: null, name: 'tag1' }, { id: null, name: 'tag2' }]
}
}));
});

it('creates a new build with cliStartTime', async () => {
client = new PercyClient({
token: 'PERCY_TOKEN',
labels: 'tag1,tag2'
});
await expectAsync(client.createBuild({ projectType: 'web', cliStartTime })).toBeResolvedTo({
data: {
id: '123',
attributes: {
'build-number': 1,
'web-url': 'https://percy.io/test/test/123'
}
}
});

expect(api.requests['/builds'][0].body.data)
.toEqual(jasmine.objectContaining({
attributes: {
branch: client.env.git.branch,
type: 'web',
'target-branch': client.env.target.branch,
'target-commit-sha': client.env.target.commit,
'commit-sha': client.env.git.sha,
'commit-committed-at': client.env.git.committedAt,
'commit-author-name': client.env.git.authorName,
'commit-author-email': client.env.git.authorEmail,
'commit-committer-name': client.env.git.committerName,
'commit-committer-email': client.env.git.committerEmail,
'commit-message': client.env.git.message,
'pull-request-number': client.env.pullRequest,
'parallel-nonce': client.env.parallel.nonce,
'parallel-total-shards': client.env.parallel.total,
'cli-start-time': cliStartTime,
partial: client.env.partial,
tags: [{ id: null, name: 'tag1' }, { id: null, name: 'tag2' }]
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/percy.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class Percy {
labels ??= config.percy?.labels;
deferUploads ??= config.percy?.deferUploads;
this.config = config;
this.cliStartTime = null;

if (testing) loglevel = 'silent';
if (loglevel) this.loglevel(loglevel);
Expand Down Expand Up @@ -159,6 +160,7 @@ export class Percy {
// already starting or started
if (this.readyState != null) return;
this.readyState = 0;
this.cliStartTime = new Date().toISOString();

try {
if (process.env.PERCY_CLIENT_ERROR_LOGS !== 'false') {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export function createSnapshotsQueue(percy) {
.handle('start', async () => {
try {
build = percy.build = {};
let { data } = await percy.client.createBuild({ projectType: percy.projectType });
let { data } = await percy.client.createBuild({ projectType: percy.projectType, cliStartTime: percy.cliStartTime });
let url = data.attributes['web-url'];
let number = data.attributes['build-number'];
percy.client.buildType = data.attributes?.type;
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/percy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,24 @@ describe('Percy', () => {
expect(percy.projectType).toEqual('web');
});

it('has cliStartTime', async () => {
let time = '2024-08-20T13:38:18.570Z';
percy = new Percy({ token: 'PERCY_TOKEN' });
// abort when the browser is launched
let ctrl = new AbortController();
spyOn(Date.prototype, 'toISOString').and.returnValue(time);
spyOn(percy.browser, 'launch');
spyOn(Date, 'now').and.returnValue(time);
spyOn(percy.client, 'createBuild').and.callThrough();

await generatePromise(percy.yield.start(), ctrl.signal);
expect(percy.cliStartTime).toEqual(time);
expect(percy.client.createBuild).toHaveBeenCalledWith(jasmine.objectContaining({
projectType: null,
cliStartTime: time
}));
});

it('syncQueue is created', async () => {
percy = new Percy({ token: 'PERCY_TOKEN', projectType: 'web' });

Expand Down

0 comments on commit 21c64d9

Please sign in to comment.