From 808e00387ab19ab7f2c9e082fd722bd81483f3e6 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 26 Nov 2023 11:52:14 +0900 Subject: [PATCH 1/5] feat: add an input `repository_ids` https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app > repository_ids array of integers > List of repository IDs that the token should have access to --- action.yml | 5 +++++ src/create-installation-access-token.ts | 4 +++- src/parse-options.ts | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 4146f35a..e064de8c 100644 --- a/action.yml +++ b/action.yml @@ -41,6 +41,11 @@ inputs: The JSON-stringified array of the full names of the repositories the token should have access to. Defaults to all repositories that the installation can access. See https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app's `repositories`. + repository_ids: + description: >- + The JSON-stringified array of the ids of the repositories the token should have access to. + Defaults to all repositories that the installation can access. + See https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app's `repository_ids`. revoke: description: Revoke the token at the end of the job. default: true diff --git a/src/create-installation-access-token.ts b/src/create-installation-access-token.ts index 2054fad5..a760b1dd 100644 --- a/src/create-installation-access-token.ts +++ b/src/create-installation-access-token.ts @@ -14,6 +14,7 @@ export type InstallationAccessTokenCreationOptions = Readonly<{ permissions?: Record; privateKey: string; repositories?: string[]; + repositoryIDs?: string[]; }>; export const createInstallationAccessToken = async ({ @@ -23,6 +24,7 @@ export const createInstallationAccessToken = async ({ permissions, privateKey, repositories, + repositoryIDs, }: InstallationAccessTokenCreationOptions): Promise => { try { const app = createAppAuth({ @@ -48,7 +50,7 @@ export const createInstallationAccessToken = async ({ data: { token }, } = await octokit.request( "POST /app/installations/{installation_id}/access_tokens", - { installation_id: installationId, permissions, repositories }, + { installation_id: installationId, permissions, repositories, repository_ids: repositoryIDs }, ); return token; } catch (error: unknown) { diff --git a/src/parse-options.ts b/src/parse-options.ts index 07d2efd6..3edaf4c6 100644 --- a/src/parse-options.ts +++ b/src/parse-options.ts @@ -46,6 +46,12 @@ export const parseOptions = (): InstallationAccessTokenCreationOptions => { : undefined; debug(`Requested repositories: ${JSON.stringify(repositories)}.`); + const repositoryIDsInput = getInput("repository_ids"); + const repositoryIDs = repositoryIDsInput + ? (JSON.parse(repositoryIDsInput) as string[]) + : undefined; + debug(`Requested repository_ids: ${JSON.stringify(repositoryIDs)}.`); + return { appId, githubApiUrl, @@ -53,5 +59,6 @@ export const parseOptions = (): InstallationAccessTokenCreationOptions => { permissions, privateKey, repositories, + repositoryIDs, }; }; From 3c31de96ea19d23e5110edd08b20422631ee9f75 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 26 Nov 2023 11:54:33 +0900 Subject: [PATCH 2/5] docs: update the document --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 6e233697..7308eb73 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,13 @@ jobs: # repositories: >- # ["actions/toolkit", "github/docs"] + # Optional. + # List of repository IDs that the token should have access to + # https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app + # https://docs.github.com/en/actions/learn-github-actions/contexts#github-context + # repository_ids: >- + # ["${{github.repository_id}}"] + # Optional. # revoke: false From e5a964ca456251ad65cf9fbe9884101d01acfe07 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 26 Nov 2023 12:13:20 +0900 Subject: [PATCH 3/5] fix: fix the type of repository_id. repository_id is an integer --- src/create-installation-access-token.ts | 2 +- src/parse-options.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/create-installation-access-token.ts b/src/create-installation-access-token.ts index a760b1dd..4a2a40a0 100644 --- a/src/create-installation-access-token.ts +++ b/src/create-installation-access-token.ts @@ -14,7 +14,7 @@ export type InstallationAccessTokenCreationOptions = Readonly<{ permissions?: Record; privateKey: string; repositories?: string[]; - repositoryIDs?: string[]; + repositoryIDs?: number[]; }>; export const createInstallationAccessToken = async ({ diff --git a/src/parse-options.ts b/src/parse-options.ts index 3edaf4c6..ebb81028 100644 --- a/src/parse-options.ts +++ b/src/parse-options.ts @@ -48,7 +48,7 @@ export const parseOptions = (): InstallationAccessTokenCreationOptions => { const repositoryIDsInput = getInput("repository_ids"); const repositoryIDs = repositoryIDsInput - ? (JSON.parse(repositoryIDsInput) as string[]) + ? (JSON.parse(repositoryIDsInput) as number[]) : undefined; debug(`Requested repository_ids: ${JSON.stringify(repositoryIDs)}.`); From e00a8511f61d5d891adb49b5a68b9fde4ff8a45c Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 26 Nov 2023 12:14:46 +0900 Subject: [PATCH 4/5] docs: fix the example of repository_ids --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7308eb73..a75194de 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ jobs: # https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app # https://docs.github.com/en/actions/learn-github-actions/contexts#github-context # repository_ids: >- - # ["${{github.repository_id}}"] + # [${{github.repository_id}}] # Optional. # revoke: false From 0fd281f34a4510745d76a395216f104cc8d96288 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 26 Nov 2023 12:15:57 +0900 Subject: [PATCH 5/5] style: format with prettier --- src/create-installation-access-token.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/create-installation-access-token.ts b/src/create-installation-access-token.ts index 4a2a40a0..a536bd96 100644 --- a/src/create-installation-access-token.ts +++ b/src/create-installation-access-token.ts @@ -50,7 +50,12 @@ export const createInstallationAccessToken = async ({ data: { token }, } = await octokit.request( "POST /app/installations/{installation_id}/access_tokens", - { installation_id: installationId, permissions, repositories, repository_ids: repositoryIDs }, + { + installation_id: installationId, + permissions, + repositories, + repository_ids: repositoryIDs, + }, ); return token; } catch (error: unknown) {