Skip to content

Commit

Permalink
feat(codebuild): add startBatchBuild option (#11743)
Browse files Browse the repository at this point in the history
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](aws-cloudformation/cloudformation-coverage-roadmap#621 (comment)).

Closes #11663
  • Loading branch information
tjenkinson committed Dec 24, 2020
1 parent 5279f37 commit d9353b7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codebuild/README.md
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/source.ts
Expand Up @@ -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.
Expand All @@ -500,19 +507,29 @@ 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);

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: {
Expand All @@ -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,
},
};
Expand Down
61 changes: 61 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts
Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d9353b7

Please sign in to comment.