Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeveloperConnect: make fetchLinkableGitRepositories list all linkable git repositories #6889

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions src/gcp/devConnect.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -23,7 +23,7 @@
metadata?: OperationMetadata;
done: boolean;
error?: { code: number; message: string; details: unknown };
response?: any;

Check warning on line 26 in src/gcp/devConnect.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
}

export interface OAuthCredential {
Expand Down Expand Up @@ -155,7 +155,10 @@
/**
* List Developer Connect Connections
*/
export async function listConnections(projectId: string, location: string): Promise<Connection[]> {
export async function listAllConnections(
projectId: string,
location: string,
): Promise<Connection[]> {
const conns: Connection[] = [];
const getNextPage = async (pageToken = ""): Promise<void> => {
const res = await client.get<{
Expand All @@ -181,22 +184,33 @@
/**
* 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<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,
mathu97 marked this conversation as resolved.
Show resolved Hide resolved
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
39 changes: 38 additions & 1 deletion src/test/gcp/devconnect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});
});
Loading