From ebf56df7424d6601fca3ed2fc12592bf6f8f22af Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Fri, 10 May 2019 11:35:11 -0700 Subject: [PATCH] fix(codecommit): add a Repository.fromRepositoryName() method. (#2515) Fixes #2514 --- .../@aws-cdk/aws-codecommit/lib/repository.ts | 39 +++++++++++++--- .../aws-codecommit/test/test.codecommit.ts | 45 +++++++++++++++++-- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/packages/@aws-cdk/aws-codecommit/lib/repository.ts b/packages/@aws-cdk/aws-codecommit/lib/repository.ts index 97542f36a0c93..804b2a6a4eae5 100644 --- a/packages/@aws-cdk/aws-codecommit/lib/repository.ts +++ b/packages/@aws-cdk/aws-codecommit/lib/repository.ts @@ -1,5 +1,5 @@ import events = require('@aws-cdk/aws-events'); -import { CfnOutput, Construct, IResource, Resource } from '@aws-cdk/cdk'; +import { CfnOutput, Construct, IConstruct, IResource, Resource, Stack } from '@aws-cdk/cdk'; import { CfnRepository } from './codecommit.generated'; export interface IRepository extends IResource { @@ -244,14 +244,12 @@ export class Repository extends RepositoryBase { public static fromRepositoryArn(scope: Construct, id: string, repositoryArn: string): IRepository { const stack = scope.node.stack; const repositoryName = stack.parseArn(repositoryArn).resource; - const makeCloneUrl = (protocol: 'https' | 'ssh') => - `${protocol}://git-codecommit.${stack.region}.${stack.urlSuffix}/v1/repos/${repositoryName}`; class Import extends RepositoryBase { public readonly repositoryArn = repositoryArn; public readonly repositoryName = repositoryName; - public readonly repositoryCloneUrlHttp = makeCloneUrl('https'); - public readonly repositoryCloneUrlSsh = makeCloneUrl('ssh'); + public readonly repositoryCloneUrlHttp = Repository.makeCloneUrl(stack, repositoryName, 'https'); + public readonly repositoryCloneUrlSsh = Repository.makeCloneUrl(stack, repositoryName, 'ssh'); public export() { return { repositoryArn: this.repositoryArn, @@ -263,6 +261,37 @@ export class Repository extends RepositoryBase { return new Import(scope, id); } + public static fromRepositoryName(scope: Construct, id: string, repositoryName: string): IRepository { + const stack = scope.node.stack; + + class Import extends RepositoryBase { + public repositoryName = repositoryName; + public repositoryArn = Repository.arnForLocalRepository(repositoryName, scope); + public readonly repositoryCloneUrlHttp = Repository.makeCloneUrl(stack, repositoryName, 'https'); + public readonly repositoryCloneUrlSsh = Repository.makeCloneUrl(stack, repositoryName, 'ssh'); + + public export() { + return { + repositoryArn: this.repositoryArn, + repositoryName: this.repositoryName, + }; + } + } + + 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 arnForLocalRepository(repositoryName: string, scope: IConstruct): string { + return scope.node.stack.formatArn({ + service: 'codecommit', + resource: repositoryName, + }); + } + private readonly repository: CfnRepository; private readonly triggers = new Array(); diff --git a/packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts b/packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts index ef60996354dd3..f691403635f84 100644 --- a/packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts +++ b/packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts @@ -3,7 +3,7 @@ import { Test } from 'nodeunit'; import { Repository, RepositoryProps } from '../lib'; export = { - 'default properties': { + 'CodeCommit Repositories': { 'add an SNS trigger to repository'(test: Test) { const app = new TestApp(); @@ -49,8 +49,47 @@ export = { test.throws(() => myRepository.notify('myTrigger')); test.done(); - } - } + }, + + 'can be imported using a Repository ARN'(test: Test) { + // GIVEN + const stack = new Stack(); + const repositoryArn = 'arn:aws:codecommit:us-east-1:585695036304:my-repo'; + + // WHEN + const repo = Repository.fromRepositoryArn(stack, 'ImportedRepo', repositoryArn); + + // THEN + test.deepEqual(repo.node.resolve(repo.repositoryArn), repositoryArn); + test.deepEqual(repo.node.resolve(repo.repositoryName), 'my-repo'); + + test.done(); + }, + + 'can be imported using just a Repository name (the ARN is deduced)'(test: Test) { + // GIVEN + const stack = new Stack(); + + // WHEN + const repo = Repository.fromRepositoryName(stack, 'ImportedRepo', 'my-repo'); + + // THEN + test.deepEqual(repo.node.resolve(repo.repositoryArn), { + 'Fn::Join': [ '', [ + 'arn:', + { Ref: 'AWS::Partition' }, + ':codecommit:', + { Ref: 'AWS::Region' }, + ':', + { Ref: 'AWS::AccountId' }, + ':my-repo' + ]], + }); + test.deepEqual(repo.node.resolve(repo.repositoryName), 'my-repo'); + + test.done(); + }, + }, }; class TestApp {