From 1755594383f4faf17b4e26ae95f9f80beb90302a Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Mon, 17 Dec 2018 17:04:39 -0800 Subject: [PATCH] feat(aws-codebuild): allow setting Webhook for GitHub Sources. --- packages/@aws-cdk/aws-codebuild/README.md | 12 ++++++++++ .../@aws-cdk/aws-codebuild/lib/project.ts | 1 + packages/@aws-cdk/aws-codebuild/lib/source.ts | 23 +++++++++++++++++- .../aws-codebuild/test/test.project.ts | 24 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-codebuild/README.md b/packages/@aws-cdk/aws-codebuild/README.md index 1edd0f58e6011..1c29ef6edbcf3 100644 --- a/packages/@aws-cdk/aws-codebuild/README.md +++ b/packages/@aws-cdk/aws-codebuild/README.md @@ -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` diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index 12b72d3450aeb..00c12034c7c8b 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -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; diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index 48ae0ba3f39f1..2ff40efd690e2 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -46,6 +46,10 @@ export abstract class BuildSource { }; } + public buildTriggers(): CfnProject.ProjectTriggersProperty | undefined { + return undefined; + } + protected toSourceProperty(): any { return { }; @@ -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; } @@ -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 }, diff --git a/packages/@aws-cdk/aws-codebuild/test/test.project.ts b/packages/@aws-cdk/aws-codebuild/test/test.project.ts index 160783a8dc9d0..99208dbd797a1 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.project.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.project.ts @@ -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) {