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 all commits
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.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();
}
};