Skip to content

Commit 7cbda32

Browse files
authored
fix(bitbucket): Bitbucket Cloud pagination not working beyond first page (#502)
1 parent aab4a92 commit 7cbda32

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
- Fixed Bitbucket Cloud pagination not working beyond first page. [#295](https://github.com/sourcebot-dev/sourcebot/issues/295)
12+
1013
## [4.6.7] - 2025-09-08
1114

1215
### Added

packages/backend/src/bitbucket.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,14 @@ function cloudClient(user: string | undefined, token: string | undefined): Bitbu
148148
**/
149149
const getPaginatedCloud = async <T>(
150150
path: CloudGetRequestPath,
151-
get: (url: CloudGetRequestPath) => Promise<CloudPaginatedResponse<T>>
151+
get: (path: CloudGetRequestPath, query?: Record<string, string>) => Promise<CloudPaginatedResponse<T>>
152152
): Promise<T[]> => {
153153
const results: T[] = [];
154-
let url = path;
154+
let nextPath = path;
155+
let nextQuery = undefined;
155156

156157
while (true) {
157-
const response = await get(url);
158+
const response = await get(nextPath, nextQuery);
158159

159160
if (!response.values || response.values.length === 0) {
160161
break;
@@ -166,25 +167,38 @@ const getPaginatedCloud = async <T>(
166167
break;
167168
}
168169

169-
url = response.next as CloudGetRequestPath;
170+
const parsedUrl = parseUrl(response.next);
171+
nextPath = parsedUrl.path as CloudGetRequestPath;
172+
nextQuery = parsedUrl.query;
170173
}
171174
return results;
172175
}
173-
176+
177+
/**
178+
* Parse the url into a path and query parameters to be used with the api client (openapi-fetch)
179+
*/
180+
function parseUrl(url: string): { path: string; query: Record<string, string>; } {
181+
const fullUrl = new URL(url);
182+
const path = fullUrl.pathname.replace(/^\/\d+(\.\d+)*/, ''); // remove version number in the beginning of the path
183+
const query = Object.fromEntries(fullUrl.searchParams);
184+
logger.debug(`Parsed url ${url} into path ${path} and query ${JSON.stringify(query)}`);
185+
return { path, query };
186+
}
187+
174188

175189
async function cloudGetReposForWorkspace(client: BitbucketClient, workspaces: string[]): Promise<{validRepos: CloudRepository[], notFoundWorkspaces: string[]}> {
176190
const results = await Promise.allSettled(workspaces.map(async (workspace) => {
177191
try {
178192
logger.debug(`Fetching all repos for workspace ${workspace}...`);
179193

180-
const path = `/repositories/${workspace}` as CloudGetRequestPath;
181194
const { durationMs, data } = await measure(async () => {
182-
const fetchFn = () => getPaginatedCloud<CloudRepository>(path, async (url) => {
183-
const response = await client.apiClient.GET(url, {
195+
const fetchFn = () => getPaginatedCloud<CloudRepository>(`/repositories/${workspace}` as CloudGetRequestPath, async (path, query) => {
196+
const response = await client.apiClient.GET(path, {
184197
params: {
185198
path: {
186199
workspace,
187-
}
200+
},
201+
query: query,
188202
}
189203
});
190204
const { data, error } = response;
@@ -238,11 +252,14 @@ async function cloudGetReposForProjects(client: BitbucketClient, projects: strin
238252

239253
logger.debug(`Fetching all repos for project ${project} for workspace ${workspace}...`);
240254
try {
241-
const path = `/repositories/${workspace}` as CloudGetRequestPath;
242-
const repos = await getPaginatedCloud<CloudRepository>(path, async (url) => {
243-
const response = await client.apiClient.GET(url, {
255+
const repos = await getPaginatedCloud<CloudRepository>(`/repositories/${workspace}` as CloudGetRequestPath, async (path, query) => {
256+
const response = await client.apiClient.GET(path, {
244257
params: {
258+
path: {
259+
workspace,
260+
},
245261
query: {
262+
...query,
246263
q: `project.key="${project_name}"`
247264
}
248265
}

0 commit comments

Comments
 (0)