From 70d83e32e3545f0bb4cea9f11a2c80ee7bd96d78 Mon Sep 17 00:00:00 2001 From: Barry Gordon Date: Wed, 25 Aug 2021 20:54:09 +0100 Subject: [PATCH] Return meaningful error types depending on what failed --- __tests__/main.test.ts | 37 ++++++++++++++++++++++++++++++++++++- src/main.ts | 23 +++++++++++++++++++---- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index de29be6..8e467da 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -2,6 +2,7 @@ import * as core from '@actions/core' import {Context} from '@actions/github/lib/context' import {APIClient} from '../src/api-client' import {Updater} from '../src/updater' +import {ImageService} from '../src/image-service' import * as inputs from '../src/inputs' import {run} from '../src/main' @@ -178,6 +179,40 @@ describe('run', () => { }) }) + describe('when there is an error pulling images', () => { + beforeEach(() => { + jest + .spyOn(ImageService, 'pull') + .mockImplementationOnce( + jest.fn(async () => + Promise.reject(new Error('error pulling an image')) + ) + ) + + process.env.GITHUB_EVENT_PATH = eventFixturePath('default') + process.env.GITHUB_EVENT_NAME = 'workflow_dispatch' + context = new Context() + }) + + test('it fails the workflow', async () => { + await run(context) + + expect(core.setFailed).toHaveBeenCalledWith( + expect.stringContaining('error pulling an image') + ) + }) + + test('it relays a failure message to the dependabot service', async () => { + await run(context) + + expect(reportJobErrorSpy).toHaveBeenCalledWith({ + 'error-type': 'actions_workflow_image', + 'error-detail': 'error pulling an image' + }) + expect(markJobAsProcessedSpy).toHaveBeenCalled() + }) + }) + describe('when there is an error running the update', () => { beforeEach(() => { jest @@ -205,7 +240,7 @@ describe('run', () => { await run(context) expect(reportJobErrorSpy).toHaveBeenCalledWith({ - 'error-type': 'actions_workflow_unknown', + 'error-type': 'actions_workflow_updater', 'error-detail': 'error running the update' }) expect(markJobAsProcessedSpy).toHaveBeenCalled() diff --git a/src/main.ts b/src/main.ts index 2e0f4d8..4baedca 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,7 +13,9 @@ export const PROXY_IMAGE_NAME = 'docker.pkg.github.com/github/dependabot-update-job-proxy:latest' export enum DependabotErrorType { - Unknown = 'actions_workflow_unknown' + Unknown = 'actions_workflow_unknown', + Image = 'actions_workflow_image', + UpdateRun = 'actions_workflow_updater' } export async function run(context: Context): Promise { @@ -42,10 +44,23 @@ export async function run(context: Context): Promise { details, credentials ) - await ImageService.pull(UPDATER_IMAGE_NAME) - await ImageService.pull(PROXY_IMAGE_NAME) - await updater.runUpdater() + try { + await ImageService.pull(UPDATER_IMAGE_NAME) + await ImageService.pull(PROXY_IMAGE_NAME) + } catch (error) { + failJob(apiClient, error, DependabotErrorType.Image) + core.setFailed(error.message) + return + } + + try { + await updater.runUpdater() + } catch (error) { + failJob(apiClient, error, DependabotErrorType.UpdateRun) + core.setFailed(error.message) + return + } core.info('🤖 ~fin~') } catch (error) { // Update Dependabot API on the job failure