-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
}; |
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; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -42,8 +38,6 @@ export const run = async () => { | |
code => code !== 0 | ||
).length; | ||
|
||
const latestVisualRegressionStatus = | ||
await getLatestVisualRegressionStatus(commitHash); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }) || []; | ||
|
@@ -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 | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 => |
||
} | ||
|
||
const latestVisualRegressionStatus = | ||
await getLatestVisualRegressionStatus(commitHash); | ||
|
||
if (diffFileCount === 0 && newFileCount === 0) { | ||
info('All visual tests passed, and no diffs found!'); | ||
|
||
|
@@ -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 ( | ||
|
@@ -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(); | ||
}; |
There was a problem hiding this comment.
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.