Skip to content

Commit

Permalink
Iterate through pages
Browse files Browse the repository at this point in the history
  • Loading branch information
laushinka committed Mar 18, 2022
1 parent 9298fa0 commit 6a402ed
Showing 1 changed file with 43 additions and 30 deletions.
73 changes: 43 additions & 30 deletions components/server/ee/src/bitbucket/bitbucket-app-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6a402ed

Please sign in to comment.