Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codebuild): add validation for source when badge is enabled #2242

Merged
merged 3 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.isBadgeSupported()) {
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
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export abstract class BuildSource {
return undefined;
}

public isBadgeSupported(): boolean {
return false;
}

protected toSourceProperty(): any {
return {
};
Expand Down Expand Up @@ -103,6 +107,10 @@ export abstract class GitBuildSource extends BuildSource {
gitCloneDepth: this.cloneDepth
};
}

public isBadgeSupported(): boolean {
return true;
}
}

/**
Expand Down Expand Up @@ -134,6 +142,10 @@ export class CodeCommitSource extends GitBuildSource {
.addResource(this.repo.repositoryArn));
}

public isBadgeSupported(): boolean {
return false;
}

protected toSourceProperty(): any {
return {
location: this.repo.repositoryCloneUrlHttp
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();
}
};