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

strip content from pull request body on merge #550

Closed
clxmstaab opened this issue Nov 3, 2020 · 13 comments
Closed

strip content from pull request body on merge #550

clxmstaab opened this issue Nov 3, 2020 · 13 comments
Labels
enhancement New feature or request

Comments

@clxmstaab
Copy link
Contributor

we are using https://pullrequestbadge.com/ which adds pull request badges into our PR descriptions based on keywords in the PR title (e.g. a JIRA Issue key)

example how this looks like:

image

the badges get inserted by a github marketplace app automatically.

since we use the PR-body as a commit message, commits contains this ugly markdown:

image

we would love to sorround the generated markup with e.g. a html comment, so kodiak could strip it when merging.

what do you think?

we have the following kodiak config regarding merge.

[merge.message]
title = "pull_request_title"
body = "pull_request_body"
include_pr_number = true
strip_html_comments = true # remove html comments to auto remove PR templates
@clxmstaab clxmstaab added the enhancement New feature or request label Nov 3, 2020
@clxmstaab
Copy link
Contributor Author

@stefanbuck would it be possible to sorround the generated badges with some "marker" markup, so tools like kodiak could strip it?

or maybe even pullrequestbadge itself could strip the markup again before the PR gets merged?

@chdsbd
Copy link
Owner

chdsbd commented Nov 3, 2020

I think if the content was wrapped with HTML with a specific id the content would be easy to identify and remove.

