Skip to content

Commit 79dfe9d

Browse files
[primer] Truncate comments that are too long for github (#6899)
Co-authored-by: Daniël van Noord <[email protected]>
1 parent c05ca30 commit 79dfe9d

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

tests/primer/primer_tool.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pylint.reporters import JSONReporter
2020
from pylint.testutils.primer import PackageToLint
2121

22+
MAX_GITHUB_COMMENT_LENGTH = 65536
2223
TESTS_DIR = Path(__file__).parent.parent
2324
PRIMER_DIRECTORY = TESTS_DIR / ".pylint_primer_tests/"
2425
PACKAGES_TO_PRIME_PATH = Path(__file__).parent / "packages_to_prime.json"
@@ -189,6 +190,9 @@ def _create_comment(
189190
) -> None:
190191
comment = ""
191192
for package, missing_messages in all_missing_messages.items():
193+
if len(comment) >= MAX_GITHUB_COMMENT_LENGTH:
194+
break
195+
192196
new_messages = all_new_messages[package]
193197
package_data = self.packages[package]
194198

@@ -255,14 +259,29 @@ def _create_comment(
255259
comment += "\n</details>\n\n"
256260

257261
if comment == "":
258-
comment = "🤖 According to the primer, this change has **no effect** on the checked open source code. 🤖🎉"
262+
comment = (
263+
"🤖 According to the primer, this change has **no effect** on the"
264+
" checked open source code. 🤖🎉\n\n"
265+
)
259266
else:
260267
comment = (
261-
"🤖 **Effect of this PR on checked open source code:** 🤖\n\n" + comment
268+
f"🤖 **Effect of this PR on checked open source code:** 🤖\n\n{comment}"
262269
)
263-
264-
comment += f"*This comment was generated for commit {self.config.commit}*"
265-
270+
hash_information = (
271+
f"*This comment was generated for commit {self.config.commit}*"
272+
)
273+
if len(comment) + len(hash_information) >= MAX_GITHUB_COMMENT_LENGTH:
274+
truncation_information = (
275+
f"*This comment was truncated because GitHub allows only"
276+
f"{MAX_GITHUB_COMMENT_LENGTH} characters in a comment.*"
277+
)
278+
max_len = (
279+
MAX_GITHUB_COMMENT_LENGTH
280+
- len(hash_information)
281+
- len(truncation_information)
282+
)
283+
comment = f"{comment[:max_len - 10]}...\n\n{truncation_information}\n\n"
284+
comment += hash_information
266285
with open(PRIMER_DIRECTORY / "comment.txt", "w", encoding="utf-8") as f:
267286
f.write(comment)
268287

0 commit comments

Comments
 (0)