Skip to content

Commit

Permalink
Change bind API in Source.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Jun 16, 2019
1 parent 18f5262 commit a06fc99
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 123 deletions.
97 changes: 41 additions & 56 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import { CfnProject } from './codebuild.generated';
import { CodePipelineArtifacts } from './codepipeline-artifacts';
import { NoArtifacts } from './no-artifacts';
import { NoSource } from './no-source';
import { Source } from './source';
import { ISource } from './source';
import { CODEPIPELINE_SOURCE_ARTIFACTS_TYPE, NO_SOURCE_TYPE } from './source-types';

const CODEPIPELINE_TYPE = 'CODEPIPELINE';
const S3_BUCKET_ENV = 'SCRIPT_S3_BUCKET';
const S3_KEY_ENV = 'SCRIPT_S3_KEY';

Expand All @@ -37,6 +36,8 @@ export interface IProject extends IResource, iam.IGrantable, ec2.IConnectable {
/** The IAM service Role of this Project. Undefined for imported Projects. */
readonly role?: iam.IRole;

addToRolePolicy(policyStatement: iam.PolicyStatement): void;

/**
* Defines a CloudWatch event rule triggered when something happens with this project.
*
Expand Down Expand Up @@ -187,6 +188,16 @@ abstract class ProjectBase extends Resource implements IProject {
return this._connections;
}

/**
* Add a permission only if there's a policy attached.
* @param statement The permissions statement to add
*/
public addToRolePolicy(statement: iam.PolicyStatement) {
if (this.role) {
this.role.addToPolicy(statement);
}
}

/**
* Defines a CloudWatch event rule triggered when something happens with this project.
*
Expand Down Expand Up @@ -526,7 +537,7 @@ export interface ProjectProps extends CommonProjectProps {
*
* @default - NoSource
*/
readonly source?: Source;
readonly source?: ISource;

/**
* Defines where build artifacts will be stored.
Expand All @@ -543,7 +554,7 @@ export interface ProjectProps extends CommonProjectProps {
* @default - No secondary sources.
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-multi-in-out.html
*/
readonly secondarySources?: Source[];
readonly secondarySources?: ISource[];

/**
* The secondary artifacts for the Project.
Expand Down Expand Up @@ -632,9 +643,9 @@ export class Project extends ProjectBase {
*/
public readonly projectName: string;

private readonly source: Source;
private readonly source: ISource;
private readonly buildImage: IBuildImage;
private readonly _secondarySources: Source[];
private readonly _secondarySources: CfnProject.SourceProperty[];
private readonly _secondaryArtifacts: Artifacts[];

constructor(scope: Construct, id: string, props: ProjectProps) {
Expand All @@ -657,9 +668,16 @@ export class Project extends ProjectBase {
// let source "bind" to the project. this usually involves granting permissions
// for the code build role to interact with the source.
this.source = props.source || new NoSource();
this.source._bind(this);
const sourceConfig = this.source.bind(this, this);
if (props.badge && !this.source.badgeSupported) {
throw new Error(`Badge is not supported for source type ${this.source.type}`);
}

const artifacts = this.parseArtifacts(props);
const artifacts = props.artifacts
? props.artifacts
: (this.source.type === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE
? new CodePipelineArtifacts()
: new NoArtifacts());
artifacts._bind(this);

const cache = props.cache || Cache.none();
Expand All @@ -670,7 +688,6 @@ export class Project extends ProjectBase {
// Inject download commands for asset if requested
const environmentVariables = props.environmentVariables || {};
let buildSpec = props.buildSpec;

if (props.buildScript) {
environmentVariables[S3_BUCKET_ENV] = { value: props.buildScript.s3BucketName };
environmentVariables[S3_KEY_ENV] = { value: props.buildScript.s3ObjectKey };
Expand All @@ -679,23 +696,9 @@ export class Project extends ProjectBase {
buildSpec = buildSpec ? mergeBuildSpecs(buildSpec, runScript) : runScript;
props.buildScript.grantRead(this.role);
}

// 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}`);
}

if (this.source.type === NO_SOURCE_TYPE && (buildSpec === undefined || !buildSpec.isImmediate)) {
throw new Error("If the Project's source is NoSource, you need to provide a concrete buildSpec");
}

const sourceJson = this.source._toSourceJSON();
return {
...sourceJson,
buildSpec: buildSpec && buildSpec.toBuildSpec()
};
};
if (this.source.type === NO_SOURCE_TYPE && (buildSpec === undefined || !buildSpec.isImmediate)) {
throw new Error("If the Project's source is NoSource, you need to provide a concrete buildSpec");
}

this._secondarySources = [];
for (const secondarySource of props.secondarySources || []) {
Expand All @@ -711,7 +714,10 @@ export class Project extends ProjectBase {

const resource = new CfnProject(this, 'Resource', {
description: props.description,
source: renderSource(),
source: {
...sourceConfig.sourceProperty,
buildSpec: buildSpec && buildSpec.toBuildSpec()
},
artifacts: artifacts.toArtifactsJSON(),
serviceRole: this.role.roleArn,
environment: this.renderEnvironment(props.environment, environmentVariables),
Expand All @@ -722,7 +728,7 @@ export class Project extends ProjectBase {
timeoutInMinutes: props.timeout,
secondarySources: Lazy.anyValue({ produce: () => this.renderSecondarySources() }),
secondaryArtifacts: Lazy.anyValue({ produce: () => this.renderSecondaryArtifacts() }),
triggers: this.source._buildTriggers(),
triggers: sourceConfig.buildTriggers,
vpcConfig: this.configureVpc(props),
});

Expand All @@ -745,16 +751,6 @@ export class Project extends ProjectBase {
}
}

/**
* Add a permission only if there's a policy attached.
* @param statement The permissions statement to add
*/
public addToRolePolicy(statement: iam.PolicyStatement) {
if (this.role) {
this.role.addToPolicy(statement);
}
}

/**
* Add a permission only if there's a policy attached.
* @param statement The permissions statement to add
Expand All @@ -775,12 +771,11 @@ export class Project extends ProjectBase {
* @param secondarySource the source to add as a secondary source
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-multi-in-out.html
*/
public addSecondarySource(secondarySource: Source): void {
public addSecondarySource(secondarySource: ISource): void {
if (!secondarySource.identifier) {
throw new Error('The identifier attribute is mandatory for secondary sources');
}
secondarySource._bind(this);
this._secondarySources.push(secondarySource);
this._secondarySources.push(secondarySource.bind(this, this).sourceProperty);
}

/**
Expand Down Expand Up @@ -874,7 +869,7 @@ export class Project extends ProjectBase {
private renderSecondarySources(): CfnProject.SourceProperty[] | undefined {
return this._secondarySources.length === 0
? undefined
: this._secondarySources.map((secondarySource) => secondarySource._toSourceJSON());
: this._secondarySources;
}

private renderSecondaryArtifacts(): CfnProject.ArtifactsProperty[] | undefined {
Expand Down Expand Up @@ -940,23 +935,13 @@ export class Project extends ProjectBase {
};
}

private parseArtifacts(props: ProjectProps) {
if (props.artifacts) {
return props.artifacts;
}
if (this.source._toSourceJSON().type === CODEPIPELINE_TYPE) {
return new CodePipelineArtifacts();
} else {
return new NoArtifacts();
}
}

private validateCodePipelineSettings(artifacts: Artifacts) {
const sourceType = this.source._toSourceJSON().type;
const sourceType = this.source.type;
const artifactsType = artifacts.toArtifactsJSON().type;

if ((sourceType === CODEPIPELINE_TYPE || artifactsType === CODEPIPELINE_TYPE) &&
(sourceType !== artifactsType)) {
if ((sourceType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE ||
artifactsType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE) &&
(sourceType !== artifactsType)) {
throw new Error('Both source and artifacts must be set to CodePipeline');
}
}
Expand Down
Loading

0 comments on commit a06fc99

Please sign in to comment.