From 86eced57e21ec1fd86d0eeaaaf6b3a5557280871 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Thu, 13 Jun 2024 22:10:50 +0200 Subject: [PATCH 1/3] fix(datasource/galaxy): support multiple results --- .../galaxy/__fixtures__/datadog.json | 1 + lib/modules/datasource/galaxy/index.spec.ts | 25 +++++++++++++++++++ lib/modules/datasource/galaxy/index.ts | 18 ++++++++----- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 lib/modules/datasource/galaxy/__fixtures__/datadog.json diff --git a/lib/modules/datasource/galaxy/__fixtures__/datadog.json b/lib/modules/datasource/galaxy/__fixtures__/datadog.json new file mode 100644 index 00000000000000..7c51a512e5f365 --- /dev/null +++ b/lib/modules/datasource/galaxy/__fixtures__/datadog.json @@ -0,0 +1 @@ +{"count":2,"next":null,"previous":null,"results":[{"id":7358,"upstream_id":null,"created":"2023-05-08T20:39:41.712620Z","modified":"2024-01-25T10:46:42.565078Z","imported":"2024-01-25T10:46:42.041295","github_user":"DataDog","username":"DataDog","github_repo":"ansible-datadog","github_branch":"main","commit":"286c8be5461a9f86063a40842a7f7ac85f1929d8","commit_message":"Prepare 4.22 release (#542)\n\n","name":"datadog","description":"Install Datadog agent and configure checks","summary_fields":{"dependencies":[],"namespace":{"id":1584,"name":"DataDog","avatar_url":"https://galaxy.ansible.com/api/pulp/api/v3/content/ansible/namespaces/018c3469-6cc2-76e4-8a81-557c3a806d80/avatar/"},"provider_namespace":{"id":12957,"name":"datadog","pulp_href":"/api/pulp/api/v3/pulp_ansible/namespaces/12957/"},"repository":{"name":"ansible-datadog","original_name":"ansible-datadog"},"tags":["monitoring"],"versions":[{"id":"5c821e30-5e2f-4cb5-a0d5-27f806cdaf44","name":"4.22.0","release_date":"2024-01-25T11:04:58+01:00"},{"id":"0f8f5505-e728-47d0-b62e-b7a32d42398a","name":"4.21.0","release_date":"2023-12-04T11:26:40+01:00"},{"id":"7d12eff3-e8b4-4d86-b1a2-6058e5d08676","name":"4.20.1","release_date":"2023-07-20T10:18:51-04:00"},{"id":"75ff5620-e11c-4f1e-829d-aa01070941db","name":"4.20.0","release_date":"2023-07-18T05:31:39-04:00"},{"id":"a2d45207-0daf-4b66-8f7c-b4be0d55b3b6","name":"4.19.0","release_date":"2023-05-10T08:04:01-04:00"},{"id":"14865b3d-7fef-4947-957b-690b1cc0c3c8","name":"4.18.0","release_date":"2023-01-12T09:53:08-05:00"},{"id":"4af6234d-12aa-4147-8dca-bfe38bbef6d4","name":"4.17.0","release_date":"2023-01-04T05:28:29-05:00"},{"id":"ded20247-ee4c-453c-a5e1-fd8ad963c575","name":"4.16.0","release_date":"2022-07-11T05:15:43-04:00"},{"id":"5f85cd03-042d-4d60-bfe9-62be7f553cb0","name":"4.15.0","release_date":"2022-04-20T02:47:29-04:00"},{"id":"6dc4da17-4a38-4fe2-a54c-f94c618edd2c","name":"4.14.0","release_date":"2022-02-08T09:34:55-05:00"},{"id":"f2b0dd5f-011b-4048-9069-937c71bfb85b","name":"4.13.0","release_date":"2022-01-21T06:57:13-05:00"}]},"download_count":10167551},{"id":33484,"upstream_id":null,"created":"2023-09-28T20:59:12.704850Z","modified":"2024-06-06T11:44:30.243787Z","imported":"2024-06-06T11:44:29.836996","github_user":"datadog","username":"datadog","github_repo":"ansible-datadog","github_branch":"main","commit":"2ebab5cbe09f30ee0257d2ea1b1625b716185702","commit_message":"Prepare 4.23.0 release, fix links on older changelog entries (#570)\n\n","name":"datadog","description":"Install Datadog agent and configure checks","summary_fields":{"dependencies":[],"namespace":{"id":8735,"name":"datadog","avatar_url":"https://galaxy.ansible.com/api/pulp/api/v3/content/ansible/namespaces/018c3469-6cc2-76e4-8a81-557c3a806d80/avatar/"},"provider_namespace":{"id":12957,"name":"datadog","pulp_href":"/api/pulp/api/v3/pulp_ansible/namespaces/12957/"},"repository":{"name":"ansible-datadog","original_name":"ansible-datadog"},"tags":["monitoring"],"versions":[{"id":"7481ba51-3c04-4208-b915-234902962c4d","name":"4.23.0","release_date":"2024-06-04T12:21:21-04:00"},{"id":"5661d630-1750-461e-acfd-93903d353e3a","name":"4.22.0","release_date":"2024-01-25T11:04:58+01:00"},{"id":"767a4ee9-b10f-4f22-b90d-7fb603f82450","name":"4.21.0","release_date":"2023-12-04T11:26:40+01:00"},{"id":"dd61aad5-e5fa-4dbe-b366-d5c2c9d9ee49","name":"4.20.1","release_date":"2023-07-20T10:18:51-04:00"},{"id":"76fdf4c1-0a82-425f-9417-e96fc309adbc","name":"4.20.0","release_date":"2023-07-18T05:31:39-04:00"},{"id":"e1eb55e6-a125-4bee-a56f-baf7faf94346","name":"4.19.0","release_date":"2023-05-10T08:04:01-04:00"},{"id":"742b5b35-b9b5-428d-ae60-35d5996e3af0","name":"4.18.0","release_date":"2023-01-12T09:53:08-05:00"},{"id":"6480bd7a-01c1-45d2-a4ec-7602310a2ac6","name":"4.17.0","release_date":"2023-01-04T05:28:29-05:00"},{"id":"a6689996-6e44-47b9-a61b-ffe62ae0f7c1","name":"4.16.0","release_date":"2022-07-11T05:15:43-04:00"},{"id":"e38041e7-2575-4a08-a3e4-fb4e95f013e4","name":"4.15.0","release_date":"2022-04-20T02:47:29-04:00"},{"id":"941282bd-4745-41eb-98a1-5356200ea4ad","name":"4.14.0","release_date":"2022-02-08T09:34:55-05:00"}]},"download_count":11933065}]} diff --git a/lib/modules/datasource/galaxy/index.spec.ts b/lib/modules/datasource/galaxy/index.spec.ts index 13cea554d13d4d..f08fff43999afa 100644 --- a/lib/modules/datasource/galaxy/index.spec.ts +++ b/lib/modules/datasource/galaxy/index.spec.ts @@ -87,6 +87,31 @@ describe('modules/datasource/galaxy/index', () => { expect(res).toBeDefined(); }); + it('handles multiple results when one matches', async () => { + httpMock + .scope(baseUrl) + .get('/api/v1/roles/?owner__username=datadog&name=datadog') + .reply(200, Fixtures.get('datadog.json')); + const res = await getPkgReleases({ + datasource: GalaxyDatasource.id, + packageName: 'datadog.datadog', + }); + expect(res).not.toBeNull(); + expect(res?.releases).toHaveLength(11); + }); + + it('rejects multiple results when one matches', async () => { + httpMock + .scope(baseUrl) + .get('/api/v1/roles/?owner__username=nope&name=nope') + .reply(200, Fixtures.get('datadog.json')); + const res = await getPkgReleases({ + datasource: GalaxyDatasource.id, + packageName: 'nope.nope', + }); + expect(res).toBeNull(); + }); + it('return null if searching random username and project name', async () => { httpMock .scope(baseUrl) diff --git a/lib/modules/datasource/galaxy/index.ts b/lib/modules/datasource/galaxy/index.ts index e85de9d2ee7387..9dc3b9c90b8980 100644 --- a/lib/modules/datasource/galaxy/index.ts +++ b/lib/modules/datasource/galaxy/index.ts @@ -50,13 +50,19 @@ export class GalaxyDatasource extends Datasource { this.handleGenericErrors(err); } - // istanbul ignore if if (body.results.length > 1) { - logger.warn( - { dependency: packageName }, - `Received multiple results from ${galaxyAPIUrl}`, - ); - return null; + const githubUsernames = body.results.map((result) => result.github_user); + if (githubUsernames.includes(userName)) { + body.results = body.results.filter( + (result) => result.github_user === userName, + ); + } else { + logger.warn( + { dependency: packageName, githubUsernames }, + `Multiple results from galaxy for ${packageName}, none match`, + ); + return null; + } } if (body.results.length === 0) { logger.info( From e311544cba4c451ff05c0400a3f96235744020c7 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Fri, 14 Jun 2024 16:06:01 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Sebastian Poxhofer --- lib/modules/datasource/galaxy/index.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/modules/datasource/galaxy/index.spec.ts b/lib/modules/datasource/galaxy/index.spec.ts index f08fff43999afa..15e5505ed8140d 100644 --- a/lib/modules/datasource/galaxy/index.spec.ts +++ b/lib/modules/datasource/galaxy/index.spec.ts @@ -87,7 +87,7 @@ describe('modules/datasource/galaxy/index', () => { expect(res).toBeDefined(); }); - it('handles multiple results when one matches', async () => { + it('handles multiple results when one user matches exactly', async () => { httpMock .scope(baseUrl) .get('/api/v1/roles/?owner__username=datadog&name=datadog') @@ -100,7 +100,7 @@ describe('modules/datasource/galaxy/index', () => { expect(res?.releases).toHaveLength(11); }); - it('rejects multiple results when one matches', async () => { + it('rejects multiple results when no user matches exactly', async () => { httpMock .scope(baseUrl) .get('/api/v1/roles/?owner__username=nope&name=nope') From 05ee612cbec4817f3495096d99fb887148241a59 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Mon, 23 Sep 2024 06:53:01 +0200 Subject: [PATCH 3/3] simplify --- lib/modules/datasource/galaxy/index.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/modules/datasource/galaxy/index.ts b/lib/modules/datasource/galaxy/index.ts index 9dc3b9c90b8980..a0a01011c53241 100644 --- a/lib/modules/datasource/galaxy/index.ts +++ b/lib/modules/datasource/galaxy/index.ts @@ -51,14 +51,12 @@ export class GalaxyDatasource extends Datasource { } if (body.results.length > 1) { - const githubUsernames = body.results.map((result) => result.github_user); - if (githubUsernames.includes(userName)) { - body.results = body.results.filter( - (result) => result.github_user === userName, - ); - } else { + body.results = body.results.filter( + (result) => result.github_user === userName, + ); + if (!body.results.length) { logger.warn( - { dependency: packageName, githubUsernames }, + { dependency: packageName, userName }, `Multiple results from galaxy for ${packageName}, none match`, ); return null;