@@ -148,13 +148,14 @@ function cloudClient(user: string | undefined, token: string | undefined): Bitbu
148
148
**/
149
149
const getPaginatedCloud = async < T > (
150
150
path : CloudGetRequestPath ,
151
- get : ( url : CloudGetRequestPath ) => Promise < CloudPaginatedResponse < T > >
151
+ get : ( path : CloudGetRequestPath , query ?: Record < string , string > ) => Promise < CloudPaginatedResponse < T > >
152
152
) : Promise < T [ ] > => {
153
153
const results : T [ ] = [ ] ;
154
- let url = path ;
154
+ let nextPath = path ;
155
+ let nextQuery = undefined ;
155
156
156
157
while ( true ) {
157
- const response = await get ( url ) ;
158
+ const response = await get ( nextPath , nextQuery ) ;
158
159
159
160
if ( ! response . values || response . values . length === 0 ) {
160
161
break ;
@@ -166,25 +167,38 @@ const getPaginatedCloud = async <T>(
166
167
break ;
167
168
}
168
169
169
- url = response . next as CloudGetRequestPath ;
170
+ const parsedUrl = parseUrl ( response . next ) ;
171
+ nextPath = parsedUrl . path as CloudGetRequestPath ;
172
+ nextQuery = parsedUrl . query ;
170
173
}
171
174
return results ;
172
175
}
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
+
174
188
175
189
async function cloudGetReposForWorkspace ( client : BitbucketClient , workspaces : string [ ] ) : Promise < { validRepos : CloudRepository [ ] , notFoundWorkspaces : string [ ] } > {
176
190
const results = await Promise . allSettled ( workspaces . map ( async ( workspace ) => {
177
191
try {
178
192
logger . debug ( `Fetching all repos for workspace ${ workspace } ...` ) ;
179
193
180
- const path = `/repositories/${ workspace } ` as CloudGetRequestPath ;
181
194
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 , {
184
197
params : {
185
198
path : {
186
199
workspace,
187
- }
200
+ } ,
201
+ query : query ,
188
202
}
189
203
} ) ;
190
204
const { data, error } = response ;
@@ -238,11 +252,14 @@ async function cloudGetReposForProjects(client: BitbucketClient, projects: strin
238
252
239
253
logger . debug ( `Fetching all repos for project ${ project } for workspace ${ workspace } ...` ) ;
240
254
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 , {
244
257
params : {
258
+ path : {
259
+ workspace,
260
+ } ,
245
261
query : {
262
+ ...query ,
246
263
q : `project.key="${ project_name } "`
247
264
}
248
265
}
0 commit comments