Skip to content

Commit 423d72f

Browse files
authored
feat(codebuild): add ability to customize build status reporting for third-party Git sources (#19408)
[Documentation](https://docs.aws.amazon.com/codebuild/latest/userguide/create-project-cli.html#cli.source.buildstatusconfig.context) The idea is to be able to customise the Github "check" through the context parameter I linked in the AWS doc. #19403
1 parent 4f3a340 commit 423d72f

File tree

2 files changed

+153
-4
lines changed

2 files changed

+153
-4
lines changed

packages/@aws-cdk/aws-codebuild/lib/source.ts

+81-4
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,18 @@ interface ThirdPartyGitSourceProps extends GitSourceProps {
499499
* @default every push and every Pull Request (create or update) triggers a build
500500
*/
501501
readonly webhookFilters?: FilterGroup[];
502+
503+
/**
504+
* The URL that the build will report back to the source provider.
505+
* Can use built-in CodeBuild variables, like $AWS_REGION.
506+
*
507+
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/create-project-cli.html#cli.source.buildstatusconfig.targeturl
508+
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
509+
*
510+
* @example "$CODEBUILD_PUBLIC_BUILD_URL"
511+
* @default - link to the AWS Console for CodeBuild to a particular build execution
512+
*/
513+
readonly buildStatusUrl?: string;
502514
}
503515

504516
/**
@@ -510,6 +522,7 @@ abstract class ThirdPartyGitSource extends GitSource {
510522
private readonly reportBuildStatus: boolean;
511523
private readonly webhook?: boolean;
512524
private readonly webhookTriggersBatchBuild?: boolean;
525+
protected readonly buildStatusUrl?: string;
513526

514527
protected constructor(props: ThirdPartyGitSourceProps) {
515528
super(props);
@@ -518,6 +531,7 @@ abstract class ThirdPartyGitSource extends GitSource {
518531
this.reportBuildStatus = props.reportBuildStatus ?? true;
519532
this.webhookFilters = props.webhookFilters || [];
520533
this.webhookTriggersBatchBuild = props.webhookTriggersBatchBuild;
534+
this.buildStatusUrl = props.buildStatusUrl;
521535
}
522536

523537
public bind(_scope: CoreConstruct, project: IProject): SourceConfig {
@@ -636,10 +650,53 @@ class S3Source extends Source {
636650
}
637651
}
638652

653+
/**
654+
* Common properties between {@link GitHubSource} and {@link GitHubEnterpriseSource}.
655+
*/
656+
interface CommonGithubSourceProps extends ThirdPartyGitSourceProps {
657+
/**
658+
* This parameter is used for the `context` parameter in the GitHub commit status.
659+
* Can use built-in CodeBuild variables, like $AWS_REGION.
660+
*
661+
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/create-project-cli.html#cli.source.buildstatusconfig.context
662+
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
663+
*
664+
* @example "My build #$CODEBUILD_BUILD_NUMBER"
665+
* @default "AWS CodeBuild $AWS_REGION ($PROJECT_NAME)"
666+
*/
667+
readonly buildStatusContext?: string
668+
}
669+
670+
abstract class CommonGithubSource extends ThirdPartyGitSource {
671+
private readonly buildStatusContext?: string;
672+
673+
constructor(props: CommonGithubSourceProps) {
674+
super(props);
675+
this.buildStatusContext = props.buildStatusContext;
676+
}
677+
678+
public bind(scope: CoreConstruct, project: IProject): SourceConfig {
679+
const superConfig = super.bind(scope, project);
680+
return {
681+
sourceProperty: {
682+
...superConfig.sourceProperty,
683+
buildStatusConfig: this.buildStatusContext !== undefined || this.buildStatusUrl !== undefined
684+
? {
685+
context: this.buildStatusContext,
686+
targetUrl: this.buildStatusUrl,
687+
}
688+
: undefined,
689+
},
690+
sourceVersion: superConfig.sourceVersion,
691+
buildTriggers: superConfig.buildTriggers,
692+
};
693+
}
694+
}
695+
639696
/**
640697
* Construction properties for {@link GitHubSource} and {@link GitHubEnterpriseSource}.
641698
*/
642-
export interface GitHubSourceProps extends ThirdPartyGitSourceProps {
699+
export interface GitHubSourceProps extends CommonGithubSourceProps {
643700
/**
644701
* The GitHub account/user that owns the repo.
645702
*
@@ -658,7 +715,7 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps {
658715
/**
659716
* GitHub Source definition for a CodeBuild project.
660717
*/
661-
class GitHubSource extends ThirdPartyGitSource {
718+
class GitHubSource extends CommonGithubSource {
662719
public readonly type = GITHUB_SOURCE_TYPE;
663720
private readonly httpsCloneUrl: string;
664721

@@ -683,7 +740,7 @@ class GitHubSource extends ThirdPartyGitSource {
683740
/**
684741
* Construction properties for {@link GitHubEnterpriseSource}.
685742
*/
686-
export interface GitHubEnterpriseSourceProps extends ThirdPartyGitSourceProps {
743+
export interface GitHubEnterpriseSourceProps extends CommonGithubSourceProps {
687744
/**
688745
* The HTTPS URL of the repository in your GitHub Enterprise installation.
689746
*/
@@ -700,7 +757,7 @@ export interface GitHubEnterpriseSourceProps extends ThirdPartyGitSourceProps {
700757
/**
701758
* GitHub Enterprise Source definition for a CodeBuild project.
702759
*/
703-
class GitHubEnterpriseSource extends ThirdPartyGitSource {
760+
class GitHubEnterpriseSource extends CommonGithubSource {
704761
public readonly type = GITHUB_ENTERPRISE_SOURCE_TYPE;
705762
private readonly httpsCloneUrl: string;
706763
private readonly ignoreSslErrors?: boolean;
@@ -768,6 +825,18 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps {
768825
* @example 'aws-cdk'
769826
*/
770827
readonly repo: string;
828+
829+
/**
830+
* This parameter is used for the `name` parameter in the Bitbucket commit status.
831+
* Can use built-in CodeBuild variables, like $AWS_REGION.
832+
*
833+
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/create-project-cli.html#cli.source.buildstatusconfig.context
834+
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
835+
*
836+
* @example "My build #$CODEBUILD_BUILD_NUMBER"
837+
* @default "AWS CodeBuild $AWS_REGION ($PROJECT_NAME)"
838+
*/
839+
readonly buildStatusName?: string;
771840
}
772841

773842
/**
@@ -776,10 +845,12 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps {
776845
class BitBucketSource extends ThirdPartyGitSource {
777846
public readonly type = BITBUCKET_SOURCE_TYPE;
778847
private readonly httpsCloneUrl: any;
848+
private readonly buildStatusName?: string;
779849

780850
constructor(props: BitBucketSourceProps) {
781851
super(props);
782852
this.httpsCloneUrl = `https://bitbucket.org/${props.owner}/${props.repo}.git`;
853+
this.buildStatusName = props.buildStatusName;
783854
}
784855

785856
public bind(_scope: CoreConstruct, _project: IProject): SourceConfig {
@@ -793,6 +864,12 @@ class BitBucketSource extends ThirdPartyGitSource {
793864
sourceProperty: {
794865
...superConfig.sourceProperty,
795866
location: this.httpsCloneUrl,
867+
buildStatusConfig: this.buildStatusName !== undefined || this.buildStatusUrl !== undefined
868+
? {
869+
context: this.buildStatusName,
870+
targetUrl: this.buildStatusUrl,
871+
}
872+
: undefined,
796873
},
797874
sourceVersion: superConfig.sourceVersion,
798875
buildTriggers: superConfig.buildTriggers,

packages/@aws-cdk/aws-codebuild/test/codebuild.test.ts

+72
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,78 @@ describe('secondary sources', () => {
10281028
});
10291029
});
10301030

1031+
describe('sources with customised build status configuration', () => {
1032+
test('GitHub', () => {
1033+
const context = 'My custom CodeBuild worker!';
1034+
const stack = new cdk.Stack();
1035+
const source = codebuild.Source.gitHub({
1036+
owner: 'awslabs',
1037+
repo: 'aws-cdk',
1038+
buildStatusContext: context,
1039+
});
1040+
1041+
new codebuild.Project(stack, 'MyProject', { source });
1042+
Template.fromStack(stack).findParameters('AWS::CodeBuild::Project', {
1043+
Source: {
1044+
buildStatusConfig: {
1045+
context: context,
1046+
},
1047+
},
1048+
});
1049+
});
1050+
1051+
test('GitHub Enterprise', () => {
1052+
const context = 'My custom CodeBuild worker!';
1053+
const stack = new cdk.Stack();
1054+
const source = codebuild.Source.gitHubEnterprise({
1055+
httpsCloneUrl: 'url',
1056+
buildStatusContext: context,
1057+
});
1058+
new codebuild.Project(stack, 'MyProject', { source });
1059+
Template.fromStack(stack).findParameters('AWS::CodeBuild::Project', {
1060+
Source: {
1061+
buildStatusConfig: {
1062+
context: context,
1063+
},
1064+
},
1065+
});
1066+
});
1067+
1068+
test('BitBucket', () => {
1069+
const context = 'My custom CodeBuild worker!';
1070+
const stack = new cdk.Stack();
1071+
const source = codebuild.Source.bitBucket({ owner: 'awslabs', repo: 'aws-cdk' });
1072+
new codebuild.Project(stack, 'MyProject', { source });
1073+
Template.fromStack(stack).findParameters('AWS::CodeBuild::Project', {
1074+
Source: {
1075+
buildStatusConfig: {
1076+
context: context,
1077+
},
1078+
},
1079+
});
1080+
});
1081+
});
1082+
1083+
describe('sources with customised build status configuration', () => {
1084+
test('GitHub with targetUrl', () => {
1085+
const targetUrl = 'https://example.com';
1086+
const stack = new cdk.Stack();
1087+
const source = codebuild.Source.gitHub({
1088+
owner: 'awslabs',
1089+
repo: 'aws-cdk',
1090+
buildStatusUrl: targetUrl,
1091+
});
1092+
new codebuild.Project(stack, 'MyProject', { source });
1093+
Template.fromStack(stack).findParameters('AWS::CodeBuild::Project', {
1094+
Source: {
1095+
buildStatusConfig: {
1096+
targetUrl: targetUrl,
1097+
},
1098+
},
1099+
});
1100+
});
1101+
});
1102+
10311103
describe('secondary source versions', () => {
10321104
test('allow secondary source versions', () => {
10331105
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)