Skip to content

Commit f818630

Browse files
committed
Support pagination to fetch _all_ Bitbucket branches
1 parent 4bf139e commit f818630

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

components/server/src/bitbucket/bitbucket-repository-provider.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,39 @@ export class BitbucketRepositoryProvider implements RepositoryProvider {
5959
async getBranches(user: User, owner: string, repo: string): Promise<Branch[]> {
6060
const branches: Branch[] = [];
6161
const api = await this.apiFactory.create(user);
62-
const response = await api.repositories.listBranches({
63-
workspace: owner,
64-
repo_slug: repo,
65-
sort: "target.date",
66-
});
6762

68-
for (const branch of response.data.values!) {
69-
branches.push({
70-
htmlUrl: branch.links?.html?.href!,
71-
name: branch.name!,
72-
commit: {
73-
sha: branch.target?.hash!,
74-
author: branch.target?.author?.user?.display_name!,
75-
authorAvatarUrl: branch.target?.author?.user?.links?.avatar?.href,
76-
authorDate: branch.target?.date!,
77-
commitMessage: branch.target?.message || "missing commit message",
78-
},
63+
// Handle pagination.
64+
let nextPage: number | undefined = 1;
65+
let isMoreDataAvailable: boolean = true;
66+
67+
while (isMoreDataAvailable) {
68+
const response = await api.repositories.listBranches({
69+
workspace: owner,
70+
repo_slug: repo,
71+
sort: "target.date",
72+
page: String(nextPage),
7973
});
74+
75+
for (const branch of response.data.values!) {
76+
branches.push({
77+
htmlUrl: branch.links?.html?.href!,
78+
name: branch.name!,
79+
commit: {
80+
sha: branch.target?.hash!,
81+
author: branch.target?.author?.user?.display_name!,
82+
authorAvatarUrl: branch.target?.author?.user?.links?.avatar?.href,
83+
authorDate: branch.target?.date!,
84+
commitMessage: branch.target?.message || "missing commit message",
85+
},
86+
});
87+
}
88+
89+
// If the response has a "next" property, it indicates there are more pages.
90+
if (response.data.next) {
91+
nextPage++;
92+
} else {
93+
isMoreDataAvailable = false;
94+
}
8095
}
8196

8297
return branches;

0 commit comments

Comments
 (0)