Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
improve bitbucket repos fetch
x032205de41a46174c9
diff --git a/backend/src/services/app-connection/bitbucket/bitbucket-connection-fns.ts b/backend/src/services/app-connection/bitbucket/bitbucket-connection-fns.tsindex 4abf879f8f..c81d549f8c 100644--- a/backend/src/services/app-connection/bitbucket/bitbucket-connection-fns.ts+++ b/backend/src/services/app-connection/bitbucket/bitbucket-connection-fns.ts@@ -85,11 +85,32 @@ export const listBitbucketWorkspaces = async (appConnection: TBitbucketConnectioreturn allWorkspaces;};-interface BitbucketRepositoriesResponse {- values: TBitbucketRepo[];+interface BitbucketPaginatedResponse<T> {+ values: T[];next?: string;+ size?: number;}+const BITBUCKET_MAX_PAGES = 10;+const BITBUCKET_PAGE_SIZE = 100;++const paginateBitbucketRequest = async <T>(url: string, headers: Record<string, string>): Promise<T[]> => {+ let allItems: T[] = [];+ let nextUrl: string | undefined = url;+ let iterationCount = 0;++ while (nextUrl && iterationCount < BITBUCKET_MAX_PAGES) {+ // eslint-disable-next-line no-await-in-loop+ const { data }: { data: BitbucketPaginatedResponse<T> } = await request.get(nextUrl, { headers });++ allItems = allItems.concat(data.values);+ nextUrl = data.next;+ iterationCount += 1;+ }++ return allItems;+};+export const listBitbucketRepositories = async (appConnection: TBitbucketConnection, workspaceSlug: string) => {const { email, apiToken } = appConnection.credentials;@@ -98,27 +119,24 @@ export const listBitbucketRepositories = async (appConnection: TBitbucketConnectAccept: "application/json"};- let allRepos: TBitbucketRepo[] = [];- let nextUrl: string | undefined =- `${IntegrationUrls.BITBUCKET_API_URL}/2.0/repositories/${encodeURIComponent(workspaceSlug)}?pagelen=100`;- let iterationCount = 0;+ const encodedSlug = encodeURIComponent(workspaceSlug);- // Limit to 10 iterations, fetching at most 10 * 100 = 1000 repositories- while (nextUrl && iterationCount < 10) {- // eslint-disable-next-line no-await-in-loop- const { data }: { data: BitbucketRepositoriesResponse } = await request.get<BitbucketRepositoriesResponse>(- nextUrl,- {- headers- }- );+ // Fetch repos per-project to avoid Bitbucket's 1,000-result pagination cap+ const projects = await paginateBitbucketRequest<{ key: string }>(+ `${IntegrationUrls.BITBUCKET_API_URL}/2.0/workspaces/${encodedSlug}/projects?pagelen=${BITBUCKET_PAGE_SIZE}`,+ headers+ );- allRepos = allRepos.concat(data.values);- nextUrl = data.next;- iterationCount += 1;- }+ const reposByProject = await Promise.all(+ projects.map((project) =>+ paginateBitbucketRequest<TBitbucketRepo>(+ `${IntegrationUrls.BITBUCKET_API_URL}/2.0/repositories/${encodedSlug}?pagelen=${BITBUCKET_PAGE_SIZE}&sort=slug&q=project.key="${project.key}"`,+ headers+ )+ )+ );- return allRepos;+ return reposByProject.flat();};export const listBitbucketEnvironments = async (diff --git a/docs/integrations/app-connections/bitbucket.mdx b/docs/integrations/app-connections/bitbucket.mdxindex c638738421..e6fd93593b 100644--- a/docs/integrations/app-connections/bitbucket.mdx+++ b/docs/integrations/app-connections/bitbucket.mdx@@ -39,6 +39,7 @@ Infisical supports the use of [API Tokens](https://support.atlassian.com/bitbuck```read:workspace:bitbucketread:user:bitbucket+ read:project:bitbucketread:webhook:bitbucketwrite:webhook:bitbucketdelete:webhook:bitbucket@@ -52,6 +53,7 @@ Infisical supports the use of [API Tokens](https://support.atlassian.com/bitbuckread:workspace:bitbucketadmin:workspace:bitbucketread:user:bitbucket+ read:project:bitbucketread:repository:bitbucketread:pipeline:bitbucketwrite:pipeline:bitbucket