Skip to content

Commit

Permalink
add(bot): cut_body_after option for merge message
Browse files Browse the repository at this point in the history
`cut_body_before` was added via #589

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

rel: #550
  • Loading branch information
sbdchd committed Dec 12, 2020
1 parent be21a47 commit e8d3587
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 7 deletions.
1 change: 1 addition & 0 deletions bot/kodiak/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MergeMessage(BaseModel):
include_coauthors: bool = False
include_pull_request_url: bool = False
cut_body_before: str = ""
cut_body_after: str = ""


# this pattern indicates that the user has the field unset.
Expand Down
18 changes: 13 additions & 5 deletions bot/kodiak/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,23 @@


def get_body_content(
*,
body_type: BodyText,
strip_html_comments: bool,
cut_body_before: str,
cut_body_after: str,
pull_request: PullRequest,
) -> str:
if body_type == BodyText.markdown:
body = pull_request.body
if cut_body_before != "":
start_index = body.find(cut_body_before)
body = body[start_index:]
if start_index != -1:
body = body[start_index:]
if cut_body_after != "":
end_index = body.find(cut_body_after)
if end_index != -1:
body = body[: end_index + len(cut_body_after)]
if strip_html_comments:
return strip_html_comments_from_markdown(body)
return body
Expand Down Expand Up @@ -168,10 +175,11 @@ def get_merge_body(
merge_body = MergeBody(merge_method=merge_method)
if config.merge.message.body == MergeBodyStyle.pull_request_body:
body = get_body_content(
config.merge.message.body_type,
config.merge.message.strip_html_comments,
config.merge.message.cut_body_before,
pull_request,
body_type=config.merge.message.body_type,
strip_html_comments=config.merge.message.strip_html_comments,
cut_body_before=config.merge.message.cut_body_before,
cut_body_after=config.merge.message.cut_body_after,
pull_request=pull_request,
)
merge_body.commit_message = body
if config.merge.message.body == MergeBodyStyle.empty:
Expand Down
11 changes: 9 additions & 2 deletions bot/kodiak/test/fixtures/config/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"include_pull_request_author": false,
"include_coauthors": false,
"include_pull_request_url": false,
"cut_body_before": ""
"cut_body_before": "",
"cut_body_after": ""
},
"dont_wait_on_status_checks": [],
"update_branch_immediately": false,
Expand Down Expand Up @@ -143,6 +144,11 @@
"title": "Cut Body Before",
"default": "",
"type": "string"
},
"cut_body_after": {
"title": "Cut Body After",
"default": "",
"type": "string"
}
}
},
Expand Down Expand Up @@ -236,7 +242,8 @@
"include_pull_request_author": false,
"include_coauthors": false,
"include_pull_request_url": false,
"cut_body_before": ""
"cut_body_before": "",
"cut_body_after": ""
},
"allOf": [
{
Expand Down
126 changes: 126 additions & 0 deletions bot/kodiak/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3048,6 +3048,132 @@ def test_get_merge_body_strip_html_comments() -> None:
assert actual == expected


def test_get_merge_body_cut_body_after() -> None:
"""
Basic check of cut_body_after removing content.
"""
pull_request = create_pull_request()
pull_request.body = "hello <!-- testing -->world"
actual = get_merge_body(
config=V1(
version=1,
merge=Merge(
message=MergeMessage(
body=MergeBodyStyle.pull_request_body,
cut_body_after="<!-- testing -->",
)
),
),
pull_request=pull_request,
merge_method=MergeMethod.squash,
commits=[],
)
expected = MergeBody(merge_method="squash", commit_message="hello <!-- testing -->")
assert actual == expected


def test_get_merge_body_cut_body_after_strip_html() -> None:
"""
We should be able to use strip_html_comments with cut_body_after.
"""
pull_request = create_pull_request()
pull_request.body = "hello <!-- testing -->world"
actual = get_merge_body(
config=V1(
version=1,
merge=Merge(
message=MergeMessage(
body=MergeBodyStyle.pull_request_body,
cut_body_after="<!-- testing -->",
strip_html_comments=True,
)
),
),
pull_request=pull_request,
merge_method=MergeMethod.squash,
commits=[],
)
expected = MergeBody(merge_method="squash", commit_message="hello ")
assert actual == expected


def test_get_merge_body_cut_body_after_multiple_markers() -> None:
"""
We should choose the first substring matching cut_body_after.
"""
pull_request = create_pull_request()
pull_request.body = "hello <!-- testing -->world<!-- testing --> 123"
actual = get_merge_body(
config=V1(
version=1,
merge=Merge(
message=MergeMessage(
body=MergeBodyStyle.pull_request_body,
cut_body_after="<!-- testing -->",
strip_html_comments=True,
)
),
),
pull_request=pull_request,
merge_method=MergeMethod.squash,
commits=[],
)
expected = MergeBody(merge_method="squash", commit_message="hello ")
assert actual == expected


def test_get_merge_body_cut_body_after_no_match_found() -> None:
"""
Ensure we don't edit the message if there isn't any match found with
cut_body_after.
"""
pull_request = create_pull_request()
pr_body = "hello <!-- foo -->world<!-- bar --> 123"
pull_request.body = pr_body
actual = get_merge_body(
config=V1(
version=1,
merge=Merge(
message=MergeMessage(
body=MergeBodyStyle.pull_request_body,
cut_body_after="<!-- buzz -->",
)
),
),
pull_request=pull_request,
merge_method=MergeMethod.squash,
commits=[],
)
expected = MergeBody(merge_method="squash", commit_message=pr_body)
assert actual == expected


def test_get_merge_body_cut_body_before_no_match_found() -> None:
"""
Ensure we don't edit the message if there isn't any match found with
cut_body_before.
"""
pull_request = create_pull_request()
pr_body = "hello <!-- foo -->world<!-- bar --> 123"
pull_request.body = pr_body
actual = get_merge_body(
config=V1(
version=1,
merge=Merge(
message=MergeMessage(
body=MergeBodyStyle.pull_request_body,
cut_body_before="<!-- buzz -->",
)
),
),
pull_request=pull_request,
merge_method=MergeMethod.squash,
commits=[],
)
expected = MergeBody(merge_method="squash", commit_message=pr_body)
assert actual == expected


def test_get_merge_body_cut_body_before() -> None:
pull_request = create_pull_request()
pull_request.body = "hello <!-- testing -->world"
Expand Down

0 comments on commit e8d3587

Please sign in to comment.