<div id="pull-request-badge">
[![CircleCI](https://circleci.com/gh/chdsbd/kodiak.svg?style=svg&circle-token=4879604a0cca6fa815c4d22936350f5bdf455905)](https://circleci.com/gh/chdsbd/kodiak)
</div>
# .kodiak.toml

[merge.message]
strip_html_ids = ["pull-request-badge"] # remove HTML nodes with `id`

We could also maybe do something with comments and remove content between specific comments.

<!-- pull-request-badge:start -->
[![CircleCI](https://circleci.com/gh/chdsbd/kodiak.svg?style=svg&circle-token=4879604a0cca6fa815c4d22936350f5bdf455905)](https://circleci.com/gh/chdsbd/kodiak)
<!-- pull-request-badge:end -->
# .kodiak.toml

[merge.message]
strip_html_comment_spans = ["pull-request-badge"]

I think the challenge is finding syntax that looks okay if it's not removed.

@stefanbuck
Copy link

or maybe even pullrequestbadge itself could strip the markup again before the PR gets merged?

Possible, but that way you remove badges from merged PRs. Personally I find it quite useful when looking at an older PR (particular the JIRA badge). However, there is already a separator comment: <!-- PR-BADGE: PLEASE DO NOT REMOVE THIS COMMENT -->. Everything before this comment can safely removed since Pull Request Badge insert badges on top always.

@sbdchd
Copy link
Collaborator

sbdchd commented Nov 4, 2020

I think to be safe, it would be better if there are comments before and after the PR Badge content.
That way we don't accidentally delete someone's commit message if they happen to put stuff at the top.

So I think a generic solution would look something like:

text before badges

<!-- PR-BADGE: whatever text you want -->
badge content here
<!-- PR-BADGE: can put any text here as well -->

text after badges

and then Kodiak would delete any content inside comments that begin with a prefix like PR-BADGE

I think the config @chdsbd outlined would work with this comment setup:

# .kodiak.toml

[merge.message]
strip_html_comment_spans = ["PR-BADGE"]

@stefanbuck
Copy link

I see your point, but this is not possible. Pull Request Badge runs whenever the description is added or updated. That way badges are always on top. If you still have strong feeling I'm happy to add such an opening comment

@sbdchd
Copy link
Collaborator

sbdchd commented Nov 5, 2020

Gotcha, I think if there is an opening comment as well, then this feature can be used with any tool that adds stuff to a PR body.

With only the closing comment we'd have to do a one-off for PR Badge which is doable, but it would be nice if we added something more general for other bots

@ardeois
Copy link

ardeois commented Nov 24, 2020

I would be interested in a similar feature, but I would like to suggest a different configuration approach.

At my work, we used bors in the past but switched to Kodiak, as it's more flexible. Bors has a configuration named cut_body_after :

A marker in the PR description that indicates boilerplate that does not belong in the commit message.

We found this config very useful, and would like a similar feature for Kodiak.

What would you think if we had:

# .kodiak.toml
[merge.message]
cut_body_before = "kodiak-message:start"
cut_body_after = "kodiak-message:end"

The marker could even be an html comment so it's not seen in the PR description:

# .kodiak.toml
[merge.message]
cut_body_before = "<!-- kodiak-message:start -->"
cut_body_after = "<!-- kodiak-message:end -->"

This way we would be in full control of the commit message and that should cover your use case @clxmstaab ?

What do you think?

@chdsbd
Copy link
Owner

chdsbd commented Nov 25, 2020

@ardeois's suggestion seems like it would work.

# .kodiak.toml
[merge.message]
cut_body_before = "<!-- PR-BADGE: PLEASE DO NOT REMOVE THIS COMMENT -->"

I think we'd need to add a couple config options to:

class MergeMessage(BaseModel):
"""
https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button
"""
title: MergeTitleStyle = MergeTitleStyle.github_default
body: MergeBodyStyle = MergeBodyStyle.github_default
include_pr_number: bool = True
body_type: BodyText = BodyText.markdown
strip_html_comments: bool = False
include_pull_request_author: bool = False
include_coauthors: bool = False
include_pull_request_url: bool = False

And update this function to use those new config options:

def get_body_content(
body_type: BodyText, strip_html_comments: bool, pull_request: PullRequest
) -> str:
if body_type == BodyText.markdown:
body = pull_request.body
if strip_html_comments:
return strip_html_comments_from_markdown(body)
return body
if body_type == BodyText.plain_text:
return pull_request.bodyText
if body_type == BodyText.html:
return pull_request.bodyHTML
raise Exception(f"Unknown body_type: {body_type}")

@stefanbuck
Copy link

@chdsbd please let me know if you still need the opening comment. I just had no time to do it, but should be easy.

@chdsbd
Copy link
Owner

chdsbd commented Nov 26, 2020

@stefanbuck I think we're all set know thanks to @ardeois's suggestion.

With the "cut_body_before" option we can remove the badges by specifying the PR-Badge comment.

@clxmstaab
Copy link
Contributor Author

As I understood it, the solution of @ardeois will work for us, in case @stefanbuck will add the necessary markup

kodiakhq bot pushed a commit that referenced this issue Dec 10, 2020
Addresses #550

I had been fully dependent on the CI environment for testing this PR since I had [trouble](#588) setting up my local development machine.
sbdchd added a commit that referenced this issue Dec 12, 2020
`cut_body_before` was added via #589

Add the second option, also handle the case where the match isn't found.

rel: #550
kodiakhq bot pushed a commit that referenced this issue Dec 12, 2020
`cut_body_before` was added via #589

Add the second option, also handle the case where the match isn't found.

rel: #550
@chdsbd
Copy link
Owner

chdsbd commented Dec 13, 2020

merge.message.cut_body_before and merge.message.cut_body_after are now released via #589 and #595.

So to remove the PR Badge labels from a message like:

[![CircleCI](https://circleci.com/gh/chdsbd/kodiak.svg?style=svg&circle-token=4879604a0cca6fa815c4d22936350f5bdf455905)](https://circleci.com/gh/chdsbd/kodiak)
<!-- PR-BADGE: PLEASE DO NOT REMOVE THIS COMMENT -->

Add new feature "foo" to accept user feedback.

We can use the following config:

# .kodiak.toml
[merge.message]
body = "pull_request_body"
cut_body_before = "<!-- PR-BADGE: PLEASE DO NOT REMOVE THIS COMMENT -->"
strip_html_comments = true

And on merge we'll have the following commit message:


Add new feature "foo" to accept user feedback.

The merge.message.cut_body_before option will remove the content for the badges and merge.message.strip_html_comments will remove the "PR-BADGE: PLEASE DO NOT REMOVE" comment.

Please open an issue if you run into any problems.

@chdsbd chdsbd closed this as completed Dec 13, 2020
@clxmstaab
Copy link
Contributor Author

clxmstaab commented Dec 15, 2020

just stumbled upon another use-case.

when merging dependabot-PRs using kodiak we get a long list of useless stuff at the end of the merge-commit.

example:

image

we will use cut_body_after ="Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself." as a mean to make this shorter. lets see if it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants