Skip to content

Commit

Permalink
feat(aws-codebuild): change the API of GitHub and BitBucket Sources.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this changes the API of CodeBuild's GitHub and BitBucket Sources
to take an owner/repo pair instead of an entire cloneUrl,
to make it consistent with the GitHubSourceAction in the CodePipeline package.

Also adds handling the reportBuildStatus and insecureSsl Source properties.
  • Loading branch information
skinny85 committed Dec 13, 2018
1 parent 48b9bdd commit fdcba49
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 27 deletions.
80 changes: 69 additions & 11 deletions packages/@aws-cdk/aws-codebuild/lib/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,56 +151,102 @@ 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;
}

/**
* GitHub Source definition for a CodeBuild project.
*/
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,
};
}
}
Expand All @@ -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;
}

/**
Expand All @@ -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 {
Expand Down
85 changes: 69 additions & 16 deletions packages/@aws-cdk/aws-codebuild/test/test.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,55 +47,108 @@ 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")
})
});

// 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();
Expand Down

0 comments on commit fdcba49

Please sign in to comment.