Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codecommit): use region given in fromRepositoryArn when creating clone urls #10639

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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