Skip to content

Commit 79d41f4

Browse files
author
Sayali Warule
committed
feat: add diff-id input param as alternative to commit-hash
1 parent f378763 commit 79d41f4

File tree

8 files changed

+327
-31
lines changed

8 files changed

+327
-31
lines changed

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ inputs:
1010
required: true
1111
commit-hash:
1212
description: 'The commit hash'
13-
required: true
13+
required: false
14+
diff-id:
15+
description: 'Alternative to commit-hash as a unique identifier for visual tests.'
16+
required: false
1417
screenshots-directory:
1518
description: 'The directory where your visual tests expect screenshots to be'
1619
required: false

action/dist/main.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29440,21 +29440,20 @@ var downloadBaseImages = async () => {
2944029440
`aws s3 cp s3://${bucketName}/${BASE_IMAGES_DIRECTORY} ${screenshotsDirectory} --recursive`
2944129441
);
2944229442
};
29443-
var uploadAllImages = async () => {
29443+
var uploadAllImages = async (hash) => {
2944429444
const bucketName = (0, import_core.getInput)("bucket-name", { required: true });
2944529445
const screenshotsDirectory = (0, import_core.getInput)("screenshots-directory");
29446-
const commitHash = (0, import_core.getInput)("commit-hash", { required: true });
2944729446
const packagePaths = (0, import_core.getInput)("package-paths")?.split(",");
2944829447
if (packagePaths) {
2944929448
return (0, import_bluebird.map)(
2945029449
packagePaths,
2945129450
(packagePath) => (0, import_exec.exec)(
29452-
`aws s3 cp ${screenshotsDirectory}/${packagePath} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${commitHash}/${packagePath} --recursive`
29451+
`aws s3 cp ${screenshotsDirectory}/${packagePath} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${hash}/${packagePath} --recursive`
2945329452
)
2945429453
);
2945529454
}
2945629455
return (0, import_exec.exec)(
29457-
`aws s3 cp ${screenshotsDirectory} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${commitHash} --recursive`
29456+
`aws s3 cp ${screenshotsDirectory} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${hash} --recursive`
2945829457
);
2945929458
};
2946029459
var uploadBaseImages = async (newFilePaths) => {
@@ -35866,19 +35865,18 @@ var import_core4 = __toESM(require_core());
3586635865
// src/build-comparadise-url.ts
3586735866
var import_core3 = __toESM(require_core());
3586835867
var import_github2 = __toESM(require_github());
35869-
var buildComparadiseUrl = () => {
35868+
var buildComparadiseUrl = (hash) => {
3587035869
const bucketName = (0, import_core3.getInput)("bucket-name", { required: true });
35871-
const commitHash = (0, import_core3.getInput)("commit-hash", { required: true });
3587235870
const comparadiseHost = (0, import_core3.getInput)("comparadise-host");
3587335871
const { owner, repo } = import_github2.context.repo;
35874-
return `${comparadiseHost}/?hash=${commitHash}&owner=${owner}&repo=${repo}&bucket=${bucketName}`;
35872+
return `${comparadiseHost}/?hash=${hash}&owner=${owner}&repo=${repo}&bucket=${bucketName}`;
3587535873
};
3587635874

3587735875
// src/comment.ts
3587835876
var createGithubComment = async () => {
3587935877
const commitHash = (0, import_core4.getInput)("commit-hash", { required: true });
3588035878
const comparadiseHost = (0, import_core4.getInput)("comparadise-host");
35881-
const comparadiseUrl = buildComparadiseUrl();
35879+
const comparadiseUrl = buildComparadiseUrl(commitHash);
3588235880
const comparadiseLink = comparadiseHost ? `[Comparadise](${comparadiseUrl})` : "Comparadise";
3588335881
const comparadiseBaseComment = `**Visual tests failed!**
3588435882
Check out the diffs on ${comparadiseLink}! :palm_tree:`;
@@ -35949,13 +35947,24 @@ var disableAutoMerge = async (commitHash) => {
3594935947
};
3595035948

3595135949
// src/run.ts
35950+
var VISUAL_TEST_EXECUTION_FAILURE = "Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?";
35951+
var VISUAL_TESTS_PASSED = "All visual tests passed, and no diffs found!";
3595235952
var run = async () => {
3595335953
const runAttempt = Number(process.env.GITHUB_RUN_ATTEMPT);
3595435954
const isRetry = runAttempt > 1;
3595535955
const visualTestCommands = (0, import_core6.getMultilineInput)("visual-test-command", {
3595635956
required: true
3595735957
});
35958-
const commitHash = (0, import_core6.getInput)("commit-hash", { required: true });
35958+
const commitHash = (0, import_core6.getInput)("commit-hash");
35959+
const diffId = (0, import_core6.getInput)("diff-id");
35960+
if (!commitHash && !diffId) {
35961+
(0, import_core6.setFailed)("You must provide either commit-hash or diff-id.");
35962+
return;
35963+
}
35964+
if (commitHash && diffId) {
35965+
(0, import_core6.setFailed)("You cannot provide both commit-hash and diff-id. Choose one.");
35966+
return;
35967+
}
3595935968
const screenshotsDirectory = (0, import_core6.getInput)("screenshots-directory");
3596035969
await downloadBaseImages();
3596135970
const visualTestExitCode = await Promise.all(
@@ -35964,7 +35973,6 @@ var run = async () => {
3596435973
const numVisualTestFailures = visualTestExitCode.filter(
3596535974
(code) => code !== 0
3596635975
).length;
35967-
const latestVisualRegressionStatus = await getLatestVisualRegressionStatus(commitHash);
3596835976
const screenshotsPath = path3.join(process.cwd(), screenshotsDirectory);
3596935977
const filesInScreenshotDirectory = sync(`${screenshotsPath}/**`, { absolute: false }) || [];
3597035978
const diffFilePaths = filesInScreenshotDirectory.filter(
@@ -35983,6 +35991,29 @@ var run = async () => {
3598335991
return count;
3598435992
}, 0);
3598535993
const newFileCount = newFilePaths.length;
35994+
if (diffId) {
35995+
if (numVisualTestFailures > diffFileCount) {
35996+
(0, import_core6.setFailed)(VISUAL_TEST_EXECUTION_FAILURE);
35997+
return;
35998+
}
35999+
if (diffFileCount === 0) {
36000+
if (newFileCount === 0) {
36001+
(0, import_core6.info)(VISUAL_TESTS_PASSED);
36002+
return;
36003+
} else if (newFileCount > 0) {
36004+
(0, import_core6.info)(
36005+
`New visual tests found! ${newFileCount} images will be uploaded as new base images.`
36006+
);
36007+
await uploadBaseImages(newFilePaths);
36008+
return;
36009+
}
36010+
}
36011+
(0, import_core6.info)(
36012+
`A visual regression was detected. ${diffFileCount} visual differences found`
36013+
);
36014+
await uploadAllImages(diffId);
36015+
return;
36016+
}
3598636017
if (numVisualTestFailures > diffFileCount) {
3598736018
(0, import_core6.setFailed)(
3598836019
"Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?"
@@ -35995,6 +36026,7 @@ var run = async () => {
3599536026
...import_github6.context.repo
3599636027
});
3599736028
}
36029+
const latestVisualRegressionStatus = await getLatestVisualRegressionStatus(commitHash);
3599836030
if (diffFileCount === 0 && newFileCount === 0) {
3599936031
(0, import_core6.info)("All visual tests passed, and no diffs found!");
3600036032
if (isRetry) {
@@ -36038,13 +36070,13 @@ var run = async () => {
3603836070
...import_github6.context.repo
3603936071
});
3604036072
}
36041-
await uploadAllImages();
36073+
await uploadAllImages(commitHash);
3604236074
await octokit.rest.repos.createCommitStatus({
3604336075
sha: commitHash,
3604436076
context: VISUAL_REGRESSION_CONTEXT,
3604536077
state: "failure",
3604636078
description: "A visual regression was detected. Check Comparadise!",
36047-
target_url: buildComparadiseUrl(),
36079+
target_url: buildComparadiseUrl(commitHash),
3604836080
...import_github6.context.repo
3604936081
});
3605036082
await createGithubComment();

action/dist/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { getInput } from '@actions/core';
22
import { context } from '@actions/github';
33

4-
export const buildComparadiseUrl = () => {
4+
export const buildComparadiseUrl = (hash: string) => {
55
const bucketName = getInput('bucket-name', { required: true });
6-
const commitHash = getInput('commit-hash', { required: true });
76
const comparadiseHost = getInput('comparadise-host');
87
const { owner, repo } = context.repo;
98

10-
return `${comparadiseHost}/?hash=${commitHash}&owner=${owner}&repo=${repo}&bucket=${bucketName}`;
9+
return `${comparadiseHost}/?hash=${hash}&owner=${owner}&repo=${repo}&bucket=${bucketName}`;
1110
};

action/src/comment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { buildComparadiseUrl } from './build-comparadise-url';
66
export const createGithubComment = async () => {
77
const commitHash = getInput('commit-hash', { required: true });
88
const comparadiseHost = getInput('comparadise-host');
9-
const comparadiseUrl = buildComparadiseUrl();
9+
const comparadiseUrl = buildComparadiseUrl(commitHash);
1010
const comparadiseLink = comparadiseHost
1111
? `[Comparadise](${comparadiseUrl})`
1212
: 'Comparadise';

action/src/run.ts

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,31 @@ import {
2424
import { buildComparadiseUrl } from './build-comparadise-url';
2525
import { disableAutoMerge } from './disableAutoMerge';
2626

27+
const VISUAL_TEST_EXECUTION_FAILURE =
28+
'Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?';
29+
const VISUAL_TESTS_PASSED = 'All visual tests passed, and no diffs found!';
30+
2731
export const run = async () => {
2832
const runAttempt = Number(process.env.GITHUB_RUN_ATTEMPT);
2933
const isRetry = runAttempt > 1;
3034
const visualTestCommands = getMultilineInput('visual-test-command', {
3135
required: true
3236
});
33-
const commitHash = getInput('commit-hash', { required: true });
37+
const commitHash = getInput('commit-hash');
38+
const diffId = getInput('diff-id');
39+
40+
if (!commitHash && !diffId) {
41+
setFailed('Please provide either a commit-hash or a diff-id.');
42+
return;
43+
}
44+
45+
if (commitHash && diffId) {
46+
setFailed(
47+
'You cannot provide both commit-hash and diff-id. Please choose one.'
48+
);
49+
return;
50+
}
51+
3452
const screenshotsDirectory = getInput('screenshots-directory');
3553

3654
await downloadBaseImages();
@@ -42,8 +60,6 @@ export const run = async () => {
4260
code => code !== 0
4361
).length;
4462

45-
const latestVisualRegressionStatus =
46-
await getLatestVisualRegressionStatus(commitHash);
4763
const screenshotsPath = path.join(process.cwd(), screenshotsDirectory);
4864
const filesInScreenshotDirectory =
4965
sync(`${screenshotsPath}/**`, { absolute: false }) || [];
@@ -66,10 +82,32 @@ export const run = async () => {
6682
}, 0);
6783
const newFileCount = newFilePaths.length;
6884

69-
if (numVisualTestFailures > diffFileCount) {
70-
setFailed(
71-
'Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?'
85+
if (diffId) {
86+
if (numVisualTestFailures > diffFileCount) {
87+
setFailed(VISUAL_TEST_EXECUTION_FAILURE);
88+
return;
89+
}
90+
if (diffFileCount === 0) {
91+
if (newFileCount === 0) {
92+
info(VISUAL_TESTS_PASSED);
93+
return;
94+
} else if (newFileCount > 0) {
95+
info(
96+
`New visual tests found! ${newFileCount} images will be uploaded as new base images.`
97+
);
98+
await uploadBaseImages(newFilePaths);
99+
return;
100+
}
101+
}
102+
await uploadAllImages(diffId);
103+
info(
104+
`Visual regression detected with ${diffFileCount} visual differences. Check out the details on Comparadise: ${buildComparadiseUrl(diffId)}.`
72105
);
106+
return;
107+
}
108+
109+
if (numVisualTestFailures > diffFileCount) {
110+
setFailed(VISUAL_TEST_EXECUTION_FAILURE);
73111
return octokit.rest.repos.createCommitStatus({
74112
sha: commitHash,
75113
context: VISUAL_REGRESSION_CONTEXT,
@@ -79,8 +117,11 @@ export const run = async () => {
79117
});
80118
}
81119

120+
const latestVisualRegressionStatus =
121+
await getLatestVisualRegressionStatus(commitHash);
122+
82123
if (diffFileCount === 0 && newFileCount === 0) {
83-
info('All visual tests passed, and no diffs found!');
124+
info(VISUAL_TESTS_PASSED);
84125

85126
if (isRetry) {
86127
warning(
@@ -133,13 +174,13 @@ export const run = async () => {
133174
});
134175
}
135176

136-
await uploadAllImages();
177+
await uploadAllImages(commitHash);
137178
await octokit.rest.repos.createCommitStatus({
138179
sha: commitHash,
139180
context: VISUAL_REGRESSION_CONTEXT,
140181
state: 'failure',
141182
description: 'A visual regression was detected. Check Comparadise!',
142-
target_url: buildComparadiseUrl(),
183+
target_url: buildComparadiseUrl(commitHash),
143184
...context.repo
144185
});
145186
await createGithubComment();

action/src/s3-operations.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,20 @@ export const downloadBaseImages = async () => {
2828
);
2929
};
3030

31-
export const uploadAllImages = async () => {
31+
export const uploadAllImages = async (hash: string) => {
3232
const bucketName = getInput('bucket-name', { required: true });
3333
const screenshotsDirectory = getInput('screenshots-directory');
34-
const commitHash = getInput('commit-hash', { required: true });
3534
const packagePaths = getInput('package-paths')?.split(',');
3635
if (packagePaths) {
3736
return map(packagePaths, packagePath =>
3837
exec(
39-
`aws s3 cp ${screenshotsDirectory}/${packagePath} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${commitHash}/${packagePath} --recursive`
38+
`aws s3 cp ${screenshotsDirectory}/${packagePath} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${hash}/${packagePath} --recursive`
4039
)
4140
);
4241
}
4342

4443
return exec(
45-
`aws s3 cp ${screenshotsDirectory} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${commitHash} --recursive`
44+
`aws s3 cp ${screenshotsDirectory} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${hash} --recursive`
4645
);
4746
};
4847

0 commit comments

Comments
 (0)