diff --git a/packages/@aws-cdk/aws-codebuild/lib/source.ts b/packages/@aws-cdk/aws-codebuild/lib/source.ts index 181767b46a2c9..48ae0ba3f39f1 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/source.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/source.ts @@ -151,14 +151,32 @@ export class CodePipelineSource extends BuildSource { */ export interface GitHubSourceProps extends BuildSourceProps { /** - * The git url to clone for this code build project. + * The GitHub account/user that owns the repo. + * + * @example 'awslabs' */ - cloneUrl: string; + owner: string; + + /** + * The name of the repo (without the username). + * + * @example 'aws-cdk' + */ + repo: string; /** * The oAuthToken used to authenticate when cloning source git repo. + * Note that you need to give CodeBuild permissions to your GitHub account in order for the token to work. + * That is a one-time operation that can be done through the AWS Console for CodeBuild. */ oauthToken: cdk.Secret; + + /** + * Whether to send GitHub notifications on your build's start and end. + * + * @default true + */ + reportBuildStatus?: boolean; } /** @@ -166,41 +184,69 @@ export interface GitHubSourceProps extends BuildSourceProps { */ export class GitHubSource extends BuildSource { public readonly type: SourceType = SourceType.GitHub; - private readonly cloneUrl: string; + private readonly httpsCloneUrl: string; private readonly oauthToken: cdk.Secret; + private readonly reportBuildStatus: boolean; constructor(props: GitHubSourceProps) { super(props); - this.cloneUrl = props.cloneUrl; + this.httpsCloneUrl = `https://github.com/${props.owner}/${props.repo}.git`; this.oauthToken = props.oauthToken; + this.reportBuildStatus = props.reportBuildStatus === undefined ? true : props.reportBuildStatus; } protected toSourceProperty(): any { return { auth: { type: 'OAUTH', resource: this.oauthToken }, - location: this.cloneUrl, + location: this.httpsCloneUrl, + reportBuildStatus: this.reportBuildStatus, }; } } +/** + * Construction properties for {@link GitHubEnterpriseSource}. + */ +export interface GitHubEnterpriseSourceProps extends BuildSourceProps { + /** + * The HTTPS URL of the repository in your GitHub Enterprise installation. + */ + httpsCloneUrl: string; + + /** + * The OAuth token used to authenticate when cloning the git repository. + */ + oauthToken: cdk.Secret; + + /** + * Whether to ignore SSL errors when connecting to the repository. + * + * @default false + */ + ignoreSslErrors?: boolean; +} + /** * GitHub Enterprise Source definition for a CodeBuild project. */ export class GitHubEnterpriseSource extends BuildSource { public readonly type: SourceType = SourceType.GitHubEnterPrise; - private readonly cloneUrl: string; + private readonly httpsCloneUrl: string; private readonly oauthToken: cdk.Secret; + private readonly ignoreSslErrors?: boolean; - constructor(props: GitHubSourceProps) { + constructor(props: GitHubEnterpriseSourceProps) { super(props); - this.cloneUrl = props.cloneUrl; + this.httpsCloneUrl = props.httpsCloneUrl; this.oauthToken = props.oauthToken; + this.ignoreSslErrors = props.ignoreSslErrors; } protected toSourceProperty(): any { return { auth: { type: 'OAUTH', resource: this.oauthToken }, - location: this.cloneUrl, + location: this.httpsCloneUrl, + insecureSsl: this.ignoreSslErrors, }; } } @@ -209,7 +255,19 @@ export class GitHubEnterpriseSource extends BuildSource { * Construction properties for {@link BitBucketSource}. */ export interface BitBucketSourceProps extends BuildSourceProps { - httpsCloneUrl: string; + /** + * The BitBucket account/user that owns the repo. + * + * @example 'awslabs' + */ + owner: string; + + /** + * The name of the repo (without the username). + * + * @example 'aws-cdk' + */ + repo: string; } /** @@ -221,7 +279,7 @@ export class BitBucketSource extends BuildSource { constructor(props: BitBucketSourceProps) { super(props); - this.httpsCloneUrl = props.httpsCloneUrl; + this.httpsCloneUrl = `https://bitbucket.org/${props.owner}/${props.repo}.git`; } protected toSourceProperty(): any { diff --git a/packages/@aws-cdk/aws-codebuild/test/test.project.ts b/packages/@aws-cdk/aws-codebuild/test/test.project.ts index 15bb7743daa4c..160783a8dc9d0 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.project.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.project.ts @@ -47,14 +47,70 @@ export = { test.done(); }, - 'github auth test'(test: Test) { + 'GitHub source': { + 'has reportBuildStatus on by default'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new codebuild.Project(stack, 'Project', { + source: new codebuild.GitHubSource({ + owner: 'testowner', + repo: 'testrepo', + oauthToken: new cdk.Secret("test_oauth_token") + }) + }); + + // THEN + expect(stack).to(haveResource('AWS::CodeBuild::Project', { + Source: { + Type: "GITHUB", + Auth: { + Type: 'OAUTH', + Resource: 'test_oauth_token' + }, + Location: 'https://github.com/testowner/testrepo.git', + ReportBuildStatus: true, + } + })); + + test.done(); + }, + + 'can explicitly set reportBuildStatus to false'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new codebuild.Project(stack, 'Project', { + source: new codebuild.GitHubSource({ + owner: 'testowner', + repo: 'testrepo', + oauthToken: new cdk.Secret('test_oauth_token'), + reportBuildStatus: false, + }) + }); + + // THEN + expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', { + Source: { + ReportBuildStatus: false, + }, + })); + + test.done(); + }, + }, + + 'github enterprise auth test'(test: Test) { // GIVEN const stack = new cdk.Stack(); // WHEN new codebuild.Project(stack, 'Project', { - source: new codebuild.GitHubSource({ - cloneUrl: "https://github.com/testowner/testrepo", + source: new codebuild.GitHubEnterpriseSource({ + httpsCloneUrl: 'https://github.testcompany.com/testowner/testrepo', + ignoreSslErrors: true, oauthToken: new cdk.Secret("test_oauth_token") }) }); @@ -62,40 +118,37 @@ export = { // THEN expect(stack).to(haveResource('AWS::CodeBuild::Project', { Source: { - Type: "GITHUB", + Type: "GITHUB_ENTERPRISE", Auth: { Type: 'OAUTH', Resource: 'test_oauth_token' }, - Location: 'https://github.com/testowner/testrepo' + InsecureSsl: true, + Location: 'https://github.testcompany.com/testowner/testrepo' } })); test.done(); }, - 'github enterprise auth test'(test: Test) { + 'bitbucket auth test'(test: Test) { // GIVEN const stack = new cdk.Stack(); // WHEN new codebuild.Project(stack, 'Project', { - source: new codebuild.GitHubEnterpriseSource({ - cloneUrl: "https://github.testcompany.com/testowner/testrepo", - oauthToken: new cdk.Secret("test_oauth_token") + source: new codebuild.BitBucketSource({ + owner: 'testowner', + repo: 'testrepo', }) }); // THEN expect(stack).to(haveResource('AWS::CodeBuild::Project', { Source: { - Type: "GITHUB_ENTERPRISE", - Auth: { - Type: 'OAUTH', - Resource: 'test_oauth_token' - }, - Location: 'https://github.testcompany.com/testowner/testrepo' - } + Type: 'BITBUCKET', + Location: 'https://bitbucket.org/testowner/testrepo.git', + }, })); test.done();