From 0dff101712db5c540dabac44def8d617fc045efc Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Thu, 9 May 2019 12:57:17 -0700 Subject: [PATCH] fix(codecommit): add a Repository.fromRepositoryName() method. Fixes #2514 --- .../@aws-cdk/aws-codecommit/lib/repository.ts | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 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();