Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codebuild): add startBatchBuild option #11743

Merged
merged 14 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codebuild/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
},
tjenkinson marked this conversation as resolved.
Show resolved Hide resolved

'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