Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: wrap git api in try catch #595

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions action/src/create-commit-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { octokit } from './octokit';
import { context } from '@actions/github';
import { VISUAL_REGRESSION_CONTEXT } from 'shared';
import { warning } from '@actions/core';

export const createCommitStatus = async (
commitHash: string,
state: 'failure' | 'success',
description: string,
targetUrl?: string
) => {
try {
return octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: VISUAL_REGRESSION_CONTEXT,
state: state,
description: description,
...(targetUrl && {target_url: targetUrl}),
...context.repo
});
} catch (err) {
warning('Failed to update commit status.');
throw err;
}
};
File renamed without changes.
19 changes: 14 additions & 5 deletions action/src/get-latest-visual-regression-status.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { octokit } from './octokit';
import { context } from '@actions/github';
import { warning } from '@actions/core';
import { VISUAL_REGRESSION_CONTEXT } from 'shared';

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

return data.find(status => status.context === VISUAL_REGRESSION_CONTEXT);
return data.find(status => status.context === VISUAL_REGRESSION_CONTEXT);
} catch (error) {
warning(
'Failed to get latest visual regression status.'
);
warning(error as Error);
return null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using optional chaining operator wherever we are using latestVisualRegressionStatus value so returning null shouldn't break anything.

}
};
65 changes: 27 additions & 38 deletions action/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ import {
uploadAllImages
} from './s3-operations';
import { exec } from '@actions/exec';
import { octokit } from './octokit';
import { context } from '@actions/github';
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,
VISUAL_TESTS_FAILED_TO_EXECUTE
} from 'shared';
import { VISUAL_TESTS_FAILED_TO_EXECUTE } from 'shared';
import { buildComparadiseUrl } from './build-comparadise-url';
import { disableAutoMerge } from './disableAutoMerge';
import { disableAutoMerge } from './disable-auto-merge';
import { createCommitStatus } from './create-commit-status';

export const run = async () => {
const runAttempt = Number(process.env.GITHUB_RUN_ATTEMPT);
Expand All @@ -42,8 +38,6 @@ export const run = async () => {
code => code !== 0
).length;

const latestVisualRegressionStatus =
await getLatestVisualRegressionStatus(commitHash);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it down just before the usage.

const screenshotsPath = path.join(process.cwd(), screenshotsDirectory);
const filesInScreenshotDirectory =
sync(`${screenshotsPath}/**`, { absolute: false }) || [];
Expand All @@ -70,15 +64,16 @@ 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,
state: 'failure',
description: VISUAL_TESTS_FAILED_TO_EXECUTE,
...context.repo
});
return createCommitStatus(
commitHash,
'failure',
VISUAL_TESTS_FAILED_TO_EXECUTE
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrapped it in common method so that we can print the error message => Failed to update commit status.
This would give little more clarity on reason why job failed.

}

const latestVisualRegressionStatus =
await getLatestVisualRegressionStatus(commitHash);

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

Expand All @@ -94,13 +89,11 @@ export const run = async () => {
return;
}

return octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: VISUAL_REGRESSION_CONTEXT,
state: 'success',
description: `Visual tests passed${isRetry ? ' on retry' : ''}!`,
...context.repo
});
return createCommitStatus(
commitHash,
'success',
`Visual tests passed${isRetry ? ' on retry' : ''}!`
);
}

if (
Expand All @@ -124,23 +117,19 @@ export const run = async () => {
`New visual tests found! ${newFileCount} images will be uploaded as new base images.`
);
await uploadBaseImages(newFilePaths);
return octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: VISUAL_REGRESSION_CONTEXT,
state: 'success',
description: 'New base images were created!',
...context.repo
});
return createCommitStatus(
commitHash,
'success',
'New base images were created!'
)
}

await uploadAllImages();
await octokit.rest.repos.createCommitStatus({
sha: commitHash,
context: VISUAL_REGRESSION_CONTEXT,
state: 'failure',
description: 'A visual regression was detected. Check Comparadise!',
target_url: buildComparadiseUrl(),
...context.repo
});
await createCommitStatus(
commitHash,
'failure',
'A visual regression was detected. Check Comparadise!',
buildComparadiseUrl()
);
await createGithubComment();
};
Loading