Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(conan): Use schema for datasource #33528

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/modules/datasource/conan/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const defaultRegistryUrl = 'https://center2.conan.io/';
export const datasource = 'conan';

export const conanDatasourceRegex = regEx(
/(?<name>[a-zA-Z\-_0-9]+)\/(?<version>[^@/\n]+)(?<userChannel>@\S+\/\S+)/gim,
/^(?<name>[a-zA-Z\-_0-9]+)\/(?<version>[^@/\n]+)(?<userChannel>@\S+\/\S+)$/im,
);

export function getConanPackage(packageName: string): ConanPackage {
Expand Down
1 change: 1 addition & 0 deletions lib/modules/datasource/conan/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ describe('modules/datasource/conan/index', () => {
version: '1.1.1',
},
],
sourceUrl: 'https://fake.conan.url.com',
});
});

Expand Down
77 changes: 27 additions & 50 deletions lib/modules/datasource/conan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { logger } from '../../../logger';
import { cache } from '../../../util/cache/package/decorator';
import { GithubHttp } from '../../../util/http/github';
import { ensureTrailingSlash, joinUrlParts } from '../../../util/url';
import { parseSingleYaml } from '../../../util/yaml';
import * as allVersioning from '../../versioning';
import { Datasource } from '../datasource';
import type {
Expand All @@ -13,19 +12,14 @@ import type {
ReleaseResult,
} from '../types';
import { isArtifactoryServer } from '../util';
import { datasource, defaultRegistryUrl, getConanPackage } from './common';
import {
conanDatasourceRegex,
datasource,
defaultRegistryUrl,
getConanPackage,
} from './common';
import type {
ConanCenterReleases,
ConanJSON,
ConanLatestRevision,
ConanProperties,
ConanRevisionJSON,
ConanRevisionsJSON,
ConanYAML,
} from './types';
} from './schema';

