Skip to content

Commit 9cebf0d

Browse files
authored
feat(aws-codebuild): change the API of GitHub and BitBucket Sources. (#1345)
BREAKING CHANGE: this changes the API of CodeBuild's GitHub and BitBucket Sources to take an owner/repo pair instead of an entire cloneUrl, to make it consistent with the GitHubSourceAction in the CodePipeline package. Also adds handling the reportBuildStatus and insecureSsl Source properties.
1 parent d648b58 commit 9cebf0d

File tree

2 files changed

+138
-27
lines changed

2 files changed

+138
-27
lines changed

Diff for: packages/@aws-cdk/aws-codebuild/lib/source.ts

+69-11
Original file line numberDiff line numberDiff line change
@@ -151,56 +151,102 @@ export class CodePipelineSource extends BuildSource {
151151
*/
152152
export interface GitHubSourceProps extends BuildSourceProps {
153153
/**
154-
* The git url to clone for this code build project.
154+
* The GitHub account/user that owns the repo.
155+
*
156+
* @example 'awslabs'
155157
*/
156-
cloneUrl: string;
158+
owner: string;
159+
160+
/**
161+
* The name of the repo (without the username).
162+
*
163+
* @example 'aws-cdk'
164+
*/
165+
repo: string;
157166

158167
/**
159168
* The oAuthToken used to authenticate when cloning source git repo.
169+
* Note that you need to give CodeBuild permissions to your GitHub account in order for the token to work.
170+
* That is a one-time operation that can be done through the AWS Console for CodeBuild.
160171
*/
161172
oauthToken: cdk.Secret;
173+
174+
/**
175+
* Whether to send GitHub notifications on your build's start and end.
176+
*
177+
* @default true
178+
*/
179+
reportBuildStatus?: boolean;
162180
}
163181

164182
/**
165183
* GitHub Source definition for a CodeBuild project.
166184
*/
167185
export class GitHubSource extends BuildSource {
168186
public readonly type: SourceType = SourceType.GitHub;
169-
private readonly cloneUrl: string;
187+
private readonly httpsCloneUrl: string;
170188
private readonly oauthToken: cdk.Secret;
189+
private readonly reportBuildStatus: boolean;
171190

172191
constructor(props: GitHubSourceProps) {
173192
super(props);
174-
this.cloneUrl = props.cloneUrl;
193+
this.httpsCloneUrl = `https://github.com/${props.owner}/${props.repo}.git`;
175194
this.oauthToken = props.oauthToken;
195+
this.reportBuildStatus = props.reportBuildStatus === undefined ? true : props.reportBuildStatus;
176196
}
177197

178198
protected toSourceProperty(): any {
179199
return {
180200
auth: { type: 'OAUTH', resource: this.oauthToken },
181-
location: this.cloneUrl,
201+
location: this.httpsCloneUrl,
202+
reportBuildStatus: this.reportBuildStatus,
182203
};
183204
}
184205
}
185206

207+
/**
208+
* Construction properties for {@link GitHubEnterpriseSource}.
209+
*/
210+
export interface GitHubEnterpriseSourceProps extends BuildSourceProps {
211+
/**
212+
* The HTTPS URL of the repository in your GitHub Enterprise installation.
213+
*/
214+
httpsCloneUrl: string;
215+
216+
/**
217+
* The OAuth token used to authenticate when cloning the git repository.
218+
*/
219+
oauthToken: cdk.Secret;
220+
221+
/**
222+
* Whether to ignore SSL errors when connecting to the repository.
223+
*
224+
* @default false
225+
*/
226+
ignoreSslErrors?: boolean;
227+
}
228+
186229
/**
187230
* GitHub Enterprise Source definition for a CodeBuild project.
188231
*/
189232
export class GitHubEnterpriseSource extends BuildSource {
190233
public readonly type: SourceType = SourceType.GitHubEnterPrise;
191-
private readonly cloneUrl: string;
234+
private readonly httpsCloneUrl: string;
192235
private readonly oauthToken: cdk.Secret;
236+
private readonly ignoreSslErrors?: boolean;
193237

194-
constructor(props: GitHubSourceProps) {
238+
constructor(props: GitHubEnterpriseSourceProps) {
195239
super(props);
196-
this.cloneUrl = props.cloneUrl;
240+
this.httpsCloneUrl = props.httpsCloneUrl;
197241
this.oauthToken = props.oauthToken;
242+
this.ignoreSslErrors = props.ignoreSslErrors;
198243
}
199244

200245
protected toSourceProperty(): any {
201246
return {
202247
auth: { type: 'OAUTH', resource: this.oauthToken },
203-
location: this.cloneUrl,
248+
location: this.httpsCloneUrl,
249+
insecureSsl: this.ignoreSslErrors,
204250
};
205251
}
206252
}
@@ -209,7 +255,19 @@ export class GitHubEnterpriseSource extends BuildSource {
209255
* Construction properties for {@link BitBucketSource}.
210256
*/
211257
export interface BitBucketSourceProps extends BuildSourceProps {
212-
httpsCloneUrl: string;
258+
/**
259+
* The BitBucket account/user that owns the repo.
260+
*
261+
* @example 'awslabs'
262+
*/
263+
owner: string;
264+
265+
/**
266+
* The name of the repo (without the username).
267+
*
268+
* @example 'aws-cdk'
269+
*/
270+
repo: string;
213271
}
214272

215273
/**
@@ -221,7 +279,7 @@ export class BitBucketSource extends BuildSource {
221279

222280
constructor(props: BitBucketSourceProps) {
223281
super(props);
224-
this.httpsCloneUrl = props.httpsCloneUrl;
282+
this.httpsCloneUrl = `https://bitbucket.org/${props.owner}/${props.repo}.git`;
225283
}
226284

227285
protected toSourceProperty(): any {

Diff for: packages/@aws-cdk/aws-codebuild/test/test.project.ts

+69-16
Original file line numberDiff line numberDiff line change
@@ -47,55 +47,108 @@ export = {
4747
test.done();
4848
},
4949

50-
'github auth test'(test: Test) {
50+
'GitHub source': {
51+
'has reportBuildStatus on by default'(test: Test) {
52+
// GIVEN
53+
const stack = new cdk.Stack();
54+
55+
// WHEN
56+
new codebuild.Project(stack, 'Project', {
57+
source: new codebuild.GitHubSource({
58+
owner: 'testowner',
59+
repo: 'testrepo',
60+
oauthToken: new cdk.Secret("test_oauth_token")
61+
})
62+
});
63+
64+
// THEN
65+
expect(stack).to(haveResource('AWS::CodeBuild::Project', {
66+
Source: {
67+
Type: "GITHUB",
68+
Auth: {
69+
Type: 'OAUTH',
70+
Resource: 'test_oauth_token'
71+
},
72+
Location: 'https://github.com/testowner/testrepo.git',
73+
ReportBuildStatus: true,
74+
}
75+
}));
76+
77+
test.done();
78+
},
79+
80+
'can explicitly set reportBuildStatus to false'(test: Test) {
81+
// GIVEN
82+
const stack = new cdk.Stack();
83+
84+
// WHEN
85+
new codebuild.Project(stack, 'Project', {
86+
source: new codebuild.GitHubSource({
87+
owner: 'testowner',
88+
repo: 'testrepo',
89+
oauthToken: new cdk.Secret('test_oauth_token'),
90+
reportBuildStatus: false,
91+
})
92+
});
93+
94+
// THEN
95+
expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', {
96+
Source: {
97+
ReportBuildStatus: false,
98+
},
99+
}));
100+
101+
test.done();
102+
},
103+
},
104+
105+
'github enterprise auth test'(test: Test) {
51106
// GIVEN
52107
const stack = new cdk.Stack();
53108

54109
// WHEN
55110
new codebuild.Project(stack, 'Project', {
56-
source: new codebuild.GitHubSource({
57-
cloneUrl: "https://github.com/testowner/testrepo",
111+
source: new codebuild.GitHubEnterpriseSource({
112+
httpsCloneUrl: 'https://github.testcompany.com/testowner/testrepo',
113+
ignoreSslErrors: true,
58114
oauthToken: new cdk.Secret("test_oauth_token")
59115
})
60116
});
61117

62118
// THEN
63119
expect(stack).to(haveResource('AWS::CodeBuild::Project', {
64120
Source: {
65-
Type: "GITHUB",
121+
Type: "GITHUB_ENTERPRISE",
66122
Auth: {
67123
Type: 'OAUTH',
68124
Resource: 'test_oauth_token'
69125
},
70-
Location: 'https://github.com/testowner/testrepo'
126+
InsecureSsl: true,
127+
Location: 'https://github.testcompany.com/testowner/testrepo'
71128
}
72129
}));
73130

74131
test.done();
75132
},
76133

77-
'github enterprise auth test'(test: Test) {
134+
'bitbucket auth test'(test: Test) {
78135
// GIVEN
79136
const stack = new cdk.Stack();
80137

81138
// WHEN
82139
new codebuild.Project(stack, 'Project', {
83-
source: new codebuild.GitHubEnterpriseSource({
84-
cloneUrl: "https://github.testcompany.com/testowner/testrepo",
85-
oauthToken: new cdk.Secret("test_oauth_token")
140+
source: new codebuild.BitBucketSource({
141+
owner: 'testowner',
142+
repo: 'testrepo',
86143
})
87144
});
88145

89146
// THEN
90147
expect(stack).to(haveResource('AWS::CodeBuild::Project', {
91148
Source: {
92-
Type: "GITHUB_ENTERPRISE",
93-
Auth: {
94-
Type: 'OAUTH',
95-
Resource: 'test_oauth_token'
96-
},
97-
Location: 'https://github.testcompany.com/testowner/testrepo'
98-
}
149+
Type: 'BITBUCKET',
150+
Location: 'https://bitbucket.org/testowner/testrepo.git',
151+
},
99152
}));
100153

101154
test.done();

0 commit comments

Comments
 (0)