Ensure we fail out cleanly if there is an exception

This commit is contained in:
Barry Gordon
2021-06-02 15:22:41 +01:00
parent 1438237def
commit c51850a258
2 changed files with 29 additions and 12 deletions

View File

@@ -144,3 +144,16 @@ test('if there are multiple dependencies, it summarizes them', async () => {
expect(core.setOutput).toBeCalledWith('dependency-type', 'direct:production')
expect(core.setOutput).toBeCalledWith('update-type', 'version-update:semver-major')
})
test("it sets the action to failed if there is an unexpected exception", async () => {
jest.spyOn(core, 'getInput').mockReturnValue('mock-token')
jest.spyOn(dependabotCommits, 'getMessage').mockImplementation(jest.fn(
() => Promise.reject( new Error("Something bad happened!") )
))
await run()
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining('Something bad happened!')
)
})

View File

@@ -16,24 +16,28 @@ export async function run (): Promise<void> {
return
}
const githubClient = github.getOctokit(token)
try {
const githubClient = github.getOctokit(token)
// Validate the job
const commitMessage = await verifiedCommits.getMessage(githubClient, github.context)
// Validate the job
const commitMessage = await verifiedCommits.getMessage(githubClient, github.context)
if (commitMessage) {
// Parse metadata
core.info('Parsing Dependabot metadata')
if (commitMessage) {
// Parse metadata
core.info('Parsing Dependabot metadata')
const updatedDependencies = updateMetadata.parse(commitMessage)
const updatedDependencies = updateMetadata.parse(commitMessage)
if (updatedDependencies.length > 0) {
output.set(updatedDependencies)
if (updatedDependencies.length > 0) {
output.set(updatedDependencies)
} else {
core.setFailed('PR does not contain metadata, nothing to do.')
}
} else {
core.setFailed('PR does not contain metadata, nothing to do.')
core.setFailed('PR is not from Dependabot, nothing to do.')
}
} else {
core.setFailed('PR is not from Dependabot, nothing to do.')
} catch (error) {
core.setFailed(error.message);
}
}