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(events-targets): allow specifying event for codebuild project target #3637

Merged
merged 9 commits into from
Aug 15, 2019
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']
}
}
]
}));
});