2020-06-09 02:13:52 -06:00
|
|
|
import { logger } from '../../logger';
|
2023-11-06 03:30:11 -07:00
|
|
|
import { parseJson } from '../../util/common';
|
2022-05-02 08:34:09 -06:00
|
|
|
import { regEx } from '../../util/regex';
|
2020-06-09 02:13:52 -06:00
|
|
|
import { ensureTrailingSlash } from '../../util/url';
|
2021-05-11 11:08:02 -06:00
|
|
|
import type { FetchPresetConfig, Preset } from './types';
|
2020-06-09 02:13:52 -06:00
|
|
|
|
|
|
|
export const PRESET_DEP_NOT_FOUND = 'dep not found';
|
2021-04-19 07:31:38 -06:00
|
|
|
export const PRESET_INVALID = 'invalid preset';
|
2021-04-06 07:49:06 -06:00
|
|
|
export const PRESET_INVALID_JSON = 'invalid preset JSON';
|
2021-04-19 07:31:38 -06:00
|
|
|
export const PRESET_NOT_FOUND = 'preset not found';
|
|
|
|
export const PRESET_PROHIBITED_SUBPRESET = 'prohibited sub-preset';
|
|
|
|
export const PRESET_RENOVATE_CONFIG_NOT_FOUND =
|
|
|
|
'preset renovate-config not found';
|
2020-06-09 02:13:52 -06:00
|
|
|
|
|
|
|
export async function fetchPreset({
|
2022-02-28 09:39:44 -07:00
|
|
|
repo,
|
2020-06-09 02:13:52 -06:00
|
|
|
filePreset,
|
2021-03-20 17:03:10 -06:00
|
|
|
presetPath,
|
2021-11-08 23:04:34 -07:00
|
|
|
endpoint: _endpoint,
|
2023-03-19 10:09:46 -06:00
|
|
|
tag,
|
2020-06-09 02:13:52 -06:00
|
|
|
fetch,
|
|
|
|
}: FetchPresetConfig): Promise<Preset | undefined> {
|
2023-08-15 03:31:15 -06:00
|
|
|
// TODO: fix me, can be undefiend #22198
|
2022-04-24 16:48:54 -06:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
|
|
const endpoint = ensureTrailingSlash(_endpoint!);
|
2020-06-09 02:13:52 -06:00
|
|
|
const [fileName, presetName, subPresetName] = filePreset.split('/');
|
2021-03-20 17:03:10 -06:00
|
|
|
const pathPrefix = presetPath ? `${presetPath}/` : '';
|
|
|
|
const buildFilePath = (name: string): string => `${pathPrefix}${name}`;
|
2023-08-02 09:07:49 -06:00
|
|
|
let jsonContent: any;
|
2020-06-09 02:13:52 -06:00
|
|
|
if (fileName === 'default') {
|
|
|
|
try {
|
2021-03-20 17:03:10 -06:00
|
|
|
jsonContent = await fetch(
|
2022-02-28 09:39:44 -07:00
|
|
|
repo,
|
2021-03-20 17:03:10 -06:00
|
|
|
buildFilePath('default.json'),
|
2021-11-05 07:12:47 -06:00
|
|
|
endpoint,
|
2023-11-07 08:50:29 -07:00
|
|
|
tag,
|
2021-03-20 17:03:10 -06:00
|
|
|
);
|
2020-06-09 02:13:52 -06:00
|
|
|
} catch (err) {
|
|
|
|
if (err.message !== PRESET_DEP_NOT_FOUND) {
|
|
|
|
throw err;
|
|
|
|
}
|
2021-03-20 17:03:10 -06:00
|
|
|
jsonContent = await fetch(
|
2022-02-28 09:39:44 -07:00
|
|
|
repo,
|
2021-03-20 17:03:10 -06:00
|
|
|
buildFilePath('renovate.json'),
|
2021-11-05 07:12:47 -06:00
|
|
|
endpoint,
|
2023-11-07 08:50:29 -07:00
|
|
|
tag,
|
2021-03-20 17:03:10 -06:00
|
|
|
);
|
2023-09-06 05:01:37 -06:00
|
|
|
logger.warn(
|
|
|
|
{
|
|
|
|
repo,
|
|
|
|
filePreset,
|
|
|
|
presetPath,
|
|
|
|
endpoint,
|
|
|
|
tag,
|
|
|
|
},
|
2023-11-07 08:50:29 -07:00
|
|
|
'Fallback to renovate.json file as a preset is deprecated, please use a default.json file instead.',
|
2022-02-17 19:54:40 -07:00
|
|
|
);
|
2020-06-09 02:13:52 -06:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-20 17:03:10 -06:00
|
|
|
jsonContent = await fetch(
|
2022-02-28 09:39:44 -07:00
|
|
|
repo,
|
2022-05-02 08:34:09 -06:00
|
|
|
buildFilePath(
|
2023-11-07 08:50:29 -07:00
|
|
|
regEx(/\.json5?$/).test(fileName) ? fileName : `${fileName}.json`,
|
2022-05-02 08:34:09 -06:00
|
|
|
),
|
2021-11-05 07:12:47 -06:00
|
|
|
endpoint,
|
2023-11-07 08:50:29 -07:00
|
|
|
tag,
|
2021-03-20 17:03:10 -06:00
|
|
|
);
|
2020-06-09 02:13:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!jsonContent) {
|
|
|
|
throw new Error(PRESET_DEP_NOT_FOUND);
|
|
|
|
}
|
|
|
|
if (presetName) {
|
|
|
|
const preset = jsonContent[presetName];
|
|
|
|
if (!preset) {
|
|
|
|
throw new Error(PRESET_NOT_FOUND);
|
|
|
|
}
|
|
|
|
if (subPresetName) {
|
|
|
|
const subPreset = preset[subPresetName];
|
|
|
|
if (!subPreset) {
|
|
|
|
throw new Error(PRESET_NOT_FOUND);
|
|
|
|
}
|
|
|
|
return subPreset;
|
|
|
|
}
|
|
|
|
return preset;
|
|
|
|
}
|
|
|
|
return jsonContent;
|
|
|
|
}
|
2022-04-29 02:34:55 -06:00
|
|
|
|
2023-11-06 03:30:11 -07:00
|
|
|
export function parsePreset(content: string, fileName: string): Preset {
|
2022-04-29 02:34:55 -06:00
|
|
|
try {
|
2023-11-06 03:30:11 -07:00
|
|
|
return parseJson(content, fileName) as Preset;
|
2024-08-14 04:33:02 -06:00
|
|
|
} catch {
|
2022-04-29 02:34:55 -06:00
|
|
|
throw new Error(PRESET_INVALID_JSON);
|
|
|
|
}
|
|
|
|
}
|