Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(github): separate platformConfig #12473

Merged
merged 5 commits into from Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 21 additions & 17 deletions lib/platform/github/index.ts
Expand Up @@ -66,6 +66,7 @@ import {
GhRepo,
GhRestPr,
LocalRepoConfig,
PlatformConfig,
PrList,
} from './types';
import { UserDetails, getUserDetails, getUserEmail } from './user';
Expand All @@ -74,14 +75,29 @@ const githubApi = new githubHttp.GithubHttp();

let config: LocalRepoConfig = {} as any;

const platformConfig = {
const platformConfig: PlatformConfig = {
hostType: PlatformId.Github,
endpoint: 'https://api.github.com/',
};

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,
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -205,8 +211,6 @@ export async function initRepo({
repository,
cloneSubmodules,
ignorePrAuthor,
isGhe: config.isGhe,
gheVersion: config.gheVersion,
} as any;
// istanbul ignore if
if (endpoint) {
Expand All @@ -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');
}
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions lib/platform/github/types.ts
Expand Up @@ -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;
Expand All @@ -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;
Expand Down