Skip to content

Commit

Permalink
Support pagination to fetch _all_ Bitbucket branches (#18563)
Browse files Browse the repository at this point in the history
Co-authored-by: Filip Troníček <filip@gitpod.io>
  • Loading branch information
jankeromnes and filiptronicek authored Aug 23, 2023
1 parent 2419f89 commit c85581c
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions components/server/src/bitbucket/bitbucket-repository-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,40 @@ export class BitbucketRepositoryProvider implements RepositoryProvider {
async getBranches(user: User, owner: string, repo: string): Promise<Branch[]> {
const branches: Branch[] = [];
const api = await this.apiFactory.create(user);
const response = await api.repositories.listBranches({
workspace: owner,
repo_slug: repo,
sort: "target.date",
});

for (const branch of response.data.values!) {
branches.push({
htmlUrl: branch.links?.html?.href!,
name: branch.name!,
commit: {
sha: branch.target?.hash!,
author: branch.target?.author?.user?.display_name!,
authorAvatarUrl: branch.target?.author?.user?.links?.avatar?.href,
authorDate: branch.target?.date!,
commitMessage: branch.target?.message || "missing commit message",
},
// Handle pagination.
let nextPage = 1;
let isMoreDataAvailable = true;

while (isMoreDataAvailable) {
const response = await api.repositories.listBranches({
workspace: owner,
repo_slug: repo,
sort: "target.date",
page: String(nextPage),
pagelen: 100,
});

for (const branch of response.data.values!) {
branches.push({
htmlUrl: branch.links?.html?.href!,
name: branch.name!,
commit: {
sha: branch.target?.hash!,
author: branch.target?.author?.user?.display_name!,
authorAvatarUrl: branch.target?.author?.user?.links?.avatar?.href,
authorDate: branch.target?.date!,
commitMessage: branch.target?.message || "missing commit message",
},
});
}

// If the response has a "next" property, it indicates there are more pages.
if (response.data.next) {
nextPage++;
} else {
isMoreDataAvailable = false;
}
}

return branches;
Expand Down

0 comments on commit c85581c

Please sign in to comment.