Skip to content

Commit 594a2d8

Browse files
committed
fix: paginate platform branch list
1 parent 58eadb2 commit 594a2d8

2 files changed

Lines changed: 47 additions & 11 deletions

File tree

packages/cli/src/controllers/branch.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,33 @@ async function listBranches(
7777
projectId: string,
7878
signal: AbortSignal,
7979
): Promise<RawBranchRecord[]> {
80-
const { data, error, response } = await client.GET("/v1/projects/{projectId}/branches", {
81-
params: { path: { projectId } },
82-
signal,
83-
});
84-
if (error || !data) {
85-
throw branchApiError("Failed to list branches", response, error);
80+
const collected: RawBranchRecord[] = [];
81+
let cursor: string | undefined;
82+
83+
// eslint-disable-next-line no-constant-condition
84+
while (true) {
85+
const query: Record<string, string | undefined> = {};
86+
if (cursor !== undefined) {
87+
query.cursor = cursor;
88+
}
89+
90+
const { data, error, response } = await client.GET("/v1/projects/{projectId}/branches", {
91+
params: { path: { projectId }, query },
92+
signal,
93+
});
94+
if (error || !data) {
95+
throw branchApiError("Failed to list branches", response, error);
96+
}
97+
98+
collected.push(...data.data as RawBranchRecord[]);
99+
100+
if (!data.pagination.hasMore || !data.pagination.nextCursor) {
101+
break;
102+
}
103+
cursor = data.pagination.nextCursor;
86104
}
87105

88-
return data.data as RawBranchRecord[];
106+
return collected;
89107
}
90108

91109
function toBranchSummary(branch: RawBranchRecord): BranchSummary {

packages/cli/tests/branch-controller.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ afterEach(() => {
1414

1515
function createMockClient() {
1616
return {
17-
GET: vi.fn().mockImplementation((pathName: string) => {
17+
GET: vi.fn().mockImplementation((pathName: string, request?: { params?: { query?: { cursor?: string } } }) => {
1818
if (pathName === "/v1/projects") {
1919
return {
2020
data: {
@@ -32,13 +32,25 @@ function createMockClient() {
3232
}
3333

3434
if (pathName === "/v1/projects/{projectId}/branches") {
35+
const cursor = request?.params?.query?.cursor;
36+
if (cursor === "cursor_2") {
37+
return {
38+
data: {
39+
data: [
40+
{ id: "br_feature", gitName: "feature/auth", role: "preview" },
41+
],
42+
pagination: { hasMore: false, nextCursor: null },
43+
},
44+
response: { status: 200 },
45+
};
46+
}
47+
3548
return {
3649
data: {
3750
data: [
3851
{ id: "br_main", gitName: "main", role: "production" },
39-
{ id: "br_feature", gitName: "feature/auth", role: "preview" },
4052
],
41-
pagination: { hasMore: false, nextCursor: null },
53+
pagination: { hasMore: true, nextCursor: "cursor_2" },
4254
},
4355
response: { status: 200 },
4456
};
@@ -93,7 +105,13 @@ describe("branch controller", () => {
93105
expect(client.GET).toHaveBeenCalledWith(
94106
"/v1/projects/{projectId}/branches",
95107
expect.objectContaining({
96-
params: { path: { projectId: "proj_123" } },
108+
params: { path: { projectId: "proj_123" }, query: {} },
109+
}),
110+
);
111+
expect(client.GET).toHaveBeenCalledWith(
112+
"/v1/projects/{projectId}/branches",
113+
expect.objectContaining({
114+
params: { path: { projectId: "proj_123" }, query: { cursor: "cursor_2" } },
97115
}),
98116
);
99117
expect(result).toEqual({

0 commit comments

Comments
 (0)