Skip to content

Commit

Permalink
Link to the dependabot job on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Oct 21, 2021
1 parent 135c765 commit 461ba8c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('run', () => {
context = new Context()
})

test('it fails the workflow', async () => {
test('it skips the rest of the job', async () => {
await run(context)

expect(core.setFailed).not.toHaveBeenCalled()
Expand All @@ -98,7 +98,7 @@ describe('run', () => {
context = new Context()
})

test('it fails the workflow', async () => {
test('it skips the rest of the job', async () => {
await run(context)

expect(core.setFailed).not.toHaveBeenCalled()
Expand Down
43 changes: 34 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ export enum DependabotErrorType {
UpdateRun = 'actions_workflow_updater'
}

let jobId: number

export async function run(context: Context): Promise<void> {
try {
core.info('🤖 ~ starting update ~')
botSay('starting update')

// Retrieve JobParameters from the Actions environment
const params = getJobParameters(context)

// The parameters will be null if the Action environment
// is not a valid Dependabot-triggered dynamic event.
if (params === null) {
core.info('🤖 ~ finished: nothing to do ~')
botSay('finished: nothing to do')
return // TODO: This should be setNeutral in future
}

jobId = params.jobId
core.setSecret(params.jobToken)
core.setSecret(params.credentialsToken)

Expand Down Expand Up @@ -77,18 +80,18 @@ export async function run(context: Context): Promise<void> {
// reported the error and marked the job as processed, so we only need to
// set an exit status.
if (error instanceof UpdaterFetchError) {
core.setFailed(
setFailed(
'Dependabot was unable to retrieve the files required to perform the update'
)
core.info('🤖 ~ finished: unable to fetch files ~')
botSay('finished: unable to fetch files')
return
} else {
core.error('Error performing update')
await failJob(apiClient, error, DependabotErrorType.UpdateRun)
return
}
}
core.info('🤖 ~ finished ~')
botSay('finished')
} catch (error) {
await failJob(apiClient, error)
return
Expand All @@ -99,8 +102,8 @@ 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.
core.setFailed(error)
core.info('🤖 ~ finished: unexpected error ~')
setFailed(error)
botSay('finished: unexpected error')
}
}

Expand All @@ -116,8 +119,30 @@ async function failJob(
}
})
await apiClient.markJobAsProcessed()
core.setFailed(error.message)
core.info('🤖 ~ finished: error reported to Dependabot ~')
setFailed(error.message)
botSay('finished: error reported to Dependabot')
}

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

function setFailed(message: string | Error): void {
core.setFailed(message)
if (jobId) {
core.error(`For more information see: ${dependabotJobUrl(jobId)}`)
}
}

function dependabotJobUrl(id: number): string {
const url_parts = [
process.env.GITHUB_SERVER_URL,
process.env.GITHUB_REPOSITORY,
'network/updates',
id
]

return url_parts.filter(Boolean).join('/')
}

run(github.context)

0 comments on commit 461ba8c

Please sign in to comment.