Skip to content

Commit

Permalink
feat(aws-codebuild): allow setting Webhook for GitHub Sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Dec 18, 2018
1 parent fe6923d commit 1755594
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-codebuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ CodePipeline action.
#### `GitHubSource` and `GitHubEnterpriseSource`

These source types can be used to build code from a GitHub repository.
Example:

```typescript
const gitHubSource = new codebuild.GitHubSource({
owner: 'awslabs',
repo: 'aws-cdk',
oauthToken: new cdk.SecretParameter(this, 'GitHubOAuthToken', {
ssmParameter: 'my-github-token',
}),
webhook: true, // optional, default: false
});
```

#### `BitBucketSource`

Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ export class Project extends ProjectRef {
timeoutInMinutes: props.timeout,
secondarySources: new cdk.Token(() => this.renderSecondarySources()),
secondaryArtifacts: new cdk.Token(() => this.renderSecondaryArtifacts()),
triggers: this.source.buildTriggers(),
});

this.projectArn = resource.projectArn;
Expand Down
23 changes: 22 additions & 1 deletion packages/@aws-cdk/aws-codebuild/lib/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export abstract class BuildSource {
};
}

public buildTriggers(): CfnProject.ProjectTriggersProperty | undefined {
return undefined;
}

protected toSourceProperty(): any {
return {
};
Expand Down Expand Up @@ -171,10 +175,17 @@ export interface GitHubSourceProps extends BuildSourceProps {
*/
oauthToken: cdk.Secret;

/**
* Whether to create a webhook that will trigger a build every time a commit is pushed to the GitHub repository.
*
* @default false
*/
webhook?: boolean;

/**
* Whether to send GitHub notifications on your build's start and end.
*
* @default true
* @default false
*/
reportBuildStatus?: boolean;
}
Expand All @@ -187,14 +198,24 @@ export class GitHubSource extends BuildSource {
private readonly httpsCloneUrl: string;
private readonly oauthToken: cdk.Secret;
private readonly reportBuildStatus: boolean;
private readonly webhook?: boolean;

constructor(props: GitHubSourceProps) {
super(props);
this.httpsCloneUrl = `https://github.com/${props.owner}/${props.repo}.git`;
this.oauthToken = props.oauthToken;
this.webhook = props.webhook;
this.reportBuildStatus = props.reportBuildStatus === undefined ? true : props.reportBuildStatus;
}

public buildTriggers(): CfnProject.ProjectTriggersProperty | undefined {
return this.webhook === undefined
? undefined
: {
webhook: this.webhook,
};
}

protected toSourceProperty(): any {
return {
auth: { type: 'OAUTH', resource: this.oauthToken },
Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ export = {

test.done();
},

'can explicitly set webhook to true'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new codebuild.Project(stack, 'Project', {
source: new codebuild.GitHubSource({
owner: 'testowner',
repo: 'testrepo',
oauthToken: new cdk.Secret('test_oauth_token'),
webhook: true,
})
});

// THEN
expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', {
Triggers: {
Webhook: true,
},
}));

test.done();
},
},

'github enterprise auth test'(test: Test) {
Expand Down

0 comments on commit 1755594

Please sign in to comment.