Skip to content

Commit

Permalink
feat(events-targets): allow specifying event for codebuild project ta…
Browse files Browse the repository at this point in the history
…rget (#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 e7dbd2e.

* feat(events-targets): Add unit test
  • Loading branch information
hupe1980 authored and mergify[bot] committed Aug 15, 2019
1 parent 0bf2591 commit c240e1e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
33 changes: 26 additions & 7 deletions packages/@aws-cdk/aws-events-targets/lib/codebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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']
}
}
]
}));
});

0 comments on commit c240e1e

Please sign in to comment.