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(datasource/cpan): populate latest tag #32677

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions lib/modules/datasource/cpan/__fixtures__/Plack.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"version" : "1.0048"
}
],
"status": "latest",
"date" : "2020-11-30T00:21:36",
"maturity" : "released",
"distribution" : "Plack",
Expand All @@ -47,6 +48,7 @@
"name" : "Plack"
}
],
"status": "cpan",
"date" : "2018-02-10T09:25:30",
"maturity" : "released"
},
Expand All @@ -63,6 +65,7 @@
"name" : "Plack"
}
],
"status": "cpan",
"date" : "2018-02-10T07:52:31",
"deprecated" : false
},
Expand All @@ -84,6 +87,7 @@
"name" : "Plack"
}
],
"status": "cpan",
"date" : "2017-12-31T20:42:50",
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0045.tar.gz"
Expand All @@ -107,6 +111,7 @@
"version" : "1.0044"
}
],
"status": "cpan",
"date" : "2017-04-27T17:48:20",
"maturity" : "released",
"deprecated" : false
Expand Down Expand Up @@ -134,6 +139,7 @@
"version" : "1.0043"
}
],
"status": "cpan",
"date" : "2017-02-22T03:02:05",
"maturity" : "released",
"distribution" : "Plack",
Expand All @@ -158,6 +164,7 @@
"version" : "1.0042"
}
],
"status": "cpan",
"date" : "2016-09-29T05:38:42",
"deprecated" : false
}
Expand All @@ -172,6 +179,7 @@
"version" : "1.0041"
}
],
"status": "cpan",
"date" : "2016-09-25T21:25:47",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0041.tar.gz",
"distribution" : "Plack"
Expand All @@ -195,6 +203,7 @@
"name" : "Plack"
}
],
"status": "cpan",
"date" : "2016-04-01T16:58:21",
"maturity" : "developer"
},
Expand All @@ -217,6 +226,7 @@
"name" : "Plack"
}
],
"status": "cpan",
"date" : "2015-12-06T11:29:40",
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0039.tar.gz"
Expand All @@ -227,6 +237,29 @@
"sort" : [
1449401380000
]
},
{
"_type" : "file",
"_source" : {
"deprecated" : false,
"maturity" : "released",
"module" : [
{
"version" : "1.0038",
"name" : "Plack"
}
],
"date" : "2015-12-06T11:29:40",
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0039.tar.gz"
},
"status": "invalid",
"_index" : "cpan_v1_01",
"_id" : "Y7WlIYOZjk3rh9O05F3UZE6WwGo",
"_score" : null,
"sort" : [
1449401380000
]
}
],
"max_score" : null
Expand Down
1 change: 1 addition & 0 deletions lib/modules/datasource/cpan/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('modules/datasource/cpan/index', () => {
releaseTimestamp: '2020-11-30T00:21:36.000Z',
version: '1.0048',
});
expect(res?.tags?.latest).toBe('1.0048');
});
});
});
73 changes: 33 additions & 40 deletions lib/modules/datasource/cpan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { cache } from '../../../util/cache/package/decorator';
import { joinUrlParts } from '../../../util/url';
import * as perlVersioning from '../../versioning/perl';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import type { MetaCpanApiFile, MetaCpanApiFileSearchResult } from './types';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import { MetaCpanApiFileSearchResponse } from './schema';
import type { CpanRelease } from './types';

