Skip to content

Commit c85581c

Browse files
Support pagination to fetch _all_ Bitbucket branches (#18563)
Co-authored-by: Filip Troníček <[email protected]>
1 parent 2419f89 commit c85581c

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,40 @@ 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 = 1;
65+
let isMoreDataAvailable = 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),
73+
pagelen: 100,
7974
});
75+
76+
for (const branch of response.data.values!) {
77+
branches.push({
78+
htmlUrl: branch.links?.html?.href!,
79+
name: branch.name!,
80+
commit: {
81+
sha: branch.target?.hash!,
82+
author: branch.target?.author?.user?.display_name!,
83+
authorAvatarUrl: branch.target?.author?.user?.links?.avatar?.href,
84+
authorDate: branch.target?.date!,
85+
commitMessage: branch.target?.message || "missing commit message",
86+
},
87+
});
88+
}
89+
90+
// If the response has a "next" property, it indicates there are more pages.
91+
if (response.data.next) {
92+
nextPage++;
93+
} else {
94+
isMoreDataAvailable = false;
95+
}
8096
}
8197

8298
return branches;

0 commit comments

Comments
 (0)