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): add a Repository.fromRepositoryName() method #2515

Merged
merged 1 commit into from
May 10, 2019
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
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);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change these to free floating functions.

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