From 64d64dfb685afc82142a6dde309506447de23dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Zu=CC=88hlke?= Date: Tue, 11 Jul 2023 14:52:34 +0200 Subject: [PATCH 1/2] use github API URL * use the given URL to get tokens * use the variable `github.api_url` as default value (https://docs.github.com/en/actions/learn-github-actions/contexts) --- action.yml | 2 +- package-lock.json | 1 + package.json | 1 + src/action/main.ts | 16 ++++++++++++---- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index bd0da361..adcbe1ec 100644 --- a/action.yml +++ b/action.yml @@ -49,7 +49,7 @@ inputs: description: | The URL of the GitHub API, only use this input if you are using GitHub Enterprise. - default: https://api.github.com + default: ${{ github.api_url }} required: false github-app-auth-only: description: | diff --git a/package-lock.json b/package-lock.json index 67b4bac3..8e67a5c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@actions/io": "^1.1.3", "@actions/tool-cache": "^2.0.1", "@octokit/auth-app": "^4.0.9", + "@octokit/request": "^6.0.0", "@types/node-fetch": "^2.6.4", "@types/sinon": "^10.0.13", "jssha": "^3.3.0", diff --git a/package.json b/package.json index 0f052701..c778c2ec 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "@actions/io": "^1.1.3", "@actions/tool-cache": "^2.0.1", "@octokit/auth-app": "^4.0.9", + "@octokit/request": "^6.0.0", "@types/node-fetch": "^2.6.4", "@types/sinon": "^10.0.13", "jssha": "^3.3.0", diff --git a/src/action/main.ts b/src/action/main.ts index 9a021b63..36f12f8e 100644 --- a/src/action/main.ts +++ b/src/action/main.ts @@ -6,6 +6,7 @@ import * as core from '@actions/core' import {getOctokit} from '@actions/github' import * as io from '@actions/io' import {createAppAuth} from '@octokit/auth-app' +import {request} from '@octokit/request' import {type Files} from '../core/files' import {type Logger} from '../core/logger' import {nonEmpty, NonEmptyString} from '../core/types' @@ -26,13 +27,13 @@ async function run(): Promise { const logger: Logger = core const files: Files = {...fs, ...io} const inputs = Input.from(core, files, logger).all() - const gitHubToken = await gitHubAppToken(inputs.github.app, 'installation') ?? inputs.github.token.value const gitHubApiUrl = inputs.github.apiUrl.value + const gitHubToken = await gitHubAppToken(inputs.github.app, gitHubApiUrl, 'installation') ?? inputs.github.token.value const octokit = getOctokit(gitHubToken, {baseUrl: gitHubApiUrl}) const github = GitHub.from(logger, octokit) const workspace = Workspace.from(logger, files, os, cache) - const user = await gitHubAppToken(inputs.github.app, 'app') + const user = await gitHubAppToken(inputs.github.app, gitHubApiUrl, 'app') .then(appToken => appToken ? getOctokit(appToken, {baseUrl: gitHubApiUrl}) : undefined) .then(async octokit => octokit ? octokit.rest.apps.getAuthenticated() : undefined) .then(async response => response ? github.getAppUser(response.data.slug) : github.getAuthUser()) @@ -91,15 +92,22 @@ async function run(): Promise { * Returns a GitHub App Token. * * @param app The GitHub App information. + * @param gitHubApiUrl The GitHub API URL. * @param type The type of token to retrieve, either `app` or `installation`. * @returns the GitHub App Token for the provided installation. */ -async function gitHubAppToken(app: GitHubAppInfo | undefined, type: 'app' | 'installation') { +async function gitHubAppToken(app: GitHubAppInfo | undefined, gitHubApiUrl: string, type: 'app' | 'installation') { if (!app) { return undefined } - const auth = createAppAuth({appId: app.id.value, privateKey: app.key.value}) + const auth = createAppAuth({ + appId: app.id.value, + privateKey: app.key.value, + request: request.defaults({ + baseUrl: gitHubApiUrl, + }), + }) const response = type === 'app' ? await auth({type: 'app'}) From 2c8df87d0ffeddebc80abf6575dfa52fe31895df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Zu=CC=88hlke?= Date: Fri, 14 Jul 2023 16:50:03 +0200 Subject: [PATCH 2/2] convert multiple env into single line as expected from JWT lib --- src/modules/input.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/input.ts b/src/modules/input.ts index 9a883405..69127f7d 100644 --- a/src/modules/input.ts +++ b/src/modules/input.ts @@ -171,7 +171,7 @@ export class Input { const authOnly = this.inputs.getBooleanInput('github-app-auth-only') const id = nonEmpty(this.inputs.getInput('github-app-id')) const installation = nonEmpty(this.inputs.getInput('github-app-installation-id')) - const key = nonEmpty(this.inputs.getInput('github-app-key')) + const key = nonEmpty(this.inputs.getInput('github-app-key')?.replace(/\\n/g, '\n')) if (!id && !key) { return undefined