diff --git a/src/gcp/devConnect.ts b/src/gcp/devConnect.ts index d7fde818c66..9dc8caf5edb 100644 --- a/src/gcp/devConnect.ts +++ b/src/gcp/devConnect.ts @@ -1,7 +1,7 @@ import { Client } from "../apiv2"; import { developerConnectOrigin, developerConnectP4SAOrigin } from "../api"; -const PAGE_SIZE_MAX = 100; +const PAGE_SIZE_MAX = 1000; export const client = new Client({ urlPrefix: developerConnectOrigin, @@ -155,7 +155,10 @@ export async function getConnection( /** * List Developer Connect Connections */ -export async function listConnections(projectId: string, location: string): Promise { +export async function listAllConnections( + projectId: string, + location: string, +): Promise { const conns: Connection[] = []; const getNextPage = async (pageToken = ""): Promise => { const res = await client.get<{ @@ -181,22 +184,33 @@ export async function listConnections(projectId: string, location: string): Prom /** * Gets a list of repositories that can be added to the provided Connection. */ -export async function fetchLinkableGitRepositories( +export async function listAllLinkableGitRepositories( 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 1b1c86cca86..6d6b617e604 100644 --- a/src/test/gcp/devconnect.spec.ts +++ b/src/test/gcp/devconnect.spec.ts @@ -62,9 +62,46 @@ describe("developer connect", () => { }, }); - const conns = await devconnect.listConnections(projectId, location); + const conns = await devconnect.listAllConnections(projectId, location); expect(get).callCount(3); expect(conns).to.deep.equal([firstConnection, secondConnection, thirdConnection]); }); }); + describe("listAllLinkableGitRepositories", () => { + 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.listAllLinkableGitRepositories( + projectId, + location, + connectionId, + ); + expect(get).callCount(3); + expect(conns).to.deep.equal([firstRepo, secondRepo, thirdRepo]); + }); + }); });