From 5c899f045d4bb4239f407cf3895094b910593ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadeusz=20So=C5=9Bnierz?= Date: Tue, 7 Jun 2022 10:58:45 +0200 Subject: [PATCH] Fix Github API URLs (#366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix Github API URLs * Add changelog entry * Fixes * Tidyup Co-authored-by: Will Hunt Co-authored-by: Tadeusz SoĊ›nierz --- changelog.d/366.bugfix | 1 + src/Config/Config.ts | 4 +++- src/Github/GithubInstance.ts | 10 +++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 changelog.d/366.bugfix diff --git a/changelog.d/366.bugfix b/changelog.d/366.bugfix new file mode 100644 index 00000000..b1c6968d --- /dev/null +++ b/changelog.d/366.bugfix @@ -0,0 +1 @@ +Fix Github API URLs diff --git a/src/Config/Config.ts b/src/Config/Config.ts index 14a0a2f1..bdc11299 100644 --- a/src/Config/Config.ts +++ b/src/Config/Config.ts @@ -9,6 +9,7 @@ import { BridgeConfigActorPermission, BridgePermissions } from "../libRs"; import LogWrapper from "../LogWrapper"; import { ConfigError } from "../errors"; import { ApiError, ErrCode } from "../api"; +import { GITHUB_CLOUD_URL } from "../Github/GithubInstance"; const log = new LogWrapper("Config"); @@ -77,6 +78,7 @@ export class BridgeConfigGitHub { @configKey("URL for enterprise deployments. Does not include /api/v3", true) private enterpriseUrl?: string; + @hideKey() public readonly baseUrl: URL; @@ -86,7 +88,7 @@ export class BridgeConfigGitHub { this.oauth = yaml.oauth; this.defaultOptions = yaml.defaultOptions; this.userIdPrefix = yaml.userIdPrefix || "_github_"; - this.baseUrl = new URL(yaml.enterpriseUrl ?? "https://github.com"); + this.baseUrl = yaml.enterpriseUrl ? new URL(yaml.enterpriseUrl) : GITHUB_CLOUD_URL; } } diff --git a/src/Github/GithubInstance.ts b/src/Github/GithubInstance.ts index d61e3610..ba557083 100644 --- a/src/Github/GithubInstance.ts +++ b/src/Github/GithubInstance.ts @@ -10,6 +10,8 @@ import UserAgent from "../UserAgent"; const log = new LogWrapper("GithubInstance"); +export const GITHUB_CLOUD_URL = new URL("https://api.github.com"); + interface Installation { account: { login?: string; @@ -42,7 +44,9 @@ export class GithubInstance { public static baseOctokitConfig(baseUrl: URL) { return { userAgent: UserAgent, - baseUrl: baseUrl && new URL("/api/v3", baseUrl).toString(), + // Enterprise GitHub uses a /api/v3 basepath (https://github.com/octokit/octokit.js#constructor-options) + // Cloud uses api.github.com + baseUrl: baseUrl.hostname === GITHUB_CLOUD_URL.hostname ? baseUrl.toString() : new URL("/api/v3", baseUrl).toString(), } } @@ -155,11 +159,11 @@ export class GithubInstance { } public get newInstallationUrl() { - if (this.baseUrl.hostname === "github.com") { + if (this.baseUrl.hostname === GITHUB_CLOUD_URL.hostname) { // Cloud return new URL(`/apps/${this.appSlug}/installations/new`, this.baseUrl); } - // Enterprise + // Enterprise (yes, i know right) return new URL(`/github-apps/${this.appSlug}/installations/new`, this.baseUrl); } }