Skip to content

Commit

Permalink
fix bad INPUT_ALLOWED_CONTEXTS input
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Aug 21, 2023
1 parent 4621722 commit 8c2d30c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
9 changes: 9 additions & 0 deletions __tests__/functions/context-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ test('checks the event context for an issue comment and finds that it is valid -
})
})

test('checks the event context and exits because bad input was used', async () => {
process.env.INPUT_ALLOWED_CONTEXTS = 'bad'
context.payload.issue = {}
expect(await contextCheck(context)).toStrictEqual({
valid: false,
context: 'issue_comment'
})
})

test('checks the event context for a pr comment and finds that it is valid - when only pr comments are allowed', async () => {
process.env.INPUT_ALLOWED_CONTEXTS = 'pull_request'
expect(await contextCheck(context)).toStrictEqual({
Expand Down
13 changes: 13 additions & 0 deletions dist/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/index.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/functions/context-check.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as core from '@actions/core'
import {stringToArray} from './string-to-array'

const contextDefaults = ['pull_request', 'issue']

// A simple function that checks the event context to make sure it is valid
// :param context: The GitHub Actions event context
// :returns: Map - {valid: true/false, context: 'issue'/'pull_request'}
Expand All @@ -22,6 +24,17 @@ export async function contextCheck(context) {
core.getInput('allowed_contexts', {required: true})
)

// check to see if the allowedContexts variable contains at least one item from the contextDefaults array
// if it does not, log a warning and exit
if (!allowedContexts.some(r => contextDefaults.includes(r))) {
core.warning(
`the 'allowed_contexts' input must contain at least one of the following: ${contextDefaults.join(
', '
)}`
)
return {valid: false, context: context.eventName}
}

// check if the event is a PR
const isPullRequest = context?.payload?.issue?.pull_request !== undefined

Expand Down

0 comments on commit 8c2d30c

Please sign in to comment.