Skip to content

Commit

Permalink
Move the failJob method up to the main file
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Aug 25, 2021
1 parent 5d128b3 commit 533bb57
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
34 changes: 19 additions & 15 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ 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'
Expand Down Expand Up @@ -56,7 +53,6 @@ describe('run', () => {
await run(context)

expect(markJobAsProcessedSpy).not.toHaveBeenCalled()
expect(failJobSpy).not.toHaveBeenCalled()
expect(reportJobErrorSpy).not.toHaveBeenCalled()
})
})
Expand All @@ -80,7 +76,8 @@ describe('run', () => {
test('it does not report this failed run to dependabot-api', async () => {
await run(context)

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

Expand Down Expand Up @@ -108,7 +105,8 @@ describe('run', () => {
test('it does not inform dependabot-api as it cannot instantiate a client without the params', async () => {
await run(context)

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

Expand Down Expand Up @@ -138,9 +136,11 @@ describe('run', () => {
test('it relays a failure message to the dependabot service', async () => {
await run(context)

expect(failJobSpy).toHaveBeenCalledWith(
new Error('error getting job details')
)
expect(reportJobErrorSpy).toHaveBeenCalledWith({
'error-type': 'actions_workflow_unknown',
'error-detail': 'error getting job details'
})
expect(markJobAsProcessedSpy).toHaveBeenCalled()
})
})

Expand Down Expand Up @@ -170,9 +170,11 @@ describe('run', () => {
test('it relays a failure message to the dependabot service', async () => {
await run(context)

expect(failJobSpy).toHaveBeenCalledWith(
new Error('error getting credentials')
)
expect(reportJobErrorSpy).toHaveBeenCalledWith({
'error-type': 'actions_workflow_unknown',
'error-detail': 'error getting credentials'
})
expect(markJobAsProcessedSpy).toHaveBeenCalled()
})
})

Expand Down Expand Up @@ -202,9 +204,11 @@ describe('run', () => {
test('it relays a failure message to the dependabot service', async () => {
await run(context)

expect(failJobSpy).toHaveBeenCalledWith(
new Error('error running the update')
)
expect(reportJobErrorSpy).toHaveBeenCalledWith({
'error-type': 'actions_workflow_unknown',
'error-detail': 'error running the update'
})
expect(markJobAsProcessedSpy).toHaveBeenCalled()
})
})
})
21 changes: 1 addition & 20 deletions src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ export type JobDetails = {
'package-manager': PackageManager
}

export enum JobErrorType {
Unknown = 'actions_workflow_unknown'
}

export type JobError = {
'error-type': JobErrorType
'error-type': string
'error-detail': any
}

Expand Down Expand Up @@ -95,19 +91,4 @@ export class APIClient {

return res.data.data.attributes
}

async failJob(error: Error): Promise<void> {
const jobError = this.jobErrorFor(error)
await this.reportJobError(jobError)
await this.markJobAsProcessed()
}

private jobErrorFor(error: Error): JobError {
const errorType = JobErrorType.Unknown

return {
'error-type': errorType,
'error-detail': error.message
}
}
}
18 changes: 17 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const UPDATER_IMAGE_NAME =
export const PROXY_IMAGE_NAME =
'docker.pkg.github.com/github/dependabot-update-job-proxy:latest'

export enum DependabotErrorType {
Unknown = 'actions_workflow_unknown'
}

export async function run(context: Context): Promise<void> {
try {
// Decode JobParameters:
Expand Down Expand Up @@ -45,7 +49,7 @@ export async function run(context: Context): Promise<void> {
core.info('🤖 ~fin~')
} catch (error) {
// Update Dependabot API on the job failure
apiClient.failJob(error)
failJob(apiClient, error)
core.setFailed(error.message)
}
} catch (error) {
Expand All @@ -58,4 +62,16 @@ export async function run(context: Context): Promise<void> {
}
}

async function failJob(
apiClient: APIClient,
error: Error,
errorType = DependabotErrorType.Unknown
): Promise<void> {
await apiClient.reportJobError({
'error-type': errorType,
'error-detail': error.message
})
await apiClient.markJobAsProcessed()
}

run(github.context)

0 comments on commit 533bb57

Please sign in to comment.