From e26868601e8f8a1f70e05ffe48c316972894d2e6 Mon Sep 17 00:00:00 2001 From: ivgo Date: Tue, 26 Sep 2023 13:57:27 +0200 Subject: [PATCH 1/4] Make sure the archived parameter is set in URL Signed-off-by: ivgo --- plugins/catalog-backend-module-gitlab/src/lib/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/catalog-backend-module-gitlab/src/lib/client.ts b/plugins/catalog-backend-module-gitlab/src/lib/client.ts index 79ae328ce2..daf82245e8 100644 --- a/plugins/catalog-backend-module-gitlab/src/lib/client.ts +++ b/plugins/catalog-backend-module-gitlab/src/lib/client.ts @@ -309,7 +309,7 @@ export class GitLabClient { ): Promise> { const request = new URL(`${this.config.apiBaseUrl}${endpoint}`); for (const key in options) { - if (options[key]) { + if (options[key] !== undefined && options[key] !== '') { request.searchParams.append(key, options[key]!.toString()); } } From 2da314370c0ca644b2b06ad333930deae1463889 Mon Sep 17 00:00:00 2001 From: ivgo Date: Tue, 26 Sep 2023 13:58:07 +0200 Subject: [PATCH 2/4] Remove unnecessary entry from test mock Signed-off-by: ivgo --- .../src/GitLabDiscoveryProcessor.test.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts b/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts index a56c229105..f836500c6e 100644 --- a/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts +++ b/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts @@ -237,14 +237,6 @@ describe('GitlabDiscoveryProcessor', () => { web_url: 'https://gitlab.fake/3', path_with_namespace: '3', }, - { - id: 4, - archived: true, // ARCHIVED - default_branch: 'master', - last_activity_at: '2021-08-05T11:03:05.774Z', - web_url: 'https://gitlab.fake/4', - path_with_namespace: '4', - }, { id: 5, archived: false, From c11113ddedcd80ebbbfe512d4c38d43ee8e05e2e Mon Sep 17 00:00:00 2001 From: ivgo Date: Fri, 29 Sep 2023 08:39:59 +0200 Subject: [PATCH 3/4] Add tests to verify the searchParams are added as expected Signed-off-by: ivgo --- .../src/lib/client.test.ts | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts b/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts index dda30fd86a..dad126e3e4 100644 --- a/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts +++ b/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts @@ -120,6 +120,14 @@ function setupFakeHasFileEndpoint(srv: SetupServer, apiBaseUrl: string) { ); } +function setupFakeURLReturnEndpoint(srv: SetupServer, url: string) { + srv.use( + rest.get(url, (req, res, ctx) => { + return res(ctx.json([{ endpoint: req.url.toString() }])); + }), + ); +} + describe('GitLabClient', () => { describe('isSelfManaged', () => { it('returns true if self managed instance', () => { @@ -933,3 +941,84 @@ describe('hasFile', () => { expect(hasFile).toBe(false); }); }); + +describe('pagedRequest search params', () => { + beforeEach(() => { + // setup fake paginated endpoint with 4 pages each returning one item + setupFakeURLReturnEndpoint(server, FAKE_PAGED_URL); + }); + + it('no search params provided', async () => { + const client = new GitLabClient({ + config: MOCK_CONFIG, + logger: getVoidLogger(), + }); + + const { items } = await client.pagedRequest<{ endpoint: string }>( + FAKE_PAGED_ENDPOINT, + ); + // fake page contains exactly one item + expect(items).toHaveLength(1); + expect(items).toEqual([ + { endpoint: 'https://example.com/api/v4/some-endpoint' }, + ]); + }); + + it('defined numeric search params', async () => { + const client = new GitLabClient({ + config: MOCK_CONFIG, + logger: getVoidLogger(), + }); + + const { items } = await client.pagedRequest<{ endpoint: string }>( + FAKE_PAGED_ENDPOINT, + { page: 1, per_page: 50 }, + ); + // fake page contains exactly one item + expect(items).toHaveLength(1); + expect(items).toEqual([ + { + endpoint: 'https://example.com/api/v4/some-endpoint?page=1&per_page=50', + }, + ]); + }); + + it('defined string search params', async () => { + const client = new GitLabClient({ + config: MOCK_CONFIG, + logger: getVoidLogger(), + }); + + const { items } = await client.pagedRequest<{ endpoint: string }>( + FAKE_PAGED_ENDPOINT, + { test: 'value', empty: '' }, + ); + // fake page contains exactly one item + expect(items).toHaveLength(1); + expect(items).toEqual([ + { + endpoint: 'https://example.com/api/v4/some-endpoint?test=value', + }, + ]); + }); + + it('defined boolean search params', async () => { + const client = new GitLabClient({ + config: MOCK_CONFIG, + logger: getVoidLogger(), + }); + + const { items } = await client.pagedRequest<{ endpoint: string }>( + FAKE_PAGED_ENDPOINT, + { active: true, archived: false }, + ); + // fake page contains exactly one item + expect(items).toHaveLength(1); + expect(items).toEqual([ + { + endpoint: + 'https://example.com/api/v4/some-endpoint?active=true&archived=false', + }, + ]); + }); +}); From 6ae7f12abb720529ff3e477f8d09890363a5d4e5 Mon Sep 17 00:00:00 2001 From: ivgo Date: Tue, 26 Sep 2023 14:07:44 +0200 Subject: [PATCH 4/4] Add changeset Signed-off-by: ivgo --- .changeset/shy-seas-sip.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shy-seas-sip.md diff --git a/.changeset/shy-seas-sip.md b/.changeset/shy-seas-sip.md new file mode 100644 index 0000000000..dfbc4ff0e0 --- /dev/null +++ b/.changeset/shy-seas-sip.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-gitlab': patch +--- + +Make sure the archived projects are skipped with the Gitlab API