Skip to content

Commit

Permalink
Compress use of core.error and setFailed into one
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Mar 8, 2022
1 parent 6269879 commit 2e5a468
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
6 changes: 3 additions & 3 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('run', () => {
await run(context)

expect(core.setFailed).toHaveBeenCalledWith(
`Error: unexpected error retrieving job params\n\nFor more information see: https://test.dev/foo/bar/network/updates/1 (write access required)`
`Dependabot encountered an unexpected problem\n\nError: unexpected error retrieving job params\n\nFor more information see: https://test.dev/foo/bar/network/updates/1 (write access required)`
)
})

Expand Down Expand Up @@ -162,7 +162,7 @@ describe('run', () => {
await run(context)

expect(core.setFailed).toHaveBeenCalledWith(
`Error: error getting job details\n\nFor more information see: https://test.dev/foo/bar/network/updates/1 (write access required)`
`Dependabot encountered an unexpected problem\n\nError: error getting job details\n\nFor more information see: https://test.dev/foo/bar/network/updates/1 (write access required)`
)
})

Expand Down Expand Up @@ -259,7 +259,7 @@ describe('run', () => {
await run(context)

expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining('error running the update')
`Dependabot encountered an error performing the update\n\nError: error running the update\n\nFor more information see: https://test.dev/foo/bar/network/updates/1 (write access required)`
)
})

Expand Down
64 changes: 43 additions & 21 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ export async function run(context: Context): Promise<void> {
try {
await ImageService.pull(UPDATER_IMAGE_NAME)
await ImageService.pull(PROXY_IMAGE_NAME)
} catch (error) {
core.error('Error fetching updater images')

await failJob(apiClient, error, DependabotErrorType.Image)
} catch (error: any) {
await failJob(
apiClient,
'Error fetching updater images',
error,
DependabotErrorType.Image
)
return
}
core.endGroup()
Expand All @@ -71,29 +74,42 @@ export async function run(context: Context): Promise<void> {
core.info('Starting update process')

await updater.runUpdater()
} catch (error) {
} catch (error: any) {
// If we have encountered a UpdaterFetchError, the Updater will already have
// reported the error and marked the job as processed, so we only need to
// set an exit status.
if (error instanceof UpdaterFetchError) {
setFailed(
'Dependabot was unable to retrieve the files required to perform the update'
'Dependabot was unable to retrieve the files required to perform the update',
null
)
botSay('finished: unable to fetch files')
return
} else {
core.error('Error performing update')
await failJob(apiClient, error, DependabotErrorType.UpdateRun)
await failJob(
apiClient,
'Dependabot encountered an error performing the update',
error,
DependabotErrorType.UpdateRun
)
return
}
}
botSay('finished')
} catch (error) {
} catch (error: any) {
if (error instanceof CredentialFetchingError) {
core.error('Error retrieving update job credentials')
await failJob(apiClient, error, DependabotErrorType.UpdateRun)
await failJob(
apiClient,
'Dependabot was unable to retrieve job credentials',
error,
DependabotErrorType.UpdateRun
)
} else {
await failJob(apiClient, error)
await failJob(
apiClient,
'Dependabot was unable to start the update',
error
)
}

return
Expand All @@ -104,13 +120,14 @@ export async function run(context: Context): Promise<void> {
//
// We output the raw error in the Action logs and defer
// to workflow_run monitoring to detect the job failure.
setFailed(error)
setFailed('Dependabot encountered an unexpected problem', error)
botSay('finished: unexpected error')
}
}

async function failJob(
apiClient: ApiClient,
message: string,
error: Error,
errorType = DependabotErrorType.Unknown
): Promise<void> {
Expand All @@ -121,27 +138,32 @@ async function failJob(
}
})
await apiClient.markJobAsProcessed()
setFailed(error.message)
setFailed(message, error)
botSay('finished: error reported to Dependabot')
}

function botSay(message: string): void {
core.info(`🤖 ~ ${message} ~`)
}

function setFailed(message: string | Error): void {
function setFailed(message: string, error: Error | null): void {
if (jobId) {
message = [
message,
`For more information see: ${dependabotJobUrl(
jobId
)} (write access required)`
].join('\n\n')
message = [message, error, dependabotJobHelp()].filter(Boolean).join('\n\n')
}

core.setFailed(message)
}

function dependabotJobHelp(): string | null {
if (jobId) {
return `For more information see: ${dependabotJobUrl(
jobId
)} (write access required)`
} else {
return null
}
}

function dependabotJobUrl(id: number): string {
const url_parts = [
process.env.GITHUB_SERVER_URL,
Expand Down

0 comments on commit 2e5a468

Please sign in to comment.