From fca28e5a1da005e9fbde731fa7107c32a340811c Mon Sep 17 00:00:00 2001 From: Barry Gordon Date: Wed, 2 Jun 2021 17:20:24 +0100 Subject: [PATCH] Handle API errors specifically --- dist/index.js | 36 ++++++++++++++++++++++++------------ src/main.test.ts | 27 +++++++++++++++++++++++++-- src/main.ts | 7 ++++++- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8ac5364..03e26f4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -13327,6 +13327,8 @@ __nccwpck_require__.d(__webpack_exports__, { var core = __nccwpck_require__(2186); // EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js var github = __nccwpck_require__(5438); +// EXTERNAL MODULE: ./node_modules/@octokit/request-error/dist-node/index.js +var dist_node = __nccwpck_require__(537); ;// CONCATENATED MODULE: ./src/dependabot/verified_commits.ts var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } @@ -13466,6 +13468,7 @@ var main_awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arg + function run() { return main_awaiter(this, void 0, void 0, function* () { const token = core.getInput('github-token'); @@ -13475,22 +13478,31 @@ function run() { /* eslint-enable no-template-curly-in-string */ return; } - const githubClient = github.getOctokit(token); - // Validate the job - const commitMessage = yield getMessage(githubClient, github.context); - if (commitMessage) { - // Parse metadata - core.info('Parsing Dependabot metadata'); - const updatedDependencies = parse(commitMessage); - if (updatedDependencies.length > 0) { - set(updatedDependencies); + try { + const githubClient = github.getOctokit(token); + // Validate the job + const commitMessage = yield getMessage(githubClient, github.context); + if (commitMessage) { + // Parse metadata + core.info('Parsing Dependabot metadata'); + const updatedDependencies = parse(commitMessage); + if (updatedDependencies.length > 0) { + 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) { + if (error instanceof dist_node.RequestError) { + core.setFailed(`Api Error: (${error.status}) ${error.message}`); + return; + } + core.setFailed(error.message); } }); } diff --git a/src/main.test.ts b/src/main.test.ts index 23ec33e..0ade76d 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -1,5 +1,6 @@ import * as core from '@actions/core' import { run } from './main' +import { RequestError } from '@octokit/request-error' import * as dependabotCommits from './dependabot/verified_commits' beforeEach(() => { @@ -145,10 +146,10 @@ test('if there are multiple dependencies, it summarizes them', async () => { expect(core.setOutput).toBeCalledWith('update-type', 'version-update:semver-major') }) -test("it sets the action to failed if there is an unexpected exception", async () => { +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!") ) + () => Promise.reject(new Error('Something bad happened!')) )) await run() @@ -157,3 +158,25 @@ test("it sets the action to failed if there is an unexpected exception", async ( expect.stringContaining('Something bad happened!') ) }) + +test('it sets the action to failed if there is a request error', async () => { + jest.spyOn(core, 'getInput').mockReturnValue('mock-token') + jest.spyOn(dependabotCommits, 'getMessage').mockImplementation(jest.fn( + () => Promise.reject(new RequestError('Something bad happened!', 500, { + headers: {}, + request: { + method: 'GET', + url: 'https://api.github.com/repos/dependabot/dependabot/pulls/101/commits', + headers: { + authorization: 'foo' + } + } + })) + )) + + await run() + + expect(core.setFailed).toHaveBeenCalledWith( + expect.stringContaining('(500) Something bad happened!') + ) +}) diff --git a/src/main.ts b/src/main.ts index 0daa414..8298e83 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,6 @@ import * as core from '@actions/core' import * as github from '@actions/github' +import { RequestError } from '@octokit/request-error' import * as verifiedCommits from './dependabot/verified_commits' import * as updateMetadata from './dependabot/update_metadata' import * as output from './dependabot/output' @@ -37,7 +38,11 @@ export async function run (): Promise { core.setFailed('PR is not from Dependabot, nothing to do.') } } catch (error) { - core.setFailed(error.message); + if (error instanceof RequestError) { + core.setFailed(`Api Error: (${error.status}) ${error.message}`) + return + } + core.setFailed(error.message) } }