Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codebuild): add ability to customize build status reporting for third-party Git sources #19408

Merged
merged 3 commits into from
Apr 14, 2022

Conversation

Lilja
Copy link
Contributor

@Lilja Lilja commented Mar 15, 2022

Documentation

The idea is to be able to customise the Github "check" through the context parameter I linked in the AWS doc.

#19403

@gitpod-io
Copy link

gitpod-io bot commented Mar 15, 2022

@github-actions github-actions bot added the @aws-cdk/aws-codebuild Related to AWS CodeBuild label Mar 15, 2022
Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @Lilja, but I don't love the current modeling of these capabilities.

Here are the docs of these properties:

        /**
         * Specifies the context of the build status CodeBuild sends to the source provider. The usage of this parameter depends on the source provider.
         *
         * - **Bitbucket** - This parameter is used for the `name` parameter in the Bitbucket commit status. For more information, see [build](https://docs.aws.amazon.com/https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/commit/%7Bnode%7D/statuses/build) in the Bitbucket API documentation.
         * - **GitHub/GitHub Enterprise Server** - This parameter is used for the `context` parameter in the GitHub commit status. For more information, see [Create a commit status](https://docs.aws.amazon.com/https://developer.github.com/v3/repos/statuses/#create-a-commit-status) in the GitHub developer guide.
         *
         * @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-codebuild-project-buildstatusconfig.html#cfn-codebuild-project-buildstatusconfig-context
         */
        readonly context?: string;
        /**
         * Specifies the target url of the build status CodeBuild sends to the source provider. The usage of this parameter depends on the source provider.
         *
         * - **Bitbucket** - This parameter is used for the `url` parameter in the Bitbucket commit status. For more information, see [build](https://docs.aws.amazon.com/https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/commit/%7Bnode%7D/statuses/build) in the Bitbucket API documentation.
         * - **GitHub/GitHub Enterprise Server** - This parameter is used for the `target_url` parameter in the GitHub commit status. For more information, see [Create a commit status](https://docs.aws.amazon.com/https://developer.github.com/v3/repos/statuses/#create-a-commit-status) in the GitHub developer guide.
         *
         * @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-codebuild-project-buildstatusconfig.html#cfn-codebuild-project-buildstatusconfig-targeturl
         */
        readonly targetUrl?: string;

