diff --git a/packages/@aws-cdk/aws-codecommit/lib/repository.ts b/packages/@aws-cdk/aws-codecommit/lib/repository.ts index f86e2f4a82a11..7345d669ed763 100644 --- a/packages/@aws-cdk/aws-codecommit/lib/repository.ts +++ b/packages/@aws-cdk/aws-codecommit/lib/repository.ts @@ -284,13 +284,15 @@ export class Repository extends RepositoryBase { */ public static fromRepositoryArn(scope: Construct, id: string, repositoryArn: string): IRepository { const stack = Stack.of(scope); - const repositoryName = stack.parseArn(repositoryArn).resource; + const arn = stack.parseArn(repositoryArn); + const repositoryName = arn.resource; + const region = arn.region; class Import extends RepositoryBase { public readonly repositoryArn = repositoryArn; public readonly repositoryName = repositoryName; - public readonly repositoryCloneUrlHttp = Repository.makeCloneUrl(stack, repositoryName, 'https'); - public readonly repositoryCloneUrlSsh = Repository.makeCloneUrl(stack, repositoryName, 'ssh'); + public readonly repositoryCloneUrlHttp = Repository.makeCloneUrl(stack, repositoryName, 'https', region); + public readonly repositoryCloneUrlSsh = Repository.makeCloneUrl(stack, repositoryName, 'ssh', region); } return new Import(scope, id); @@ -309,8 +311,8 @@ export class Repository extends RepositoryBase { return new Import(scope, id); } - private static makeCloneUrl(stack: Stack, repositoryName: string, protocol: 'https' | 'ssh') { - return `${protocol}://git-codecommit.${stack.region}.${stack.urlSuffix}/v1/repos/${repositoryName}`; + private static makeCloneUrl(stack: Stack, repositoryName: string, protocol: 'https' | 'ssh', region?: string) { + return `${protocol}://git-codecommit.${region || stack.region}.${stack.urlSuffix}/v1/repos/${repositoryName}`; } private static arnForLocalRepository(repositoryName: string, scope: IConstruct): string { diff --git a/packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts b/packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts index f920844f072a8..68239f88834b3 100644 --- a/packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts +++ b/packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts @@ -67,6 +67,44 @@ export = { test.done(); }, + /** + * Fix for https://github.com/aws/aws-cdk/issues/10630 + */ + 'can be imported using a Repository ARN and respect the region in clone urls'(test: Test) { + // GIVEN + const stack = new Stack(); + const repositoryArn = 'arn:aws:codecommit:us-west-2:585695036304:my-repo'; + + // WHEN + const repo = Repository.fromRepositoryArn(stack, 'ImportedRepo', repositoryArn); + + // THEN + // a fully qualified arn should use the region from the arn + test.deepEqual(stack.resolve(repo.repositoryCloneUrlHttp), { + 'Fn::Join': [ + '', + [ + 'https://git-codecommit.us-west-2.', + { Ref: 'AWS::URLSuffix' }, + '/v1/repos/my-repo', + ], + ], + }); + + test.deepEqual(stack.resolve(repo.repositoryCloneUrlSsh), { + 'Fn::Join': [ + '', + [ + 'ssh://git-codecommit.us-west-2.', + { Ref: 'AWS::URLSuffix' }, + '/v1/repos/my-repo', + ], + ], + }); + + test.done(); + }, + 'can be imported using just a Repository name (the ARN is deduced)'(test: Test) { // GIVEN const stack = new Stack(); @@ -88,6 +126,20 @@ export = { }); test.deepEqual(stack.resolve(repo.repositoryName), 'my-repo'); + //local name resolution should use stack region + test.deepEqual(stack.resolve(repo.repositoryCloneUrlHttp), { + 'Fn::Join': [ + '', + [ + 'https://git-codecommit.', + { Ref: 'AWS::Region' }, + '.', + { Ref: 'AWS::URLSuffix' }, + '/v1/repos/my-repo', + ], + ], + }); + test.done(); },