Skip to content

Commit

Permalink
Merge pull request #14 from dependabot/brrygrdn/actor-must-be-dependabot
Browse files Browse the repository at this point in the history
Verify the triggering actor is Dependabot
  • Loading branch information
Barry Gordon authored and GitHub committed Oct 18, 2021
2 parents f2ebb06 + af5e4b7 commit b3b80dc
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 42 deletions.
17 changes: 0 additions & 17 deletions __tests__/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,3 @@ test('loads dynamic', () => {
expect(params?.jobToken).toEqual('xxx')
expect(params?.credentialsToken).toEqual('yyy')
})

test('loads workflow_dispatch', () => {
const ctx = new Context()
ctx.eventName = 'workflow_dispatch'
ctx.payload = {
inputs: {
jobId: '1',
jobToken: 'xxx',
credentialsToken: 'yyy'
}
}

const params = getJobParameters(ctx)
expect(params?.jobId).toEqual(1)
expect(params?.jobToken).toEqual('xxx')
expect(params?.credentialsToken).toEqual('yyy')
})
44 changes: 38 additions & 6 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('run', () => {
describe('when the run follows the happy path', () => {
beforeAll(() => {
process.env.GITHUB_EVENT_PATH = eventFixturePath('default')
process.env.GITHUB_EVENT_NAME = 'workflow_dispatch'
process.env.GITHUB_EVENT_NAME = 'dynamic'
process.env.GITHUB_ACTOR = 'dependabot[bot]'
context = new Context()
})

Expand All @@ -58,10 +59,36 @@ describe('run', () => {
})
})

describe('when the action is triggered on by a different actor', () => {
beforeAll(() => {
process.env.GITHUB_EVENT_PATH = eventFixturePath('default')
process.env.GITHUB_EVENT_NAME = 'dynamic'
process.env.GITHUB_ACTOR = 'classic-rando'
context = new Context()
})

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

expect(core.setFailed).not.toHaveBeenCalled()
expect(core.info).toHaveBeenCalledWith(
'This workflow can only be triggered by Dependabot.'
)
})

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

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

describe('when the action is triggered on an unsupported event', () => {
beforeAll(() => {
process.env.GITHUB_EVENT_PATH = eventFixturePath('default')
process.env.GITHUB_EVENT_NAME = 'issue_created'
process.env.GITHUB_ACTOR = 'dependabot[bot]'
context = new Context()
})

Expand Down Expand Up @@ -91,7 +118,8 @@ describe('run', () => {
)

process.env.GITHUB_EVENT_PATH = eventFixturePath('default')
process.env.GITHUB_EVENT_NAME = 'workflow_dispatch'
process.env.GITHUB_EVENT_NAME = 'dynamic'
process.env.GITHUB_ACTOR = 'dependabot[bot]'
context = new Context()
})

Expand Down Expand Up @@ -122,7 +150,8 @@ describe('run', () => {
)

process.env.GITHUB_EVENT_PATH = eventFixturePath('default')
process.env.GITHUB_EVENT_NAME = 'workflow_dispatch'
process.env.GITHUB_EVENT_NAME = 'dynamic'
process.env.GITHUB_ACTOR = 'dependabot[bot]'
context = new Context()
})

Expand Down Expand Up @@ -153,7 +182,8 @@ describe('run', () => {
)

process.env.GITHUB_EVENT_PATH = eventFixturePath('default')
process.env.GITHUB_EVENT_NAME = 'workflow_dispatch'
process.env.GITHUB_EVENT_NAME = 'dynamic'
process.env.GITHUB_ACTOR = 'dependabot[bot]'
context = new Context()
})

Expand Down Expand Up @@ -189,7 +219,8 @@ describe('run', () => {
)

process.env.GITHUB_EVENT_PATH = eventFixturePath('default')
process.env.GITHUB_EVENT_NAME = 'workflow_dispatch'
process.env.GITHUB_EVENT_NAME = 'dynamic'
process.env.GITHUB_ACTOR = 'dependabot[bot]'
context = new Context()
})

Expand Down Expand Up @@ -225,7 +256,8 @@ describe('run', () => {
)

process.env.GITHUB_EVENT_PATH = eventFixturePath('default')
process.env.GITHUB_EVENT_NAME = 'workflow_dispatch'
process.env.GITHUB_EVENT_NAME = 'dynamic'
process.env.GITHUB_ACTOR = 'dependabot[bot]'
context = new Context()
})

Expand Down
21 changes: 14 additions & 7 deletions dist/main/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/main/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ cli

const options = cli.opts()
const ctx = new Context()
ctx.eventName = 'workflow_dispatch'
ctx.eventName = 'dynamic'
ctx.actor = 'dependabot[bot]'
ctx.payload = {
inputs: options
}
Expand Down
17 changes: 9 additions & 8 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import {Context} from '@actions/github/lib/context'
import {WorkflowDispatchEvent} from '@octokit/webhooks-types'
import {JobParameters} from './api-client'

const DYNAMIC = 'dynamic'

export function getJobParameters(ctx: Context): JobParameters | null {
switch (ctx.eventName) {
case 'dynamic':
case 'workflow_dispatch':
return fromWorkflowInputs(ctx)
if (ctx.eventName === DYNAMIC) {
return fromWorkflowInputs(ctx)
} else {
core.info(
`Dependabot Updater Action does not support '${ctx.eventName}' events.`
)
return null
}
core.info(
`Dependabot Updater Action does not support '${ctx.eventName}' events.`
)
return null
}

function fromWorkflowInputs(ctx: Context): JobParameters {
Expand Down
11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {Updater} from './updater'
import {ApiClient} from './api-client'
import axios from 'axios'

const DEPENDABOT_ACTOR = 'dependabot[bot]'

export const UPDATER_IMAGE_NAME =
'docker.pkg.github.com/dependabot/dependabot-updater:latest'
export const PROXY_IMAGE_NAME =
Expand All @@ -21,12 +23,19 @@ export enum DependabotErrorType {
export async function run(context: Context): Promise<void> {
try {
core.info('🤖 ~ starting update ~')

if (context.actor !== DEPENDABOT_ACTOR) {
core.info('This workflow can only be triggered by Dependabot.')
core.info('🤖 ~ finished: nothing to do ~')
return // TODO: This should be setNeutral in future
}

// Decode JobParameters
const params = getJobParameters(context)
if (params === null) {
core.info('No job parameters')
core.info('🤖 ~ finished: nothing to do ~')
return
return // TODO: This should be setNeutral in future
}

core.setSecret(params.jobToken)
Expand Down

0 comments on commit b3b80dc

Please sign in to comment.