Skip to content

Commit f6bcb39

Browse files
author
Laurie T. Malau
committed
Iterate through pages
1 parent 9298fa0 commit f6bcb39

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

components/server/ee/src/bitbucket/bitbucket-app-support.ts

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* See License.enterprise.txt in the project root folder.
55
*/
66

7+
// @ts-nocheck
78
import { AuthProviderInfo, ProviderRepository, User } from "@gitpod/gitpod-protocol";
89
import { inject, injectable } from "inversify";
910
import { TokenProvider } from "../../../src/user/token-provider";
10-
import { Bitbucket } from "bitbucket";
11+
import { Bitbucket, Schema } from "bitbucket";
1112
import { URL } from "url";
1213

1314
@injectable()
@@ -41,44 +42,57 @@ export class BitbucketAppSupport {
4142
const workspaces =
4243
(await api.workspaces.getWorkspaces({ pagelen: 100 })).data.values?.map((w) => w.slug!) || [];
4344

44-
const reposPromise = Promise.all(
45-
workspaces.map((workspace) =>
46-
api.repositories
45+
const fetchAllRepos = async (workspace: string) => {
46+
const reposResponse: (Schema.Repository[] | undefined)[] = [];
47+
let page = "1";
48+
let hasMorePages = true;
49+
const pagelen = 100;
50+
51+
while (hasMorePages) {
52+
const response = await api.repositories
4753
.list({
4854
workspace,
49-
pagelen: 100,
55+
pagelen,
56+
page,
5057
role: "admin", // installation of webhooks is allowed for admins only
5158
})
5259
.catch((e) => {
5360
console.error(e);
54-
}),
55-
),
56-
);
57-
58-
const reposInWorkspace = await reposPromise;
59-
for (const repos of reposInWorkspace) {
60-
if (repos) {
61-
for (const repo of repos.data.values || []) {
62-
let cloneUrl = repo.links!.clone!.find((x: any) => x.name === "https")!.href!;
63-
if (cloneUrl) {
64-
const url = new URL(cloneUrl);
65-
url.username = "";
66-
cloneUrl = url.toString();
67-
}
68-
const fullName = repo.full_name!;
69-
const updatedAt = repo.updated_on!;
70-
const accountAvatarUrl = repo.links!.avatar?.href!;
71-
const account = fullName.split("/")[0];
72-
73-
(account === usersBitbucketAccount ? ownersRepos : result).push({
74-
name: repo.name!,
75-
account,
76-
cloneUrl,
77-
updatedAt,
78-
accountAvatarUrl,
7961
});
62+
if (response) {
63+
reposResponse.push(response.data.values);
64+
page++;
65+
hasMorePages = response.data.size! > pagelen;
66+
} else {
67+
hasMorePages = false;
8068
}
8169
}
70+
return reposResponse.flat();
71+
};
72+
73+
const reposPromise = Promise.all(workspaces.map((workspace) => fetchAllRepos(workspace)));
74+
const reposInWorkspace: (Schema.Repository | undefined)[][] = await reposPromise;
75+
76+
for (let repo of reposInWorkspace) {
77+
repo = repo[0];
78+
let cloneUrl = repo.links!.clone.find((x: any) => x.name === "https").href;
79+
if (cloneUrl) {
80+
const url = new URL(cloneUrl);
81+
url.username = "";
82+
cloneUrl = url.toString();
83+
}
84+
const fullName = repo.full_name!;
85+
const updatedAt = repo.updated_on!;
86+
const accountAvatarUrl = repo.links!.avatar?.href!;
87+
const account = fullName.split("/")[0];
88+
89+
(account === usersBitbucketAccount ? ownersRepos : result).push({
90+
name: repo.name!,
91+
account,
92+
cloneUrl,
93+
updatedAt,
94+
accountAvatarUrl,
95+
});
8296
}
8397

8498
// put owner's repos first. the frontend will pick first account to continue with

0 commit comments

Comments
 (0)