Files
renovate/lib/config/presets/github/index.ts
Sergei Zharinov 930cf6633c refactor: Rewrite imports with .ts extensions (#40700)
fix: Additional vi.mock path corrections and prettier formatting
2026-01-26 22:48:17 +00:00

70 lines
1.9 KiB
TypeScript

import { isNonEmptyString } from '@sindresorhus/is';
import { logger } from '../../../logger/index.ts';
import { ExternalHostError } from '../../../types/errors/external-host-error.ts';
import type { Nullish } from '../../../types/index.ts';
import { repoCacheProvider } from '../../../util/http/cache/repository-http-cache-provider.ts';
import { GithubHttp } from '../../../util/http/github.ts';
import { fromBase64 } from '../../../util/string.ts';
import type { Preset, PresetConfig } from '../types.ts';
import { PRESET_DEP_NOT_FOUND, fetchPreset, parsePreset } from '../util.ts';
export const Endpoint = 'https://api.github.com/';
const http = new GithubHttp();
export async function fetchJSONFile(
repo: string,
fileName: string,
endpoint: string,
tag?: string,
): Promise<Nullish<Preset>> {
let ref = '';
if (isNonEmptyString(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.getJsonUnchecked(url, {
cacheProvider: repoCacheProvider,
});
} catch (err) {
if (err instanceof ExternalHostError) {
throw err;
}
logger.debug(
`Preset file ${fileName} not found in ${repo}: ${err.message}`,
);
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<Nullish<Preset>> {
return fetchPreset({
repo,
filePreset,
presetPath,
endpoint,
tag,
fetch: fetchJSONFile,
});
}
export function getPreset({
repo,
presetName = 'default',
presetPath,
tag,
}: PresetConfig): Promise<Nullish<Preset>> {
return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
}