Skip to content

Commit

Permalink
feat(datasource): add cpan datasource (#16545)
Browse files Browse the repository at this point in the history
* feat(datasource): add cpan datasource

* Update perl versioning's readme

* Extract copystr function to lib/util/string.ts

* Remove unnecessary optional chaining

* Use registryUrl and logger.trace

* Use `perl` versioning's `isStable`

* Remove possibly unnecessary try/catch

* Use MetaCPAN API

* Use reduce

* Use maturity field for `isStable` handling

https://metacpan.org/pod/CPAN::DistnameInfo#maturity

* Revert "Extract copystr function to lib/util/string.ts"

This reverts commit 51ebf55.

# Conflicts:
#	lib/modules/datasource/cpan/index.ts

* Remove downloadUrl

* Extract types

* Use toMatchObject

* Improve tests

* Rename to MetaCpanSearchResult

* Rename to searchUrl

* Rename to MetaCpanFileSearchResult and add tsdoc

* Use for...of over reduce

* More safely handle the response JSON

* Use joinUrlParts

* Don't embed distribution info

* Split tsdoc comments
  • Loading branch information
ikesyo committed Jul 26, 2022
1 parent 5bcbedc commit 8bff91a
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/modules/datasource/api.ts
Expand Up @@ -6,6 +6,7 @@ import { CdnJsDatasource } from './cdnjs';
import { ClojureDatasource } from './clojure';
import { ConanDatasource } from './conan';
import { CondaDatasource } from './conda';
import { CpanDatasource } from './cpan';
import { CrateDatasource } from './crate';
import { DartDatasource } from './dart';
import { DockerDatasource } from './docker';
Expand Down Expand Up @@ -55,6 +56,7 @@ api.set(CdnJsDatasource.id, new CdnJsDatasource());
api.set(ClojureDatasource.id, new ClojureDatasource());
api.set(ConanDatasource.id, new ConanDatasource());
api.set(CondaDatasource.id, new CondaDatasource());
api.set(CpanDatasource.id, new CpanDatasource());
api.set(CrateDatasource.id, new CrateDatasource());
api.set(DartDatasource.id, new DartDatasource());
api.set(DockerDatasource.id, new DockerDatasource());
Expand Down
235 changes: 235 additions & 0 deletions lib/modules/datasource/cpan/__fixtures__/Plack.json
@@ -0,0 +1,235 @@
{
"took" : 3,
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
},
"hits" : {
"total" : 169,
"hits" : [
{
"_type" : "file",
"_id" : "iGmpDWEqEQB4FO1lXb0j3tLgIDw",
"_index" : "cpan_v1_01",
"sort" : [
1606695696000
],
"_score" : null,
"_source" : {
"deprecated" : false,
"module" : [
{
"name" : "Plack",
"version" : "1.0048"
}
],
"date" : "2020-11-30T00:21:36",
"maturity" : "released",
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0048.tar.gz"
}
},
{
"_index" : "cpan_v1_01",
"_id" : "rV2b5IZa2PG_JbjbYRdzPXZPFw4",
"sort" : [
1518254730000
],
"_score" : null,
"_source" : {
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0047.tar.gz",
"deprecated" : false,
"module" : [
{
"version" : "1.0047",
"name" : "Plack"
}
],
"date" : "2018-02-10T09:25:30",
"maturity" : "released"
},
"_type" : "file"
},
{
"_source" : {
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0046.tar.gz",
"maturity" : "released",
"module" : [
{
"version" : "1.0046",
"name" : "Plack"
}
],
"date" : "2018-02-10T07:52:31",
"deprecated" : false
},
"sort" : [
1518249151000
],
"_score" : null,
"_index" : "cpan_v1_01",
"_id" : "K3Pl0SIFDJ2K3kt5HxLnT2gVIxc",
"_type" : "file"
},
{
"_source" : {
"deprecated" : false,
"maturity" : "released",
"module" : [
{
"version" : "1.0045",
"name" : "Plack"
}
],
"date" : "2017-12-31T20:42:50",
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0045.tar.gz"
},
"_index" : "cpan_v1_01",
"_id" : "mMVzdQVT1y8stbntvAQQc87mGeM",
"sort" : [
1514752970000
],
"_score" : null,
"_type" : "file"
},
{
"_type" : "file",
"_source" : {
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0044.tar.gz",
"distribution" : "Plack",
"module" : [
{
"name" : "Plack",
"version" : "1.0044"
}
],
"date" : "2017-04-27T17:48:20",
"maturity" : "released",
"deprecated" : false
},
"_score" : null,
"sort" : [
1493315300000
],
"_index" : "cpan_v1_01",
"_id" : "FCsCemrqDXJgxmIF1MYaf3yxHtk"
},
{
"_type" : "file",
"_index" : "cpan_v1_01",
"_id" : "Noy_4t07AY__XEYnpJ9efRqKkNY",
"_score" : null,
"sort" : [
1487732525000
],
"_source" : {
"deprecated" : false,
"module" : [
{
"name" : "Plack",
"version" : "1.0043"
}
],
"date" : "2017-02-22T03:02:05",
"maturity" : "released",
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0043.tar.gz"
}
},
{
"_type" : "file",
"_score" : null,
"sort" : [
1475127522000
],
"_index" : "cpan_v1_01",
"_id" : "xC2vIHM6w4Bruk0EsQCeVm25d2c",
"_source" : {
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0042.tar.gz",
"maturity" : "released",
"module" : [
{
"name" : "Plack",
"version" : "1.0042"
}
],
"date" : "2016-09-29T05:38:42",
"deprecated" : false
}
},
{
"_source" : {
"deprecated" : false,
"maturity" : "released",
"module" : [
{
"name" : "Plack",
"version" : "1.0041"
}
],
"date" : "2016-09-25T21:25:47",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0041.tar.gz",
"distribution" : "Plack"
},
"_index" : "cpan_v1_01",
"_id" : "JBBPOsgzGyMPb5CkrBY8GnrCocs",
"_score" : null,
"sort" : [
1474838747000
],
"_type" : "file"
},
{
"_source" : {
"distribution" : "Plack",
"download_url" : "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Plack-1.0040-TRIAL.tar.gz",
"deprecated" : false,
"module" : [
{
"version" : "1.0040",
"name" : "Plack"
}
],
"date" : "2016-04-01T16:58:21",
"maturity" : "developer"
},
"sort" : [
1459529901000
],
"_score" : null,
"_index" : "cpan_v1_01",
"_id" : "NkAJcXogQmuVpc46K4jm07QqUS8",
"_type" : "file"
},
{
"_type" : "file",
"_source" : {
"deprecated" : false,
"maturity" : "released",
"module" : [
{
"version" : "1.0039",
"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"
},
"_index" : "cpan_v1_01",
"_id" : "Y7WlIYOZjk3rh9O05F3UZE6WwGo",
"_score" : null,
"sort" : [
1449401380000
]
}
],
"max_score" : null
},
"timed_out" : false
}
14 changes: 14 additions & 0 deletions lib/modules/datasource/cpan/__fixtures__/empty.json
@@ -0,0 +1,14 @@
{
"took" : 3,
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
},
"hits" : {
"max_score" : null,
"hits" : [],
"total" : 0
},
"timed_out" : false
}
91 changes: 91 additions & 0 deletions lib/modules/datasource/cpan/index.spec.ts
@@ -0,0 +1,91 @@
import { getPkgReleases } from '..';
import { Fixtures } from '../../../../test/fixtures';
import * as httpMock from '../../../../test/http-mock';
import { EXTERNAL_HOST_ERROR } from '../../../constants/error-messages';
import { CpanDatasource } from '.';

const baseUrl = 'https://fastapi.metacpan.org/';

describe('modules/datasource/cpan/index', () => {
describe('getReleases', () => {
it('returns null for empty result', async () => {
httpMock
.scope(baseUrl)
.post(
'/v1/file/_search',
(body) =>
body.query.filtered.filter.and[0].term['module.name'] === 'FooBar'
)
.reply(200, Fixtures.get('empty.json'));
expect(
await getPkgReleases({
datasource: CpanDatasource.id,
depName: 'FooBar',
})
).toBeNull();
});

it('returns null for 404', async () => {
httpMock.scope(baseUrl).post('/v1/file/_search').reply(404);
expect(
await getPkgReleases({
datasource: CpanDatasource.id,
depName: 'Plack',
})
).toBeNull();
});

it('throws for 5xx', async () => {
httpMock.scope(baseUrl).post('/v1/file/_search').reply(502);
await expect(
getPkgReleases({
datasource: CpanDatasource.id,
depName: 'Plack',
})
).rejects.toThrow(EXTERNAL_HOST_ERROR);
});

it('returns null for unknown error', async () => {
httpMock.scope(baseUrl).post('/v1/file/_search').replyWithError('');
expect(
await getPkgReleases({
datasource: CpanDatasource.id,
depName: 'Plack',
})
).toBeNull();
});

it('processes real data', async () => {
httpMock
.scope(baseUrl)
.post(
'/v1/file/_search',
(body) =>
body.query.filtered.filter.and[0].term['module.name'] === 'Plack'
)
.reply(200, Fixtures.get('Plack.json'));
const res = await getPkgReleases({
datasource: CpanDatasource.id,
depName: 'Plack',
});
expect(res).toMatchObject({
changelogUrl: 'https://metacpan.org/dist/Plack/changes',
homepage: 'https://metacpan.org/pod/Plack',
registryUrl: 'https://fastapi.metacpan.org/',
releases: expect.toBeArrayOfSize(10),
});
expect(res?.releases[1]).toMatchObject({
isDeprecated: false,
isStable: false,
releaseTimestamp: '2016-04-01T16:58:21.000Z',
version: '1.0040',
});
expect(res?.releases[9]).toMatchObject({
isDeprecated: false,
isStable: true,
releaseTimestamp: '2020-11-30T00:21:36.000Z',
version: '1.0048',
});
});
});
});

0 comments on commit 8bff91a

Please sign in to comment.