Skip to content

Commit

Permalink
fix(codecommit): use region given in fromRepositoryArn when creating …
Browse files Browse the repository at this point in the history
…clone urls (#10639)

As a CDK User I expect Repository.fromRepositoryArn to use the region
given when building the repository clone urls.

Prior to this commit, a stack deploy in us-east-1, would return clone
urls for us-east-1 even if given an ARN for us-west-2.

Fixes #10630

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
raykrueger committed Oct 1, 2020
1 parent b207888 commit 934553c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/@aws-cdk/aws-codecommit/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
},

Expand Down

0 comments on commit 934553c

Please sign in to comment.