From f2a9950be9d91cfced21444907b69dc65b7768be Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Thu, 24 Dec 2020 19:18:56 +0000 Subject: [PATCH] feat(codebuild): add `startBatchBuild` option (#11743) This adds a `startBatchBuild` option to the code build source, to trigger a batch build. The cloudformation property isn't in the official docs yet but is mentioned [here](https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/621#issuecomment-732336650). Closes #11663 --- packages/@aws-cdk/aws-codebuild/README.md | 1 + packages/@aws-cdk/aws-codebuild/lib/source.ts | 18 ++++++ .../aws-codebuild/test/test.codebuild.ts | 61 +++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/packages/@aws-cdk/aws-codebuild/README.md b/packages/@aws-cdk/aws-codebuild/README.md index 83b1977014074..7eff891a9ca59 100644 --- a/packages/@aws-cdk/aws-codebuild/README.md +++ b/packages/@aws-cdk/aws-codebuild/README.md @@ -92,6 +92,7 @@ const gitHubSource = codebuild.Source.gitHub({ owner: 'awslabs', repo: 'aws-cdk', webhook: true, // optional, default: true if `webhookFilters` were provided, false otherwise + webhookTriggersBatchBuild: true, // optional, default is false webhookFilters: [ codebuild.FilterGroup .inEventOf(codebuild.EventAction.PUSH) diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index f0a61373c0fbd..b8002f2db9e36 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -482,6 +482,13 @@ interface ThirdPartyGitSourceProps extends GitSourceProps { */ readonly webhook?: boolean; + /** + * Trigger a batch build from a webhook instead of a standard one. + * + * @default false + */ + readonly webhookTriggersBatchBuild?: boolean; + /** * A list of webhook filters that can constraint what events in the repository will trigger a build. * A build is triggered if any of the provided filter groups match. @@ -500,6 +507,7 @@ abstract class ThirdPartyGitSource extends GitSource { protected readonly webhookFilters: FilterGroup[]; private readonly reportBuildStatus: boolean; private readonly webhook?: boolean; + private readonly webhookTriggersBatchBuild?: boolean; protected constructor(props: ThirdPartyGitSourceProps) { super(props); @@ -507,12 +515,21 @@ abstract class ThirdPartyGitSource extends GitSource { this.webhook = props.webhook; this.reportBuildStatus = props.reportBuildStatus === undefined ? true : props.reportBuildStatus; this.webhookFilters = props.webhookFilters || []; + this.webhookTriggersBatchBuild = props.webhookTriggersBatchBuild; } public bind(_scope: CoreConstruct, _project: IProject): SourceConfig { const anyFilterGroupsProvided = this.webhookFilters.length > 0; const webhook = this.webhook === undefined ? (anyFilterGroupsProvided ? true : undefined) : this.webhook; + if (!webhook && anyFilterGroupsProvided) { + throw new Error('`webhookFilters` cannot be used when `webhook` is `false`'); + } + + if (!webhook && this.webhookTriggersBatchBuild) { + throw new Error('`webhookTriggersBatchBuild` cannot be used when `webhook` is `false`'); + } + const superConfig = super.bind(_scope, _project); return { sourceProperty: { @@ -522,6 +539,7 @@ abstract class ThirdPartyGitSource extends GitSource { sourceVersion: superConfig.sourceVersion, buildTriggers: webhook === undefined ? undefined : { webhook, + buildType: this.webhookTriggersBatchBuild ? 'BUILD_BATCH' : undefined, filterGroups: anyFilterGroupsProvided ? this.webhookFilters.map(fg => fg._toJson()) : undefined, }, }; diff --git a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts index 66a4ccd09e8db..5a7766918c67b 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts @@ -694,6 +694,48 @@ export = { test.done(); }, + + 'with webhookTriggersBatchBuild option'(test: Test) { + const stack = new cdk.Stack(); + + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.gitHub({ + owner: 'testowner', + repo: 'testrepo', + webhook: true, + webhookTriggersBatchBuild: true, + }), + }); + + expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', { + Triggers: { + Webhook: true, + BuildType: 'BUILD_BATCH', + }, + })); + + test.done(); + }, + + 'fail creating a Project when webhook false and webhookTriggersBatchBuild option'(test: Test) { + [false, undefined].forEach((webhook) => { + const stack = new cdk.Stack(); + + test.throws(() => { + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.gitHub({ + owner: 'testowner', + repo: 'testrepo', + webhook, + webhookTriggersBatchBuild: true, + }), + }); + }, /`webhookTriggersBatchBuild` cannot be used when `webhook` is `false`/); + }); + + test.done(); + }, + 'fail creating a Project when no build spec is given'(test: Test) { const stack = new cdk.Stack(); @@ -1670,6 +1712,25 @@ export = { test.done(); }, + 'cannot be used when webhook is false'(test: Test) { + const stack = new cdk.Stack(); + + test.throws(() => { + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.bitBucket({ + owner: 'owner', + repo: 'repo', + webhook: false, + webhookFilters: [ + codebuild.FilterGroup.inEventOf(codebuild.EventAction.PUSH), + ], + }), + }); + }, /`webhookFilters` cannot be used when `webhook` is `false`/); + + test.done(); + }, + 'can have FILE_PATH filters if the Group contains PUSH and PR_CREATED events'(test: Test) { codebuild.FilterGroup.inEventOf( codebuild.EventAction.PULL_REQUEST_CREATED,