diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index e178ffd3287c9..db172fb4966ec 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -644,8 +644,11 @@ export class Project extends ProjectBase { } // Render the source and add in the buildspec - const renderSource = () => { + if (props.badge && !this.source.badgeSupported) { + throw new Error(`Badge is not supported for source type ${this.source.type}`); + } + const sourceJson = this.source.toSourceJSON(); if (typeof buildSpec === 'string') { return { diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index d25bd0db3f34d..0e04cfc30bb07 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -22,6 +22,7 @@ export interface BuildSourceProps { export abstract class BuildSource { public readonly identifier?: string; public abstract readonly type: SourceType; + public readonly badgeSupported: boolean = false; constructor(props: BuildSourceProps) { this.identifier = props.identifier; @@ -89,6 +90,7 @@ export interface GitBuildSourceProps extends BuildSourceProps { * A common superclass of all build sources that are backed by Git. */ export abstract class GitBuildSource extends BuildSource { + public readonly badgeSupported: boolean = true; private readonly cloneDepth?: number; protected constructor(props: GitBuildSourceProps) { @@ -117,6 +119,7 @@ export interface CodeCommitSourceProps extends GitBuildSourceProps { */ export class CodeCommitSource extends GitBuildSource { public readonly type: SourceType = SourceType.CodeCommit; + public readonly badgeSupported: boolean = false; private readonly repo: codecommit.IRepository; constructor(props: CodeCommitSourceProps) { diff --git a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts index 8cd3dd1891e5e..edceed1a66a37 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts @@ -1046,6 +1046,40 @@ export = { }); }, /Windows images do not support the Small ComputeType/); + test.done(); + }, + + 'badge support test'(test: Test) { + const stack = new cdk.Stack(); + + interface BadgeValidationTestCase { + source: codebuild.BuildSource, + shouldPassValidation: boolean + } + + const repo = new codecommit.Repository(stack, 'MyRepo', { repositoryName: 'hello-cdk' }); + const bucket = new s3.Bucket(stack, 'MyBucket'); + + const cases: BadgeValidationTestCase[] = [ + { source: new codebuild.NoSource(), shouldPassValidation: false }, + { source: new codebuild.CodePipelineSource(), shouldPassValidation: false }, + { source: new codebuild.CodeCommitSource({ repository: repo }), shouldPassValidation: false }, + { source: new codebuild.S3BucketSource({ bucket, path: 'path/to/source.zip' }), shouldPassValidation: false }, + { source: new codebuild.GitHubSource({ owner: 'awslabs', repo: 'aws-cdk', oauthToken: new cdk.SecretValue()}), shouldPassValidation: true }, + { source: new codebuild.GitHubEnterpriseSource({ httpsCloneUrl: 'url', oauthToken: new cdk.SecretValue()}), shouldPassValidation: true }, + { source: new codebuild.BitBucketSource({ owner: 'awslabs', repo: 'aws-cdk' }), shouldPassValidation: true } + ]; + + cases.forEach(testCase => { + const source = testCase.source; + const validationBlock = () => { new codebuild.Project(stack, `MyProject-${source.type}`, { source, badge: true }); }; + if (testCase.shouldPassValidation) { + test.doesNotThrow(validationBlock, Error, `Badge is not supported for source type ${source.type}`); + } else { + test.throws(validationBlock, Error, `Badge is not supported for source type ${source.type}`); + } + }); + test.done(); } };