export class ConanDatasource extends Datasource {
static readonly id = datasource;
Expand Down Expand Up @@ -62,13 +56,7 @@ export class ConanDatasource extends Datasource {
const res = await this.githubHttp.get(url, {
headers: { accept: 'application/vnd.github.v3.raw' },
});
// TODO: use schema (#9610)
const doc = parseSingleYaml<ConanYAML>(res.body);
return {
releases: Object.keys(doc?.versions ?? {}).map((version) => ({
version,
})),
};
return ConanCenterReleases.parse(res.body);
}

@cache({
Expand All @@ -94,10 +82,11 @@ export class ConanDatasource extends Datasource {
conanPackage.userAndChannel,
'/revisions',
);
const revisionRep =
await this.http.getJson<ConanRevisionsJSON>(revisionLookUp);
const revisions = revisionRep?.body.revisions;
return revisions?.[0].revision ?? null;
const { body: digest } = await this.http.getJson(
revisionLookUp,
ConanLatestRevision,
);
return digest;
}

@cache({
Expand Down Expand Up @@ -135,25 +124,16 @@ export class ConanDatasource extends Datasource {
);

try {
const rep = await this.http.getJson<ConanJSON>(lookupUrl);
const versions = rep?.body;
if (versions) {
const rep = await this.http.getJson(lookupUrl);
const conanJson = ConanJSON.parse(rep.body);
if (conanJson) {
logger.trace({ lookupUrl }, 'Got conan api result');
const dep: ReleaseResult = { releases: [] };

for (const resultString of Object.values(versions.results ?? {})) {
conanDatasourceRegex.lastIndex = 0;
const fromMatch = conanDatasourceRegex.exec(resultString);
if (fromMatch?.groups?.version && fromMatch?.groups?.userChannel) {
const version = fromMatch.groups.version;
if (fromMatch.groups.userChannel === userAndChannel) {
const result: Release = {
version,
};
dep.releases.push(result);
}
}
}
const conanJsonReleases: Release[] = conanJson
.filter(({ userChannel }) => userChannel === userAndChannel)
.map(({ version }) => ({ version }));
dep.releases.push(...conanJsonReleases);

try {
if (isArtifactoryServer(rep)) {
Expand Down Expand Up @@ -182,25 +162,22 @@ export class ConanDatasource extends Datasource {
url,
`v2/conans/${conanPackage.conanName}/${latestVersion}/${conanPackage.userAndChannel}/latest`,
);
const revResp =
await this.http.getJson<ConanRevisionJSON>(latestRevisionUrl);
const packageRev = revResp.body.revision;
const {
body: { revision: packageRev },
} = await this.http.getJson(latestRevisionUrl, ConanRevisionJSON);

const [user, channel] = conanPackage.userAndChannel.split('/');
const packageUrl = joinUrlParts(
`${groups.host}/artifactory/api/storage/${groups.repo}`,
`${user}/${conanPackage.conanName}/${latestVersion}/${channel}/${packageRev}/export/conanfile.py?properties=conan.package.url`,
);
const packageUrlResp =
await this.http.getJson<ConanProperties>(packageUrl);

if (
packageUrlResp.body.properties &&
'conan.package.url' in packageUrlResp.body.properties
) {
const conanPackageUrl =
packageUrlResp.body.properties['conan.package.url'][0];
dep.sourceUrl = conanPackageUrl;
const { body: conanProperties } = await this.http.getJson(
packageUrl,
ConanProperties,
);
const { sourceUrl } = conanProperties;
if (sourceUrl) {
dep.sourceUrl = sourceUrl;
}
}
} catch (err) {
Expand Down
66 changes: 66 additions & 0 deletions lib/modules/datasource/conan/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { z } from 'zod';
import { LooseArray, Yaml } from '../../../util/schema-utils';
import type { ReleaseResult } from '../types';
import { conanDatasourceRegex } from './common';

export const ConanCenterReleases = Yaml.pipe(
z.object({
versions: z.record(z.string(), z.unknown()),
}),
)
.transform(
({ versions }): ReleaseResult => ({
releases: Object.keys(versions).map((version) => ({ version })),
}),
)
.nullable()
.catch(null);

export const ConanJSON = z
.object({
results: z
.string()
.array()
.transform((array) =>
array.map((val) => val.match(conanDatasourceRegex)?.groups),
)
.pipe(
LooseArray(
z.object({
name: z.string(),
version: z.string(),
userChannel: z.string(),
}),
),
),
})
.transform(({ results }) => results)
.nullable()
.catch(null);

export const ConanRevisionJSON = z.object({
revision: z.string(),
time: z.string(),
});

export const ConanLatestRevision = z
.object({ revisions: z.unknown().array() })
.transform(({ revisions }) => revisions[0])
.pipe(ConanRevisionJSON)
.transform(({ revision }) => revision)
.nullable()
.catch(null);

export const ConanProperties = z
.object({
properties: z.object({
'conan.package.url': z.union([
z.string().transform((url) => [url]),
z.string().array(),
]),
}),
})
.transform(({ properties }) => {
const sourceUrl = properties['conan.package.url'][0];
return { sourceUrl };
});
31 changes: 0 additions & 31 deletions lib/modules/datasource/conan/types.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,4 @@
export interface ConanJSON {
results?: Record<string, string>;
}

export interface ConanRevisionJSON {
revision: string;
time: string;
}

export interface ConanRevisionsJSON {
revisions?: Record<string, ConanRevisionJSON>;
}

export interface ConanYAML {
versions?: Record<string, unknown>;
}

export interface ConanPackage {
conanName: string;
userAndChannel: string;
}

export interface ConanRecipeProperties {
'conan.package.channel': string[];
'conan.package.license': string[];
'conan.package.name': string[];
'conan.package.url': string[];
'conan.package.user': string[];
'conan.package.version': string[];
}

export interface ConanProperties {
properties: ConanRecipeProperties;
uri: string;
}
Loading