Skip to content

Commit 0a29629

Browse files
committed
create a new allow_github_apps input to allow Apps to be disabled if need be
1 parent 723b728 commit 0a29629

File tree

6 files changed

+64
-1
lines changed

6 files changed

+64
-1
lines changed

__tests__/functions/valid-permissions.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ beforeEach(() => {
1111
jest.spyOn(core, 'setOutput').mockImplementation(() => {})
1212
jest.spyOn(core, 'info').mockImplementation(() => {})
1313
process.env.INPUT_PERMISSIONS = 'write,admin'
14+
process.env.INPUT_ALLOW_GITHUB_APPS = true
1415

1516
context = {
1617
repo: {
@@ -67,6 +68,7 @@ test('determines that a user has does not valid permissions to invoke the Action
6768
'👋 __monalisa__, seems as if you have not write/admin permissions in this repo, permissions: read'
6869
)
6970
expect(setOutputMock).toHaveBeenCalledWith('actor', 'monalisa')
71+
expect(setOutputMock).toHaveBeenCalledWith('actor_type', 'User')
7072
})
7173

7274
test('fails to get actor information', async () => {
@@ -114,6 +116,7 @@ test('determines that a GitHub App has valid permissions', async () => {
114116

115117
expect(await validPermissions(octokit, context)).toEqual(true)
116118
expect(setOutputMock).toHaveBeenCalledWith('actor', 'github-actions[bot]')
119+
expect(setOutputMock).toHaveBeenCalledWith('actor_type', 'Bot')
117120
expect(infoMock).toHaveBeenCalledWith(
118121
`🔍 Detected actor type: Bot (${context.actor})`
119122
)
@@ -142,6 +145,28 @@ test('determines that a GitHub App does not have valid permissions', async () =>
142145
'👋 __monalisa[bot]__ does not have "issues" permission set to "write". Current permissions: {"issues":"read"}'
143146
)
144147
expect(setOutputMock).toHaveBeenCalledWith('actor', 'monalisa[bot]')
148+
expect(setOutputMock).toHaveBeenCalledWith('actor_type', 'Bot')
149+
})
150+
151+
test('fails since GitHub Apps are configured to be rejected', async () => {
152+
process.env.INPUT_ALLOW_GITHUB_APPS = false
153+
context.actor = 'monalisa[bot]'
154+
155+
octokit.rest.users.getByUsername = jest.fn().mockReturnValueOnce({
156+
status: 200,
157+
data: {
158+
type: 'Bot'
159+
}
160+
})
161+
162+
expect(await validPermissions(octokit, context)).toEqual(
163+
'GitHub Apps are not allowed to use this Action based on the "allow_github_apps" input.'
164+
)
165+
expect(setOutputMock).toHaveBeenCalledWith('actor', 'monalisa[bot]')
166+
expect(setOutputMock).toHaveBeenCalledWith('actor_type', 'Bot')
167+
expect(infoMock).toHaveBeenCalledWith(
168+
`🔍 Detected actor type: Bot (${context.actor})`
169+
)
145170
})
146171

147172
test('fails to fetch installation details for GitHub App', async () => {

__tests__/schemas/action.schema.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ inputs:
197197
default:
198198
type: string
199199
required: true
200+
allow_github_apps:
201+
description:
202+
type: string
203+
required: true
204+
required:
205+
type: boolean
206+
required: true
207+
default:
208+
type: string
209+
required: true
200210

201211
# outputs section
202212
outputs:
@@ -264,3 +274,7 @@ outputs:
264274
description:
265275
type: string
266276
required: true
277+
actor_type:
278+
description:
279+
type: string
280+
required: true

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ inputs:
7272
description: 'If set to "true", allow forks to bypass the review requirement if the operation is being made on a pull request from a fork. This option is potentially dangerous if you are checking out code in your workflow as a result of invoking this Action. If the code you are checking out has not been reviewed, then you might open yourself up to a TOCTOU vulnerability. You should always ensure that the code you are checking out has been reviewed, and that you checkout an exact commit sha rather than a ref.'
7373
required: true
7474
default: "false"
75+
allow_github_apps:
76+
description: 'If set to "true", allow GitHub Apps to interact with or trigger this Action view issue/pull_request comments. If you want to explicitly prevent GitHub Apps from invoking this Action, then set this to "false".'
77+
required: true
78+
default: "true"
7579
outputs:
7680
triggered:
7781
description: 'The string "true" if the trigger was found, otherwise the string "false" - Just because the workflow was triggered does not mean it should continue. This is a step 1/2 check'
@@ -105,6 +109,8 @@ outputs:
105109
description: 'The ref if being used in the context of a pull request'
106110
base_ref:
107111
description: The base ref that the pull request is merging into (if available and run in the context of a pull request)
112+
actor_type:
113+
description: 'The type of user/actor that triggered the IssueOps command. Values can be "User" or "Bot"'
108114
runs:
109115
using: "node20"
110116
main: "dist/index.js"

dist/index.js

Lines changed: 9 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/valid-permissions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export async function validPermissions(octokit, context) {
1111
core.getInput('permissions', {required: true})
1212
)
1313

14+
const allowGitHubApps = core.getBooleanInput('allow_github_apps', {
15+
required: true
16+
})
17+
1418
core.setOutput('actor', context.actor)
1519

1620
// Get Actor Type from GitHub API
@@ -24,9 +28,14 @@ export async function validPermissions(octokit, context) {
2428

2529
const actorType = userRes.data.type // "User" or "Bot"
2630
core.info(`🔍 Detected actor type: ${actorType} (${context.actor})`)
31+
core.setOutput('actor_type', actorType)
2732

2833
// Handle GitHub Apps (Bots)
2934
if (actorType === 'Bot') {
35+
if (!allowGitHubApps) {
36+
return `GitHub Apps are not allowed to use this Action based on the "allow_github_apps" input.`
37+
}
38+
3039
// Fetch installation details for the GitHub App
3140
const installationRes = await octokit.rest.apps.getRepoInstallation({
3241
...context.repo

0 commit comments

Comments
 (0)