Skip to content

Commit d5daaea

Browse files
committed
fix(bitbucket): parse pagination URLs correctly for openapi-fetch
1 parent 2309b67 commit d5daaea

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

packages/backend/src/bitbucket.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,33 @@ const getPaginatedCloud = async <T>(
170170
}
171171
return results;
172172
}
173-
173+
174+
/**
175+
* Parse the url into a path and query parameters to be used with the api client (openapi-fetch)
176+
*/
177+
function parseUrl(url: string, baseUrl: string): { path: string; query: Record<string, string>; } {
178+
const fullUrl = new URL(url);
179+
const path = fullUrl.pathname;
180+
const query = Object.fromEntries(fullUrl.searchParams);
181+
logger.debug(`Parsed url ${url} into path ${path} and query ${JSON.stringify(query)}`);
182+
return { path, query };
183+
}
184+
174185

175186
async function cloudGetReposForWorkspace(client: BitbucketClient, workspaces: string[]): Promise<{validRepos: CloudRepository[], notFoundWorkspaces: string[]}> {
176187
const results = await Promise.allSettled(workspaces.map(async (workspace) => {
177188
try {
178189
logger.debug(`Fetching all repos for workspace ${workspace}...`);
179190

180-
const path = `/repositories/${workspace}` as CloudGetRequestPath;
181191
const { durationMs, data } = await measure(async () => {
182-
const fetchFn = () => getPaginatedCloud<CloudRepository>(path, async (url) => {
183-
const response = await client.apiClient.GET(url, {
192+
const fetchFn = () => getPaginatedCloud<CloudRepository>(`/repositories/${workspace}` as CloudGetRequestPath, async (url) => {
193+
const { path, query } = parseUrl(url, client.baseUrl);
194+
const response = await client.apiClient.GET(path, {
184195
params: {
185196
path: {
186197
workspace,
187-
}
198+
},
199+
query: query,
188200
}
189201
});
190202
const { data, error } = response;
@@ -238,11 +250,15 @@ async function cloudGetReposForProjects(client: BitbucketClient, projects: strin
238250

239251
logger.debug(`Fetching all repos for project ${project} for workspace ${workspace}...`);
240252
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, {
253+
const repos = await getPaginatedCloud<CloudRepository>(`/repositories/${workspace}` as CloudGetRequestPath, async (url) => {
254+
const { path, query } = parseUrl(url, client.baseUrl);
255+
const response = await client.apiClient.GET(path, {
244256
params: {
257+
path: {
258+
workspace,
259+
},
245260
query: {
261+
...query,
246262
q: `project.key="${project_name}"`
247263
}
248264
}

0 commit comments

Comments
 (0)