Skip to content

Commit 0468ece

Browse files
feat(github-codestarconnection): add github code star connection construct (#12)
We create a code star connection for github with default values
1 parent 6c38dd6 commit 0468ece

11 files changed

+792
-17
lines changed

.cz-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626

2727
scopes: [
2828
{ name: 'codestarconnection' },
29+
{ name: 'github-codestarconnection' },
2930
{ name: 'projen' },
3031
{ name: 'docs' },
3132
],

.projenrc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
1313
constructsVersion: '10.3.0',
1414
repositoryUrl: 'https://github.com/JumpToTheCloud/aws-codestarconnection',
1515
prettier: true,
16+
keywords: ['aws', 'cdk', 'codestarconnection'],
1617
prettierOptions: {
1718
settings: {
1819
trailingComma: TrailingComma.ES5,

API.md

Lines changed: 490 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ npm install @jttc/aws-codestarconnection
1818

1919
The Construct Library has the following L2 Construct:
2020

21-
| Construct | Description |
22-
|-------------------------|----------------------------------------------------------------|
23-
| [CodeStarConnection](#) | We create a Code Star Connection for every Provider available |
21+
| Construct | Description |
22+
|-------------------------------|----------------------------------------------------------------|
23+
| [CodeStarConnection](#) | We create a Code Star Connection for every Provider available |
24+
| [GithubCodeStarConnection](#) | We create a Code Star Connection for Github |

package.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/code-star-connection.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,21 @@ export abstract class CodeStarConnectionBase
299299
*/
300300
public abstract readonly connectionArn: string;
301301

302+
/**
303+
* Validate if the name of the code connection is
304+
* longer thatn 32 characters
305+
* @param {string} name Name of the connection
306+
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codestarconnections-connection.html#cfn-codestarconnections-connection-connectionname
307+
*/
308+
public validateConnectionName(name: string): void {
309+
// Rules codified from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codestarconnections-connection.html#cfn-codestarconnections-connection-connectionname
310+
if (name.length < 2 || name.length > 32) {
311+
Annotations.of(this).addError(
312+
'Connection Name must be at least 1 and no more than 32 characters'
313+
);
314+
}
315+
}
316+
302317
/**
303318
* Grant the given principal identity permissions to perform the actions on this code star connection
304319
*/
@@ -470,13 +485,4 @@ export class CodeStarConnection extends CodeStarConnectionBase {
470485
this.connectionName = props.connectionName;
471486
this.connectionArn = resource.attrConnectionArn;
472487
}
473-
474-
private validateConnectionName(qualifier: string): void {
475-
// Rules codified from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codestarconnections-connection.html#cfn-codestarconnections-connection-connectionname
476-
if (qualifier.length < 2 || qualifier.length > 32) {
477-
Annotations.of(this).addError(
478-
'Connection Name must be at least 1 and no more than 32 characters'
479-
);
480-
}
481-
}
482488
}

src/github-code-star-connection.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { RemovalPolicy, Tag } from 'aws-cdk-lib';
2+
import { CfnConnection } from 'aws-cdk-lib/aws-codestarconnections';
3+
import { Construct } from 'constructs';
4+
5+
import {
6+
CodeStarConnectionBase,
7+
CodeStarConnectionProviderType,
8+
ICodeStarConnection,
9+
} from './code-star-connection';
10+
11+
export interface GithubCodeStarConnetionProps {
12+
/**
13+
* The name of the connection.
14+
* Connection names must be in an AWS user account.
15+
*/
16+
readonly connectionName?: string;
17+
18+
/**
19+
* The Amazon Resource Name (ARN) of the host associated with the connection.
20+
*/
21+
readonly hostArn?: string;
22+
23+
/**
24+
* The list of tags associated with the connection.
25+
*/
26+
readonly tags?: Tag[];
27+
28+
/**
29+
* Determine what happens to the code star connection when the resource/stack is deleted.
30+
*
31+
* @default RemovalPolicy.Retain
32+
*/
33+
readonly removalPolicy?: RemovalPolicy;
34+
}
35+
36+
/**
37+
* Define a Github CodeStar Connection resource
38+
* @resource AWS::CodeStarConnections::Connection
39+
* @example
40+
* new GithubCodeStarConnection(this, 'GithubConnection')
41+
*/
42+
export class GithubCodeStarConnection extends CodeStarConnectionBase {
43+
/**
44+
* Import an externally defined Code Star Connection using its ARN.
45+
*
46+
* @param scope the construct that will "own" the imported key.
47+
* @param id the id of the imported code star conection in the construct tree.
48+
* @param codestarConnectionArn the ARN of an existing Code Star Connection.
49+
*/
50+
public static fromCodeStarConnectionArn(
51+
scope: Construct,
52+
id: string,
53+
codestarConnectionArn: string
54+
): ICodeStarConnection {
55+
class Import extends CodeStarConnectionBase {
56+
public connectionName = '';
57+
public connectionArn = codestarConnectionArn;
58+
}
59+
60+
return new Import(scope, id, {
61+
environmentFromArn: codestarConnectionArn,
62+
});
63+
}
64+
65+
/**
66+
* The name of the CodeStar connection
67+
*/
68+
public readonly connectionName: string;
69+
70+
/**
71+
* The ARN of the Code Star connection
72+
*/
73+
public readonly connectionArn: string;
74+
75+
constructor(
76+
scope: Construct,
77+
id: string,
78+
props?: GithubCodeStarConnetionProps
79+
) {
80+
super(scope, id, {
81+
// physicalName: props.connectionName,
82+
});
83+
84+
this.connectionName = 'github-connection';
85+
86+
if (props && props.connectionName) {
87+
this.validateConnectionName(props.connectionName);
88+
this.connectionName = props.connectionName;
89+
}
90+
91+
const resource = new CfnConnection(this, 'Resource', {
92+
connectionName: this.connectionName,
93+
providerType: CodeStarConnectionProviderType.GITHUB,
94+
hostArn: props?.hostArn,
95+
tags: props?.tags,
96+
});
97+
98+
resource.applyRemovalPolicy(props?.removalPolicy);
99+
100+
this.connectionArn = resource.attrConnectionArn;
101+
}
102+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './code-star-connection';
2+
export * from './github-code-star-connection';

test/__snapshots__/codestar-connection.snapshot.test.ts.snap

Lines changed: 51 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/codestar-connection.snapshot.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { Stack } from 'aws-cdk-lib';
22
import { Template } from 'aws-cdk-lib/assertions';
33

4-
import { CodeStarConnection, CodeStarConnectionProviderType } from '../src';
4+
import {
5+
CodeStarConnection,
6+
CodeStarConnectionProviderType,
7+
} from '../src/code-star-connection';
58

6-
describe('Snapshot test validation', () => {
9+
import { GithubCodeStarConnection } from '../src/github-code-star-connection';
10+
11+
describe('Code Star Connection Snapshot test validation', () => {
712
// Given a new Stack
813
const stack = new Stack();
914

@@ -19,3 +24,17 @@ describe('Snapshot test validation', () => {
1924
expect(template.toJSON()).toMatchSnapshot();
2025
});
2126
});
27+
28+
describe('Github Code Star Snapshot test validation', () => {
29+
// Given a new Stack
30+
const stack = new Stack();
31+
32+
// WHEN
33+
new GithubCodeStarConnection(stack, 'MyGithubConnection');
34+
35+
// THEN
36+
it('Code Star Connection validation test', () => {
37+
const template = Template.fromStack(stack);
38+
expect(template.toJSON()).toMatchSnapshot();
39+
});
40+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { App, Stack } from 'aws-cdk-lib';
2+
import { Template } from 'aws-cdk-lib/assertions';
3+
4+
import { AnyPrincipal, Role } from 'aws-cdk-lib/aws-iam';
5+
import {
6+
CodeStarConnectionPolicyActions,
7+
GithubCodeStarConnection,
8+
} from '../src';
9+
10+
describe('Code Star Connection', () => {
11+
// GIVEN
12+
let stack = new Stack();
13+
14+
// WHEN
15+
new GithubCodeStarConnection(stack, 'GithubCodeStarConnection');
16+
17+
it('should create a Code Star Connection', () => {
18+
// THEN
19+
Template.fromStack(stack).resourceCountIs(
20+
'AWS::CodeStarConnections::Connection',
21+
1
22+
);
23+
});
24+
25+
it('should have a default connection name', () => {
26+
// THEN
27+
Template.fromStack(stack).hasResourceProperties(
28+
'AWS::CodeStarConnections::Connection',
29+
{
30+
ConnectionName: 'github-connection',
31+
}
32+
);
33+
});
34+
35+
it('should have a Github provider', () => {
36+
// THEN
37+
Template.fromStack(stack).hasResourceProperties(
38+
'AWS::CodeStarConnections::Connection',
39+
{
40+
ProviderType: 'GitHub',
41+
}
42+
);
43+
});
44+
45+
it('should have Retention Policy by default', () => {
46+
Template.fromStack(stack).hasResource(
47+
'AWS::CodeStarConnections::Connection',
48+
{
49+
DeletionPolicy: 'Retain',
50+
UpdateReplacePolicy: 'Retain',
51+
}
52+
);
53+
});
54+
55+
it('should thrown an error if the connection name is to long', () => {
56+
// GIVEN
57+
const app = new App();
58+
stack = new Stack(app, 'TestStack');
59+
60+
// WHEN
61+
new GithubCodeStarConnection(stack, 'CodeStarConnection', {
62+
connectionName: 'test-connection-is-longer-than-32-characters',
63+
});
64+
65+
expect(() =>
66+
Template.fromStack(app.synth().getStackArtifact(stack.stackId).template)
67+
).toThrowError();
68+
});
69+
});
70+
71+
describe('Github Code Star Connection Static methods', () => {
72+
it('should return a instance of the github code star connection and use the grantUse method', () => {
73+
// GIVEN
74+
const app = new App();
75+
const stack = new Stack(app, 'TestStack');
76+
77+
// WHEN
78+
const codestarConnection =
79+
GithubCodeStarConnection.fromCodeStarConnectionArn(
80+
stack,
81+
'CodeStarConnectionFromArn',
82+
'arn:aws:codestar-connections:eu-west-1:123456789012:connection/8c86942e-a7ca-4a4a-8b63-e2f7f5efaeee'
83+
);
84+
85+
const role = new Role(stack, 'Role', {
86+
assumedBy: new AnyPrincipal(),
87+
});
88+
89+
codestarConnection.grantUse(role);
90+
91+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
92+
PolicyDocument: {
93+
Statement: [
94+
{
95+
Action: CodeStarConnectionPolicyActions.USE_CONNECTION,
96+
Effect: 'Allow',
97+
Resource: codestarConnection.connectionArn,
98+
},
99+
],
100+
},
101+
});
102+
});
103+
});

0 commit comments

Comments
 (0)