Skip to content

Commit c240e1e

Browse files
hupe1980mergify[bot]
authored andcommitted
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 e7dbd2e. * feat(events-targets): Add unit test
1 parent 0bf2591 commit c240e1e

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

packages/@aws-cdk/aws-events-targets/lib/codebuild.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ import events = require('@aws-cdk/aws-events');
33
import iam = require('@aws-cdk/aws-iam');
44
import { singletonEventRole } from './util';
55

6+
/**
7+
* Customize the CodeBuild Event Target
8+
*/
9+
export interface CodeBuildProjectProps {
10+
/**
11+
* The event to send to CodeBuild
12+
*
13+
* This will be the payload for the StartBuild API.
14+
*
15+
* @default - the entire CloudWatch event
16+
*/
17+
readonly event?: events.RuleTargetInput;
18+
}
19+
620
/**
721
* Start a CodeBuild build when an AWS CloudWatch events rule is triggered.
822
*/
923
export class CodeBuildProject implements events.IRuleTarget {
10-
constructor(private readonly project: codebuild.IProject) {
11-
}
24+
constructor(
25+
private readonly project: codebuild.IProject,
26+
private readonly props: CodeBuildProjectProps = {}
27+
) {}
1228

1329
/**
1430
* Allows using build projects as event rule targets.
@@ -17,11 +33,14 @@ export class CodeBuildProject implements events.IRuleTarget {
1733
return {
1834
id: '',
1935
arn: this.project.projectArn,
20-
role: singletonEventRole(this.project, [new iam.PolicyStatement({
21-
actions: ['codebuild:StartBuild'],
22-
resources: [this.project.projectArn],
23-
})]),
24-
targetResource: this.project,
36+
role: singletonEventRole(this.project, [
37+
new iam.PolicyStatement({
38+
actions: ['codebuild:StartBuild'],
39+
resources: [this.project.projectArn]
40+
})
41+
]),
42+
input: this.props.event,
43+
targetResource: this.project
2544
};
2645
}
2746
}

packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,39 @@ test('use codebuild project as an eventrule target', () => {
6767
}
6868
}));
6969
});
70+
71+
test('specifying event for codebuild project target', () => {
72+
// GIVEN
73+
const stack = new Stack();
74+
const project = new codebuild.PipelineProject(stack, 'MyProject');
75+
const rule = new events.Rule(stack, 'Rule', {
76+
schedule: events.Schedule.expression('rate(1 hour)')
77+
});
78+
79+
// WHEN
80+
const eventInput = {
81+
buildspecOverride: 'buildspecs/hourly.yml'
82+
};
83+
84+
rule.addTarget(
85+
new targets.CodeBuildProject(project, {
86+
event: events.RuleTargetInput.fromObject(eventInput)
87+
})
88+
);
89+
90+
// THEN
91+
expect(stack).to(haveResource('AWS::Events::Rule', {
92+
Targets: [
93+
{
94+
Arn: {
95+
'Fn::GetAtt': ['MyProject39F7B0AE', 'Arn']
96+
},
97+
Id: 'Target0',
98+
Input: JSON.stringify(eventInput),
99+
RoleArn: {
100+
'Fn::GetAtt': ['MyProjectEventsRole5B7D93F5', 'Arn']
101+
}
102+
}
103+
]
104+
}));
105+
});

0 commit comments

Comments
 (0)