Skip to content

Commit

Permalink
fix(aws-codebuild): allow passing oauth token to GitHubEnterpriseSour…
Browse files Browse the repository at this point in the history
…ce (#908)
  • Loading branch information
eaddingtonwhite authored and Elad Ben-Israel committed Oct 12, 2018
1 parent 8b3ae43 commit c23da91
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
33 changes: 26 additions & 7 deletions packages/@aws-cdk/aws-codebuild/lib/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,57 @@ export class CodePipelineSource extends BuildSource {
}
}

export interface GithubSourceProps {
/**
* The git url to clone for this code build project.
*/
cloneUrl: string;

/**
* The oAuthToken used to authenticate when cloning source git repo.
*/
oauthToken: cdk.Secret;

}

/**
* GitHub Source definition for a CodeBuild project
*/
export class GitHubSource extends BuildSource {
constructor(private readonly httpscloneUrl: string, private readonly oauthToken: any) {
private cloneUrl: string;
private oauthToken: cdk.Secret;
constructor(props: GithubSourceProps) {
super();
this.httpscloneUrl = httpscloneUrl;
this.oauthToken = oauthToken;
this.cloneUrl = props.cloneUrl;
this.oauthToken = props.oauthToken;
}

public toSourceJSON(): cloudformation.ProjectResource.SourceProperty {
return {
type: SourceType.GitHub,
auth: this.oauthToken != null ? { type: 'OAUTH', resource: this.oauthToken } : undefined,
location: this.httpscloneUrl
location: this.cloneUrl
};
}
}

/**
* GitHub Enterprice Source definition for a CodeBuild project
* GitHub Enterprise Source definition for a CodeBuild project
*/
export class GitHubEnterpriseSource extends BuildSource {
constructor(private readonly cloneUrl: string) {
private cloneUrl: string;
private oauthToken: cdk.Secret;
constructor(props: GithubSourceProps) {
super();
this.cloneUrl = cloneUrl;
this.cloneUrl = props.cloneUrl;
this.oauthToken = props.oauthToken;
}

public toSourceJSON(): cloudformation.ProjectResource.SourceProperty {
return {
type: SourceType.GitHubEnterPrise,
location: this.cloneUrl,
auth: this.oauthToken != null ? { type: 'OAUTH', resource: this.oauthToken } : undefined,
};
}
}
Expand Down
66 changes: 61 additions & 5 deletions packages/@aws-cdk/aws-codebuild/test/test.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export = {
// WHEN
new codebuild.Project(stack, 'Project', {
source: new codebuild.CodePipelineSource(),
buildSpec: { phases: [ 'say hi' ] }
buildSpec: { phases: ['say hi'] }
});

// THEN
Expand All @@ -47,6 +47,60 @@ export = {
test.done();
},

'github 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",
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'
}
}));

test.done();
},

'github enterprise 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")
})
});

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

test.done();
},

'construct from asset'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand All @@ -70,10 +124,12 @@ export = {
{
Name: "SCRIPT_S3_KEY",
Type: "PLAINTEXT",
Value: { "Fn::Join": [ "", [
{ "Fn::Select": [ 0, { "Fn::Split": [ "||", { Ref: "AssetS3VersionKeyA852DDAE" } ] } ] },
{ "Fn::Select": [ 1, { "Fn::Split": [ "||", { Ref: "AssetS3VersionKeyA852DDAE" } ] } ] }
] ] }
Value: {
"Fn::Join": ["", [
{ "Fn::Select": [0, { "Fn::Split": ["||", { Ref: "AssetS3VersionKeyA852DDAE" }] }] },
{ "Fn::Select": [1, { "Fn::Split": ["||", { Ref: "AssetS3VersionKeyA852DDAE" }] }] }
]]
}
}
],
},
Expand Down

0 comments on commit c23da91

Please sign in to comment.