Skip to content

Commit

Permalink
make fetchLinkableGitRepositories get all linkable git repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
mathu97 committed Mar 19, 2024
1 parent c76e863 commit 3da72f5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/gcp/devConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,29 @@ export async function fetchLinkableGitRepositories(
projectId: string,
location: string,
connectionId: string,
pageToken = "",
pageSize = 1000,
): Promise<LinkableGitRepositories> {
): Promise<LinkableGitRepository[]> {
const name = `projects/${projectId}/locations/${location}/connections/${connectionId}:fetchLinkableRepositories`;
const res = await client.get<LinkableGitRepositories>(name, {
queryParams: {
pageSize,
pageToken,
},
});
const repos: LinkableGitRepository[] = [];

return res.body;
const getNextPage = async (pageToken = ""): Promise<void> => {
const res = await client.get<LinkableGitRepositories>(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;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/test/gcp/devconnect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});
});

0 comments on commit 3da72f5

Please sign in to comment.