Skip to content

Commit 2950cbc

Browse files
parkerbxyzgr2m
andauthored
fix: permission input handling (#243)
This pull request fixes the handling of permissions inputs. - Updated `getPermissionsFromInputs` in `lib/get-permissions-from-inputs.js` to use hyphens (`INPUT_PERMISSION-`) instead of underscores (`INPUT_PERMISSION_`) in input keys, added a check to skip empty values, and clarified behavior when no permissions are set. - Added a `shouldRetry` function to retry requests when server errors (HTTP status 500 or higher) occur in the `main` function in `lib/main.js` to prevent unnecessary retries. - Updated test cases in `tests/main-token-permissions-set.test.js` to match the new input key format with hyphens. - Added a default empty string for unset inputs (e.g., `INPUT_PERMISSION-ADMINISTRATION`) in `tests/main.js` to simulate the behavior of the Actions runner. - Updated snapshots in `tests/snapshots/index.js.md` to reflect the updated hyphenated input keys in permissions. --------- Co-authored-by: Gregor Martynus <[email protected]>
1 parent 30bf625 commit 2950cbc

File tree

6 files changed

+14
-8
lines changed

6 files changed

+14
-8
lines changed

lib/get-permissions-from-inputs.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
*/
88
export function getPermissionsFromInputs(env) {
99
return Object.entries(env).reduce((permissions, [key, value]) => {
10-
if (!key.startsWith("INPUT_PERMISSION_")) return permissions;
10+
if (!key.startsWith("INPUT_PERMISSION-")) return permissions;
11+
if (!value) return permissions;
1112

12-
const permission = key.slice("INPUT_PERMISSION_".length).toLowerCase();
13+
const permission = key.slice("INPUT_PERMISSION-".length).toLowerCase();
14+
15+
// Inherit app permissions if no permissions inputs are set
1316
if (permissions === undefined) {
1417
return { [permission]: value };
1518
}

lib/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export async function main(
8989
permissions
9090
),
9191
{
92+
shouldRetry: (error) => error.status >= 500,
9293
onFailedAttempt: (error) => {
9394
core.info(
9495
`Failed to create token for "${parsedRepositoryNames.join(

tests/main-token-permissions-set.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { test } from "./main.js";
22

33
// Verify `main` successfully sets permissions
44
await test(() => {
5-
process.env.INPUT_PERMISSION_ISSUES = `write`;
6-
process.env.INPUT_PERMISSION_PULL_REQUESTS = `read`;
5+
process.env["INPUT_PERMISSION-ISSUES"] = `write`;
6+
process.env["INPUT_PERMISSION-PULL-REQUESTS"] = `read`;
77
});

tests/main.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ so0tiQKBgGQXZaxaXhYUcxYHuCkQ3V4Vsj3ezlM92xXlP32SGFm3KgFhYy9kATxw
3838
Cax1ytZzvlrKLQyQFVK1COs2rHt7W4cJ7op7C8zXfsigXCiejnS664oAuX8sQZID
3939
x3WQZRiXlWejSMUAHuMwXrhGlltF3lw83+xAjnqsVp75kGS6OH61
4040
-----END RSA PRIVATE KEY-----`,
41+
// The Actions runner sets all inputs to empty strings if not set.
42+
"INPUT_PERMISSION-ADMINISTRATION": "",
4143
};
4244

4345
export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
@@ -61,7 +63,7 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
6163
const owner = env.INPUT_OWNER ?? env.GITHUB_REPOSITORY_OWNER;
6264
const currentRepoName = env.GITHUB_REPOSITORY.split("/")[1];
6365
const repo = encodeURIComponent(
64-
(env.INPUT_REPOSITORIES ?? currentRepoName).split(",")[0],
66+
(env.INPUT_REPOSITORIES ?? currentRepoName).split(",")[0]
6567
);
6668

6769
mockPool
@@ -77,7 +79,7 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
7779
.reply(
7880
200,
7981
{ id: mockInstallationId, app_slug: mockAppSlug },
80-
{ headers: { "content-type": "application/json" } },
82+
{ headers: { "content-type": "application/json" } }
8183
);
8284

8385
// Mock installation access token request
@@ -98,7 +100,7 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
98100
.reply(
99101
201,
100102
{ token: mockInstallationAccessToken, expires_at: mockExpiresAt },
101-
{ headers: { "content-type": "application/json" } },
103+
{ headers: { "content-type": "application/json" } }
102104
);
103105

104106
// Run the callback

tests/snapshots/index.js.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ Generated by [AVA](https://avajs.dev).
331331
--- REQUESTS ---␊
332332
GET /repos/actions/create-github-app-token/installation␊
333333
POST /app/installations/123456/access_tokens␊
334-
{"repositories":["create-github-app-token"],"permissions":{"issues":"write","pull_requests":"read"}}`
334+
{"repositories":["create-github-app-token"],"permissions":{"issues":"write","pull-requests":"read"}}`
335335

336336
## post-revoke-token-fail-response.test.js
337337

tests/snapshots/index.js.snap

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)