Skip to content

Commit

Permalink
fix(comparadise): ensure all checks other than Visual Regression pass (
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored May 23, 2023
1 parent 125a176 commit 8fb87a0
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 30 deletions.
22 changes: 18 additions & 4 deletions action/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14124,12 +14124,13 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getLatestVisualRegressionStatus = void 0;
const octokit_1 = __nccwpck_require__(1428);
const github_1 = __nccwpck_require__(5438);
const constants_1 = __nccwpck_require__(9079);
const getLatestVisualRegressionStatus = async (commitHash) => {
const { data } = await octokit_1.octokit.rest.repos.listCommitStatusesForRef({
ref: commitHash,
...github_1.context.repo
});
return data.find(status => status.context === 'Visual Regression');
return data.find(status => status.context === constants_1.VISUAL_REGRESSION_CONTEXT);
};
exports.getLatestVisualRegressionStatus = getLatestVisualRegressionStatus;

Expand Down Expand Up @@ -14189,6 +14190,7 @@ const path = __importStar(__nccwpck_require__(1017));
const glob_1 = __nccwpck_require__(1957);
const comment_1 = __nccwpck_require__(5925);
const get_latest_visual_regression_status_1 = __nccwpck_require__(9737);
const constants_1 = __nccwpck_require__(9079);
const run = async () => {
const visualTestCommands = (0, core_1.getMultilineInput)('visual-test-command', { required: true });
const commitHash = (0, core_1.getInput)('commit-hash', { required: true });
Expand All @@ -14199,7 +14201,7 @@ const run = async () => {
(0, core_1.setFailed)('Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?');
return octokit_1.octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: 'Visual Regression',
context: constants_1.VISUAL_REGRESSION_CONTEXT,
state: 'failure',
description: 'Visual tests failed to execute successfully.',
...github_1.context.repo
Expand All @@ -14218,7 +14220,7 @@ const run = async () => {
}
return octokit_1.octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: 'Visual Regression',
context: constants_1.VISUAL_REGRESSION_CONTEXT,
state: 'success',
description: 'Visual tests passed!',
...github_1.context.repo
Expand All @@ -14234,7 +14236,7 @@ const run = async () => {
await (0, s3_operations_1.uploadBaseImages)();
await octokit_1.octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: 'Visual Regression',
context: constants_1.VISUAL_REGRESSION_CONTEXT,
state: 'failure',
description: diffFileCount === 0 ? 'A new visual test was created!' : 'A visual regression was detected!',
...github_1.context.repo
Expand Down Expand Up @@ -14279,6 +14281,18 @@ const uploadBaseImages = async () => {
exports.uploadBaseImages = uploadBaseImages;


/***/ }),

/***/ 9079:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.VISUAL_REGRESSION_CONTEXT = void 0;
exports.VISUAL_REGRESSION_CONTEXT = 'Visual Regression';


/***/ }),

/***/ 2877:
Expand Down
2 changes: 1 addition & 1 deletion action/dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion action/get-latest-visual-regression-status.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { octokit } from './octokit';
import { context } from '@actions/github';
import { VISUAL_REGRESSION_CONTEXT } from '../constants';

export const getLatestVisualRegressionStatus = async (commitHash: string) => {
const { data } = await octokit.rest.repos.listCommitStatusesForRef({
ref: commitHash,
...context.repo
});

return data.find(status => status.context === 'Visual Regression');
return data.find(status => status.context === VISUAL_REGRESSION_CONTEXT);
};
21 changes: 11 additions & 10 deletions action/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { exec } from '@actions/exec';
import { getInput, getMultilineInput, setFailed } from '@actions/core';
import { octokit } from './octokit';
import { sync } from 'glob';
import { VISUAL_REGRESSION_CONTEXT } from '../constants';

jest.mock('glob');
jest.mock('@actions/core');
Expand Down Expand Up @@ -45,7 +46,7 @@ describe('main', () => {
owner: 'owner',
repo: 'repo',
sha: 'sha',
context: 'Visual Regression',
context: VISUAL_REGRESSION_CONTEXT,
state: 'failure',
description: 'Visual tests failed to execute successfully.'
});
Expand All @@ -60,7 +61,7 @@ describe('main', () => {
owner: 'owner',
repo: 'repo',
sha: 'sha',
context: 'Visual Regression',
context: VISUAL_REGRESSION_CONTEXT,
state: 'success',
description: 'Visual tests passed!'
});
Expand All @@ -75,7 +76,7 @@ describe('main', () => {
owner: 'owner',
repo: 'repo',
sha: 'sha',
context: 'Visual Regression',
context: VISUAL_REGRESSION_CONTEXT,
state: 'failure',
description: 'A visual regression was detected!'
});
Expand All @@ -91,7 +92,7 @@ describe('main', () => {
owner: 'owner',
repo: 'repo',
sha: 'sha',
context: 'Visual Regression',
context: VISUAL_REGRESSION_CONTEXT,
state: 'failure',
description: 'A new visual test was created!'
});
Expand Down Expand Up @@ -119,12 +120,12 @@ describe('main', () => {
data: [
{ context: 'some context', created_at: '2023-05-21T16:51:29Z', state: 'success' },
{
context: 'Visual Regression',
context: VISUAL_REGRESSION_CONTEXT,
created_at: '2023-05-21T16:51:29Z',
state: 'failure',
description: 'A visual regression was detected!'
},
{ context: 'Visual Regression', created_at: '2023-05-21T15:51:29Z', state: 'success' }
{ context: VISUAL_REGRESSION_CONTEXT, created_at: '2023-05-21T15:51:29Z', state: 'success' }
]
});
await run();
Expand All @@ -138,8 +139,8 @@ describe('main', () => {
(octokit.rest.repos.listCommitStatusesForRef as unknown as jest.Mock).mockResolvedValue({
data: [
{ context: 'some context', created_at: '2023-05-21T16:51:29Z', state: 'success' },
{ context: 'Visual Regression', created_at: '2023-05-21T16:51:29Z', state: 'success' },
{ context: 'Visual Regression', created_at: '2023-05-21T15:51:29Z', state: 'success' }
{ context: VISUAL_REGRESSION_CONTEXT, created_at: '2023-05-21T16:51:29Z', state: 'success' },
{ context: VISUAL_REGRESSION_CONTEXT, created_at: '2023-05-21T15:51:29Z', state: 'success' }
]
});
await run();
Expand All @@ -153,12 +154,12 @@ describe('main', () => {
data: [
{ context: 'some context', created_at: '2023-05-21T16:51:29Z', state: 'success' },
{
context: 'Visual Regression',
context: VISUAL_REGRESSION_CONTEXT,
created_at: '2023-05-21T16:51:29Z',
state: 'failure',
description: 'Visual tests failed to execute successfully.'
},
{ context: 'Visual Regression', created_at: '2023-05-21T15:51:29Z', state: 'success' }
{ context: VISUAL_REGRESSION_CONTEXT, created_at: '2023-05-21T15:51:29Z', state: 'success' }
]
});
await run();
Expand Down
7 changes: 4 additions & 3 deletions action/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as path from 'path';
import { sync } from 'glob';
import { createGithubComment } from './comment';
import { getLatestVisualRegressionStatus } from './get-latest-visual-regression-status';
import { VISUAL_REGRESSION_CONTEXT } from '../constants';

export const run = async () => {
const visualTestCommands = getMultilineInput('visual-test-command', { required: true });
Expand All @@ -20,7 +21,7 @@ export const run = async () => {
setFailed('Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?');
return octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: 'Visual Regression',
context: VISUAL_REGRESSION_CONTEXT,
state: 'failure',
description: 'Visual tests failed to execute successfully.',
...context.repo
Expand All @@ -42,7 +43,7 @@ export const run = async () => {

return octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: 'Visual Regression',
context: VISUAL_REGRESSION_CONTEXT,
state: 'success',
description: 'Visual tests passed!',
...context.repo
Expand All @@ -62,7 +63,7 @@ export const run = async () => {
await uploadBaseImages();
await octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: 'Visual Regression',
context: VISUAL_REGRESSION_CONTEXT,
state: 'failure',
description: diffFileCount === 0 ? 'A new visual test was created!' : 'A visual regression was detected!',
...context.repo
Expand Down
43 changes: 38 additions & 5 deletions backend/allNonVisualChecksHavePassed.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { allNonVisualChecksHavePassed } from './allNonVisualChecksHavePassed';
import { getOctokit } from './getOctokit';
import { VISUAL_REGRESSION_CONTEXT } from '../constants';

jest.mock('./getOctokit');

Expand All @@ -17,7 +18,7 @@ describe('allNonVisualChecksHavePassed', () => {
completed_at: '2023-05-02T19:11:02Z'
},
{
name: 'visual tests',
name: VISUAL_REGRESSION_CONTEXT,
conclusion: 'failure',
completed_at: '2023-05-02T19:11:02Z'
},
Expand All @@ -36,7 +37,7 @@ describe('allNonVisualChecksHavePassed', () => {
expect(result).toBe(true);
});

it('should return false when at least one non-visual check failed', async () => {
it('should return false when at least one visual test job failed', async () => {
(getOctokit as jest.Mock).mockImplementation(() => ({
rest: {
checks: {
Expand All @@ -48,10 +49,42 @@ describe('allNonVisualChecksHavePassed', () => {
conclusion: 'success',
completed_at: '2023-05-02T19:11:02Z'
},
{
name: VISUAL_REGRESSION_CONTEXT,
conclusion: 'failure',
completed_at: '2023-05-02T19:11:02Z'
},
{
name: 'visual tests',
conclusion: 'failure',
completed_at: '2023-05-02T19:11:02Z'
}
]
}
})
}
}
}));
const result = await allNonVisualChecksHavePassed('github-owner', 'github-repo', 'sha');
expect(result).toBe(false);
});

it('should return false when at least one non-visual check failed', async () => {
(getOctokit as jest.Mock).mockImplementation(() => ({
rest: {
checks: {
listForRef: jest.fn().mockReturnValue({
data: {
check_runs: [
{
name: 'unit tests',
conclusion: 'success',
completed_at: '2023-05-02T19:11:02Z'
},
{
name: VISUAL_REGRESSION_CONTEXT,
conclusion: 'failure',
completed_at: '2023-05-02T19:11:02Z'
},
{
name: 'other tests',
Expand Down Expand Up @@ -81,7 +114,7 @@ describe('allNonVisualChecksHavePassed', () => {
completed_at: '2023-05-02T19:11:02Z'
},
{
name: 'visual tests',
name: VISUAL_REGRESSION_CONTEXT,
conclusion: 'failure',
completed_at: '2023-05-02T19:11:02Z'
},
Expand Down Expand Up @@ -118,7 +151,7 @@ describe('allNonVisualChecksHavePassed', () => {
completed_at: '2023-05-02T19:11:02Z'
},
{
name: 'visual tests',
name: VISUAL_REGRESSION_CONTEXT,
conclusion: 'failure',
completed_at: '2023-05-02T19:11:02Z'
},
Expand Down Expand Up @@ -155,7 +188,7 @@ describe('allNonVisualChecksHavePassed', () => {
completed_at: '2023-05-02T19:10:02Z'
},
{
name: 'visual tests',
name: VISUAL_REGRESSION_CONTEXT,
conclusion: 'failure',
completed_at: '2023-05-02T19:11:02Z'
},
Expand Down
5 changes: 2 additions & 3 deletions backend/allNonVisualChecksHavePassed.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { getOctokit } from './getOctokit';
import { RestEndpointMethodTypes } from '@octokit/rest';
import { groupBy, isEqual, sortBy } from 'lodash';
import { VISUAL_REGRESSION_CONTEXT } from '../constants';

type CheckRunConclusion = RestEndpointMethodTypes['checks']['listForRef']['response']['data']['check_runs'][number]['conclusion'];

const allowedConclusions: CheckRunConclusion[] = ['success', 'skipped'];
const visualTestRegex = new RegExp('visual', 'i');
const isVisualTest = (testName: string) => visualTestRegex.test(testName);

export const allNonVisualChecksHavePassed = async (owner: string, repo: string, sha: string): Promise<boolean> => {
const octokit = getOctokit(owner, repo);
Expand All @@ -16,7 +15,7 @@ export const allNonVisualChecksHavePassed = async (owner: string, repo: string,
repo,
ref: sha
});
const nonVisualChecks = data.check_runs.filter(({ name }) => !isVisualTest(name));
const nonVisualChecks = data.check_runs.filter(({ name }) => name !== VISUAL_REGRESSION_CONTEXT);
const groupedChecks = groupBy(nonVisualChecks, 'name');
const mostRecentChecks = nonVisualChecks.filter(check => {
const checksSortedByDescTime = sortBy(groupedChecks[check.name], 'completed_at').reverse();
Expand Down
3 changes: 2 additions & 1 deletion backend/updateCommitStatus.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { updateCommitStatus } from './updateCommitStatus';
import { getOctokit } from './getOctokit';
import { VISUAL_REGRESSION_CONTEXT } from '../constants';

jest.mock('./getOctokit');
const createCommitStatus = jest.fn(() => ({ catch: jest.fn() }));
Expand All @@ -20,7 +21,7 @@ describe('updateCommitStatus', () => {
sha: 'hash',
state: 'success',
description: 'Your visual tests have passed.',
context: 'Visual Regression'
context: VISUAL_REGRESSION_CONTEXT
});
});
});
3 changes: 2 additions & 1 deletion backend/updateCommitStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TRPCError } from '@trpc/server';
import { UpdateCommitStatusInput } from './schema';
import { getOctokit } from './getOctokit';
import { VISUAL_REGRESSION_CONTEXT } from '../constants';

export const updateCommitStatus = async ({ owner, repo, hash }: UpdateCommitStatusInput) => {
const octokit = getOctokit(owner, repo);
Expand All @@ -11,7 +12,7 @@ export const updateCommitStatus = async ({ owner, repo, hash }: UpdateCommitStat
sha: hash,
state: 'success',
description: 'Your visual tests have passed.',
context: 'Visual Regression'
context: VISUAL_REGRESSION_CONTEXT
})
.catch(error => {
throw new TRPCError({
Expand Down
1 change: 1 addition & 0 deletions constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const VISUAL_REGRESSION_CONTEXT = 'Visual Regression';
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"strict": true,
"noEmit": true
},
"include": ["action", "backend", "frontend"],
"include": ["action", "backend", "constants", "frontend"],
"exclude": ["dist", "node_modules"],
"references": [{ "path": "./tsconfig.node.json" }]
}

0 comments on commit 8fb87a0

Please sign in to comment.