Files
renovate/lib/config/presets/gitlab/index.ts
Sebastian Poxhofer b106668a95 refactor(oxlint): enable typescript/no-useless-default-assignment (#44419)
- Convert `param: T | undefined = undefined` to optional `param?: T`.
- Drop dead `= false` defaults on the required cache-provider options.
- Make `GraphqlPaginatedContent.nodes/edges` optional so the `= []`
  fallbacks (load-bearing for partial GraphQL connections) are valid.
- Type the unchecked `default_branch` response field as optional so
  the `'HEAD'` fallback is retained.
- Drop `tag = undefined` from the Forgejo/Gitea/GitLab preset configs.

Part of #43845

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 16:38:48 +00:00

86 lines
2.5 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 type { GitlabProject } from '../../../types/platform/gitlab/index.ts';
import { GitlabHttp } from '../../../util/http/gitlab.ts';
import type { HttpResponse } from '../../../util/http/types.ts';
import type { Preset, PresetConfig } from '../types.ts';
import { PRESET_DEP_NOT_FOUND, fetchPreset, parsePreset } from '../util.ts';
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.getJsonUnchecked<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<Nullish<Preset>> {
let url = endpoint;
let ref = '';
let res: HttpResponse;
try {
const urlEncodedRepo = encodeURIComponent(repo);
const urlEncodedPkgName = encodeURIComponent(fileName);
if (isNonEmptyString(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.getText(url);
} 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(res.body, fileName);
}
export function getPresetFromEndpoint(
repo: string,
presetName: string,
presetPath?: string,
endpoint = Endpoint,
tag?: string,
): Promise<Nullish<Preset>> {
return fetchPreset({
repo,
filePreset: presetName,
presetPath,
endpoint,
tag,
fetch: fetchJSONFile,
});
}
export function getPreset({
repo,
presetPath,
presetName = 'default',
tag,
}: PresetConfig): Promise<Nullish<Preset>> {
return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
}