From ee488ef0c99e6289cc754fb732ae16bf7f6e10c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Bergstro=CC=88m?= Date: Mon, 20 Dec 2021 18:53:55 -0300 Subject: [PATCH] feat: optionally strip body and text If `cut_body_after` or `cut_body_before` is in use, setting this option to `true` also removes the text itself. --- bot/kodiak/config.py | 1 + bot/kodiak/evaluation.py | 7 + .../test/fixtures/config/config-schema.json | 412 ------------------ .../evaluation/test_merge_message_cut_body.py | 47 ++ docs/docs/config-reference.md | 11 + 5 files changed, 66 insertions(+), 412 deletions(-) diff --git a/bot/kodiak/config.py b/bot/kodiak/config.py index 2db00edf9..c3efa21b3 100644 --- a/bot/kodiak/config.py +++ b/bot/kodiak/config.py @@ -47,6 +47,7 @@ class MergeMessage(BaseModel): include_pull_request_url: bool = False cut_body_before: str = "" cut_body_after: str = "" + cut_body_and_text: bool = False # this pattern indicates that the user has the field unset. diff --git a/bot/kodiak/evaluation.py b/bot/kodiak/evaluation.py index 808bcf4fd..f95c8cf5b 100644 --- a/bot/kodiak/evaluation.py +++ b/bot/kodiak/evaluation.py @@ -74,18 +74,24 @@ def get_body_content( strip_html_comments: bool, cut_body_before: str, cut_body_after: str, + cut_body_and_text: bool, pull_request: PullRequest, ) -> str: if body_type is BodyText.markdown: body = pull_request.body + # TODO(jbergstroem): This could probably be made faster by just cutting the string differently if cut_body_before != "": start_index = body.find(cut_body_before) if start_index != -1: body = body[start_index:] + if cut_body_and_text: + body = body.replace(cut_body_before, "", 1) if cut_body_after != "": end_index = body.find(cut_body_after) if end_index != -1: body = body[: end_index + len(cut_body_after)] + if cut_body_and_text: + body = body.replace(cut_body_after, "", 1) if strip_html_comments: return strip_html_comments_from_markdown(body) return body @@ -182,6 +188,7 @@ def get_merge_body( body = get_body_content( body_type=config.merge.message.body_type, strip_html_comments=config.merge.message.strip_html_comments, + cut_body_and_text=config.merge.message.cut_body_and_text, cut_body_before=config.merge.message.cut_body_before, cut_body_after=config.merge.message.cut_body_after, pull_request=pull_request, diff --git a/bot/kodiak/test/fixtures/config/config-schema.json b/bot/kodiak/test/fixtures/config/config-schema.json index 8e7ba08ab..e69de29bb 100644 --- a/bot/kodiak/test/fixtures/config/config-schema.json +++ b/bot/kodiak/test/fixtures/config/config-schema.json @@ -1,412 +0,0 @@ -{ - "title": "V1", - "type": "object", - "properties": { - "version": { - "title": "Version", - "type": "integer" - }, - "app_id": { - "title": "App Id", - "type": "string" - }, - "merge": { - "title": "Merge", - "default": { - "automerge_label": "automerge", - "automerge_dependencies": { - "versions": [], - "usernames": [] - }, - "require_automerge_label": true, - "show_missing_automerge_label_message": true, - "blacklist_title_regex": ":::|||kodiak|||internal|||reserved|||:::", - "blocking_title_regex": ":::|||kodiak|||internal|||reserved|||:::", - "blacklist_labels": [], - "blocking_labels": [], - "method": null, - "delete_branch_on_merge": false, - "block_on_reviews_requested": false, - "notify_on_conflict": true, - "optimistic_updates": true, - "message": { - "title": "github_default", - "body": "github_default", - "include_pr_number": true, - "body_type": "markdown", - "strip_html_comments": false, - "include_pull_request_author": false, - "include_coauthors": false, - "include_pull_request_url": false, - "cut_body_before": "", - "cut_body_after": "" - }, - "dont_wait_on_status_checks": [], - "update_branch_immediately": false, - "prioritize_ready_to_merge": false, - "priority_merge_label": null, - "do_not_merge": false - }, - "allOf": [ - { - "$ref": "#/definitions/Merge" - } - ] - }, - "update": { - "title": "Update", - "default": { - "always": false, - "require_automerge_label": true, - "autoupdate_label": null, - "blacklist_usernames": [], - "ignored_usernames": [] - }, - "allOf": [ - { - "$ref": "#/definitions/Update" - } - ] - }, - "approve": { - "title": "Approve", - "default": { - "auto_approve_usernames": [] - }, - "allOf": [ - { - "$ref": "#/definitions/Approve" - } - ] - }, - "disable_bot_label": { - "title": "Disable Bot Label", - "default": "kodiak:disabled", - "type": "string" - } - }, - "required": [ - "version" - ], - "definitions": { - "AutomergeDependencies": { - "title": "AutomergeDependencies", - "type": "object", - "properties": { - "versions": { - "title": "Versions", - "default": [], - "type": "array", - "items": { - "enum": [ - "major", - "minor", - "patch" - ], - "type": "string" - } - }, - "usernames": { - "title": "Usernames", - "default": [], - "type": "array", - "items": { - "type": "string" - } - } - } - }, - "MergeMethod": { - "title": "MergeMethod", - "description": "An enumeration.", - "enum": [ - "merge", - "squash", - "rebase", - "rebase_fast_forward" - ], - "type": "string" - }, - "MergeTitleStyle": { - "title": "MergeTitleStyle", - "description": "An enumeration.", - "enum": [ - "github_default", - "pull_request_title" - ] - }, - "MergeBodyStyle": { - "title": "MergeBodyStyle", - "description": "An enumeration.", - "enum": [ - "github_default", - "pull_request_body", - "empty" - ] - }, - "BodyText": { - "title": "BodyText", - "description": "An enumeration.", - "enum": [ - "plain_text", - "markdown", - "html" - ] - }, - "MergeMessage": { - "title": "MergeMessage", - "description": "https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button", - "type": "object", - "properties": { - "title": { - "default": "github_default", - "allOf": [ - { - "$ref": "#/definitions/MergeTitleStyle" - } - ] - }, - "body": { - "default": "github_default", - "allOf": [ - { - "$ref": "#/definitions/MergeBodyStyle" - } - ] - }, - "include_pr_number": { - "title": "Include Pr Number", - "default": true, - "type": "boolean" - }, - "body_type": { - "default": "markdown", - "allOf": [ - { - "$ref": "#/definitions/BodyText" - } - ] - }, - "strip_html_comments": { - "title": "Strip Html Comments", - "default": false, - "type": "boolean" - }, - "include_pull_request_author": { - "title": "Include Pull Request Author", - "default": false, - "type": "boolean" - }, - "include_coauthors": { - "title": "Include Coauthors", - "default": false, - "type": "boolean" - }, - "include_pull_request_url": { - "title": "Include Pull Request Url", - "default": false, - "type": "boolean" - }, - "cut_body_before": { - "title": "Cut Body Before", - "default": "", - "type": "string" - }, - "cut_body_after": { - "title": "Cut Body After", - "default": "", - "type": "string" - } - } - }, - "Merge": { - "title": "Merge", - "type": "object", - "properties": { - "automerge_label": { - "title": "Automerge Label", - "default": "automerge", - "anyOf": [ - { - "type": "string" - }, - { - "type": "array", - "items": { - "type": "string" - } - } - ] - }, - "automerge_dependencies": { - "title": "Automerge Dependencies", - "default": { - "versions": [], - "usernames": [] - }, - "allOf": [ - { - "$ref": "#/definitions/AutomergeDependencies" - } - ] - }, - "require_automerge_label": { - "title": "Require Automerge Label", - "default": true, - "type": "boolean" - }, - "show_missing_automerge_label_message": { - "title": "Show Missing Automerge Label Message", - "default": true, - "type": "boolean" - }, - "blacklist_title_regex": { - "title": "Blacklist Title Regex", - "default": ":::|||kodiak|||internal|||reserved|||:::", - "type": "string" - }, - "blocking_title_regex": { - "title": "Blocking Title Regex", - "default": ":::|||kodiak|||internal|||reserved|||:::", - "type": "string" - }, - "blacklist_labels": { - "title": "Blacklist Labels", - "default": [], - "type": "array", - "items": { - "type": "string" - } - }, - "blocking_labels": { - "title": "Blocking Labels", - "default": [], - "type": "array", - "items": { - "type": "string" - } - }, - "method": { - "$ref": "#/definitions/MergeMethod" - }, - "delete_branch_on_merge": { - "title": "Delete Branch On Merge", - "default": false, - "type": "boolean" - }, - "block_on_reviews_requested": { - "title": "Block On Reviews Requested", - "default": false, - "type": "boolean" - }, - "notify_on_conflict": { - "title": "Notify On Conflict", - "default": true, - "type": "boolean" - }, - "optimistic_updates": { - "title": "Optimistic Updates", - "default": true, - "type": "boolean" - }, - "message": { - "title": "Message", - "default": { - "title": "github_default", - "body": "github_default", - "include_pr_number": true, - "body_type": "markdown", - "strip_html_comments": false, - "include_pull_request_author": false, - "include_coauthors": false, - "include_pull_request_url": false, - "cut_body_before": "", - "cut_body_after": "" - }, - "allOf": [ - { - "$ref": "#/definitions/MergeMessage" - } - ] - }, - "dont_wait_on_status_checks": { - "title": "Dont Wait On Status Checks", - "default": [], - "type": "array", - "items": { - "type": "string" - } - }, - "update_branch_immediately": { - "title": "Update Branch Immediately", - "default": false, - "type": "boolean" - }, - "prioritize_ready_to_merge": { - "title": "Prioritize Ready To Merge", - "default": false, - "type": "boolean" - }, - "priority_merge_label": { - "title": "Priority Merge Label", - "type": "string" - }, - "do_not_merge": { - "title": "Do Not Merge", - "default": false, - "type": "boolean" - } - } - }, - "Update": { - "title": "Update", - "type": "object", - "properties": { - "always": { - "title": "Always", - "default": false, - "type": "boolean" - }, - "require_automerge_label": { - "title": "Require Automerge Label", - "default": true, - "type": "boolean" - }, - "autoupdate_label": { - "title": "Autoupdate Label", - "type": "string" - }, - "blacklist_usernames": { - "title": "Blacklist Usernames", - "default": [], - "type": "array", - "items": { - "type": "string" - } - }, - "ignored_usernames": { - "title": "Ignored Usernames", - "default": [], - "type": "array", - "items": { - "type": "string" - } - } - } - }, - "Approve": { - "title": "Approve", - "type": "object", - "properties": { - "auto_approve_usernames": { - "title": "Auto Approve Usernames", - "default": [], - "type": "array", - "items": { - "type": "string" - } - } - } - } - } -} diff --git a/bot/kodiak/tests/evaluation/test_merge_message_cut_body.py b/bot/kodiak/tests/evaluation/test_merge_message_cut_body.py index e75b53e7f..858de121d 100644 --- a/bot/kodiak/tests/evaluation/test_merge_message_cut_body.py +++ b/bot/kodiak/tests/evaluation/test_merge_message_cut_body.py @@ -30,6 +30,53 @@ def test_get_merge_body_cut_body_after() -> None: expected = MergeBody(merge_method="squash", commit_message="hello ") assert actual == expected +def test_get_merge_body_cut_body_and_text_after() -> None: + """ + Verify that the separator is also gone after removing content. + """ + pull_request = create_pull_request() + pull_request.body = "hello world" + actual = get_merge_body( + config=V1( + version=1, + merge=Merge( + message=MergeMessage( + body=MergeBodyStyle.pull_request_body, + cut_body_after="", + cut_body_and_text=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_and_text_before() -> None: + """ + Verify that the separator is also gone after removing content. + """ + pull_request = create_pull_request() + pull_request.body = "hello world" + actual = get_merge_body( + config=V1( + version=1, + merge=Merge( + message=MergeMessage( + body=MergeBodyStyle.pull_request_body, + cut_body_before="", + cut_body_and_text=True + ) + ), + ), + pull_request=pull_request, + merge_method=MergeMethod.squash, + commits=[], + ) + expected = MergeBody(merge_method="squash", commit_message="world") + assert actual == expected def test_get_merge_body_cut_body_after_strip_html() -> None: """ diff --git a/docs/docs/config-reference.md b/docs/docs/config-reference.md index 86e48ae76..de43ff250 100755 --- a/docs/docs/config-reference.md +++ b/docs/docs/config-reference.md @@ -349,6 +349,17 @@ Remove all content _after_ the configured string in the pull request body. This setting is useful when we want to include only a part of the pull request description as the commit message. This option only applies when `merge.message.body_type = "markdown"`. +_ +### `merge.message.cut_body_and_text` + +- **type:** `boolean` +- **default:** `False` + +Remove the string used to handle body in `cut_body_before` and `cut_body_after`. + +This setting is useful if you believe that the separator doesn't belong in the commit message. + +This option only applies if `cut_body_after` or `cut_body_before` is in use. ### `merge.message.include_coauthors`