Skip to content

Commit 8c2d30c

Browse files
committed
fix bad INPUT_ALLOWED_CONTEXTS input
1 parent 4621722 commit 8c2d30c

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

__tests__/functions/context-check.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ test('checks the event context for an issue comment and finds that it is valid -
4949
})
5050
})
5151

52+
test('checks the event context and exits because bad input was used', async () => {
53+
process.env.INPUT_ALLOWED_CONTEXTS = 'bad'
54+
context.payload.issue = {}
55+
expect(await contextCheck(context)).toStrictEqual({
56+
valid: false,
57+
context: 'issue_comment'
58+
})
59+
})
60+
5261
test('checks the event context for a pr comment and finds that it is valid - when only pr comments are allowed', async () => {
5362
process.env.INPUT_ALLOWED_CONTEXTS = 'pull_request'
5463
expect(await contextCheck(context)).toStrictEqual({

dist/index.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions/context-check.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as core from '@actions/core'
22
import {stringToArray} from './string-to-array'
33

4+
const contextDefaults = ['pull_request', 'issue']
5+
46
// A simple function that checks the event context to make sure it is valid
57
// :param context: The GitHub Actions event context
68
// :returns: Map - {valid: true/false, context: 'issue'/'pull_request'}
@@ -22,6 +24,17 @@ export async function contextCheck(context) {
2224
core.getInput('allowed_contexts', {required: true})
2325
)
2426

27+
// check to see if the allowedContexts variable contains at least one item from the contextDefaults array
28+
// if it does not, log a warning and exit
29+
if (!allowedContexts.some(r => contextDefaults.includes(r))) {
30+
core.warning(
31+
`the 'allowed_contexts' input must contain at least one of the following: ${contextDefaults.join(
32+
', '
33+
)}`
34+
)
35+
return {valid: false, context: context.eventName}
36+
}
37+
2538
// check if the event is a PR
2639
const isPullRequest = context?.payload?.issue?.pull_request !== undefined
2740

0 commit comments

Comments
 (0)