0
0
mirror of https://github.com/renovatebot/renovate.git synced 2024-12-22 21:48:32 +00:00
renovatebot_renovate/lib/config/presets/gitlab/index.ts
renovate[bot] 4f1fb3a163
chore(deps): update linters to v8.5.0 (#31349)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
2024-09-16 07:10:17 +00:00

83 lines
2.3 KiB
TypeScript

import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import type { GitlabProject } from '../../../types/platform/gitlab';
import { GitlabHttp } from '../../../util/http/gitlab';
import type { HttpResponse } from '../../../util/http/types';
import type { Preset, PresetConfig } from '../types';
import { PRESET_DEP_NOT_FOUND, fetchPreset, parsePreset } from '../util';
const gitlabApi = new GitlabHttp();
export const Endpoint = 'https://gitlab.com/api/v4/';
async function getDefaultBranchName(
urlEncodedPkgName: string,
endpoint: string,
): Promise<string> {
const res = await gitlabApi.getJson<GitlabProject>(
`${endpoint}projects/${urlEncodedPkgName}`,
);
return res.body.default_branch ?? 'master'; // should never happen, but we keep this to ensure the current behavior
}
export async function fetchJSONFile(
repo: string,
fileName: string,
endpoint: string,
tag?: string,
): Promise<Preset> {
let url = endpoint;
let ref = '';
let res: HttpResponse;
try {
const urlEncodedRepo = encodeURIComponent(repo);
const urlEncodedPkgName = encodeURIComponent(fileName);
if (is.nonEmptyString(tag)) {
ref = `?ref=${tag}`;
} else {
const defaultBranchName = await getDefaultBranchName(
urlEncodedRepo,
endpoint,
);
ref = `?ref=${defaultBranchName}`;
}
url += `projects/${urlEncodedRepo}/repository/files/${urlEncodedPkgName}/raw${ref}`;
logger.trace({ url }, `Preset URL`);
res = await gitlabApi.get(url);
} catch (err) {
if (err instanceof ExternalHostError) {
throw err;
}
logger.debug(`Preset file ${fileName} not found in ${repo}`);
throw new Error(PRESET_DEP_NOT_FOUND);
}
return parsePreset(res.body, fileName);
}
export function getPresetFromEndpoint(
repo: string,
presetName: string,
presetPath?: string,
endpoint = Endpoint,
tag?: string,
): Promise<Preset | undefined> {
return fetchPreset({
repo,
filePreset: presetName,
presetPath,
endpoint,
tag,
fetch: fetchJSONFile,
});
}
export function getPreset({
repo,
presetPath,
presetName = 'default',
tag = undefined,
}: PresetConfig): Promise<Preset | undefined> {
return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
}