renovate/lib/manager/helmv3/utils.ts
Sebastian Poxhofer 9e92712b7f
fix(helmv3): adapt to new OCI format and allow aliases for OCI (#13603)
* feat(helmv3): adapt to new OCI format and refactor

* add whitespace to tests and null return value of `resolveAlias`

* docs(helmv3): use JSDocs for function documentation

Co-authored-by: Rhys Arkins <rhys@arkins.net>
2022-01-20 09:31:16 +00:00

54 lines
1.6 KiB
TypeScript

import * as datasourceDocker from '../../datasource/docker';
import { logger } from '../../logger';
import { SkipReason } from '../../types';
import type { PackageDependency } from '../types';
export function parseRepository(
depName: string,
repositoryURL: string
): PackageDependency {
const res: PackageDependency = {};
try {
const url = new URL(repositoryURL);
switch (url.protocol) {
case 'oci:':
res.datasource = datasourceDocker.id;
res.lookupName = `${repositoryURL.replace('oci://', '')}/${depName}`;
break;
case 'file:':
res.skipReason = SkipReason.LocalDependency;
break;
default:
res.registryUrls = [repositoryURL];
}
} catch (err) {
logger.debug({ err }, 'Error parsing url');
res.skipReason = SkipReason.InvalidUrl;
}
return res;
}
/**
* Resolves alias in repository string.
*
* @param repository to be resolved string
* @param aliases Records containing aliases as key and to be resolved URLs as values
*
* @returns resolved alias. If repository does not contain an alias the repository string will be returned. Should it contain an alias which can not be resolved using `aliases`, null will be returned
*/
export function resolveAlias(
repository: string,
aliases: Record<string, string>
): string | null {
if (!(repository.startsWith('@') || repository.startsWith('alias:'))) {
return repository;
}
const repoWithPrefixRemoved = repository.slice(repository[0] === '@' ? 1 : 6);
const alias = aliases[repoWithPrefixRemoved];
if (alias) {
return alias;
}
return null;
}