@@ -10,6 +10,7 @@ import { inject, injectable } from "inversify";
10
10
import { AuthProviderParams } from "../auth/auth-provider" ;
11
11
import { BitbucketServerTokenHelper } from "./bitbucket-server-token-handler" ;
12
12
import { CancellationToken } from "vscode-jsonrpc" ;
13
+ import { URLSearchParams } from "url" ;
13
14
14
15
@injectable ( )
15
16
export class BitbucketServerApi {
@@ -311,24 +312,36 @@ export class BitbucketServerApi {
311
312
if ( isCancelled ( ) ) {
312
313
return [ ] ;
313
314
}
314
- const cap = ( query ?. cap || 0 ) > 0 ? query . cap ! : 10 ;
315
- let requestsLeft = cap ;
316
- const limit = `limit=${ ( query ?. limit || 0 ) > 0 ? query . limit ! : 1000 } &` ;
317
- const permission = query . permission ? `permission=${ query . permission } &` : "" ;
318
- const runQuery = async ( params : string ) => {
315
+
316
+ const fetchRepos = async ( params : { [ key : string ] : string } = { } ) => {
319
317
if ( isCancelled ( ) ) {
320
318
return [ ] ;
321
319
}
320
+ // Ensure we only load as many pages as requested
321
+ let requestsLeft = query ?. cap ?? 10 ;
322
+
322
323
const result : BitbucketServer . Repository [ ] = [ ] ;
323
324
let isLastPage = false ;
324
325
let start = 0 ;
325
326
while ( ! isLastPage && requestsLeft > 0 ) {
326
327
if ( isCancelled ( ) ) {
327
328
return [ ] ;
328
329
}
330
+
331
+ const requestParams = new URLSearchParams ( {
332
+ // Apply default params
333
+ limit : `${ query ?. limit ?? 1000 } ` ,
334
+ permission : query . permission ?? "REPO_READ" ,
335
+ start : `${ start } ` ,
336
+ } ) ;
337
+ // Merge params from argument in
338
+ Object . keys ( params ) . forEach ( ( key ) => {
339
+ requestParams . set ( key , params [ key ] ) ;
340
+ } ) ;
341
+
329
342
const pageResult = await this . runQuery < BitbucketServer . Paginated < BitbucketServer . Repository > > (
330
343
userOrToken ,
331
- `/repos?${ permission } ${ limit } start= ${ start } & ${ params } ` ,
344
+ `/repos?${ requestParams . toString ( ) } ` ,
332
345
) ;
333
346
requestsLeft = requestsLeft - 1 ;
334
347
if ( pageResult . values ) {
@@ -349,24 +362,16 @@ export class BitbucketServerApi {
349
362
350
363
// Query by name & projectname in parrallel
351
364
const [ nameResults , projectResults ] = await Promise . all ( [
352
- runQuery ( `name= ${ query . searchString } ` ) ,
353
- runQuery ( `projectname= ${ query . searchString } ` ) ,
365
+ fetchRepos ( { name : query . searchString } ) ,
366
+ fetchRepos ( { projectname : query . searchString } ) ,
354
367
] ) ;
355
368
for ( const repo of [ ...nameResults , ...projectResults ] ) {
356
369
results . set ( repo . id , repo ) ;
357
370
}
358
371
359
372
return Array . from ( results . values ( ) ) ;
360
- } else if ( query . searchString ?. trim ( ) === "" && ( query . limit || query . cap ) ) {
361
- // Empty search w/ limit/cap set - just grab latest repos
362
- const { values = [ ] } = await this . runQuery < BitbucketServer . Paginated < BitbucketServer . Repository > > (
363
- userOrToken ,
364
- `/profile/recent/repos?${ permission } ${ limit } ` ,
365
- ) ;
366
-
367
- return values ;
368
373
} else {
369
- return await runQuery ( `limit=1000` ) ;
374
+ return await fetchRepos ( ) ;
370
375
}
371
376
}
372
377
0 commit comments