Handle API errors specifically

This commit is contained in:
Barry Gordon
2021-06-02 17:20:24 +01:00
parent c51850a258
commit fca28e5a1d
3 changed files with 55 additions and 15 deletions

36
dist/index.js generated vendored
View File

@@ -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);
}
});
}

View File

@@ -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!')
)
})

View File

@@ -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<void> {
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)
}
}