export class CpanDatasource extends Datasource {
static readonly id = 'cpan';
Expand Down Expand Up @@ -38,7 +39,7 @@ export class CpanDatasource extends Datasource {
let result: ReleaseResult | null = null;
const searchUrl = joinUrlParts(registryUrl, 'v1/file/_search');

let hits: MetaCpanApiFile[] | null = null;
let releases: CpanRelease[] | null = null;
try {
const body = {
query: {
Expand All @@ -60,55 +61,47 @@ export class CpanDatasource extends Datasource {
'date',
'deprecated',
'maturity',
'status',
],
sort: [{ date: 'desc' }],
};
const res = await this.http.postJson<MetaCpanApiFileSearchResult>(
searchUrl,
{ body },
);
hits = res.body?.hits?.hits?.map(({ _source }) => _source);

releases = (
await this.http.postJson(
searchUrl,
{ body },
MetaCpanApiFileSearchResponse,
)
).body;
} catch (err) {
this.handleGenericErrors(err);
}

let latestDistribution: string | null = null;
if (hits) {
const releases: Release[] = [];
for (const hit of hits) {
const {
module,
distribution,
date: releaseTimestamp,
deprecated: isDeprecated,
maturity,
} = hit;
const version = module.find(
({ name }) => name === packageName,
)?.version;
if (version) {
// https://metacpan.org/pod/CPAN::DistnameInfo#maturity
const isStable = maturity === 'released';
releases.push({
isDeprecated,
isStable,
releaseTimestamp,
version,
});

if (!latestDistribution) {
latestDistribution = distribution;
}
let latestVersion: string | null = null;
if (releases) {
for (const release of releases) {
if (!latestDistribution) {
latestDistribution = release.distribution;
}
if (!latestVersion && release.isLatest) {
latestVersion = release.version;
}
}
if (releases.length > 0 && latestDistribution) {
result = {
releases,
changelogUrl: `https://metacpan.org/dist/${latestDistribution}/changes`,
homepage: `https://metacpan.org/pod/${packageName}`,
};
}
if (releases.length > 0 && latestDistribution) {
result = {
releases,
changelogUrl: `https://metacpan.org/dist/${latestDistribution}/changes`,
homepage: `https://metacpan.org/pod/${packageName}`,
};

if (latestVersion) {
result.tags ??= {};
result.tags.latest = latestVersion;
}
}

RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
return result;
}
}
64 changes: 64 additions & 0 deletions lib/modules/datasource/cpan/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { z } from 'zod';
import { LooseArray } from '../../../util/schema-utils';
import type { CpanRelease } from './types';

/**
* https://fastapi.metacpan.org/v1/file/_mapping
*/
const MetaCpanApiFileSchema = z
.object({
module: LooseArray(
z.object({
name: z.string(),
version: z.string(),
}),
),
distribution: z.string(),
date: z.string(),
deprecated: z.boolean(),
maturity: z.string(),
status: z.union([
z.literal('backpan'),
z.literal('cpan'),
z.literal('latest'),
]),
})
.transform(
({
module,
distribution,
date,
deprecated,
maturity,
status,
}): CpanRelease | undefined => {
return {
version: module[0].version,
distribution,
isDeprecated: deprecated,
isStable: maturity === 'released',
releaseTimestamp: date,
isLatest: status === 'latest',
};
},
)
.catch(undefined);
/**
* https://github.com/metacpan/metacpan-api/blob/master/docs/API-docs.md#available-fields
*/
export const MetaCpanApiFileSearchResponse = z
.object({
hits: z.object({
hits: LooseArray(
z.object({
_source: MetaCpanApiFileSchema,
}),
),
}),
})
.transform((data): CpanRelease[] => {
// Extract all hits and filter out ones where _source transformed to undefined
return data.hits.hits
.map((hit) => hit._source)
.filter((source) => source !== undefined);
});
25 changes: 3 additions & 22 deletions lib/modules/datasource/cpan/types.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
/**
* https://fastapi.metacpan.org/v1/file/_mapping
*/
export interface MetaCpanApiFile {
module: {
name: string;
version?: string;
}[];
distribution: string;
date: string;
deprecated: boolean;
maturity: string;
}
import type { Release } from '../types';

/**
* https://github.com/metacpan/metacpan-api/blob/master/docs/API-docs.md#available-fields
*/
export interface MetaCpanApiFileSearchResult {
hits: {
hits: {
_source: MetaCpanApiFile;
}[];
};
export interface CpanRelease extends Release {
distribution: string;
}
1 change: 1 addition & 0 deletions lib/modules/datasource/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface Release {
sourceUrl?: string | undefined;
sourceDirectory?: string;
currentAge?: string;
isLatest?: boolean;
}

export interface ReleaseResult {
Expand Down