Skip to content
New issue

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

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

Already on GitHub? # to your account

✨ Check review-state in build:wait #1205

Merged
merged 7 commits into from
Mar 14, 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
10 changes: 7 additions & 3 deletions packages/cli-build/src/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export const wait = command('wait', {
name: 'fail-on-changes',
description: 'Exit with an error when diffs are found',
short: 'f'
}, {
name: 'pass-if-approved',
description: "Doesn't exit with an error if the build is approved, regardless of if diffs are found.",
inclusive: ['fail-on-changes']
}],

examples: [
Expand Down Expand Up @@ -124,12 +128,12 @@ function failureMessage(type, {

// Return true or false if a build is considered failing or not
function isFailing({
attributes: { state, 'total-comparisons-diff': diffs } = {}
} = {}, { failOnChanges }) {
attributes: { state, 'review-state': reviewState, 'total-comparisons-diff': diffs } = {}
} = {}, { failOnChanges, passIfApproved }) {
// not pending and not processing
return state != null && state !== 'pending' && state !== 'processing' &&
// not finished or finished with diffs
(state !== 'finished' || (failOnChanges && !!diffs));
(state !== 'finished' || (failOnChanges && !!diffs && !(passIfApproved && reviewState === 'approved')));
}

export default wait;
57 changes: 57 additions & 0 deletions packages/cli-build/test/wait.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,54 @@ describe('percy build:wait', () => {
]));
});

it('errors on diffs if the pass-if-approved is on but diffs are unreviewed', async () => {
api.reply('/builds/123', () => [200, build({
'total-comparisons-diff': 16,
'review-state': 'unreviewed',
state: 'finished'
})]);

await expectAsync(wait(['--build=123', '-f', '--pass-if-approved'])).toBeRejected();

expect(logger.stderr).toEqual([]);
expect(logger.stdout).toEqual([
'[percy] Build #10 finished! https://percy.io/test/test/123',
'[percy] Found 16 changes'
]);
});

it('errors on diffs if the pass-if-approved is on but diffs have changes requested', async () => {
api.reply('/builds/123', () => [200, build({
'total-comparisons-diff': 16,
'review-state': 'changes_requested',
state: 'finished'
})]);

await expectAsync(wait(['--build=123', '-f', '--pass-if-approved'])).toBeRejected();

expect(logger.stderr).toEqual([]);
expect(logger.stdout).toEqual([
'[percy] Build #10 finished! https://percy.io/test/test/123',
'[percy] Found 16 changes'
]);
});

it('does not error on diffs if the review status is approved and pass-if-approved is on', async () => {
api.reply('/builds/123', () => [200, build({
'total-comparisons-diff': 16,
'review-state': 'approved',
state: 'finished'
})]);

await wait(['--build=123', '-f', '--pass-if-approved']);

expect(logger.stderr).toEqual([]);
expect(logger.stdout).toEqual(jasmine.arrayContaining([
'[percy] Build #10 finished! https://percy.io/test/test/123',
'[percy] Found 16 changes'
]));
});

it('does not error when diffs are not found', async () => {
api.reply('/builds/123', () => [200, build({
'total-comparisons-diff': 0,
Expand Down Expand Up @@ -272,5 +320,14 @@ describe('percy build:wait', () => {
'[percy] Error: unrecognized_reason'
]));
});

it('logs an error if --pass-if-approved is provided without --fail-on-changes', async () => {
await expectAsync(wait(['--build=123', '--pass-if-approved'])).toBeRejected();

expect(logger.stdout).toEqual([]);
expect(logger.stderr).toEqual(jasmine.arrayContaining([
"[percy] ParseError: Options must be used together: '--pass-if-approved', '-f, --fail-on-changes'"
]));
});
});
});