Skip to content

Commit a6aa571

Browse files
[refactor] Reorganize the primer comment code before modification
1 parent 01be3cc commit a6aa571

File tree

1 file changed

+72
-67
lines changed

1 file changed

+72
-67
lines changed

pylint/testutils/_primer/primer_compare_command.py

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -26,81 +26,23 @@ def run(self) -> None:
2626
except ValueError:
2727
final_main_dict[package].append(message)
2828

29-
self._create_comment(final_main_dict, new_dict)
29+
comment = self._create_comment(final_main_dict, new_dict)
30+
with open(self.primer_directory / "comment.txt", "w", encoding="utf-8") as f:
31+
f.write(comment)
3032

3133
def _create_comment(
3234
self, all_missing_messages: PackageMessages, all_new_messages: PackageMessages
33-
) -> None:
35+
) -> str:
3436
comment = ""
3537
for package, missing_messages in all_missing_messages.items():
3638
if len(comment) >= MAX_GITHUB_COMMENT_LENGTH:
3739
break
38-
3940
new_messages = all_new_messages[package]
40-
package_data = self.packages[package]
41-
4241
if not missing_messages and not new_messages:
4342
continue
44-
45-
comment += f"\n\n**Effect on [{package}]({self.packages[package].url}):**\n"
46-
47-
# Create comment for new messages
48-
count = 1
49-
astroid_errors = 0
50-
new_non_astroid_messages = ""
51-
if new_messages:
52-
print("Now emitted:")
53-
for message in new_messages:
54-
filepath = str(message["path"]).replace(
55-
str(package_data.clone_directory), ""
56-
)
57-
# Existing astroid errors may still show up as "new" because the timestamp
58-
# in the message is slightly different.
59-
if message["symbol"] == "astroid-error":
60-
astroid_errors += 1
61-
else:
62-
new_non_astroid_messages += (
63-
f"{count}) {message['symbol']}:\n*{message['message']}*\n"
64-
f"{package_data.url}/blob/{package_data.branch}{filepath}#L{message['line']}\n"
65-
)
66-
print(message)
67-
count += 1
68-
69-
if astroid_errors:
70-
comment += (
71-
f"{astroid_errors} error(s) were found stemming from the `astroid` library. "
72-
"This is unlikely to have been caused by your changes. "
73-
"A GitHub Actions warning links directly to the crash report template. "
74-
"Please open an issue against `astroid` if one does not exist already. \n\n"
75-
)
76-
if new_non_astroid_messages:
77-
comment += (
78-
"The following messages are now emitted:\n\n<details>\n\n"
79-
+ new_non_astroid_messages
80-
+ "\n</details>\n\n"
81-
)
82-
83-
# Create comment for missing messages
84-
count = 1
85-
if missing_messages:
86-
comment += (
87-
"The following messages are no longer emitted:\n\n<details>\n\n"
88-
)
89-
print("No longer emitted:")
90-
for message in missing_messages:
91-
comment += f"{count}) {message['symbol']}:\n*{message['message']}*\n"
92-
filepath = str(message["path"]).replace(
93-
str(package_data.clone_directory), ""
94-
)
95-
assert not package_data.url.endswith(
96-
".git"
97-
), "You don't need the .git at the end of the github url."
98-
comment += f"{package_data.url}/blob/{package_data.branch}{filepath}#L{message['line']}\n"
99-
count += 1
100-
print(message)
101-
if missing_messages:
102-
comment += "\n</details>\n\n"
103-
43+
comment += self._create_comment_for_package(
44+
package, new_messages, missing_messages
45+
)
10446
if comment == "":
10547
comment = (
10648
"🤖 According to the primer, this change has **no effect** on the"
@@ -110,6 +52,70 @@ def _create_comment(
11052
comment = (
11153
f"🤖 **Effect of this PR on checked open source code:** 🤖\n\n{comment}"
11254
)
55+
return self._truncate_comment(comment)
56+
57+
def _create_comment_for_package(
58+
self, package: str, new_messages, missing_messages
59+
) -> str:
60+
comment = f"\n\n**Effect on [{package}]({self.packages[package].url}):**\n"
61+
# Create comment for new messages
62+
count = 1
63+
astroid_errors = 0
64+
new_non_astroid_messages = ""
65+
if new_messages:
66+
print("Now emitted:")
67+
for message in new_messages:
68+
filepath = str(message["path"]).replace(
69+
str(self.packages[package].clone_directory), ""
70+
)
71+
# Existing astroid errors may still show up as "new" because the timestamp
72+
# in the message is slightly different.
73+
if message["symbol"] == "astroid-error":
74+
astroid_errors += 1
75+
else:
76+
new_non_astroid_messages += (
77+
f"{count}) {message['symbol']}:\n*{message['message']}*\n"
78+
f"{self.packages[package].url}/blob/{self.packages[package].branch}{filepath}#L{message['line']}\n"
79+
)
80+
print(message)
81+
count += 1
82+
83+
if astroid_errors:
84+
comment += (
85+
f"{astroid_errors} error(s) were found stemming from the `astroid` library. "
86+
"This is unlikely to have been caused by your changes. "
87+
"A GitHub Actions warning links directly to the crash report template. "
88+
"Please open an issue against `astroid` if one does not exist already. \n\n"
89+
)
90+
if new_non_astroid_messages:
91+
comment += (
92+
"The following messages are now emitted:\n\n<details>\n\n"
93+
+ new_non_astroid_messages
94+
+ "\n</details>\n\n"
95+
)
96+
97+
# Create comment for missing messages
98+
count = 1
99+
if missing_messages:
100+
comment += "The following messages are no longer emitted:\n\n<details>\n\n"
101+
print("No longer emitted:")
102+
for message in missing_messages:
103+
comment += f"{count}) {message['symbol']}:\n*{message['message']}*\n"
104+
filepath = str(message["path"]).replace(
105+
str(self.packages[package].clone_directory), ""
106+
)
107+
assert not self.packages[package].url.endswith(
108+
".git"
109+
), "You don't need the .git at the end of the github url."
110+
comment += f"{self.packages[package].url}/blob/{self.packages[package].branch}{filepath}#L{message['line']}\n"
111+
count += 1
112+
print(message)
113+
if missing_messages:
114+
comment += "\n</details>\n\n"
115+
return comment
116+
117+
def _truncate_comment(self, comment: str) -> str:
118+
"""GitHub allows only a set number of characters in a comment."""
113119
hash_information = (
114120
f"*This comment was generated for commit {self.config.commit}*"
115121
)
@@ -125,5 +131,4 @@ def _create_comment(
125131
)
126132
comment = f"{comment[:max_len - 10]}...\n\n{truncation_information}\n\n"
127133
comment += hash_information
128-
with open(self.primer_directory / "comment.txt", "w", encoding="utf-8") as f:
129-
f.write(comment)
134+
return comment

0 commit comments

Comments
 (0)