Fix Github API URLs (#366)

* Fix Github API URLs

* Add changelog entry

* Fixes

* Tidyup

Co-authored-by: Will Hunt <will@half-shot.uk>
Co-authored-by: Tadeusz Sośnierz <tadeusz@sosnierz.com>
This commit is contained in:
Tadeusz Sośnierz 2022-06-07 10:58:45 +02:00 committed by GitHub
parent 21b6d79c3f
commit 5c899f045d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 4 deletions

1
changelog.d/366.bugfix Normal file
View File

@ -0,0 +1 @@
Fix Github API URLs

View File

@ -9,6 +9,7 @@ import { BridgeConfigActorPermission, BridgePermissions } from "../libRs";
import LogWrapper from "../LogWrapper"; import LogWrapper from "../LogWrapper";
import { ConfigError } from "../errors"; import { ConfigError } from "../errors";
import { ApiError, ErrCode } from "../api"; import { ApiError, ErrCode } from "../api";
import { GITHUB_CLOUD_URL } from "../Github/GithubInstance";
const log = new LogWrapper("Config"); const log = new LogWrapper("Config");
@ -77,6 +78,7 @@ export class BridgeConfigGitHub {
@configKey("URL for enterprise deployments. Does not include /api/v3", true) @configKey("URL for enterprise deployments. Does not include /api/v3", true)
private enterpriseUrl?: string; private enterpriseUrl?: string;
@hideKey() @hideKey()
public readonly baseUrl: URL; public readonly baseUrl: URL;
@ -86,7 +88,7 @@ export class BridgeConfigGitHub {
this.oauth = yaml.oauth; this.oauth = yaml.oauth;
this.defaultOptions = yaml.defaultOptions; this.defaultOptions = yaml.defaultOptions;
this.userIdPrefix = yaml.userIdPrefix || "_github_"; 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;
} }
} }

View File

@ -10,6 +10,8 @@ import UserAgent from "../UserAgent";
const log = new LogWrapper("GithubInstance"); const log = new LogWrapper("GithubInstance");
export const GITHUB_CLOUD_URL = new URL("https://api.github.com");
interface Installation { interface Installation {
account: { account: {
login?: string; login?: string;
@ -42,7 +44,9 @@ export class GithubInstance {
public static baseOctokitConfig(baseUrl: URL) { public static baseOctokitConfig(baseUrl: URL) {
return { return {
userAgent: UserAgent, 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() { public get newInstallationUrl() {
if (this.baseUrl.hostname === "github.com") { if (this.baseUrl.hostname === GITHUB_CLOUD_URL.hostname) {
// Cloud // Cloud
return new URL(`/apps/${this.appSlug}/installations/new`, this.baseUrl); 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); return new URL(`/github-apps/${this.appSlug}/installations/new`, this.baseUrl);
} }
} }