Skip to content

Commit

Permalink
fix(codebuild): add validation for Source when the badge property is …
Browse files Browse the repository at this point in the history
…true (#2242)

Badge should not be allowed to be true if Source is not of type GitHub, GitHub Enterprise or Bitbucket.

Fixes #1749
  • Loading branch information
Kaixiang-AWS authored and skinny85 committed Apr 11, 2019
1 parent 8521b6f commit 07812b2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
34 changes: 34 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

0 comments on commit 07812b2

Please sign in to comment.