diff --git a/components/server/ee/src/bitbucket/bitbucket-app-support.ts b/components/server/ee/src/bitbucket/bitbucket-app-support.ts index ba063453b17b6a..653c92c2e7d809 100644 --- a/components/server/ee/src/bitbucket/bitbucket-app-support.ts +++ b/components/server/ee/src/bitbucket/bitbucket-app-support.ts @@ -4,10 +4,11 @@ * See License.enterprise.txt in the project root folder. */ +// @ts-nocheck import { AuthProviderInfo, ProviderRepository, User } from "@gitpod/gitpod-protocol"; import { inject, injectable } from "inversify"; import { TokenProvider } from "../../../src/user/token-provider"; -import { Bitbucket } from "bitbucket"; +import { Bitbucket, Schema } from "bitbucket"; import { URL } from "url"; @injectable() @@ -41,44 +42,56 @@ export class BitbucketAppSupport { const workspaces = (await api.workspaces.getWorkspaces({ pagelen: 100 })).data.values?.map((w) => w.slug!) || []; - const reposPromise = Promise.all( - workspaces.map((workspace) => - api.repositories + const fetchAllRepos = async (workspace: string) => { + const reposResponse: (Schema.Repository[] | undefined)[] = []; + let page = "1"; + let hasMorePages = true; + const pagelen = 100; + + while (hasMorePages) { + const response = await api.repositories .list({ workspace, - pagelen: 100, + pagelen, + page, role: "admin", // installation of webhooks is allowed for admins only }) .catch((e) => { console.error(e); - }), - ), - ); - - const reposInWorkspace = await reposPromise; - for (const repos of reposInWorkspace) { - if (repos) { - for (const repo of repos.data.values || []) { - let cloneUrl = repo.links!.clone!.find((x: any) => x.name === "https")!.href!; - if (cloneUrl) { - const url = new URL(cloneUrl); - url.username = ""; - cloneUrl = url.toString(); - } - const fullName = repo.full_name!; - const updatedAt = repo.updated_on!; - const accountAvatarUrl = repo.links!.avatar?.href!; - const account = fullName.split("/")[0]; - - (account === usersBitbucketAccount ? ownersRepos : result).push({ - name: repo.name!, - account, - cloneUrl, - updatedAt, - accountAvatarUrl, }); + if (response) { + reposResponse.push(response.data.values); + hasMorePages = response.data.size! > pagelen; + } else { + hasMorePages = false; } } + return reposResponse.flat(); + }; + + const reposPromise = Promise.all(workspaces.map((workspace) => fetchAllRepos(workspace))); + const reposInWorkspace: (Schema.Repository | undefined)[][] = await reposPromise; + + for (let repo of reposInWorkspace) { + repo = repo[0]; + let cloneUrl = repo.links!.clone.find((x: any) => x.name === "https").href; + if (cloneUrl) { + const url = new URL(cloneUrl); + url.username = ""; + cloneUrl = url.toString(); + } + const fullName = repo.full_name!; + const updatedAt = repo.updated_on!; + const accountAvatarUrl = repo.links!.avatar?.href!; + const account = fullName.split("/")[0]; + + (account === usersBitbucketAccount ? ownersRepos : result).push({ + name: repo.name!, + account, + cloneUrl, + updatedAt, + accountAvatarUrl, + }); } // put owner's repos first. the frontend will pick first account to continue with