From 3da72f55abc7ae2e79c0ac0afaa210e39d741cc5 Mon Sep 17 00:00:00 2001 From: Mathusan Selvarajah Date: Tue, 19 Mar 2024 18:08:55 +0000 Subject: [PATCH] make fetchLinkableGitRepositories get all linkable git repositories --- src/gcp/devConnect.ts | 31 ++++++++++++++++++--------- src/test/gcp/devconnect.spec.ts | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/gcp/devConnect.ts b/src/gcp/devConnect.ts index d7fde818c66d..87bc582dfb9f 100644 --- a/src/gcp/devConnect.ts +++ b/src/gcp/devConnect.ts @@ -185,18 +185,29 @@ export async function fetchLinkableGitRepositories( projectId: string, location: string, connectionId: string, - pageToken = "", - pageSize = 1000, -): Promise { +): Promise { const name = `projects/${projectId}/locations/${location}/connections/${connectionId}:fetchLinkableRepositories`; - const res = await client.get(name, { - queryParams: { - pageSize, - pageToken, - }, - }); + const repos: LinkableGitRepository[] = []; - return res.body; + const getNextPage = async (pageToken = ""): Promise => { + const res = await client.get(name, { + queryParams: { + PAGE_SIZE_MAX, + pageToken, + }, + }); + + if (Array.isArray(res.body.repositories)) { + repos.push(...res.body.repositories); + } + + if (res.body.nextPageToken) { + await getNextPage(res.body.nextPageToken); + } + }; + + await getNextPage(); + return repos; } /** diff --git a/src/test/gcp/devconnect.spec.ts b/src/test/gcp/devconnect.spec.ts index 1b1c86cca869..0c9ae8236119 100644 --- a/src/test/gcp/devconnect.spec.ts +++ b/src/test/gcp/devconnect.spec.ts @@ -67,4 +67,41 @@ describe("developer connect", () => { expect(conns).to.deep.equal([firstConnection, secondConnection, thirdConnection]); }); }); + describe("fetchLinkableGitRepositories", () => { + it("interates through all pages and returns a single list", async () => { + const firstRepo = { cloneUri: "repo1" }; + const secondRepo = { cloneUri: "repo2" }; + const thirdRepo = { cloneUri: "repo3" }; + + get + .onFirstCall() + .returns({ + body: { + repositories: [firstRepo], + nextPageToken: "someToken", + }, + }) + .onSecondCall() + .returns({ + body: { + repositories: [secondRepo], + nextPageToken: "someToken2", + }, + }) + .onThirdCall() + .returns({ + body: { + repositories: [thirdRepo], + }, + }); + + const conns = await devconnect.fetchLinkableGitRepositories( + projectId, + location, + connectionId, + ); + expect(get).callCount(3); + expect(conns).to.deep.equal([firstRepo, secondRepo, thirdRepo]); + }); + }); });