0
0
mirror of https://github.com/renovatebot/renovate.git synced 2024-12-22 21:48:32 +00:00
renovatebot_renovate/lib/modules/datasource/orb/index.ts
Mike Glazer 7eef0d3ed0
feat(datasource/orb): Add support for internal CircleCI Registries (#33213)
Co-authored-by: Michael Kriese <michael.kriese@gmx.de>
2024-12-20 16:07:28 +00:00

82 lines
2.2 KiB
TypeScript

import { logger } from '../../../logger';
import { cache } from '../../../util/cache/package/decorator';
import { joinUrlParts } from '../../../util/url';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import type { OrbResponse } from './types';
const MAX_VERSIONS = 100;
const query = `
query($packageName: String!, $maxVersions: Int!) {
orb(name: $packageName) {
name,
homeUrl,
isPrivate,
versions(count: $maxVersions) {
version,
createdAt
}
}
}
`;
export class OrbDatasource extends Datasource {
static readonly id = 'orb';
constructor() {
super(OrbDatasource.id);
}
override readonly customRegistrySupport = true;
override readonly defaultRegistryUrls = ['https://circleci.com/'];
override readonly registryStrategy = 'hunt';
override readonly releaseTimestampSupport = true;
override readonly releaseTimestampNote =
'The release timestamp is determined from the `createdAt` field in the results.';
@cache({
namespace: `datasource-${OrbDatasource.id}`,
key: ({ packageName }: GetReleasesConfig) => packageName,
})
async getReleases({
packageName,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
// istanbul ignore if
if (!registryUrl) {
return null;
}
const url = joinUrlParts(registryUrl, 'graphql-unstable');
const body = {
query,
variables: { packageName, maxVersions: MAX_VERSIONS },
};
const res = (
await this.http.postJson<OrbResponse>(url, {
body,
})
).body;
if (!res?.data?.orb) {
logger.debug({ res }, `Failed to look up orb ${packageName}`);
return null;
}
const { orb } = res.data;
// Simplify response before caching and returning
const homepage = orb.homeUrl?.length
? orb.homeUrl
: `https://circleci.com/developer/orbs/orb/${packageName}`;
const releases = orb.versions.map(({ version, createdAt }) => ({
version,
releaseTimestamp: createdAt ?? null,
}));
const dep = { homepage, isPrivate: !!orb.isPrivate, releases };
logger.trace({ dep }, 'dep');
return dep;
}
}