2023-07-10 12:22:35 -06:00
|
|
|
import is from '@sindresorhus/is';
|
|
|
|
import { logger } from '../../../logger';
|
2024-10-08 03:11:15 -06:00
|
|
|
import { getExpression } from '../../../util/jsonata';
|
2023-07-10 12:22:35 -06:00
|
|
|
import { Datasource } from '../datasource';
|
2024-05-01 01:41:05 -06:00
|
|
|
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
|
2023-11-24 23:37:05 -07:00
|
|
|
import { fetchers } from './formats';
|
2023-07-10 12:22:35 -06:00
|
|
|
import { ReleaseResultZodSchema } from './schema';
|
2023-11-07 23:05:22 -07:00
|
|
|
import { getCustomConfig } from './utils';
|
2023-07-10 12:22:35 -06:00
|
|
|
|
|
|
|
export class CustomDatasource extends Datasource {
|
|
|
|
static readonly id = 'custom';
|
|
|
|
|
|
|
|
override customRegistrySupport = true;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(CustomDatasource.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getReleases(
|
2023-11-07 08:50:29 -07:00
|
|
|
getReleasesConfig: GetReleasesConfig,
|
2023-07-10 12:22:35 -06:00
|
|
|
): Promise<ReleaseResult | null> {
|
2023-11-07 23:05:22 -07:00
|
|
|
const config = getCustomConfig(getReleasesConfig);
|
2023-07-10 12:22:35 -06:00
|
|
|
if (is.nullOrUndefined(config)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-08-03 08:20:36 -06:00
|
|
|
const { defaultRegistryUrlTemplate, transformTemplates, format } = config;
|
2023-11-23 23:05:46 -07:00
|
|
|
|
2023-11-24 23:37:05 -07:00
|
|
|
const fetcher = fetchers[format];
|
2023-11-23 23:05:46 -07:00
|
|
|
const isLocalRegistry = defaultRegistryUrlTemplate.startsWith('file://');
|
|
|
|
|
|
|
|
let data: unknown;
|
2023-11-24 23:37:05 -07:00
|
|
|
try {
|
|
|
|
if (isLocalRegistry) {
|
2023-12-05 10:25:45 -07:00
|
|
|
data = await fetcher.readFile(
|
|
|
|
defaultRegistryUrlTemplate.replace('file://', ''),
|
|
|
|
);
|
2023-11-24 23:37:05 -07:00
|
|
|
} else {
|
|
|
|
data = await fetcher.fetch(this.http, defaultRegistryUrlTemplate);
|
2023-08-03 08:20:36 -06:00
|
|
|
}
|
2023-11-24 23:37:05 -07:00
|
|
|
} catch (e) {
|
|
|
|
this.handleHttpErrors(e);
|
|
|
|
return null;
|
2023-07-10 12:22:35 -06:00
|
|
|
}
|
|
|
|
|
2024-07-18 15:00:59 -06:00
|
|
|
logger.trace({ data }, `Custom manager fetcher '${format}' returned data.`);
|
|
|
|
|
2023-07-10 12:22:35 -06:00
|
|
|
for (const transformTemplate of transformTemplates) {
|
2024-10-08 03:11:15 -06:00
|
|
|
const expression = getExpression(transformTemplate);
|
|
|
|
|
|
|
|
if (expression instanceof Error) {
|
|
|
|
logger.once.warn(
|
|
|
|
{ errorMessage: expression.message },
|
|
|
|
`Invalid JSONata expression: ${transformTemplate}`,
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-09-25 13:24:54 -06:00
|
|
|
try {
|
|
|
|
data = await expression.evaluate(data);
|
|
|
|
} catch (err) {
|
2024-10-08 03:11:15 -06:00
|
|
|
logger.once.warn(
|
|
|
|
{ err },
|
|
|
|
`Error while evaluating JSONata expression: ${transformTemplate}`,
|
2024-09-25 13:24:54 -06:00
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-10 12:22:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const parsed = ReleaseResultZodSchema.parse(data);
|
|
|
|
return structuredClone(parsed);
|
|
|
|
} catch (err) {
|
|
|
|
logger.debug({ err }, `Response has failed validation`);
|
|
|
|
logger.trace({ data }, 'Response that has failed validation');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2024-05-01 01:41:05 -06:00
|
|
|
|
|
|
|
override getDigest(
|
|
|
|
{ packageName }: DigestConfig,
|
|
|
|
newValue?: string,
|
|
|
|
): Promise<string | null> {
|
|
|
|
// Return null here to support setting a digest: value can be provided digest in getReleases
|
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
2023-07-10 12:22:35 -06:00
|
|
|
}
|