From c240e1e2c0f8bc59dd89c71aa9754f5dcb7074d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20H=C3=BCbner?= Date: Thu, 15 Aug 2019 13:06:14 +0200 Subject: [PATCH] feat(events-targets): allow specifying event for codebuild project target (#3637) * feat(events-targets): Add event prop * Update codebuild.ts * feat(events-targets): Add unit test * Revert "feat(events-targets): Add unit test" This reverts commit e7dbd2ec179ce6c7ae5be0da5b855343ceb614e5. * feat(events-targets): Add unit test --- .../aws-events-targets/lib/codebuild.ts | 33 +++++++++++++---- .../test/codebuild/codebuild.test.ts | 36 +++++++++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/packages/@aws-cdk/aws-events-targets/lib/codebuild.ts b/packages/@aws-cdk/aws-events-targets/lib/codebuild.ts index 2742e5039b7a7..df65100d85ea6 100644 --- a/packages/@aws-cdk/aws-events-targets/lib/codebuild.ts +++ b/packages/@aws-cdk/aws-events-targets/lib/codebuild.ts @@ -3,12 +3,28 @@ import events = require('@aws-cdk/aws-events'); import iam = require('@aws-cdk/aws-iam'); import { singletonEventRole } from './util'; +/** + * Customize the CodeBuild Event Target + */ +export interface CodeBuildProjectProps { + /** + * The event to send to CodeBuild + * + * This will be the payload for the StartBuild API. + * + * @default - the entire CloudWatch event + */ + readonly event?: events.RuleTargetInput; +} + /** * Start a CodeBuild build when an AWS CloudWatch events rule is triggered. */ export class CodeBuildProject implements events.IRuleTarget { - constructor(private readonly project: codebuild.IProject) { - } + constructor( + private readonly project: codebuild.IProject, + private readonly props: CodeBuildProjectProps = {} + ) {} /** * Allows using build projects as event rule targets. @@ -17,11 +33,14 @@ export class CodeBuildProject implements events.IRuleTarget { return { id: '', arn: this.project.projectArn, - role: singletonEventRole(this.project, [new iam.PolicyStatement({ - actions: ['codebuild:StartBuild'], - resources: [this.project.projectArn], - })]), - targetResource: this.project, + role: singletonEventRole(this.project, [ + new iam.PolicyStatement({ + actions: ['codebuild:StartBuild'], + resources: [this.project.projectArn] + }) + ]), + input: this.props.event, + targetResource: this.project }; } } diff --git a/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts b/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts index 136b245cc8ad1..2c40123ca5cec 100644 --- a/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts @@ -67,3 +67,39 @@ test('use codebuild project as an eventrule target', () => { } })); }); + +test('specifying event for codebuild project target', () => { + // GIVEN + const stack = new Stack(); + const project = new codebuild.PipelineProject(stack, 'MyProject'); + const rule = new events.Rule(stack, 'Rule', { + schedule: events.Schedule.expression('rate(1 hour)') + }); + + // WHEN + const eventInput = { + buildspecOverride: 'buildspecs/hourly.yml' + }; + + rule.addTarget( + new targets.CodeBuildProject(project, { + event: events.RuleTargetInput.fromObject(eventInput) + }) + ); + + // THEN + expect(stack).to(haveResource('AWS::Events::Rule', { + Targets: [ + { + Arn: { + 'Fn::GetAtt': ['MyProject39F7B0AE', 'Arn'] + }, + Id: 'Target0', + Input: JSON.stringify(eventInput), + RoleArn: { + 'Fn::GetAtt': ['MyProjectEventsRole5B7D93F5', 'Arn'] + } + } + ] + })); +});