mirror of
https://github.com/renovatebot/renovate.git
synced 2024-12-22 13:38:32 +00:00
4f1fb3a163
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Rhys Arkins <rhys@arkins.net>
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import is from '@sindresorhus/is';
|
|
import { logger } from '../../../logger';
|
|
import { ExternalHostError } from '../../../types/errors/external-host-error';
|
|
import { GithubHttp } from '../../../util/http/github';
|
|
import { fromBase64 } from '../../../util/string';
|
|
import type { Preset, PresetConfig } from '../types';
|
|
import { PRESET_DEP_NOT_FOUND, fetchPreset, parsePreset } from '../util';
|
|
|
|
export const Endpoint = 'https://api.github.com/';
|
|
|
|
const http = new GithubHttp();
|
|
|
|
export async function fetchJSONFile(
|
|
repo: string,
|
|
fileName: string,
|
|
endpoint: string,
|
|
tag?: string,
|
|
): Promise<Preset> {
|
|
let ref = '';
|
|
if (is.nonEmptyString(tag)) {
|
|
ref = `?ref=${tag}`;
|
|
}
|
|
const url = `${endpoint}repos/${repo}/contents/${fileName}${ref}`;
|
|
logger.trace({ url }, `Preset URL`);
|
|
let res: { body: { content: string } };
|
|
try {
|
|
res = await http.getJson(url);
|
|
} catch (err) {
|
|
// istanbul ignore if: not testable with nock
|
|
if (err instanceof ExternalHostError) {
|
|
throw err;
|
|
}
|
|
logger.debug(`Preset file ${fileName} not found in ${repo}`);
|
|
throw new Error(PRESET_DEP_NOT_FOUND);
|
|
}
|
|
|
|
return parsePreset(fromBase64(res.body.content), fileName);
|
|
}
|
|
|
|
export function getPresetFromEndpoint(
|
|
repo: string,
|
|
filePreset: string,
|
|
presetPath?: string,
|
|
endpoint = Endpoint,
|
|
tag?: string,
|
|
): Promise<Preset | undefined> {
|
|
return fetchPreset({
|
|
repo,
|
|
filePreset,
|
|
presetPath,
|
|
endpoint,
|
|
tag,
|
|
fetch: fetchJSONFile,
|
|
});
|
|
}
|
|
|
|
export function getPreset({
|
|
repo,
|
|
presetName = 'default',
|
|
presetPath,
|
|
tag,
|
|
}: PresetConfig): Promise<Preset | undefined> {
|
|
return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
|
|
}
|