Skip to content

Commit 07812b2

Browse files
Kaixiang-AWSskinny85
authored andcommitted
fix(codebuild): add validation for Source when the badge property is true (#2242)
Badge should not be allowed to be true if Source is not of type GitHub, GitHub Enterprise or Bitbucket. Fixes #1749
1 parent 8521b6f commit 07812b2

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,11 @@ export class Project extends ProjectBase {
644644
}
645645

646646
// Render the source and add in the buildspec
647-
648647
const renderSource = () => {
648+
if (props.badge && !this.source.badgeSupported) {
649+
throw new Error(`Badge is not supported for source type ${this.source.type}`);
650+
}
651+
649652
const sourceJson = this.source.toSourceJSON();
650653
if (typeof buildSpec === 'string') {
651654
return {

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

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface BuildSourceProps {
2222
export abstract class BuildSource {
2323
public readonly identifier?: string;
2424
public abstract readonly type: SourceType;
25+
public readonly badgeSupported: boolean = false;
2526

2627
constructor(props: BuildSourceProps) {
2728
this.identifier = props.identifier;
@@ -89,6 +90,7 @@ export interface GitBuildSourceProps extends BuildSourceProps {
8990
* A common superclass of all build sources that are backed by Git.
9091
*/
9192
export abstract class GitBuildSource extends BuildSource {
93+
public readonly badgeSupported: boolean = true;
9294
private readonly cloneDepth?: number;
9395

9496
protected constructor(props: GitBuildSourceProps) {
@@ -117,6 +119,7 @@ export interface CodeCommitSourceProps extends GitBuildSourceProps {
117119
*/
118120
export class CodeCommitSource extends GitBuildSource {
119121
public readonly type: SourceType = SourceType.CodeCommit;
122+
public readonly badgeSupported: boolean = false;
120123
private readonly repo: codecommit.IRepository;
121124

122125
constructor(props: CodeCommitSourceProps) {

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

+34
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,40 @@ export = {
10461046
});
10471047
}, /Windows images do not support the Small ComputeType/);
10481048

1049+
test.done();
1050+
},
1051+
1052+
'badge support test'(test: Test) {
1053+
const stack = new cdk.Stack();
1054+
1055+
interface BadgeValidationTestCase {
1056+
source: codebuild.BuildSource,
1057+
shouldPassValidation: boolean
1058+
}
1059+
1060+
const repo = new codecommit.Repository(stack, 'MyRepo', { repositoryName: 'hello-cdk' });
1061+
const bucket = new s3.Bucket(stack, 'MyBucket');
1062+
1063+
const cases: BadgeValidationTestCase[] = [
1064+
{ source: new codebuild.NoSource(), shouldPassValidation: false },
1065+
{ source: new codebuild.CodePipelineSource(), shouldPassValidation: false },
1066+
{ source: new codebuild.CodeCommitSource({ repository: repo }), shouldPassValidation: false },
1067+
{ source: new codebuild.S3BucketSource({ bucket, path: 'path/to/source.zip' }), shouldPassValidation: false },
1068+
{ source: new codebuild.GitHubSource({ owner: 'awslabs', repo: 'aws-cdk', oauthToken: new cdk.SecretValue()}), shouldPassValidation: true },
1069+
{ source: new codebuild.GitHubEnterpriseSource({ httpsCloneUrl: 'url', oauthToken: new cdk.SecretValue()}), shouldPassValidation: true },
1070+
{ source: new codebuild.BitBucketSource({ owner: 'awslabs', repo: 'aws-cdk' }), shouldPassValidation: true }
1071+
];
1072+
1073+
cases.forEach(testCase => {
1074+
const source = testCase.source;
1075+
const validationBlock = () => { new codebuild.Project(stack, `MyProject-${source.type}`, { source, badge: true }); };
1076+
if (testCase.shouldPassValidation) {
1077+
test.doesNotThrow(validationBlock, Error, `Badge is not supported for source type ${source.type}`);
1078+
} else {
1079+
test.throws(validationBlock, Error, `Badge is not supported for source type ${source.type}`);
1080+
}
1081+
});
1082+
10491083
test.done();
10501084
}
10511085
};

0 commit comments

Comments
 (0)