From 41016b8a5b6d2158a53eb54e4724c9502b7ca558 Mon Sep 17 00:00:00 2001 From: Katie Normandin Date: Tue, 21 Jan 2020 10:31:13 -0500 Subject: [PATCH 1/6] feat(aws-codebuild): allow github sourceversion branch Addresses issue #5777.Users may want to specify which github branch a codebuild should use as its source. Currently the branch always defaults to 'master', but users may want to test features in development on other branches --- .../@aws-cdk/aws-codebuild/lib/project.ts | 1 + packages/@aws-cdk/aws-codebuild/lib/source.ts | 12 +++++++++++ .../aws-codebuild/test/test.codebuild.ts | 5 +++++ .../aws-codebuild/test/test.project.ts | 21 +++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index b8511271b06d9..f9bc0bcc1fb99 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -727,6 +727,7 @@ export class Project extends ProjectBase { secondarySources: Lazy.anyValue({ produce: () => this.renderSecondarySources() }), secondaryArtifacts: Lazy.anyValue({ produce: () => this.renderSecondaryArtifacts() }), triggers: sourceConfig.buildTriggers, + sourceVersion: sourceConfig.sourceVersion, vpcConfig: this.configureVpc(props), }); diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index df7f74d3ccda9..e0b69ab5c1809 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -19,6 +19,8 @@ export interface SourceConfig { readonly sourceProperty: CfnProject.SourceProperty; readonly buildTriggers?: CfnProject.ProjectTriggersProperty; + + readonly sourceVersion?: string | undefined; } /** @@ -554,6 +556,13 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps { * @example 'aws-cdk' */ readonly repo: string; + + /** + * The name of the branch. + * + * @example 'mybranch' + */ + readonly branch?: string; } /** @@ -561,11 +570,13 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps { */ class GitHubSource extends ThirdPartyGitSource { public readonly type = GITHUB_SOURCE_TYPE; + public readonly branch?: string; private readonly httpsCloneUrl: string; constructor(props: GitHubSourceProps) { super(props); this.httpsCloneUrl = `https://github.com/${props.owner}/${props.repo}.git`; + this.branch = props.branch; } public bind(_scope: Construct, project: IProject): SourceConfig { @@ -575,6 +586,7 @@ class GitHubSource extends ThirdPartyGitSource { ...superConfig.sourceProperty, location: this.httpsCloneUrl, }, + sourceVersion: this.branch, buildTriggers: superConfig.buildTriggers, }; } diff --git a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts index 4401df8835844..39681c5fb5c31 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts @@ -479,6 +479,7 @@ export = { source: codebuild.Source.gitHub({ owner: 'testowner', repo: 'testrepo', + branch: 'testbranch', cloneDepth: 3, webhook: true, reportBuildStatus: false, @@ -498,6 +499,10 @@ export = { } })); + expect(stack).to(haveResource('AWS::CodeBuild::Project', { + SourceVersion: 'testbranch', + })); + expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', { Triggers: { Webhook: true, diff --git a/packages/@aws-cdk/aws-codebuild/test/test.project.ts b/packages/@aws-cdk/aws-codebuild/test/test.project.ts index 17402b5ae1395..c767f30077883 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.project.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.project.ts @@ -103,6 +103,27 @@ export = { test.done(); }, + 'can set a branch as the SourceVersion'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.gitHub({ + owner: 'testowner', + repo: 'testrepo', + branch: 'testbranch', + }) + }); + + // THEN + expect(stack).to(haveResource('AWS::CodeBuild::Project', { + SourceVersion: 'testbranch', + })); + + test.done(); + }, + 'can explicitly set reportBuildStatus to false'(test: Test) { // GIVEN const stack = new cdk.Stack(); From 656ae0ce91c9ea2d54aed382957bfbb69ab6f8df Mon Sep 17 00:00:00 2001 From: Katie Normandin Date: Tue, 21 Jan 2020 13:11:11 -0500 Subject: [PATCH 2/6] add required documentation --- packages/@aws-cdk/aws-codebuild/lib/source.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index e0b69ab5c1809..f6d50026b3836 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -20,6 +20,11 @@ export interface SourceConfig { readonly buildTriggers?: CfnProject.ProjectTriggersProperty; + /** + * `AWS::CodeBuild::Project.SourceVersion` + * @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html#cfn-codebuild-project-sourceversion + * @default the latest version + */ readonly sourceVersion?: string | undefined; } @@ -561,6 +566,7 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps { * The name of the branch. * * @example 'mybranch' + * @default the default branch's HEAD commit ID is used */ readonly branch?: string; } From 2b95abf3789a29cfbe928744121d6203ddd169bc Mon Sep 17 00:00:00 2001 From: Katie Normandin Date: Thu, 30 Jan 2020 14:03:25 -0500 Subject: [PATCH 3/6] add sourceVersion to all CodeBuild sources. Add secondarySourceVersions option --- .../@aws-cdk/aws-codebuild/lib/project.ts | 33 ++++++-- packages/@aws-cdk/aws-codebuild/lib/source.ts | 59 +++++++++++++-- ...-secondary-sources-artifacts.expected.json | 5 ++ .../aws-codebuild/test/test.codebuild.ts | 75 +++++++++++++++++-- .../aws-codebuild/test/test.project.ts | 66 +++++++++++++++- 5 files changed, 219 insertions(+), 19 deletions(-) diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index f9bc0bcc1fb99..af196f55edfda 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -537,6 +537,13 @@ export interface ProjectProps extends CommonProjectProps { */ readonly secondarySources?: ISource[]; + /** + * The secondary source versions for the Project. * + * @default - No secondary source versions (they default to latest). + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html#cfn-codebuild-project-sourceversion + */ + readonly secondarySourceVersions?: CfnProject.ProjectSourceVersionProperty[]; + /** * The secondary artifacts for the Project. * Can also be added after the Project has been created by using the {@link Project#addSecondaryArtifact} method. @@ -626,7 +633,7 @@ export class Project extends ProjectBase { * @returns an array of {@link CfnProject.EnvironmentVariableProperty} instances */ public static serializeEnvVariables(environmentVariables: { [name: string]: BuildEnvironmentVariable }): - CfnProject.EnvironmentVariableProperty[] { + CfnProject.EnvironmentVariableProperty[] { return Object.keys(environmentVariables).map(name => ({ name, type: environmentVariables[name].type || BuildEnvironmentVariableType.PLAINTEXT, @@ -654,6 +661,7 @@ export class Project extends ProjectBase { private readonly source: ISource; private readonly buildImage: IBuildImage; private readonly _secondarySources: CfnProject.SourceProperty[]; + private readonly _secondarySourceVersions: CfnProject.ProjectSourceVersionProperty[]; private readonly _secondaryArtifacts: CfnProject.ArtifactsProperty[]; private _encryptionKey?: kms.IKey; @@ -698,6 +706,7 @@ export class Project extends ProjectBase { } this._secondarySources = []; + this._secondarySourceVersions = []; for (const secondarySource of props.secondarySources || []) { this.addSecondarySource(secondarySource); } @@ -725,6 +734,7 @@ export class Project extends ProjectBase { name: this.physicalName, timeoutInMinutes: props.timeout && props.timeout.toMinutes(), secondarySources: Lazy.anyValue({ produce: () => this.renderSecondarySources() }), + secondarySourceVersions: Lazy.anyValue({ produce: () => this.renderSecondarySourceVersions() }), secondaryArtifacts: Lazy.anyValue({ produce: () => this.renderSecondaryArtifacts() }), triggers: sourceConfig.buildTriggers, sourceVersion: sourceConfig.sourceVersion, @@ -757,7 +767,12 @@ export class Project extends ProjectBase { if (!secondarySource.identifier) { throw new Error('The identifier attribute is mandatory for secondary sources'); } - this._secondarySources.push(secondarySource.bind(this, this).sourceProperty); + const secondarySourceConfig = secondarySource.bind(this, this); + this._secondarySources.push(secondarySourceConfig.sourceProperty); + this._secondarySourceVersions.push({ + sourceIdentifier: secondarySource.identifier, + sourceVersion: secondarySourceConfig.sourceVersion, + }); } /** @@ -789,7 +804,7 @@ export class Project extends ProjectBase { const keyStack = Stack.of(options.artifactBucket.encryptionKey); const projectStack = Stack.of(this); if (!(options.artifactBucket.encryptionKey instanceof kms.Key && - (keyStack.account !== projectStack.account || keyStack.region !== projectStack.region))) { + (keyStack.account !== projectStack.account || keyStack.region !== projectStack.region))) { this.encryptionKey = options.artifactBucket.encryptionKey; } } @@ -894,6 +909,12 @@ export class Project extends ProjectBase { : this._secondarySources; } + private renderSecondarySourceVersions(): CfnProject.ProjectSourceVersionProperty[] | undefined { + return this._secondarySourceVersions.length === 0 + ? undefined + : this._secondarySourceVersions; + } + private renderSecondaryArtifacts(): CfnProject.ArtifactsProperty[] | undefined { return this._secondaryArtifacts.length === 0 ? undefined @@ -984,8 +1005,8 @@ export class Project extends ProjectBase { const artifactsType = artifacts.type; if ((sourceType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE || - artifactsType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE) && - (sourceType !== artifactsType)) { + artifactsType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE) && + (sourceType !== artifactsType)) { throw new Error('Both source and artifacts must be set to CodePipeline'); } } @@ -1125,7 +1146,7 @@ class ArmBuildImage implements IBuildImage { public validate(buildEnvironment: BuildEnvironment): string[] { const ret = []; if (buildEnvironment.computeType && - buildEnvironment.computeType !== ComputeType.LARGE) { + buildEnvironment.computeType !== ComputeType.LARGE) { ret.push(`ARM images only support ComputeType '${ComputeType.LARGE}' - ` + `'${buildEnvironment.computeType}' was given`); } diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index f6d50026b3836..02ed42e740aca 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -25,7 +25,7 @@ export interface SourceConfig { * @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html#cfn-codebuild-project-sourceversion * @default the latest version */ - readonly sourceVersion?: string | undefined; + readonly sourceVersion?: string; } /** @@ -478,6 +478,14 @@ abstract class ThirdPartyGitSource extends GitSource { */ export interface CodeCommitSourceProps extends GitSourceProps { readonly repository: codecommit.IRepository; + + /** + * The commit ID, branch, or Git tag to use. + * + * @example 'mybranch' + * @default the default branch's HEAD commit ID is used + */ + readonly sourceVersion?: string; } /** @@ -486,10 +494,12 @@ export interface CodeCommitSourceProps extends GitSourceProps { class CodeCommitSource extends GitSource { public readonly type = CODECOMMIT_SOURCE_TYPE; private readonly repo: codecommit.IRepository; + private readonly sourceVersion?: string; constructor(props: CodeCommitSourceProps) { super(props); this.repo = props.repository; + this.sourceVersion = props.sourceVersion; } public bind(_scope: Construct, project: IProject): SourceConfig { @@ -505,6 +515,7 @@ class CodeCommitSource extends GitSource { ...superConfig.sourceProperty, location: this.repo.repositoryCloneUrlHttp, }, + sourceVersion: this.sourceVersion, }; } } @@ -515,6 +526,13 @@ class CodeCommitSource extends GitSource { export interface S3SourceProps extends SourceProps { readonly bucket: s3.IBucket; readonly path: string; + + /** + * The version ID of the object that represents the build input ZIP file to use. + * + * @default latest + */ + readonly sourceVersion?: string; } /** @@ -524,11 +542,13 @@ class S3Source extends Source { public readonly type = S3_SOURCE_TYPE; private readonly bucket: s3.IBucket; private readonly path: string; + private readonly sourceVersion?: string; constructor(props: S3SourceProps) { super(props); this.bucket = props.bucket; this.path = props.path; + this.sourceVersion = props.sourceVersion; } public bind(_scope: Construct, project: IProject): SourceConfig { @@ -540,6 +560,7 @@ class S3Source extends Source { ...superConfig.sourceProperty, location: `${this.bucket.bucketName}/${this.path}`, }, + sourceVersion: this.sourceVersion, }; } } @@ -563,12 +584,13 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps { readonly repo: string; /** - * The name of the branch. + * The commit ID, pull request ID, branch name, or tag name that corresponds to + * the version of the source code you want to build * - * @example 'mybranch' + * @example 'pr/25' * @default the default branch's HEAD commit ID is used */ - readonly branch?: string; + readonly sourceVersion?: string; } /** @@ -576,13 +598,13 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps { */ class GitHubSource extends ThirdPartyGitSource { public readonly type = GITHUB_SOURCE_TYPE; - public readonly branch?: string; + private readonly sourceVersion?: string; private readonly httpsCloneUrl: string; constructor(props: GitHubSourceProps) { super(props); this.httpsCloneUrl = `https://github.com/${props.owner}/${props.repo}.git`; - this.branch = props.branch; + this.sourceVersion = props.sourceVersion; } public bind(_scope: Construct, project: IProject): SourceConfig { @@ -592,7 +614,7 @@ class GitHubSource extends ThirdPartyGitSource { ...superConfig.sourceProperty, location: this.httpsCloneUrl, }, - sourceVersion: this.branch, + sourceVersion: this.sourceVersion, buildTriggers: superConfig.buildTriggers, }; } @@ -613,6 +635,15 @@ export interface GitHubEnterpriseSourceProps extends ThirdPartyGitSourceProps { * @default false */ readonly ignoreSslErrors?: boolean; + + /** + * The commit ID, pull request ID, branch name, or tag name that corresponds to + * the version of the source code you want to build + * + * @example 'pr/25' + * @default the default branch's HEAD commit ID is used + */ + readonly sourceVersion?: string; } /** @@ -622,11 +653,13 @@ class GitHubEnterpriseSource extends ThirdPartyGitSource { public readonly type = GITHUB_ENTERPRISE_SOURCE_TYPE; private readonly httpsCloneUrl: string; private readonly ignoreSslErrors?: boolean; + private readonly sourceVersion?: string; constructor(props: GitHubEnterpriseSourceProps) { super(props); this.httpsCloneUrl = props.httpsCloneUrl; this.ignoreSslErrors = props.ignoreSslErrors; + this.sourceVersion = props.sourceVersion; } public bind(_scope: Construct, _project: IProject): SourceConfig { @@ -637,6 +670,7 @@ class GitHubEnterpriseSource extends ThirdPartyGitSource { location: this.httpsCloneUrl, insecureSsl: this.ignoreSslErrors, }, + sourceVersion: this.sourceVersion, buildTriggers: superConfig.buildTriggers, }; } @@ -659,6 +693,14 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps { * @example 'aws-cdk' */ readonly repo: string; + + /** + * The commit ID, branch name, or tag name that corresponds to the version of the source code you want to build + * + * @example 'mybranch' + * @default the default branch's HEAD commit ID is used + */ + readonly sourceVersion?: string; } /** @@ -667,10 +709,12 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps { class BitBucketSource extends ThirdPartyGitSource { public readonly type = BITBUCKET_SOURCE_TYPE; private readonly httpsCloneUrl: any; + private readonly sourceVersion?: string; constructor(props: BitBucketSourceProps) { super(props); this.httpsCloneUrl = `https://bitbucket.org/${props.owner}/${props.repo}.git`; + this.sourceVersion = props.sourceVersion; } public bind(_scope: Construct, _project: IProject): SourceConfig { @@ -690,6 +734,7 @@ class BitBucketSource extends ThirdPartyGitSource { ...superConfig.sourceProperty, location: this.httpsCloneUrl, }, + sourceVersion: this.sourceVersion, buildTriggers: superConfig.buildTriggers, }; } diff --git a/packages/@aws-cdk/aws-codebuild/test/integ.project-secondary-sources-artifacts.expected.json b/packages/@aws-cdk/aws-codebuild/test/integ.project-secondary-sources-artifacts.expected.json index c57915ecb5ac1..c5c3bd268720b 100644 --- a/packages/@aws-cdk/aws-codebuild/test/integ.project-secondary-sources-artifacts.expected.json +++ b/packages/@aws-cdk/aws-codebuild/test/integ.project-secondary-sources-artifacts.expected.json @@ -209,6 +209,11 @@ "SourceIdentifier": "AddSource1", "Type": "S3" } + ], + "SecondarySourceVersions": [ + { + "SourceIdentifier": "AddSource1" + } ] } } diff --git a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts index 39681c5fb5c31..ca8cf29197279 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts @@ -479,7 +479,6 @@ export = { source: codebuild.Source.gitHub({ owner: 'testowner', repo: 'testrepo', - branch: 'testbranch', cloneDepth: 3, webhook: true, reportBuildStatus: false, @@ -499,10 +498,6 @@ export = { } })); - expect(stack).to(haveResource('AWS::CodeBuild::Project', { - SourceVersion: 'testbranch', - })); - expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', { Triggers: { Webhook: true, @@ -870,6 +865,76 @@ export = { }, }, + 'secondary source versions': { + 'allow secondary source versions'(test: Test) { + const stack = new cdk.Stack(); + const bucket = new s3.Bucket(stack, 'MyBucket'); + const project = new codebuild.Project(stack, 'MyProject', { + source: codebuild.Source.s3({ + bucket, + path: 'some/path', + }), + }); + + project.addSecondarySource(codebuild.Source.s3({ + bucket, + path: 'another/path', + identifier: 'source1', + sourceVersion: 'someversion' + })); + + expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', { + "SecondarySources": [ + { + "SourceIdentifier": "source1", + "Type": "S3", + }, + ], + "SecondarySourceVersions": [ + { + "SourceIdentifier": "source1", + "SourceVersion": "someversion" + } + ] + })); + + test.done(); + }, + + 'allow not to specify secondary source versions'(test: Test) { + const stack = new cdk.Stack(); + const bucket = new s3.Bucket(stack, 'MyBucket'); + const project = new codebuild.Project(stack, 'MyProject', { + source: codebuild.Source.s3({ + bucket, + path: 'some/path', + }), + }); + + project.addSecondarySource(codebuild.Source.s3({ + bucket, + path: 'another/path', + identifier: 'source1', + })); + + expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', { + "SecondarySources": [ + { + "SourceIdentifier": "source1", + "Type": "S3", + }, + ], + "SecondarySourceVersions": [ + { + "SourceIdentifier": "source1", + } + ] + })); + + test.done(); + }, + }, + 'secondary artifacts': { 'require providing an identifier when creating a Project'(test: Test) { const stack = new cdk.Stack(); diff --git a/packages/@aws-cdk/aws-codebuild/test/test.project.ts b/packages/@aws-cdk/aws-codebuild/test/test.project.ts index c767f30077883..98108af9d7f11 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.project.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.project.ts @@ -112,7 +112,27 @@ export = { source: codebuild.Source.gitHub({ owner: 'testowner', repo: 'testrepo', - branch: 'testbranch', + sourceVersion: 'testbranch', + }) + }); + + // THEN + expect(stack).to(haveResource('AWS::CodeBuild::Project', { + SourceVersion: 'testbranch', + })); + + test.done(); + }, + + 'can set the SourceVersion for a gitHubEnterprise'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.gitHubEnterprise({ + httpsCloneUrl: 'https://mygithub-enterprise.com/myuser/myrepo', + sourceVersion: 'testbranch', }) }); @@ -187,6 +207,27 @@ export = { }, }, + 'project with bitbucket and SourceVersion'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.bitBucket({ + owner: 'testowner', + repo: 'testrepo', + sourceVersion: 'testbranch', + }) + }); + + // THEN + expect(stack).to(haveResource('AWS::CodeBuild::Project', { + SourceVersion: 'testbranch', + })); + + test.done(); + }, + 'project with s3 cache bucket'(test: Test) { // GIVEN const stack = new cdk.Stack(); @@ -223,6 +264,29 @@ export = { test.done(); }, + 's3 codebuild project with sourceVersion'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.s3({ + bucket: new s3.Bucket(stack, 'Bucket'), + path: 'path', + sourceVersion: 's3version' + }), + cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER, + codebuild.LocalCacheMode.SOURCE) + }); + + // THEN + expect(stack).to(haveResource('AWS::CodeBuild::Project', { + SourceVersion: 's3version', + })); + + test.done(); + }, + 'project with local cache modes'(test: Test) { // GIVEN const stack = new cdk.Stack(); From 664ef4a8851d4968e965a11bf1dfd7c9c482efee Mon Sep 17 00:00:00 2001 From: Katie Normandin Date: Sun, 2 Feb 2020 11:13:32 -0500 Subject: [PATCH 4/6] unify version on GitSource to branch or ref. fix indentation. remove unused property. only render secondary sources versions when present. --- .../@aws-cdk/aws-codebuild/lib/project.ts | 31 ++++----- packages/@aws-cdk/aws-codebuild/lib/source.ts | 67 ++++++------------- ...-secondary-sources-artifacts.expected.json | 5 -- .../aws-codebuild/test/test.codebuild.ts | 5 -- .../aws-codebuild/test/test.project.ts | 6 +- 5 files changed, 37 insertions(+), 77 deletions(-) diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index af196f55edfda..d99653f449361 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -537,13 +537,6 @@ export interface ProjectProps extends CommonProjectProps { */ readonly secondarySources?: ISource[]; - /** - * The secondary source versions for the Project. * - * @default - No secondary source versions (they default to latest). - * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html#cfn-codebuild-project-sourceversion - */ - readonly secondarySourceVersions?: CfnProject.ProjectSourceVersionProperty[]; - /** * The secondary artifacts for the Project. * Can also be added after the Project has been created by using the {@link Project#addSecondaryArtifact} method. @@ -633,7 +626,7 @@ export class Project extends ProjectBase { * @returns an array of {@link CfnProject.EnvironmentVariableProperty} instances */ public static serializeEnvVariables(environmentVariables: { [name: string]: BuildEnvironmentVariable }): - CfnProject.EnvironmentVariableProperty[] { + CfnProject.EnvironmentVariableProperty[] { return Object.keys(environmentVariables).map(name => ({ name, type: environmentVariables[name].type || BuildEnvironmentVariableType.PLAINTEXT, @@ -769,10 +762,12 @@ export class Project extends ProjectBase { } const secondarySourceConfig = secondarySource.bind(this, this); this._secondarySources.push(secondarySourceConfig.sourceProperty); - this._secondarySourceVersions.push({ - sourceIdentifier: secondarySource.identifier, - sourceVersion: secondarySourceConfig.sourceVersion, - }); + if (secondarySourceConfig.sourceVersion) { + this._secondarySourceVersions.push({ + sourceIdentifier: secondarySource.identifier, + sourceVersion: secondarySourceConfig.sourceVersion, + }); + } } /** @@ -804,7 +799,7 @@ export class Project extends ProjectBase { const keyStack = Stack.of(options.artifactBucket.encryptionKey); const projectStack = Stack.of(this); if (!(options.artifactBucket.encryptionKey instanceof kms.Key && - (keyStack.account !== projectStack.account || keyStack.region !== projectStack.region))) { + (keyStack.account !== projectStack.account || keyStack.region !== projectStack.region))) { this.encryptionKey = options.artifactBucket.encryptionKey; } } @@ -911,8 +906,8 @@ export class Project extends ProjectBase { private renderSecondarySourceVersions(): CfnProject.ProjectSourceVersionProperty[] | undefined { return this._secondarySourceVersions.length === 0 - ? undefined - : this._secondarySourceVersions; + ? undefined + : this._secondarySourceVersions; } private renderSecondaryArtifacts(): CfnProject.ArtifactsProperty[] | undefined { @@ -1005,8 +1000,8 @@ export class Project extends ProjectBase { const artifactsType = artifacts.type; if ((sourceType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE || - artifactsType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE) && - (sourceType !== artifactsType)) { + artifactsType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE) && + (sourceType !== artifactsType)) { throw new Error('Both source and artifacts must be set to CodePipeline'); } } @@ -1146,7 +1141,7 @@ class ArmBuildImage implements IBuildImage { public validate(buildEnvironment: BuildEnvironment): string[] { const ret = []; if (buildEnvironment.computeType && - buildEnvironment.computeType !== ComputeType.LARGE) { + buildEnvironment.computeType !== ComputeType.LARGE) { ret.push(`ARM images only support ComputeType '${ComputeType.LARGE}' - ` + `'${buildEnvironment.computeType}' was given`); } diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index 02ed42e740aca..4c30184a88c18 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -110,6 +110,15 @@ interface GitSourceProps extends SourceProps { * then the full history is downloaded with each build of the project. */ readonly cloneDepth?: number; + + /** + * The commit ID, pull request ID, branch name, or tag name that corresponds to + * the version of the source code you want to build + * + * @example 'mybranch' + * @default the default branch's HEAD commit ID is used + */ + readonly branchOrRef?: string; } /** @@ -478,14 +487,6 @@ abstract class ThirdPartyGitSource extends GitSource { */ export interface CodeCommitSourceProps extends GitSourceProps { readonly repository: codecommit.IRepository; - - /** - * The commit ID, branch, or Git tag to use. - * - * @example 'mybranch' - * @default the default branch's HEAD commit ID is used - */ - readonly sourceVersion?: string; } /** @@ -494,12 +495,12 @@ export interface CodeCommitSourceProps extends GitSourceProps { class CodeCommitSource extends GitSource { public readonly type = CODECOMMIT_SOURCE_TYPE; private readonly repo: codecommit.IRepository; - private readonly sourceVersion?: string; + private readonly branchOrRef?: string; constructor(props: CodeCommitSourceProps) { super(props); this.repo = props.repository; - this.sourceVersion = props.sourceVersion; + this.branchOrRef = props.branchOrRef; } public bind(_scope: Construct, project: IProject): SourceConfig { @@ -515,7 +516,7 @@ class CodeCommitSource extends GitSource { ...superConfig.sourceProperty, location: this.repo.repositoryCloneUrlHttp, }, - sourceVersion: this.sourceVersion, + sourceVersion: this.branchOrRef, }; } } @@ -582,15 +583,6 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps { * @example 'aws-cdk' */ readonly repo: string; - - /** - * The commit ID, pull request ID, branch name, or tag name that corresponds to - * the version of the source code you want to build - * - * @example 'pr/25' - * @default the default branch's HEAD commit ID is used - */ - readonly sourceVersion?: string; } /** @@ -598,13 +590,13 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps { */ class GitHubSource extends ThirdPartyGitSource { public readonly type = GITHUB_SOURCE_TYPE; - private readonly sourceVersion?: string; private readonly httpsCloneUrl: string; + private readonly branchOrRef?: string; constructor(props: GitHubSourceProps) { super(props); this.httpsCloneUrl = `https://github.com/${props.owner}/${props.repo}.git`; - this.sourceVersion = props.sourceVersion; + this.branchOrRef = props.branchOrRef; } public bind(_scope: Construct, project: IProject): SourceConfig { @@ -614,7 +606,7 @@ class GitHubSource extends ThirdPartyGitSource { ...superConfig.sourceProperty, location: this.httpsCloneUrl, }, - sourceVersion: this.sourceVersion, + sourceVersion: this.branchOrRef, buildTriggers: superConfig.buildTriggers, }; } @@ -635,15 +627,6 @@ export interface GitHubEnterpriseSourceProps extends ThirdPartyGitSourceProps { * @default false */ readonly ignoreSslErrors?: boolean; - - /** - * The commit ID, pull request ID, branch name, or tag name that corresponds to - * the version of the source code you want to build - * - * @example 'pr/25' - * @default the default branch's HEAD commit ID is used - */ - readonly sourceVersion?: string; } /** @@ -653,13 +636,13 @@ class GitHubEnterpriseSource extends ThirdPartyGitSource { public readonly type = GITHUB_ENTERPRISE_SOURCE_TYPE; private readonly httpsCloneUrl: string; private readonly ignoreSslErrors?: boolean; - private readonly sourceVersion?: string; + private readonly branchOrRef?: string; constructor(props: GitHubEnterpriseSourceProps) { super(props); this.httpsCloneUrl = props.httpsCloneUrl; this.ignoreSslErrors = props.ignoreSslErrors; - this.sourceVersion = props.sourceVersion; + this.branchOrRef = props.branchOrRef; } public bind(_scope: Construct, _project: IProject): SourceConfig { @@ -670,7 +653,7 @@ class GitHubEnterpriseSource extends ThirdPartyGitSource { location: this.httpsCloneUrl, insecureSsl: this.ignoreSslErrors, }, - sourceVersion: this.sourceVersion, + sourceVersion: this.branchOrRef, buildTriggers: superConfig.buildTriggers, }; } @@ -693,14 +676,6 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps { * @example 'aws-cdk' */ readonly repo: string; - - /** - * The commit ID, branch name, or tag name that corresponds to the version of the source code you want to build - * - * @example 'mybranch' - * @default the default branch's HEAD commit ID is used - */ - readonly sourceVersion?: string; } /** @@ -709,12 +684,12 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps { class BitBucketSource extends ThirdPartyGitSource { public readonly type = BITBUCKET_SOURCE_TYPE; private readonly httpsCloneUrl: any; - private readonly sourceVersion?: string; + private readonly branchOrRef?: string; constructor(props: BitBucketSourceProps) { super(props); this.httpsCloneUrl = `https://bitbucket.org/${props.owner}/${props.repo}.git`; - this.sourceVersion = props.sourceVersion; + this.branchOrRef = props.branchOrRef; } public bind(_scope: Construct, _project: IProject): SourceConfig { @@ -734,7 +709,7 @@ class BitBucketSource extends ThirdPartyGitSource { ...superConfig.sourceProperty, location: this.httpsCloneUrl, }, - sourceVersion: this.sourceVersion, + sourceVersion: this.branchOrRef, buildTriggers: superConfig.buildTriggers, }; } diff --git a/packages/@aws-cdk/aws-codebuild/test/integ.project-secondary-sources-artifacts.expected.json b/packages/@aws-cdk/aws-codebuild/test/integ.project-secondary-sources-artifacts.expected.json index c5c3bd268720b..c57915ecb5ac1 100644 --- a/packages/@aws-cdk/aws-codebuild/test/integ.project-secondary-sources-artifacts.expected.json +++ b/packages/@aws-cdk/aws-codebuild/test/integ.project-secondary-sources-artifacts.expected.json @@ -209,11 +209,6 @@ "SourceIdentifier": "AddSource1", "Type": "S3" } - ], - "SecondarySourceVersions": [ - { - "SourceIdentifier": "AddSource1" - } ] } } diff --git a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts index ca8cf29197279..ab97a2aaa019f 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts @@ -923,11 +923,6 @@ export = { "SourceIdentifier": "source1", "Type": "S3", }, - ], - "SecondarySourceVersions": [ - { - "SourceIdentifier": "source1", - } ] })); diff --git a/packages/@aws-cdk/aws-codebuild/test/test.project.ts b/packages/@aws-cdk/aws-codebuild/test/test.project.ts index 98108af9d7f11..dbe4c078495a3 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.project.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.project.ts @@ -112,7 +112,7 @@ export = { source: codebuild.Source.gitHub({ owner: 'testowner', repo: 'testrepo', - sourceVersion: 'testbranch', + branchOrRef: 'testbranch', }) }); @@ -132,7 +132,7 @@ export = { new codebuild.Project(stack, 'Project', { source: codebuild.Source.gitHubEnterprise({ httpsCloneUrl: 'https://mygithub-enterprise.com/myuser/myrepo', - sourceVersion: 'testbranch', + branchOrRef: 'testbranch', }) }); @@ -216,7 +216,7 @@ export = { source: codebuild.Source.bitBucket({ owner: 'testowner', repo: 'testrepo', - sourceVersion: 'testbranch', + branchOrRef: 'testbranch', }) }); From eeb296b3e74cefae32f2d8b89f96485a6d86d972 Mon Sep 17 00:00:00 2001 From: Katie Normandin Date: Sun, 2 Feb 2020 11:18:08 -0500 Subject: [PATCH 5/6] update s3 name to version --- packages/@aws-cdk/aws-codebuild/lib/source.ts | 8 ++++---- packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts | 2 +- packages/@aws-cdk/aws-codebuild/test/test.project.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index 4c30184a88c18..60f2ae4c63d46 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -533,7 +533,7 @@ export interface S3SourceProps extends SourceProps { * * @default latest */ - readonly sourceVersion?: string; + readonly version?: string; } /** @@ -543,13 +543,13 @@ class S3Source extends Source { public readonly type = S3_SOURCE_TYPE; private readonly bucket: s3.IBucket; private readonly path: string; - private readonly sourceVersion?: string; + private readonly version?: string; constructor(props: S3SourceProps) { super(props); this.bucket = props.bucket; this.path = props.path; - this.sourceVersion = props.sourceVersion; + this.version = props.version; } public bind(_scope: Construct, project: IProject): SourceConfig { @@ -561,7 +561,7 @@ class S3Source extends Source { ...superConfig.sourceProperty, location: `${this.bucket.bucketName}/${this.path}`, }, - sourceVersion: this.sourceVersion, + sourceVersion: this.version, }; } } diff --git a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts index ab97a2aaa019f..c8c906bdf96f5 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts @@ -880,7 +880,7 @@ export = { bucket, path: 'another/path', identifier: 'source1', - sourceVersion: 'someversion' + version: 'someversion' })); expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', { diff --git a/packages/@aws-cdk/aws-codebuild/test/test.project.ts b/packages/@aws-cdk/aws-codebuild/test/test.project.ts index dbe4c078495a3..3d3a7061c4c92 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.project.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.project.ts @@ -273,7 +273,7 @@ export = { source: codebuild.Source.s3({ bucket: new s3.Bucket(stack, 'Bucket'), path: 'path', - sourceVersion: 's3version' + version: 's3version' }), cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER, codebuild.LocalCacheMode.SOURCE) From 8f5bebc834831fa9cb764cb8708c7d6ee702f64b Mon Sep 17 00:00:00 2001 From: Katie Normandin Date: Tue, 4 Feb 2020 10:07:17 -0500 Subject: [PATCH 6/6] remove duplication --- packages/@aws-cdk/aws-codebuild/lib/source.ts | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index 60f2ae4c63d46..f105b9fc38743 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -126,16 +126,19 @@ interface GitSourceProps extends SourceProps { */ abstract class GitSource extends Source { private readonly cloneDepth?: number; + private readonly branchOrRef?: string; protected constructor(props: GitSourceProps) { super(props); this.cloneDepth = props.cloneDepth; + this.branchOrRef = props.branchOrRef; } public bind(_scope: Construct, _project: IProject): SourceConfig { const superConfig = super.bind(_scope, _project); return { + sourceVersion: this.branchOrRef, sourceProperty: { ...superConfig.sourceProperty, gitCloneDepth: this.cloneDepth, @@ -474,6 +477,7 @@ abstract class ThirdPartyGitSource extends GitSource { ...superConfig.sourceProperty, reportBuildStatus: this.reportBuildStatus, }, + sourceVersion: superConfig.sourceVersion, buildTriggers: webhook === undefined ? undefined : { webhook, filterGroups: anyFilterGroupsProvided ? this.webhookFilters.map(fg => fg._toJson()) : undefined, @@ -495,12 +499,10 @@ export interface CodeCommitSourceProps extends GitSourceProps { class CodeCommitSource extends GitSource { public readonly type = CODECOMMIT_SOURCE_TYPE; private readonly repo: codecommit.IRepository; - private readonly branchOrRef?: string; constructor(props: CodeCommitSourceProps) { super(props); this.repo = props.repository; - this.branchOrRef = props.branchOrRef; } public bind(_scope: Construct, project: IProject): SourceConfig { @@ -516,7 +518,7 @@ class CodeCommitSource extends GitSource { ...superConfig.sourceProperty, location: this.repo.repositoryCloneUrlHttp, }, - sourceVersion: this.branchOrRef, + sourceVersion: superConfig.sourceVersion, }; } } @@ -591,12 +593,10 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps { class GitHubSource extends ThirdPartyGitSource { public readonly type = GITHUB_SOURCE_TYPE; private readonly httpsCloneUrl: string; - private readonly branchOrRef?: string; constructor(props: GitHubSourceProps) { super(props); this.httpsCloneUrl = `https://github.com/${props.owner}/${props.repo}.git`; - this.branchOrRef = props.branchOrRef; } public bind(_scope: Construct, project: IProject): SourceConfig { @@ -606,7 +606,7 @@ class GitHubSource extends ThirdPartyGitSource { ...superConfig.sourceProperty, location: this.httpsCloneUrl, }, - sourceVersion: this.branchOrRef, + sourceVersion: superConfig.sourceVersion, buildTriggers: superConfig.buildTriggers, }; } @@ -636,13 +636,11 @@ class GitHubEnterpriseSource extends ThirdPartyGitSource { public readonly type = GITHUB_ENTERPRISE_SOURCE_TYPE; private readonly httpsCloneUrl: string; private readonly ignoreSslErrors?: boolean; - private readonly branchOrRef?: string; constructor(props: GitHubEnterpriseSourceProps) { super(props); this.httpsCloneUrl = props.httpsCloneUrl; this.ignoreSslErrors = props.ignoreSslErrors; - this.branchOrRef = props.branchOrRef; } public bind(_scope: Construct, _project: IProject): SourceConfig { @@ -653,7 +651,7 @@ class GitHubEnterpriseSource extends ThirdPartyGitSource { location: this.httpsCloneUrl, insecureSsl: this.ignoreSslErrors, }, - sourceVersion: this.branchOrRef, + sourceVersion: superConfig.sourceVersion, buildTriggers: superConfig.buildTriggers, }; } @@ -684,12 +682,10 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps { class BitBucketSource extends ThirdPartyGitSource { public readonly type = BITBUCKET_SOURCE_TYPE; private readonly httpsCloneUrl: any; - private readonly branchOrRef?: string; constructor(props: BitBucketSourceProps) { super(props); this.httpsCloneUrl = `https://bitbucket.org/${props.owner}/${props.repo}.git`; - this.branchOrRef = props.branchOrRef; } public bind(_scope: Construct, _project: IProject): SourceConfig { @@ -709,7 +705,7 @@ class BitBucketSource extends ThirdPartyGitSource { ...superConfig.sourceProperty, location: this.httpsCloneUrl, }, - sourceVersion: this.branchOrRef, + sourceVersion: superConfig.sourceVersion, buildTriggers: superConfig.buildTriggers, }; }