Skip to content

Commit d5cae61

Browse files
authored
feat(aws-codebuild): allow setting Webhook for GitHub Sources. (#1387)
1 parent 478a714 commit d5cae61

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

packages/@aws-cdk/aws-codebuild/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ CodePipeline action.
8484
#### `GitHubSource` and `GitHubEnterpriseSource`
8585

8686
These source types can be used to build code from a GitHub repository.
87+
Example:
88+
89+
```typescript
90+
const gitHubSource = new codebuild.GitHubSource({
91+
owner: 'awslabs',
92+
repo: 'aws-cdk',
93+
oauthToken: new cdk.SecretParameter(this, 'GitHubOAuthToken', {
94+
ssmParameter: 'my-github-token',
95+
}),
96+
webhook: true, // optional, default: false
97+
});
98+
```
8799

88100
#### `BitBucketSource`
89101

packages/@aws-cdk/aws-codebuild/lib/project.ts

+1
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ export class Project extends ProjectRef {
562562
timeoutInMinutes: props.timeout,
563563
secondarySources: new cdk.Token(() => this.renderSecondarySources()),
564564
secondaryArtifacts: new cdk.Token(() => this.renderSecondaryArtifacts()),
565+
triggers: this.source.buildTriggers(),
565566
});
566567

567568
this.projectArn = resource.projectArn;

packages/@aws-cdk/aws-codebuild/lib/source.ts

+21
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export abstract class BuildSource {
4646
};
4747
}
4848

49+
public buildTriggers(): CfnProject.ProjectTriggersProperty | undefined {
50+
return undefined;
51+
}
52+
4953
protected toSourceProperty(): any {
5054
return {
5155
};
@@ -171,6 +175,13 @@ export interface GitHubSourceProps extends BuildSourceProps {
171175
*/
172176
oauthToken: cdk.Secret;
173177

178+
/**
179+
* Whether to create a webhook that will trigger a build every time a commit is pushed to the GitHub repository.
180+
*
181+
* @default false
182+
*/
183+
webhook?: boolean;
184+
174185
/**
175186
* Whether to send GitHub notifications on your build's start and end.
176187
*
@@ -187,14 +198,24 @@ export class GitHubSource extends BuildSource {
187198
private readonly httpsCloneUrl: string;
188199
private readonly oauthToken: cdk.Secret;
189200
private readonly reportBuildStatus: boolean;
201+
private readonly webhook?: boolean;
190202

191203
constructor(props: GitHubSourceProps) {
192204
super(props);
193205
this.httpsCloneUrl = `https://github.com/${props.owner}/${props.repo}.git`;
194206
this.oauthToken = props.oauthToken;
207+
this.webhook = props.webhook;
195208
this.reportBuildStatus = props.reportBuildStatus === undefined ? true : props.reportBuildStatus;
196209
}
197210

211+
public buildTriggers(): CfnProject.ProjectTriggersProperty | undefined {
212+
return this.webhook === undefined
213+
? undefined
214+
: {
215+
webhook: this.webhook,
216+
};
217+
}
218+
198219
protected toSourceProperty(): any {
199220
return {
200221
auth: { type: 'OAUTH', resource: this.oauthToken },

packages/@aws-cdk/aws-codebuild/test/test.project.ts

+24
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ export = {
100100

101101
test.done();
102102
},
103+
104+
'can explicitly set webhook to true'(test: Test) {
105+
// GIVEN
106+
const stack = new cdk.Stack();
107+
108+
// WHEN
109+
new codebuild.Project(stack, 'Project', {
110+
source: new codebuild.GitHubSource({
111+
owner: 'testowner',
112+
repo: 'testrepo',
113+
oauthToken: new cdk.Secret('test_oauth_token'),
114+
webhook: true,
115+
})
116+
});
117+
118+
// THEN
119+
expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', {
120+
Triggers: {
121+
Webhook: true,
122+
},
123+
}));
124+
125+
test.done();
126+
},
103127
},
104128

105129
'github enterprise auth test'(test: Test) {

0 commit comments

Comments
 (0)