From c4cf594262f9b44c82d795dfc7e16c0fd31f99ec Mon Sep 17 00:00:00 2001 From: Cliff Chapman <47404136+cliffchapmanrbx@users.noreply.github.com> Date: Thu, 11 Aug 2022 22:52:18 -0700 Subject: [PATCH 1/3] Add github_url input defaulting to GITHUB_API_URL --- action.yml | 2 ++ src/fetch-installation-token.ts | 7 +++---- src/index.ts | 9 +++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/action.yml b/action.yml index f101f814..c6490f7a 100644 --- a/action.yml +++ b/action.yml @@ -14,6 +14,8 @@ inputs: description: The full name of the repository for which the token will be requested (defaults to the current repository). permissions: description: The JSON-stringified permissions granted to the token (defaults to all the GitHub app permissions, see https://docs.github.com/en/rest/apps/apps#create-an-installation-access-token-for-an-app). + github_url: + description: The API URL of the GitHub server, such as https://api.github.com. Defaults to the environment variable GITHUB_API_URL, see https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables. outputs: token: description: An installation token for the GitHub App on the requested repository. diff --git a/src/fetch-installation-token.ts b/src/fetch-installation-token.ts index 069d7c2e..cac033db 100644 --- a/src/fetch-installation-token.ts +++ b/src/fetch-installation-token.ts @@ -1,4 +1,3 @@ -import { env } from "node:process"; import { getOctokit } from "@actions/github"; import { createAppAuth } from "@octokit/auth-app"; import { request } from "@octokit/request"; @@ -11,6 +10,7 @@ export const fetchInstallationToken = async ({ permissions, privateKey, repo, + baseUrl, }: Readonly<{ appId: string; installationId?: number; @@ -18,14 +18,13 @@ export const fetchInstallationToken = async ({ permissions?: Record; privateKey: string; repo: string; + baseUrl: URL; }>): Promise => { const app = createAppAuth({ appId, privateKey, request: request.defaults({ - // GITHUB_API_URL is part of GitHub Actions' built-in environment variables. - // See https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables. - baseUrl: env.GITHUB_API_URL, + baseUrl: baseUrl.toString(), }), }); diff --git a/src/index.ts b/src/index.ts index 84800907..cc332d77 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { Buffer } from "node:buffer"; +import { env } from "node:process"; import { getInput, info, setFailed, setOutput, setSecret } from "@actions/core"; import { context } from "@actions/github"; import ensureError from "ensure-error"; @@ -29,6 +30,13 @@ const run = async () => { ? repositoryInput.split("/") : [context.repo.owner, context.repo.repo]; + // GITHUB_API_URL is part of GitHub Actions' built-in environment variables. + // See https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables. + const githubUrlInput = getInput("github_url"); + const baseUrl = githubUrlInput + ? new URL(githubUrlInput) + : new URL(env.GITHUB_API_URL); + const installationToken = await fetchInstallationToken({ appId, installationId, @@ -36,6 +44,7 @@ const run = async () => { permissions, privateKey, repo, + baseUrl }); setSecret(installationToken); From 8ff692e1eb363a9b3ece8a4283888bd5dd73b202 Mon Sep 17 00:00:00 2001 From: Cliff Chapman <47404136+cliffchapmanrbx@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:47:28 -0700 Subject: [PATCH 2/3] Rename to github_api_url --- action.yml | 2 +- src/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index c6490f7a..3c274a6d 100644 --- a/action.yml +++ b/action.yml @@ -14,7 +14,7 @@ inputs: description: The full name of the repository for which the token will be requested (defaults to the current repository). permissions: description: The JSON-stringified permissions granted to the token (defaults to all the GitHub app permissions, see https://docs.github.com/en/rest/apps/apps#create-an-installation-access-token-for-an-app). - github_url: + github_api_url: description: The API URL of the GitHub server, such as https://api.github.com. Defaults to the environment variable GITHUB_API_URL, see https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables. outputs: token: diff --git a/src/index.ts b/src/index.ts index cc332d77..79c359cc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,7 +32,7 @@ const run = async () => { // GITHUB_API_URL is part of GitHub Actions' built-in environment variables. // See https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables. - const githubUrlInput = getInput("github_url"); + const githubUrlInput = getInput("github_api_url"); const baseUrl = githubUrlInput ? new URL(githubUrlInput) : new URL(env.GITHUB_API_URL); From 7eba07d9250cd66011e8dc4e026bb3761a05ca50 Mon Sep 17 00:00:00 2001 From: Cliff Chapman <47404136+cliffchapmanrbx@users.noreply.github.com> Date: Mon, 22 Aug 2022 23:19:02 -0700 Subject: [PATCH 3/3] Satisfy linter, fix url trailing slash --- src/fetch-installation-token.ts | 6 +++--- src/index.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/fetch-installation-token.ts b/src/fetch-installation-token.ts index cac033db..82fa9e42 100644 --- a/src/fetch-installation-token.ts +++ b/src/fetch-installation-token.ts @@ -5,26 +5,26 @@ import ensureError from "ensure-error"; export const fetchInstallationToken = async ({ appId, + baseUrl, installationId, owner, permissions, privateKey, repo, - baseUrl, }: Readonly<{ appId: string; + baseUrl: URL; installationId?: number; owner: string; permissions?: Record; privateKey: string; repo: string; - baseUrl: URL; }>): Promise => { const app = createAppAuth({ appId, privateKey, request: request.defaults({ - baseUrl: baseUrl.toString(), + baseUrl: baseUrl.toString().replace(/\/+$/, ''), }), }); diff --git a/src/index.ts b/src/index.ts index 79c359cc..5fa4e9a1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,16 +35,16 @@ const run = async () => { const githubUrlInput = getInput("github_api_url"); const baseUrl = githubUrlInput ? new URL(githubUrlInput) - : new URL(env.GITHUB_API_URL); + : new URL(env.GITHUB_API_URL!); const installationToken = await fetchInstallationToken({ appId, + baseUrl, installationId, owner, permissions, privateKey, repo, - baseUrl }); setSecret(installationToken);