Skip to content

Commit

Permalink
feat: add diff-id input param as alternative to commit-hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayali Warule committed Nov 22, 2024
1 parent f378763 commit c180779
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 31 deletions.
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ inputs:
required: true
commit-hash:
description: 'The commit hash'
required: true
required: false
diff-id:
description: 'Alternative to commit-hash as a unique identifier for visual tests.'
required: false
screenshots-directory:
description: 'The directory where your visual tests expect screenshots to be'
required: false
Expand Down
56 changes: 44 additions & 12 deletions action/dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -29440,21 +29440,20 @@ var downloadBaseImages = async () => {
`aws s3 cp s3://${bucketName}/${BASE_IMAGES_DIRECTORY} ${screenshotsDirectory} --recursive`
);
};
var uploadAllImages = async () => {
var uploadAllImages = async (hash) => {
const bucketName = (0, import_core.getInput)("bucket-name", { required: true });
const screenshotsDirectory = (0, import_core.getInput)("screenshots-directory");
const commitHash = (0, import_core.getInput)("commit-hash", { required: true });
const packagePaths = (0, import_core.getInput)("package-paths")?.split(",");
if (packagePaths) {
return (0, import_bluebird.map)(
packagePaths,
(packagePath) => (0, import_exec.exec)(
`aws s3 cp ${screenshotsDirectory}/${packagePath} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${commitHash}/${packagePath} --recursive`
`aws s3 cp ${screenshotsDirectory}/${packagePath} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${hash}/${packagePath} --recursive`
)
);
}
return (0, import_exec.exec)(
`aws s3 cp ${screenshotsDirectory} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${commitHash} --recursive`
`aws s3 cp ${screenshotsDirectory} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${hash} --recursive`
);
};
var uploadBaseImages = async (newFilePaths) => {
Expand Down Expand Up @@ -35866,19 +35865,18 @@ var import_core4 = __toESM(require_core());
// src/build-comparadise-url.ts
var import_core3 = __toESM(require_core());
var import_github2 = __toESM(require_github());
var buildComparadiseUrl = () => {
var buildComparadiseUrl = (hash) => {
const bucketName = (0, import_core3.getInput)("bucket-name", { required: true });
const commitHash = (0, import_core3.getInput)("commit-hash", { required: true });
const comparadiseHost = (0, import_core3.getInput)("comparadise-host");
const { owner, repo } = import_github2.context.repo;
return `${comparadiseHost}/?hash=${commitHash}&owner=${owner}&repo=${repo}&bucket=${bucketName}`;
return `${comparadiseHost}/?hash=${hash}&owner=${owner}&repo=${repo}&bucket=${bucketName}`;
};

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

// src/run.ts
var VISUAL_TEST_EXECUTION_FAILURE = "Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?";
var VISUAL_TESTS_PASSED = "All visual tests passed, and no diffs found!";
var run = async () => {
const runAttempt = Number(process.env.GITHUB_RUN_ATTEMPT);
const isRetry = runAttempt > 1;
const visualTestCommands = (0, import_core6.getMultilineInput)("visual-test-command", {
required: true
});
const commitHash = (0, import_core6.getInput)("commit-hash", { required: true });
const commitHash = (0, import_core6.getInput)("commit-hash");
const diffId = (0, import_core6.getInput)("diff-id");
if (!commitHash && !diffId) {
(0, import_core6.setFailed)("You must provide either commit-hash or diff-id.");
return;
}
if (commitHash && diffId) {
(0, import_core6.setFailed)("You cannot provide both commit-hash and diff-id. Choose one.");
return;
}
const screenshotsDirectory = (0, import_core6.getInput)("screenshots-directory");
await downloadBaseImages();
const visualTestExitCode = await Promise.all(
Expand All @@ -35964,7 +35973,6 @@ var run = async () => {
const numVisualTestFailures = visualTestExitCode.filter(
(code) => code !== 0
).length;
const latestVisualRegressionStatus = await getLatestVisualRegressionStatus(commitHash);
const screenshotsPath = path3.join(process.cwd(), screenshotsDirectory);
const filesInScreenshotDirectory = sync(`${screenshotsPath}/**`, { absolute: false }) || [];
const diffFilePaths = filesInScreenshotDirectory.filter(
Expand All @@ -35983,6 +35991,29 @@ var run = async () => {
return count;
}, 0);
const newFileCount = newFilePaths.length;
if (diffId) {
if (numVisualTestFailures > diffFileCount) {
(0, import_core6.setFailed)(VISUAL_TEST_EXECUTION_FAILURE);
return;
}
if (diffFileCount === 0) {
if (newFileCount === 0) {
(0, import_core6.info)(VISUAL_TESTS_PASSED);
return;
} else if (newFileCount > 0) {
(0, import_core6.info)(
`New visual tests found! ${newFileCount} images will be uploaded as new base images.`
);
await uploadBaseImages(newFilePaths);
return;
}
}
(0, import_core6.info)(
`A visual regression was detected. ${diffFileCount} visual differences found`
);
await uploadAllImages(diffId);
return;
}
if (numVisualTestFailures > diffFileCount) {
(0, import_core6.setFailed)(
"Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?"
Expand All @@ -35995,6 +36026,7 @@ var run = async () => {
...import_github6.context.repo
});
}
const latestVisualRegressionStatus = await getLatestVisualRegressionStatus(commitHash);
if (diffFileCount === 0 && newFileCount === 0) {
(0, import_core6.info)("All visual tests passed, and no diffs found!");
if (isRetry) {
Expand Down Expand Up @@ -36038,13 +36070,13 @@ var run = async () => {
...import_github6.context.repo
});
}
await uploadAllImages();
await uploadAllImages(commitHash);
await octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: VISUAL_REGRESSION_CONTEXT,
state: "failure",
description: "A visual regression was detected. Check Comparadise!",
target_url: buildComparadiseUrl(),
target_url: buildComparadiseUrl(commitHash),
...import_github6.context.repo
});
await createGithubComment();
Expand Down
2 changes: 1 addition & 1 deletion action/dist/main.js.map

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions action/src/build-comparadise-url.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { getInput } from '@actions/core';
import { context } from '@actions/github';

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

return `${comparadiseHost}/?hash=${commitHash}&owner=${owner}&repo=${repo}&bucket=${bucketName}`;
return `${comparadiseHost}/?hash=${hash}&owner=${owner}&repo=${repo}&bucket=${bucketName}`;
};
2 changes: 1 addition & 1 deletion action/src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { buildComparadiseUrl } from './build-comparadise-url';
export const createGithubComment = async () => {
const commitHash = getInput('commit-hash', { required: true });
const comparadiseHost = getInput('comparadise-host');
const comparadiseUrl = buildComparadiseUrl();
const comparadiseUrl = buildComparadiseUrl(commitHash);
const comparadiseLink = comparadiseHost
? `[Comparadise](${comparadiseUrl})`
: 'Comparadise';
Expand Down
59 changes: 50 additions & 9 deletions action/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,31 @@ import {
import { buildComparadiseUrl } from './build-comparadise-url';
import { disableAutoMerge } from './disableAutoMerge';

const VISUAL_TEST_EXECUTION_FAILURE =
'Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?';
const VISUAL_TESTS_PASSED = 'All visual tests passed, and no diffs found!';

export const run = async () => {
const runAttempt = Number(process.env.GITHUB_RUN_ATTEMPT);
const isRetry = runAttempt > 1;
const visualTestCommands = getMultilineInput('visual-test-command', {
required: true
});
const commitHash = getInput('commit-hash', { required: true });
const commitHash = getInput('commit-hash');
const diffId = getInput('diff-id');

if (!commitHash && !diffId) {
setFailed('Please provide either a commit-hash or a diff-id.');
return;
}

if (commitHash && diffId) {
setFailed(
'You cannot provide both commit-hash and diff-id. Please choose one.'
);
return;
}

const screenshotsDirectory = getInput('screenshots-directory');

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

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

if (numVisualTestFailures > diffFileCount) {
setFailed(
'Visual tests failed to execute successfully. Perhaps one failed to take a screenshot?'
if (diffId) {
if (numVisualTestFailures > diffFileCount) {
setFailed(VISUAL_TEST_EXECUTION_FAILURE);
return;
}
if (diffFileCount === 0) {
if (newFileCount === 0) {
info(VISUAL_TESTS_PASSED);
return;
} else if (newFileCount > 0) {
info(
`New visual tests found! ${newFileCount} images will be uploaded as new base images.`
);
await uploadBaseImages(newFilePaths);
return;
}
}
await uploadAllImages(diffId);
info(
`Visual regression detected with ${diffFileCount} visual differences. Check out the details on Comparadise: ${buildComparadiseUrl(diffId)}.`
);
return;
}

if (numVisualTestFailures > diffFileCount) {
setFailed(VISUAL_TEST_EXECUTION_FAILURE);
return octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: VISUAL_REGRESSION_CONTEXT,
Expand All @@ -79,8 +117,11 @@ export const run = async () => {
});
}

const latestVisualRegressionStatus =
await getLatestVisualRegressionStatus(commitHash);

if (diffFileCount === 0 && newFileCount === 0) {
info('All visual tests passed, and no diffs found!');
info(VISUAL_TESTS_PASSED);

if (isRetry) {
warning(
Expand Down Expand Up @@ -133,13 +174,13 @@ export const run = async () => {
});
}

await uploadAllImages();
await uploadAllImages(commitHash);
await octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: VISUAL_REGRESSION_CONTEXT,
state: 'failure',
description: 'A visual regression was detected. Check Comparadise!',
target_url: buildComparadiseUrl(),
target_url: buildComparadiseUrl(commitHash),
...context.repo
});
await createGithubComment();
Expand Down
7 changes: 3 additions & 4 deletions action/src/s3-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,20 @@ export const downloadBaseImages = async () => {
);
};

export const uploadAllImages = async () => {
export const uploadAllImages = async (hash: string) => {
const bucketName = getInput('bucket-name', { required: true });
const screenshotsDirectory = getInput('screenshots-directory');
const commitHash = getInput('commit-hash', { required: true });
const packagePaths = getInput('package-paths')?.split(',');
if (packagePaths) {
return map(packagePaths, packagePath =>
exec(
`aws s3 cp ${screenshotsDirectory}/${packagePath} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${commitHash}/${packagePath} --recursive`
`aws s3 cp ${screenshotsDirectory}/${packagePath} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${hash}/${packagePath} --recursive`
)
);
}

return exec(
`aws s3 cp ${screenshotsDirectory} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${commitHash} --recursive`
`aws s3 cp ${screenshotsDirectory} s3://${bucketName}/${NEW_IMAGES_DIRECTORY}/${hash} --recursive`
);
};

Expand Down
Loading

0 comments on commit c180779

Please sign in to comment.