Files
Michael Kriese 320f770c36 fix(cache): hash URL credentials in cache keys
Co-Authored-By: Claude Fable 5.1 <michael.kriese+claude-code@mend.io>
Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>
Co-Authored-By: Claude Opus 5.5 <michael.kriese+claude-code@mend.io>
2026-10-20 09:05:00 +01:00

74 lines
2.4 KiB
TypeScript

import { isNonEmptyString, isNullOrUndefined } from '@sindresorhus/is';
import type { CustomDatasourceConfig } from '../../../config/types.ts';
import { logger } from '../../../logger/index.ts';
import { coerceArray } from '../../../util/array.ts';
import { detachUrlCredentials } from '../../../util/host-rules.ts';
import * as template from '../../../util/template/index.ts';
import type { GetReleasesConfig } from '../types.ts';
export function massageCustomDatasourceConfig(
customDatasourceName: string,
{
customDatasources,
packageName,
currentValue,
registryUrl: defaultRegistryUrl,
}: GetReleasesConfig,
): Required<CustomDatasourceConfig> | null {
const customDatasource = customDatasources?.[customDatasourceName];
if (isNullOrUndefined(customDatasource)) {
logger.debug(
`No custom datasource config provided while ${packageName} has been requested`,
);
return null;
}
const templateInput = { packageName, currentValue };
const registryUrlTemplate =
defaultRegistryUrl ?? customDatasource.defaultRegistryUrlTemplate;
if (isNullOrUndefined(registryUrlTemplate)) {
logger.debug(
'No registry url provided by extraction nor datasource configuration',
);
return null;
}
// the credentials of a URL template are detached from the URL it compiles, the one which is requested with the `custom` host type
const registryUrl = detachUrlCredentials(
template.compile(registryUrlTemplate, templateInput),
['custom'],
);
const transformTemplates = coerceArray(customDatasource.transformTemplates);
const transform: string[] = [];
for (const transformTemplate of transformTemplates) {
const templated = template.compile(transformTemplate, templateInput);
transform.push(templated);
}
logger.trace({ transform }, `Custom datasource compiled data.`);
return {
format: customDatasource.format ?? 'json',
defaultRegistryUrlTemplate: registryUrl,
transformTemplates: transform,
};
}
export function getCustomConfig(
getReleasesConfig: GetReleasesConfig,
): Required<CustomDatasourceConfig> | null {
const customDatasourceName = getReleasesConfig.datasource?.replace(
'custom.',
'',
);
if (!isNonEmptyString(customDatasourceName)) {
logger.debug(
`No datasource has been supplied while looking up ${getReleasesConfig.packageName}`,
);
return null;
}
return massageCustomDatasourceConfig(customDatasourceName, getReleasesConfig);
}