From bc397b1cb17d4fa27324ffe56467a2c53e151707 Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 28 Mar 2024 23:19:59 -0700 Subject: [PATCH] [github] update _strip_stack_information() to recognize \r\n By default, Sapling programmatically writes the PR body on GitHub and will continue to overwrite it every time the user runs `sl pr submit`. In so doing, Sapling always uses `\n` as the line terminator for the content it generates. However, if the user has `github.preserve-pull-request-description=true` set and edits the pull request body on GitHub, then the next time Sapling pulls down the pull request body to parse it, it will find that the original line terminators it wrote will have been changed to `\r\n`. Previous to this commit, `_strip_stack_information()` always looked for `_HORIZONTAL_RULE` and `_SAPLING_FOOTER_MARKER` joined with `\n`, which failed to match the newly written version containing `\r\n`. This meant that users with `github.preserve-pull-request-description=true` set would end up getting an extra copy of the stack information written to the PR body after running `sl pr submit` because the first instance was considered the source of the PR body that Sapling was not supposed to modify based on `github.preserve-pull-request-description=true`. This commit changes `_strip_stack_information()` to match the pattern using a regex that recognizes both `\n` and `\r\n` as the line terminator. Added a doctest to `_strip_stack_information()` and verified the following passes locally: ``` ./tests/run-tests.py ./tests/test-doctest.py ``` --- .../sapling/ext/github/pull_request_body.py | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/eden/scm/sapling/ext/github/pull_request_body.py b/eden/scm/sapling/ext/github/pull_request_body.py index 8f7f5f0fb0972..78b19f80d84f2 100644 --- a/eden/scm/sapling/ext/github/pull_request_body.py +++ b/eden/scm/sapling/ext/github/pull_request_body.py @@ -152,11 +152,38 @@ def _line_has_stack_list_marker(line: str) -> bool: ) +_SAPLING_FOOTER_WITH_HRULE = re.compile( + re.escape(_HORIZONTAL_RULE) + r'\r?\n' + re.escape(_SAPLING_FOOTER_MARKER), + re.MULTILINE, +) + + def _strip_stack_information(body: str) -> str: - marker = "\n".join([_HORIZONTAL_RULE, _SAPLING_FOOTER_MARKER]) - if marker in body: - body = body.rsplit(marker, maxsplit=1)[0] - return body + r""" + Footer marker joined with \n + >>> body = ( + ... 'The original commit message.\n' + + ... 'Second line of message.\n' + + ... '---\n' + + ... '[//]: # (BEGIN SAPLING FOOTER)\n' + + ... '* #1\n' + + ... '* #2 (2 commits)\n' + + ... '* __->__ #42\n' + + ... '* #4\n') + >>> _strip_stack_information(body) + 'The original commit message.\nSecond line of message.\n' + + Footer marker joined with \r\n. If the user edits the pull request body + on github.com, GitHub will rewrite the line endings to \r\n. + >>> _strip_stack_information(body.replace('\n', '\r\n')) + 'The original commit message.\r\nSecond line of message.\r\n' + + If the footer marker appears multiple times in the body, everything will + be stripped after the first occurrence. + >>> _strip_stack_information(body + body) + 'The original commit message.\nSecond line of message.\n' + """ + return re.split(_SAPLING_FOOTER_WITH_HRULE, body, maxsplit=1)[0] def _format_stack_entry(