Skip to content

Commit

Permalink
fix(codecommit): add a Repository.fromRepositoryName() method. (#2515)
Browse files Browse the repository at this point in the history
Fixes #2514
  • Loading branch information
skinny85 authored May 10, 2019
1 parent 145da28 commit 6fc3718
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
39 changes: 34 additions & 5 deletions packages/@aws-cdk/aws-codecommit/lib/repository.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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<CfnRepository.RepositoryTriggerProperty>();

Expand Down
45 changes: 42 additions & 3 deletions packages/@aws-cdk/aws-codecommit/test/test.codecommit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6fc3718

Please sign in to comment.