Skip to content

Commit 33fa1aa

Browse files
committed
move url parsing into getPaginatedCloud
1 parent d5daaea commit 33fa1aa

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

packages/backend/src/bitbucket.ts

Lines changed: 11 additions & 10 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,17 +167,19 @@ 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
}
173176

174177
/**
175178
* Parse the url into a path and query parameters to be used with the api client (openapi-fetch)
176179
*/
177-
function parseUrl(url: string, baseUrl: string): { path: string; query: Record<string, string>; } {
180+
function parseUrl(url: string): { path: string; query: Record<string, string>; } {
178181
const fullUrl = new URL(url);
179-
const path = fullUrl.pathname;
182+
const path = fullUrl.pathname.replace(/^\/\d+(\.\d+)*/, ''); // remove version number in the beginning of the path
180183
const query = Object.fromEntries(fullUrl.searchParams);
181184
logger.debug(`Parsed url ${url} into path ${path} and query ${JSON.stringify(query)}`);
182185
return { path, query };
@@ -189,8 +192,7 @@ async function cloudGetReposForWorkspace(client: BitbucketClient, workspaces: st
189192
logger.debug(`Fetching all repos for workspace ${workspace}...`);
190193

191194
const { durationMs, data } = await measure(async () => {
192-
const fetchFn = () => getPaginatedCloud<CloudRepository>(`/repositories/${workspace}` as CloudGetRequestPath, async (url) => {
193-
const { path, query } = parseUrl(url, client.baseUrl);
195+
const fetchFn = () => getPaginatedCloud<CloudRepository>(`/repositories/${workspace}` as CloudGetRequestPath, async (path, query) => {
194196
const response = await client.apiClient.GET(path, {
195197
params: {
196198
path: {
@@ -250,8 +252,7 @@ async function cloudGetReposForProjects(client: BitbucketClient, projects: strin
250252

251253
logger.debug(`Fetching all repos for project ${project} for workspace ${workspace}...`);
252254
try {
253-
const repos = await getPaginatedCloud<CloudRepository>(`/repositories/${workspace}` as CloudGetRequestPath, async (url) => {
254-
const { path, query } = parseUrl(url, client.baseUrl);
255+
const repos = await getPaginatedCloud<CloudRepository>(`/repositories/${workspace}` as CloudGetRequestPath, async (path, query) => {
255256
const response = await client.apiClient.GET(path, {
256257
params: {
257258
path: {

0 commit comments

Comments
 (0)