Skip to content

Commit

Permalink
Return meaningful error types depending on what failed
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Aug 25, 2021
1 parent 533bb57 commit 70d83e3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
37 changes: 36 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
23 changes: 19 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand Down Expand Up @@ -42,10 +44,23 @@ export async function run(context: Context): Promise<void> {
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
Expand Down

0 comments on commit 70d83e3

Please sign in to comment.