So, what should happen is:

  1. The properties used for creating the BitBucketSource, BitBucketSourceProps, should get a new, optional property called name, of type string.
  2. The properties for creating the GitHubSource and GitHubEnterpriseSource will need to get a new, optional property, called context, of type string. (You might have to introduce a common superinterface to share between GitHubEnterpriseSourceProps and GitHubSourceProps.
  3. ThirdPartyGitSourceProps should get a new, optional property, called targetUrl, of type string.
  4. You fill the buildStatusConfig property of the SourceProperty interface that's returned from the bind() method of all of these Sources (through the sourceProperty property of the SourceConfig returned from bind()).

Let me know if this makes sense!

Thanks,
Adam

@skinny85
Copy link
Contributor

Maybe buildStatusName and buildStatusContext and buildStatusUrl are better names for the properties.

Feel free to propose better names, of course 🙂.

@mergify mergify bot dismissed skinny85’s stale review March 31, 2022 13:09

Pull request has been modified.

@Lilja
Copy link
Contributor Author

Lilja commented Mar 31, 2022

Hey @skinny85. Thanks for the feedback. I've tried to adress it in my latest commit.

  • I've added GithubContextProps that is a new interface between github and github enterprise.
  • The buildStatusConfig prop is created in ThirdPartyGitSource, I didn't exactly understand how you would construct it in the SourceProvider since it doesn't have any super config of the more concrete Sources, so it doesn't really know the context/targetUrl.
  • I don't really know how to do the defaults here. It's probably not a good idea to require these to be sent in the props of the concrete github/bitbucket sources, so buildStatusName: string should probably be buildStatusName?: string. The problem with this is that there is some tooling in this library that requires @default documentation of something that is optional. I have no clue what the actual default value is. You can see it in this PR, as name is AWS CodeBuild us-east-1 (AutoBuildProject89A8053A-LhjRyN9kxr8o). So it would seem like the default of context would be something like AWS Codebuild $AWS_REGION ($SOME_SORT_OF_ID), but I'm only guessing here. Should we really write a default here when in reality, not supplying buildStatusConfig makes the CodeBuild API pick some very opaque default.

Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're getting there, thanks for all of your work on this @Lilja!

I hope my review answers all of your questions, if something is still unclear, let me know!

Comment on lines 503 to 509
/**
* This parameter is used for the `url`/`target_url` parameter in the Bitbucket/GitHub commit status.
* BitBucket: `url`
* Github: `target_url`
*
* @default ???????????
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs are misaligned here:

Suggested change
/**
* This parameter is used for the `url`/`target_url` parameter in the Bitbucket/GitHub commit status.
* BitBucket: `url`
* Github: `target_url`
*
* @default ???????????
*/
/**
* This parameter is used for the `url`/`target_url` parameter in the Bitbucket/GitHub commit status.
* BitBucket: `url`
* Github: `target_url`
*
* @default ???????????
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to talk about what exact property this fills for each type. It should be obvious from the name.

Let's change the docs to say something like "The URL that the build will report back to the source provider. Can use built-in CodeBuild variables, like $AWS_REGION". Something like that.

Let's also add an @example, and make @default say something like "- link to the AWS Console for CodeBuild to a particular build execution".

/**
* wip
*/
export interface GithubContextProps extends ThirdPartyGitSourceProps {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name isn't the best long-term I'm fairly sure. Let's change it to CommonGitHubSourceProps.

Also, let's not make it exported (let's keep it module-private).

Comment on lines 661 to 655
/**
* wip
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you make this interface not exported anymore, you'll be able to remove these JSDocs.

Comment on lines 551 to 560
const buildStatusConfig = {
targetUrl: this.buildStatusUrl
}
if (typeof superConfig["buildStatusContext"] !== "undefined") {
// Github/GitHubEnterprise
buildStatusConfig["context"] = superConfig["buildStatusContext"]
} else if (typeof superConfig["buildStatusName"] !== "undefined") {
// BitBucket
buildStatusConfig["context"] = superConfig["buildStatusName"]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we need to handle buildStatusConfig separately in the two subclasses anyway, let's just remove all of this code.


return {
sourceProperty: {
...superConfig.sourceProperty,
reportBuildStatus: this.reportBuildStatus,
buildStatusConfig,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this too (will be handled by the subclasses).

/**
* This parameter is used for the `context` parameter in the GitHub commit status.
*
* @example 'My build #$CODEBUILD_BUILD_NUMBER'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use double quotes for the example, renders better in other languages.

Comment on lines 766 to 769
buildStatusConfig: {
context: this.buildStatusContext,
targetUrl: this.buildStatusUrl,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we need to introduce a new, module-private superclass here, CommonGitHubSource, that deals with the buildStatusConfig property, so that this code is not duplicated between GitHubEnterpriseSource and GitHubSource.

Comment on lines 813 to 836
/**
* This parameter is used for the `name` parameter in the Bitbucket commit status.
*
* @example 'My build #$CODEBUILD_BUILD_NUMBER'
*/
Copy link
Contributor

@skinny85 skinny85 Mar 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, let's brush up on the docs here: "The name that the build will report back to the BitBucket source provider. Can use built-in CodeBuild variables, like $AWS_REGION". Something like that.

Also, double quotes for @example.

And @default "AWS CodeBuild $AWS_REGION ($PROJECT_NAME)".

@@ -776,6 +824,7 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps {
class BitBucketSource extends ThirdPartyGitSource {
public readonly type = BITBUCKET_SOURCE_TYPE;
private readonly httpsCloneUrl: any;
private readonly buildStatusName: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to assign this field in the constructor.

Then, in bind(), do:

    return {
      sourceProperty: {
        ...superConfig.sourceProperty,
        location: this.httpsCloneUrl,
        buildStatusConfig: {
          context: this.buildStatusName,
          targetUrl: this.buildStatusUrl,
        },
      },
      sourceVersion: superConfig.sourceVersion,
      buildTriggers: superConfig.buildTriggers,
    };

packages/@aws-cdk/aws-codebuild/lib/source.ts Show resolved Hide resolved
@mergify mergify bot dismissed skinny85’s stale review April 5, 2022 13:19

Pull request has been modified.

Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @Lilja, but you need to fix the formatting to get the PR build to succeed, so we can merge this in!

@@ -499,6 +499,14 @@ interface ThirdPartyGitSourceProps extends GitSourceProps {
* @default every push and every Pull Request (create or update) triggers a build
*/
readonly webhookFilters?: FilterGroup[];

/**
* This parameter is used for the `url`/`target_url` parameter in the Bitbucket/GitHub commit status.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again - I don't think we need to specify exactly which parameter this fills. I think it's obvious from the name.

Suggested change
* This parameter is used for the `url`/`target_url` parameter in the Bitbucket/GitHub commit status.
* The URL that the build will report back to the source provider.
* Can use built-in CodeBuild variables, like $AWS_REGION.

/**
* This parameter is used for the `url`/`target_url` parameter in the Bitbucket/GitHub commit status.
*
* @example "The URL that the build will report back to the source provider. Can use built-in CodeBuild variables, like $AWS_REGION"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very weird example 😃.

Suggested change
* @example "The URL that the build will report back to the source provider. Can use built-in CodeBuild variables, like $AWS_REGION"
* @example "$CODEBUILD_PUBLIC_BUILD_URL"

* This parameter is used for the `url`/`target_url` parameter in the Bitbucket/GitHub commit status.
*
* @example "The URL that the build will report back to the source provider. Can use built-in CodeBuild variables, like $AWS_REGION"
* @default "link to the AWS Console for CodeBuild to a particular build execution"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the quotes here:

Suggested change
* @default "link to the AWS Console for CodeBuild to a particular build execution"
* @default - link to the AWS Console for CodeBuild to a particular build execution

packages/@aws-cdk/aws-codebuild/lib/source.ts Show resolved Hide resolved
packages/@aws-cdk/aws-codebuild/lib/source.ts Show resolved Hide resolved
Comment on lines 1076 to 1088
new codebuild.Project(stack, 'MyProject', {
source,
})
Template.fromStack(stack).hasResourceProperties(
'AWS::CodeBuild::Project', {
'Source': {
'buildStatusConfig': {
'context': context,
}
}
}
)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments here (inline source, wrong formatting).

Comment on lines 1102 to 1114
new codebuild.Project(stack, 'MyProject', {
source,
})
Template.fromStack(stack).hasResourceProperties(
'AWS::CodeBuild::Project', {
'Source': {
'buildStatusConfig': {
'context': context,
'targetUrl': targetUrl,
}
}
}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments here (inline source, wrong formatting).

})

describe('sources with customised build status configuration', () => {
test('github with targetUrl', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('github with targetUrl', () => {
test('GitHub with targetUrl', () => {

'AWS::CodeBuild::Project', {
'Source': {
'buildStatusConfig': {
'context': context,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this test checks context too, can you actually remove the first test, and just leave this one? The first test is redundant currently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, my thought was to have a unit test only having either one of those parameters. The tests above only uses context and name, so my thought was to create a unit test with only targetUrl.

Comment on lines 1115 to 1116
})
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semiicolons:

Suggested change
})
})
});
});

@skinny85 skinny85 changed the title feat(aws-codebuild): Add buildStatusConfig to ThirdPartySourceProps feat(codebuild): add ability to customize build status reporting for third-party Git sources Apr 5, 2022
@github-actions github-actions bot added the p2 label Apr 5, 2022
@skinny85 skinny85 added pr-linter/exempt-readme The PR linter will not require README changes pr-linter/exempt-integ-test The PR linter will not require integ test changes labels Apr 5, 2022
@mergify mergify bot dismissed skinny85’s stale review April 7, 2022 12:12

Pull request has been modified.

Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're almost there!

@@ -499,6 +499,17 @@ interface ThirdPartyGitSourceProps extends GitSourceProps {
* @default every push and every Pull Request (create or update) triggers a build
*/
readonly webhookFilters?: FilterGroup[];

/**
* The URL that the build will report back to the source provider. Can use built-in CodeBuild variables, like $AWS_REGION
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-line, and full sentences, please:

Suggested change
* The URL that the build will report back to the source provider. Can use built-in CodeBuild variables, like $AWS_REGION
* The URL that the build will report back to the source provider.
* Can use built-in CodeBuild variables, like $AWS_REGION.

Comment on lines 681 to 684
buildStatusConfig: {
context: this.buildStatusContext,
targetUrl: this.buildStatusUrl,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to render this property only if at least one of this.buildStatusContext or this.buildStatusUrl is defined:

Suggested change
buildStatusConfig: {
context: this.buildStatusContext,
targetUrl: this.buildStatusUrl,
},
buildStatusConfig: this.buildStatusContext !== undefined || this.buildStatusUrl !== undefined
? {
context: this.buildStatusContext,
targetUrl: this.buildStatusUrl,
}
: undefined,

This is why the integration test is failing in the build right now:

@aws-cdk/aws-codebuild: Verifying integ.github-webhook-batch.js against integ.github-webhook-batch.expected.json ... CHANGED.
@aws-cdk/aws-codebuild: Resources
@aws-cdk/aws-codebuild: [~] AWS::CodeBuild::Project MyProject39F7B0AE 
@aws-cdk/aws-codebuild:  └─ [~] Source
@aws-cdk/aws-codebuild:      └─ [+] Added: .BuildStatusConfig
@aws-cdk/aws-codebuild: Verifying integ.github.js against integ.github.expected.json ... CHANGED.
@aws-cdk/aws-codebuild: Resources
@aws-cdk/aws-codebuild: [~] AWS::CodeBuild::Project MyProject39F7B0AE 
@aws-cdk/aws-codebuild:  └─ [~] Source
@aws-cdk/aws-codebuild:      └─ [+] Added: .BuildStatusConfig

Comment on lines 862 to 865
buildStatusConfig: {
context: this.buildStatusName,
targetUrl: this.buildStatusUrl,
},
Copy link
Contributor

@skinny85 skinny85 Apr 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - we need to render this property only if at least one of this.buildStatusName or this.buildStatusUrl is defined:

Suggested change
buildStatusConfig: {
context: this.buildStatusName,
targetUrl: this.buildStatusUrl,
},
buildStatusConfig: this.buildStatusName !== undefined || this.buildStatusUrl !== undefined
? {
context: this.buildStatusName,
targetUrl: this.buildStatusUrl,
}
: undefined,

@mergify mergify bot dismissed skinny85’s stale review April 13, 2022 17:37

Pull request has been modified.

@Lilja
Copy link
Contributor Author

Lilja commented Apr 13, 2022

Thanks for being so patient with me @skinny85!

Fixed compliance with earlier test cases and rebased the branch. Now, there's only the remaining test case of testing the target_url. Is it interesting or should we remove it completely?

Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last 3 cosmetic changes, and this will be merged in!

Fixed compliance with earlier test cases and rebased the branch. Now, there's only the remaining test case of testing the target_url. Is it interesting or should we remove it completely?

Nah, we're good. Leave it as-is.

Thanks for being so patient with me @skinny85!

My pleasure, thanks for all of your hard work in contributing this feature!

packages/@aws-cdk/aws-codebuild/lib/source.ts Show resolved Hide resolved
packages/@aws-cdk/aws-codebuild/lib/source.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-codebuild/lib/source.ts Show resolved Hide resolved
Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @Lilja!

@mergify
Copy link
Contributor

mergify bot commented Apr 14, 2022

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: fe25f02
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 423d72f into aws:master Apr 14, 2022
@mergify
Copy link
Contributor

mergify bot commented Apr 14, 2022

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

StevePotter pushed a commit to StevePotter/aws-cdk that referenced this pull request Apr 27, 2022
…third-party Git sources (aws#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.

aws#19403
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-codebuild Related to AWS CodeBuild p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes pr-linter/exempt-readme The PR linter will not require README changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants