From 89efe91ae9c5d4258abe1ec7ccdda88418b2bc40 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Wed, 3 Nov 2021 19:39:17 +0100 Subject: [PATCH] fix(github): separate platformConfig (#12473) --- lib/platform/github/index.ts | 38 ++++++++++++++++++++---------------- lib/platform/github/types.ts | 9 +++++++-- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index f381b27a2e..5685f8b249 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -66,6 +66,7 @@ import { GhRepo, GhRestPr, LocalRepoConfig, + PlatformConfig, PrList, } from './types'; import { UserDetails, getUserDetails, getUserEmail } from './user'; @@ -74,7 +75,7 @@ const githubApi = new githubHttp.GithubHttp(); let config: LocalRepoConfig = {} as any; -const platformConfig = { +const platformConfig: PlatformConfig = { hostType: PlatformId.Github, endpoint: 'https://api.github.com/', }; @@ -82,6 +83,21 @@ const platformConfig = { const escapeHash = (input: string): string => input ? input.replace(regEx(/#/g), '%23') : input; +export async function detectGhe(token: string): Promise<void> { + platformConfig.isGhe = + URL.parse(platformConfig.endpoint).host !== 'api.github.com'; + if (platformConfig.isGhe) { + const gheHeaderKey = 'x-github-enterprise-version'; + const gheQueryRes = await githubApi.headJson('/', { token }); + const gheHeaders: Record<string, string> = gheQueryRes?.headers || {}; + const [, gheVersion] = + Object.entries(gheHeaders).find( + ([k]) => k.toLowerCase() === gheHeaderKey + ) ?? []; + platformConfig.gheVersion = semverValid(gheVersion) ?? null; + } +} + export async function initPlatform({ endpoint, token, @@ -99,17 +115,7 @@ export async function initPlatform({ logger.debug('Using default github endpoint: ' + platformConfig.endpoint); } - config.isGhe = URL.parse(platformConfig.endpoint).host !== 'api.github.com'; - if (config.isGhe) { - const gheHeaderKey = 'x-github-enterprise-version'; - const gheQueryRes = await githubApi.head('/', { throwHttpErrors: false }); - const gheHeaders: Record<string, string> = gheQueryRes?.headers || {}; - const [, gheVersion] = - Object.entries(gheHeaders).find( - ([k]) => k.toLowerCase() === gheHeaderKey - ) ?? []; - config.gheVersion = semverValid(gheVersion) ?? null; - } + await detectGhe(token); let userDetails: UserDetails; let renovateUsername: string; @@ -127,7 +133,7 @@ export async function initPlatform({ discoveredGitAuthor = `${userDetails.name} <${userEmail}>`; } } - logger.debug('Authenticated as GitHub user: ' + renovateUsername); + logger.debug({ platformConfig, renovateUsername }, 'Platform config'); const platformResult: PlatformResult = { endpoint: platformConfig.endpoint, gitAuthor: gitAuthor || discoveredGitAuthor, @@ -205,8 +211,6 @@ export async function initRepo({ repository, cloneSubmodules, ignorePrAuthor, - isGhe: config.isGhe, - gheVersion: config.gheVersion, } as any; // istanbul ignore if if (endpoint) { @@ -225,7 +229,7 @@ export async function initRepo({ try { let infoQuery = repoInfoQuery; - if (config.isGhe) { + if (platformConfig.isGhe) { infoQuery = infoQuery.replace(/\n\s*autoMergeAllowed\s*\n/, '\n'); infoQuery = infoQuery.replace(/\n\s*hasIssuesEnabled\s*\n/, '\n'); } @@ -1653,7 +1657,7 @@ export async function mergePr({ } export function massageMarkdown(input: string): string { - if (config.isGhe) { + if (platformConfig.isGhe) { return smartTruncate(input, 60000); } const massagedInput = massageMarkdownLinks(input) diff --git a/lib/platform/github/types.ts b/lib/platform/github/types.ts index 33d2e3e8ae..296e589e54 100644 --- a/lib/platform/github/types.ts +++ b/lib/platform/github/types.ts @@ -54,6 +54,13 @@ export interface GhGraphQlPr extends GhPr { labels: string[] & { nodes?: { name: string }[] }; } +export interface PlatformConfig { + hostType: string; + endpoint: string; + isGhe?: boolean; + gheVersion?: string | null; +} + export interface LocalRepoConfig { repositoryName: string; pushProtection: boolean; @@ -70,8 +77,6 @@ export interface LocalRepoConfig { defaultBranch: string; repositoryOwner: string; repository: string | null; - isGhe: boolean; - gheVersion?: string | null; renovateUsername: string; productLinks: any; ignorePrAuthor: boolean; -- GitLab