Skip to content

Commit b8279be

Browse files
Fix unclosed <details> tag when primer comment is truncated mid-block
When truncation cuts inside an open <details> block, the closing tag was missing, causing GitHub to render the rest of the PR comment inside the collapsed section. Now detect the open tag and close it before appending the truncation notice. Add a dedicated test case for this.
1 parent b36bc7a commit b8279be

4 files changed

Lines changed: 40 additions & 7 deletions

File tree

pylint/testutils/_primer/primer_compare_command.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,19 @@ def _truncate_comment(self, comment: str) -> str:
113113
f"*This comment was truncated because GitHub allows only"
114114
f" {MAX_GITHUB_COMMENT_LENGTH} characters in a comment.*"
115115
)
116+
# Reserve space for the suffix and a potential </details> closing tag.
117+
suffix = f"\n{truncation_information}\n\n"
118+
closing_tag = "</details>\n"
116119
max_len = (
117120
MAX_GITHUB_COMMENT_LENGTH
118121
- len(hash_information)
119-
- len(truncation_information)
122+
- len(suffix)
123+
- len(closing_tag)
120124
)
121-
comment = f"{comment[:max_len - 10]}...\n\n{truncation_information}\n\n"
125+
comment = comment[: max_len - 10] + "...\n"
126+
# Close any <details> tag left open by the truncation.
127+
if comment.count("<details>") > comment.count("</details>"):
128+
comment += closing_tag
129+
comment += suffix
122130
comment += hash_information
123131
return comment

tests/testutils/_primer/cases/message_changed/expected_truncated.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
🤖 **Effect of this PR on checked open source code:** 🤖
22

33

4-
54
**Effect on [astroid](https://github.com/pylint-dev/astroid):**
5+
66
The following messages are now emitted:
77

88
<details>
9-
109
1) locally-disabled:
1110
*Locally disabling redefined-builtin [we added some text in the message] (W0622)*
1211
https://github.com/pylint-dev/astroid/blob/123456789abcdef/astroid/__init__.py#L91
13-
1412
</details>
15-
16-
The followi...
13+
...
1714

1815
*This comment was truncated because GitHub allows only 525 characters in a comment.*
1916

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
🤖 **Effect of this PR on checked open source code:** 🤖
2+
3+
4+
**Effect on [astroid](https://github.com/pylint-dev/astroid):**
5+
6+
The following messages are now emitted:
7+
8+
<details>
9+
1) locally-disabled:
10+
*Locally disabling redefined-builtin [we added some text in the message...
11+
</details>
12+
13+
*This comment was truncated because GitHub allows only 420 characters in a comment.*
14+
15+
*This comment was generated for commit v2.14.2*

tests/testutils/_primer/test_primer.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ def test_truncated_compare(self) -> None:
8686
)
8787
assert len(content) < max_comment_length
8888

89+
def test_truncated_compare_in_details(self) -> None:
90+
"""Test for the truncation of comments that are too long inside details."""
91+
max_comment_length = 420
92+
directory = CASES_PATH / "message_changed"
93+
with patch(
94+
"pylint.testutils._primer.primer_compare_command.MAX_GITHUB_COMMENT_LENGTH",
95+
max_comment_length,
96+
):
97+
content = self.__assert_expected(
98+
directory, expected_file=directory / "expected_truncated_in_details.txt"
99+
)
100+
assert len(content) < max_comment_length
101+
89102
@staticmethod
90103
def __assert_expected(
91104
directory: Path,

0 commit comments

Comments
 (0)