From 22db6191fc188e2c761c2b964de87d245bb3f5c1 Mon Sep 17 00:00:00 2001 From: cliffchapmanrbx <47404136+cliffchapmanrbx@users.noreply.github.com> Date: Sun, 16 Oct 2022 09:34:42 -0700 Subject: [PATCH] Add `github_api_url` input defaulting to `env.GITHUB_API_URL` (#43) --- 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..3c274a6d 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_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: 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 a73e8da9..b541788d 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"; @@ -6,6 +5,7 @@ import ensureError from "ensure-error"; export const fetchInstallationToken = async ({ appId, + baseUrl, installationId, owner, permissions, @@ -13,6 +13,7 @@ export const fetchInstallationToken = async ({ repo, }: Readonly<{ appId: string; + baseUrl: URL; installationId?: number; owner: string; permissions?: Record; @@ -23,9 +24,7 @@ export const fetchInstallationToken = async ({ 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().replace(/\/+$/, ''), }), }); diff --git a/src/index.ts b/src/index.ts index 84800907..5fa4e9a1 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,8 +30,16 @@ 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_api_url"); + const baseUrl = githubUrlInput + ? new URL(githubUrlInput) + : new URL(env.GITHUB_API_URL!); + const installationToken = await fetchInstallationToken({ appId, + baseUrl, installationId, owner, permissions,