diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index f381b27a2ea2fe..5685f8b249538b 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 { + 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 = 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 = 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 33d2e3e8aeb033..296e589e54f2a1 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;