Skip to content

Commit 723b728

Browse files
committed
fix tests and bundle with npm run all
1 parent 6bac163 commit 723b728

File tree

4 files changed

+72
-21
lines changed

4 files changed

+72
-21
lines changed

__tests__/functions/valid-permissions.test.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ import * as core from '@actions/core'
22
import {validPermissions} from '../../src/functions/valid-permissions'
33

44
const setOutputMock = jest.spyOn(core, 'setOutput')
5+
const infoMock = jest.spyOn(core, 'info')
56

67
var octokit
78
var context
89
beforeEach(() => {
910
jest.clearAllMocks()
1011
jest.spyOn(core, 'setOutput').mockImplementation(() => {})
12+
jest.spyOn(core, 'info').mockImplementation(() => {})
1113
process.env.INPUT_PERMISSIONS = 'write,admin'
1214

1315
context = {
16+
repo: {
17+
owner: 'corp',
18+
repo: 'test'
19+
},
1420
actor: 'monalisa'
1521
}
1622

@@ -42,6 +48,9 @@ beforeEach(() => {
4248
test('determines that a user has valid permissions to invoke the Action', async () => {
4349
expect(await validPermissions(octokit, context)).toEqual(true)
4450
expect(setOutputMock).toHaveBeenCalledWith('actor', 'monalisa')
51+
expect(infoMock).toHaveBeenCalledWith(
52+
`🔍 Detected actor type: User (${context.actor})`
53+
)
4554
})
4655

4756
test('determines that a user has does not valid permissions to invoke the Action', async () => {
@@ -60,6 +69,17 @@ test('determines that a user has does not valid permissions to invoke the Action
6069
expect(setOutputMock).toHaveBeenCalledWith('actor', 'monalisa')
6170
})
6271

72+
test('fails to get actor information', async () => {
73+
octokit.rest.users.getByUsername = jest.fn().mockReturnValue({
74+
status: 500
75+
})
76+
77+
expect(await validPermissions(octokit, context)).toEqual(
78+
'Fetch user details returns non-200 status: 500'
79+
)
80+
expect(setOutputMock).toHaveBeenCalledWith('actor', 'monalisa')
81+
})
82+
6383
test('fails to get actor permissions due to a bad status code', async () => {
6484
octokit.rest.repos.getCollaboratorPermissionLevel = jest
6585
.fn()
@@ -76,14 +96,14 @@ test('fails to get actor permissions due to a bad status code', async () => {
7696
test('determines that a GitHub App has valid permissions', async () => {
7797
context.actor = 'github-actions[bot]'
7898

79-
octokit.rest.users.getByUsername.mockReturnValueOnce({
99+
octokit.rest.users.getByUsername = jest.fn().mockReturnValueOnce({
80100
status: 200,
81101
data: {
82102
type: 'Bot'
83103
}
84104
})
85105

86-
octokit.rest.apps.getRepoInstallation.mockReturnValueOnce({
106+
octokit.rest.apps.getRepoInstallation = jest.fn().mockReturnValueOnce({
87107
status: 200,
88108
data: {
89109
permissions: {
@@ -94,19 +114,22 @@ test('determines that a GitHub App has valid permissions', async () => {
94114

95115
expect(await validPermissions(octokit, context)).toEqual(true)
96116
expect(setOutputMock).toHaveBeenCalledWith('actor', 'github-actions[bot]')
117+
expect(infoMock).toHaveBeenCalledWith(
118+
`🔍 Detected actor type: Bot (${context.actor})`
119+
)
97120
})
98121

99122
test('determines that a GitHub App does not have valid permissions', async () => {
100123
context.actor = 'monalisa[bot]'
101124

102-
octokit.rest.users.getByUsername.mockReturnValueOnce({
125+
octokit.rest.users.getByUsername = jest.fn().mockReturnValueOnce({
103126
status: 200,
104127
data: {
105128
type: 'Bot'
106129
}
107130
})
108131

109-
octokit.rest.apps.getRepoInstallation.mockReturnValueOnce({
132+
octokit.rest.apps.getRepoInstallation = jest.fn().mockReturnValueOnce({
110133
status: 200,
111134
data: {
112135
permissions: {
@@ -124,14 +147,14 @@ test('determines that a GitHub App does not have valid permissions', async () =>
124147
test('fails to fetch installation details for GitHub App', async () => {
125148
context.actor = 'monalisa[bot]'
126149

127-
octokit.rest.users.getByUsername.mockReturnValueOnce({
150+
octokit.rest.users.getByUsername = jest.fn().mockReturnValueOnce({
128151
status: 200,
129152
data: {
130153
type: 'Bot'
131154
}
132155
})
133156

134-
octokit.rest.apps.getRepoInstallation.mockReturnValueOnce({
157+
octokit.rest.apps.getRepoInstallation = jest.fn().mockReturnValueOnce({
135158
status: 500
136159
})
137160

dist/index.js

Lines changed: 33 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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,33 @@ export async function validPermissions(octokit, context) {
1414
core.setOutput('actor', context.actor)
1515

1616
// Get Actor Type from GitHub API
17-
const userRes = await octokit.rest.users.getByUsername(
18-
{
17+
const userRes = await octokit.rest.users.getByUsername({
1918
username: context.actor
20-
}
21-
)
19+
})
2220

2321
if (userRes.status !== 200) {
2422
return `Fetch user details returns non-200 status: ${userRes.status}`
2523
}
2624

27-
const actorType = userRes.data.type; // "User" or "Bot"
28-
core.info(`🔍 Detected actor type: ${actorType} (${context.actor})`);
25+
const actorType = userRes.data.type // "User" or "Bot"
26+
core.info(`🔍 Detected actor type: ${actorType} (${context.actor})`)
2927

3028
// Handle GitHub Apps (Bots)
3129
if (actorType === 'Bot') {
3230
// Fetch installation details for the GitHub App
33-
const installationRes = await octokit.rest.apps.getRepoInstallation(
34-
{
35-
owner: context.repo.owner,
36-
repo: context.repo.repo
37-
}
38-
)
31+
const installationRes = await octokit.rest.apps.getRepoInstallation({
32+
...context.repo
33+
})
3934

4035
if (installationRes.status !== 200) {
4136
return `Failed to fetch GitHub App installation details: Status ${installationRes.status}`
4237
}
4338

44-
const appPermissions = installationRes.data.permissions || {};
39+
const appPermissions = installationRes.data.permissions || {}
4540

4641
// Ensure the bot has "issues" permission set to "write"
4742
if (appPermissions.issues !== 'write') {
48-
return `👋 __${context.actor}__ does not have "issues" permission set to "write". Current permissions: ${JSON.stringify(appPermissions)}`;
43+
return `👋 __${context.actor}__ does not have "issues" permission set to "write". Current permissions: ${JSON.stringify(appPermissions)}`
4944
}
5045

5146
return true

0 commit comments

Comments
 (0)