Files
Sebastian Poxhofer 03baf6ee4a test(noNewURL): add new rule enforcing parseUrl() from 'lib/util/url.ts' (#43247)
* test(noNewURL): add new rule enforcing parseUrl() from 'lib/util/url.ts'

* replace newURL with parseUrl

* remove non-null assertions marks if appropiate

* test: add additional tests

* test: add additional tests and ignore where it makes sense

* implement suggestions

* test: further improve coverage

* apply suggestion
2026-05-13 16:23:10 +00:00

89 lines
2.3 KiB
TypeScript

import { isArray } from '@sindresorhus/is';
import { logger } from '../../../logger/index.ts';
import { parseUrl } from '../../../util/url.ts';
import { parseSingleYaml } from '../../../util/yaml.ts';
import { HelmDatasource } from '../../datasource/helm/index.ts';
import type {
ExtractConfig,
PackageDependency,
PackageFileContent,
} from '../types.ts';
export function extractPackageFile(
content: string,
packageFile: string,
config: ExtractConfig,
): PackageFileContent | null {
let deps = [];
// TODO: fix type
let doc: any;
try {
doc = parseSingleYaml(content); // TODO #9610
} catch {
logger.debug({ packageFile }, `Failed to parse helm requirements.yaml`);
return null;
}
if (!(doc && isArray(doc.dependencies))) {
logger.debug({ packageFile }, `requirements.yaml has no dependencies`);
return null;
}
deps = doc.dependencies.map((dep: Record<string, any>) => {
let currentValue: string | undefined; // Remove when #9610 has been implemented
switch (typeof dep.version) {
case 'number':
currentValue = String(dep.version);
break;
case 'string':
currentValue = dep.version;
}
const res: PackageDependency = {
depName: dep.name,
currentValue,
};
if (!res.depName) {
res.skipReason = 'invalid-name';
return res;
}
if (!res.currentValue) {
res.skipReason = 'invalid-version';
return res;
}
if (!dep.repository) {
res.skipReason = 'no-repository';
return res;
}
res.registryUrls = [dep.repository];
if (dep.repository.startsWith('@') || dep.repository.startsWith('alias:')) {
const repoWithPrefixRemoved = dep.repository.slice(
dep.repository[0] === '@' ? 1 : 6,
);
const alias = config.registryAliases?.[repoWithPrefixRemoved];
if (alias) {
res.registryUrls = [alias];
return res;
}
res.skipReason = 'placeholder-url';
} else {
const url = parseUrl(dep.repository);
if (!url) {
logger.debug({ packageFile, url: dep.repository }, 'Error parsing url');
res.skipReason = 'invalid-url';
} else if (url.protocol === 'file:') {
res.skipReason = 'local-dependency';
}
}
return res;
});
const res = {
deps,
datasource: HelmDatasource.id,
};
return res;
}