@@ -21,6 +21,12 @@ import { CodeCatalyst } from 'aws-sdk'
21
21
import { ToolkitError } from '../errors'
22
22
import { SsoConnection } from '../../credentials/auth'
23
23
import { TokenProvider } from '../../credentials/sdkV2Compat'
24
+ import { Uri } from 'vscode'
25
+ import {
26
+ GetSourceRepositoryCloneUrlsRequest ,
27
+ ListSourceRepositoriesItem ,
28
+ ListSourceRepositoriesItems ,
29
+ } from 'aws-sdk/clients/codecatalyst'
24
30
25
31
// REMOVE ME SOON: only used for development
26
32
interface CodeCatalystConfig {
@@ -411,12 +417,33 @@ class CodeCatalystClientInternal {
411
417
412
418
/**
413
419
* Gets a flat list of all repos for the given CodeCatalyst user.
420
+ * @param thirdParty If you want to include 3P (eg github) results in
421
+ * your output.
414
422
*/
415
423
public listSourceRepositories (
416
- request : CodeCatalyst . ListSourceRepositoriesRequest
424
+ request : CodeCatalyst . ListSourceRepositoriesRequest ,
425
+ thirdParty : boolean = true
417
426
) : AsyncCollection < CodeCatalystRepo [ ] > {
418
- const requester = async ( request : CodeCatalyst . ListSourceRepositoriesRequest ) =>
419
- this . call ( this . sdkClient . listSourceRepositories ( request ) , true , { items : [ ] } )
427
+ const requester = async ( request : CodeCatalyst . ListSourceRepositoriesRequest ) => {
428
+ const allRepositories = this . call ( this . sdkClient . listSourceRepositories ( request ) , true , { items : [ ] } )
429
+ let finalRepositories = allRepositories
430
+
431
+ // Filter out 3P repos
432
+ if ( ! thirdParty ) {
433
+ finalRepositories = allRepositories . then ( async repos => {
434
+ repos . items = await excludeThirdPartyRepos (
435
+ this ,
436
+ request . spaceName ,
437
+ request . projectName ,
438
+ repos . items
439
+ )
440
+ return repos
441
+ } )
442
+ }
443
+
444
+ return finalRepositories
445
+ }
446
+
420
447
const collection = pageableToCollection ( requester , request , 'nextToken' , 'items' )
421
448
return collection . map (
422
449
summaries =>
@@ -445,12 +472,17 @@ class CodeCatalystClientInternal {
445
472
446
473
/**
447
474
* Lists ALL of the given resource in the current account
475
+ *
476
+ * @param thirdParty If you want 3P repos in the result.
448
477
*/
449
478
public listResources ( resourceType : 'org' ) : AsyncCollection < CodeCatalystOrg [ ] >
450
479
public listResources ( resourceType : 'project' ) : AsyncCollection < CodeCatalystProject [ ] >
451
- public listResources ( resourceType : 'repo' ) : AsyncCollection < CodeCatalystRepo [ ] >
480
+ public listResources ( resourceType : 'repo' , thirdParty ?: boolean ) : AsyncCollection < CodeCatalystRepo [ ] >
452
481
public listResources ( resourceType : 'devEnvironment' ) : AsyncCollection < DevEnvironment [ ] >
453
- public listResources ( resourceType : CodeCatalystResource [ 'type' ] ) : AsyncCollection < CodeCatalystResource [ ] > {
482
+ public listResources (
483
+ resourceType : CodeCatalystResource [ 'type' ] ,
484
+ ...args : any [ ]
485
+ ) : AsyncCollection < CodeCatalystResource [ ] > {
454
486
function mapInner < T , U > (
455
487
collection : AsyncCollection < T [ ] > ,
456
488
fn : ( element : T ) => AsyncCollection < U [ ] >
@@ -465,7 +497,7 @@ class CodeCatalystClientInternal {
465
497
return mapInner ( this . listResources ( 'org' ) , o => this . listProjects ( { spaceName : o . name } ) )
466
498
case 'repo' :
467
499
return mapInner ( this . listResources ( 'project' ) , p =>
468
- this . listSourceRepositories ( { projectName : p . name , spaceName : p . org . name } )
500
+ this . listSourceRepositories ( { projectName : p . name , spaceName : p . org . name } , ... args )
469
501
)
470
502
case 'branch' :
471
503
throw new Error ( 'Listing branches is not currently supported' )
@@ -481,13 +513,14 @@ class CodeCatalystClientInternal {
481
513
}
482
514
483
515
/**
484
- * Gets the git source host URL for the given CodeCatalyst repo.
516
+ * Gets the git source host URL for the given CodeCatalyst or third-party repo.
485
517
*/
486
518
public async getRepoCloneUrl ( args : CodeCatalyst . GetSourceRepositoryCloneUrlsRequest ) : Promise < string > {
487
519
const r = await this . call ( this . sdkClient . getSourceRepositoryCloneUrls ( args ) , false )
488
520
489
521
// The git extension skips over credential providers if the username is included in the authority
490
- return `https://${ r . https . replace ( / .* @ / , '' ) } `
522
+ const uri = Uri . parse ( r . https )
523
+ return uri . with ( { authority : uri . authority . replace ( / .* @ / , '' ) } ) . toString ( )
491
524
}
492
525
493
526
public async createDevEnvironment ( args : CodeCatalyst . CreateDevEnvironmentRequest ) : Promise < DevEnvironment > {
@@ -676,3 +709,48 @@ class CodeCatalystClientInternal {
676
709
return devenv
677
710
}
678
711
}
712
+
713
+ /**
714
+ * Returns only the first-party repos from the given
715
+ * list of repository items.
716
+ */
717
+ export async function excludeThirdPartyRepos (
718
+ client : CodeCatalystClient ,
719
+ spaceName : CodeCatalystOrg [ 'name' ] ,
720
+ projectName : CodeCatalystProject [ 'name' ] ,
721
+ items ?: Pick < ListSourceRepositoriesItem , 'name' > [ ]
722
+ ) : Promise < ListSourceRepositoriesItems | undefined > {
723
+ if ( items === undefined ) {
724
+ return items
725
+ }
726
+
727
+ // Filter out 3P repos.
728
+ return (
729
+ await Promise . all (
730
+ items . map ( async item => {
731
+ return ( await isThirdPartyRepo ( client , {
732
+ spaceName,
733
+ projectName,
734
+ sourceRepositoryName : item . name ,
735
+ } ) )
736
+ ? undefined
737
+ : item
738
+ } )
739
+ )
740
+ ) . filter ( item => item !== undefined ) as CodeCatalyst . ListSourceRepositoriesItem [ ]
741
+ }
742
+
743
+ /**
744
+ * Determines if a repo is third party (3P) compared to first party (1P).
745
+ *
746
+ * 1P is CodeCatalyst, 3P is something like Github.
747
+ */
748
+ async function isThirdPartyRepo (
749
+ client : CodeCatalystClient ,
750
+ codeCatalystRepo : GetSourceRepositoryCloneUrlsRequest
751
+ ) : Promise < boolean > {
752
+ const url = await client . getRepoCloneUrl ( codeCatalystRepo )
753
+ // TODO: Make more robust to work with gamma once getCodeCatalystConfig()
754
+ // can provide a valid hostname
755
+ return ! Uri . parse ( url ) . authority . endsWith ( 'codecatalyst.aws' )
756
+ }
0 commit comments