Skip to content

Commit

Permalink
Clarify the inner/outer failure loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Aug 25, 2021
1 parent f5b8139 commit 5d128b3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 25 deletions.
74 changes: 64 additions & 10 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ jest.mock('../src/updater')
describe('run', () => {
let context: Context

let failJobSpy: any
let markJobAsProcessedSpy: any
let reportJobErrorSpy: any

beforeEach(async () => {
failJobSpy = jest.spyOn(APIClient.prototype, 'failJob')

markJobAsProcessedSpy = jest.spyOn(
APIClient.prototype,
'markJobAsProcessed'
)
reportJobErrorSpy = jest.spyOn(APIClient.prototype, 'reportJobError')

jest.spyOn(core, 'info').mockImplementation(jest.fn())
jest.spyOn(core, 'setFailed').mockImplementation(jest.fn())
})
Expand All @@ -39,6 +51,14 @@ describe('run', () => {
expect.stringContaining('🤖 ~fin~')
)
})

test('it defers reporting back to dependabot-api to the updater itself', async () => {
await run(context)

expect(markJobAsProcessedSpy).not.toHaveBeenCalled()
expect(failJobSpy).not.toHaveBeenCalled()
expect(reportJobErrorSpy).not.toHaveBeenCalled()
})
})

describe('when the action is triggered on an unsupported event', () => {
Expand All @@ -48,16 +68,20 @@ describe('run', () => {
context = new Context()
})

test('it explains the event is unsupported without logging to dependabot-api', async () => {
test('it fails the workflow', async () => {
await run(context)

expect(core.setFailed).not.toHaveBeenCalled()
expect(core.info).toHaveBeenCalledWith(
expect.stringContaining(
"Dependabot Updater Action does not support 'issue_created' events."
)
"Dependabot Updater Action does not support 'issue_created' events."
)
})

test('it does not report this failed run to dependabot-api', async () => {
await run(context)

expect(failJobSpy).not.toHaveBeenCalled()
})
})

describe('when there is an error retrieving job parameters', () => {
Expand All @@ -73,13 +97,19 @@ describe('run', () => {
context = new Context()
})

test('it relays an error to dependabot-api and marks the job as processed', async () => {
test('it fails the workflow with the raw error', async () => {
await run(context)

expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining('unexpected error retrieving job params')
new Error('unexpected error retrieving job params')
)
})

test('it does not inform dependabot-api as it cannot instantiate a client without the params', async () => {
await run(context)

expect(failJobSpy).not.toHaveBeenCalled()
})
})

describe('when there is an error retrieving job details from DependabotAPI', () => {
Expand All @@ -97,13 +127,21 @@ describe('run', () => {
context = new Context()
})

test('it relays an error to dependabot-api and marks the job as processed', async () => {
test('it fails the workflow', async () => {
await run(context)

expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining('error getting job details')
)
})

test('it relays a failure message to the dependabot service', async () => {
await run(context)

expect(failJobSpy).toHaveBeenCalledWith(
new Error('error getting job details')
)
})
})

describe('when there is an error retrieving job credentials from DependabotAPI', () => {
Expand All @@ -121,17 +159,25 @@ describe('run', () => {
context = new Context()
})

test('it relays an error to dependabot-api and marks the job as processed', async () => {
test('it fails the workflow', async () => {
await run(context)

expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining('error getting credentials')
)
})

test('it relays a failure message to the dependabot service', async () => {
await run(context)

expect(failJobSpy).toHaveBeenCalledWith(
new Error('error getting credentials')
)
})
})

describe('when there is an error running the update', () => {
beforeAll(() => {
beforeEach(() => {
jest
.spyOn(Updater.prototype, 'runUpdater')
.mockImplementationOnce(
Expand All @@ -145,12 +191,20 @@ describe('run', () => {
context = new Context()
})

test('it relays an error to dependabot-api and marks the job as processed', async () => {
test('it fails the workflow', async () => {
await run(context)

expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining('error running the update')
)
})

test('it relays a failure message to the dependabot service', async () => {
await run(context)

expect(failJobSpy).toHaveBeenCalledWith(
new Error('error running the update')
)
})
})
})
2 changes: 1 addition & 1 deletion src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type JobDetails = {
}

export enum JobErrorType {
Unknown = 'actions_runner_unknown'
Unknown = 'actions_workflow_unknown'
}

export type JobError = {
Expand Down
40 changes: 26 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,33 @@ export async function run(context: Context): Promise<void> {

const client = axios.create({baseURL: params.dependabotAPIURL})
const apiClient = new APIClient(client, params)
const details = await apiClient.getJobDetails()
const credentials = await apiClient.getCredentials()
const updater = new Updater(
UPDATER_IMAGE_NAME,
PROXY_IMAGE_NAME,
apiClient,
details,
credentials
)
await ImageService.pull(UPDATER_IMAGE_NAME)
await ImageService.pull(PROXY_IMAGE_NAME)

await updater.runUpdater()
core.info('🤖 ~fin~')

try {
const details = await apiClient.getJobDetails()
const credentials = await apiClient.getCredentials()
const updater = new Updater(
UPDATER_IMAGE_NAME,
PROXY_IMAGE_NAME,
apiClient,
details,
credentials
)
await ImageService.pull(UPDATER_IMAGE_NAME)
await ImageService.pull(PROXY_IMAGE_NAME)

await updater.runUpdater()
core.info('🤖 ~fin~')
} catch (error) {
// Update Dependabot API on the job failure
apiClient.failJob(error)
core.setFailed(error.message)
}
} catch (error) {
// If we've reached this point, we do not have a viable
// API client to report back to Dependabot API.
//
// We output the raw error in the Action logs and defer
// to workflow_run monitoring to detect the job failure.
core.setFailed(error)
}
}
Expand Down

0 comments on commit 5d128b3

Please sign in to comment.