Skip to content

Commit d3b88c9

Browse files
authored
Use permanent links in the primer comment (#7332)
1 parent 8e57fe1 commit d3b88c9

File tree

10 files changed

+194
-157
lines changed

10 files changed

+194
-157
lines changed

pylint/testutils/_primer/primer_command.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@
66

77
import abc
88
import argparse
9+
import sys
910
from pathlib import Path
10-
from typing import Dict, List
11+
from typing import Dict
1112

12-
from pylint.message import Message
13+
from pylint.reporters.json_reporter import OldJsonExport
1314
from pylint.testutils._primer import PackageToLint
1415

15-
PackageMessages = Dict[str, List[Message]]
16+
if sys.version_info >= (3, 8):
17+
from typing import TypedDict
18+
else:
19+
from typing_extensions import TypedDict
20+
21+
22+
class PackageData(TypedDict):
23+
commit: str
24+
messages: list[OldJsonExport]
25+
26+
27+
PackageMessages = Dict[str, PackageData]
1628

1729

1830
class PrimerCommand:

pylint/testutils/_primer/primer_compare_command.py

+39-27
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,45 @@
44
from __future__ import annotations
55

66
import json
7-
from pathlib import Path
7+
from pathlib import Path, PurePosixPath
88

9-
from pylint.testutils._primer.primer_command import PackageMessages, PrimerCommand
9+
from pylint.reporters.json_reporter import OldJsonExport
10+
from pylint.testutils._primer.primer_command import (
11+
PackageData,
12+
PackageMessages,
13+
PrimerCommand,
14+
)
1015

1116
MAX_GITHUB_COMMENT_LENGTH = 65536
1217

1318

1419
class CompareCommand(PrimerCommand):
1520
def run(self) -> None:
16-
main_messages = self._load_json(self.config.base_file)
17-
pr_messages = self._load_json(self.config.new_file)
18-
missing_messages, new_messages = self._cross_reference(
19-
main_messages, pr_messages
21+
main_data = self._load_json(self.config.base_file)
22+
pr_data = self._load_json(self.config.new_file)
23+
missing_messages_data, new_messages_data = self._cross_reference(
24+
main_data, pr_data
2025
)
21-
comment = self._create_comment(missing_messages, new_messages)
26+
comment = self._create_comment(missing_messages_data, new_messages_data)
2227
with open(self.primer_directory / "comment.txt", "w", encoding="utf-8") as f:
2328
f.write(comment)
2429

2530
@staticmethod
2631
def _cross_reference(
27-
main_dict: PackageMessages, pr_messages: PackageMessages
32+
main_data: PackageMessages, pr_data: PackageMessages
2833
) -> tuple[PackageMessages, PackageMessages]:
29-
missing_messages: PackageMessages = {}
30-
for package, messages in main_dict.items():
31-
missing_messages[package] = []
32-
for message in messages:
34+
missing_messages_data: PackageMessages = {}
35+
for package, data in main_data.items():
36+
package_missing_messages: list[OldJsonExport] = []
37+
for message in data["messages"]:
3338
try:
34-
pr_messages[package].remove(message)
39+
pr_data[package]["messages"].remove(message)
3540
except ValueError:
36-
missing_messages[package].append(message)
37-
return missing_messages, pr_messages
41+
package_missing_messages.append(message)
42+
missing_messages_data[package] = PackageData(
43+
commit=pr_data[package]["commit"], messages=package_missing_messages
44+
)
45+
return missing_messages_data, pr_data
3846

3947
@staticmethod
4048
def _load_json(file_path: Path | str) -> PackageMessages:
@@ -50,7 +58,7 @@ def _create_comment(
5058
if len(comment) >= MAX_GITHUB_COMMENT_LENGTH:
5159
break
5260
new_messages = all_new_messages[package]
53-
if not missing_messages and not new_messages:
61+
if not missing_messages["messages"] and not new_messages["messages"]:
5462
continue
5563
comment += self._create_comment_for_package(
5664
package, new_messages, missing_messages
@@ -67,18 +75,20 @@ def _create_comment(
6775
return self._truncate_comment(comment)
6876

6977
def _create_comment_for_package(
70-
self, package: str, new_messages, missing_messages
78+
self, package: str, new_messages: PackageData, missing_messages: PackageData
7179
) -> str:
7280
comment = f"\n\n**Effect on [{package}]({self.packages[package].url}):**\n"
7381
# Create comment for new messages
7482
count = 1
7583
astroid_errors = 0
7684
new_non_astroid_messages = ""
77-
if new_messages:
85+
if new_messages["messages"]:
7886
print("Now emitted:")
79-
for message in new_messages:
80-
filepath = str(message["path"]).replace(
81-
str(self.packages[package].clone_directory), ""
87+
for message in new_messages["messages"]:
88+
filepath = str(
89+
PurePosixPath(message["path"]).relative_to(
90+
self.packages[package].clone_directory
91+
)
8292
)
8393
# Existing astroid errors may still show up as "new" because the timestamp
8494
# in the message is slightly different.
@@ -87,7 +97,7 @@ def _create_comment_for_package(
8797
else:
8898
new_non_astroid_messages += (
8999
f"{count}) {message['symbol']}:\n*{message['message']}*\n"
90-
f"{self.packages[package].url}/blob/{self.packages[package].branch}{filepath}#L{message['line']}\n"
100+
f"{self.packages[package].url}/blob/{new_messages['commit']}/{filepath}#L{message['line']}\n"
91101
)
92102
print(message)
93103
count += 1
@@ -106,18 +116,20 @@ def _create_comment_for_package(
106116

107117
# Create comment for missing messages
108118
count = 1
109-
if missing_messages:
119+
if missing_messages["messages"]:
110120
comment += "The following messages are no longer emitted:\n\n<details>\n\n"
111121
print("No longer emitted:")
112-
for message in missing_messages:
122+
for message in missing_messages["messages"]:
113123
comment += f"{count}) {message['symbol']}:\n*{message['message']}*\n"
114-
filepath = str(message["path"]).replace(
115-
str(self.packages[package].clone_directory), ""
124+
filepath = str(
125+
PurePosixPath(message["path"]).relative_to(
126+
self.packages[package].clone_directory
127+
)
116128
)
117129
assert not self.packages[package].url.endswith(
118130
".git"
119131
), "You don't need the .git at the end of the github url."
120-
comment += f"{self.packages[package].url}/blob/{self.packages[package].branch}{filepath}#L{message['line']}\n"
132+
comment += f"{self.packages[package].url}/blob/{new_messages['commit']}/{filepath}#L{message['line']}\n"
121133
count += 1
122134
print(message)
123135
if missing_messages:

pylint/testutils/_primer/primer_run_command.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,32 @@
99
import warnings
1010
from io import StringIO
1111

12+
from git.repo import Repo
13+
1214
from pylint.lint import Run
1315
from pylint.message import Message
1416
from pylint.reporters import JSONReporter
1517
from pylint.reporters.json_reporter import OldJsonExport
1618
from pylint.testutils._primer.package_to_lint import PackageToLint
17-
from pylint.testutils._primer.primer_command import PrimerCommand
19+
from pylint.testutils._primer.primer_command import (
20+
PackageData,
21+
PackageMessages,
22+
PrimerCommand,
23+
)
1824

1925
GITHUB_CRASH_TEMPLATE_LOCATION = "/home/runner/.cache"
2026
CRASH_TEMPLATE_INTRO = "There is a pre-filled template"
2127

2228

2329
class RunCommand(PrimerCommand):
2430
def run(self) -> None:
25-
packages: dict[str, list[OldJsonExport]] = {}
31+
packages: PackageMessages = {}
2632
fatal_msgs: list[Message] = []
2733
for package, data in self.packages.items():
2834
messages, p_fatal_msgs = self._lint_package(package, data)
2935
fatal_msgs += p_fatal_msgs
30-
packages[package] = messages
36+
local_commit = Repo(data.clone_directory).head.object.hexsha
37+
packages[package] = PackageData(commit=local_commit, messages=messages)
3138
path = (
3239
self.primer_directory
3340
/ f"output_{'.'.join(str(i) for i in sys.version_info[:3])}_{self.config.type}.txt"

tests/testutils/_primer/fixtures/message_changed/expected.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The following messages are now emitted:
99

1010
1) locally-disabled:
1111
*Locally disabling redefined-builtin [we added some text in the message] (W0622)*
12-
https://github.com/PyCQA/astroid/blob/main/astroid/__init__.py#L91
12+
https://github.com/PyCQA/astroid/blob/123456789abcdef/astroid/__init__.py#L91
1313

1414
</details>
1515

@@ -19,7 +19,7 @@ The following messages are no longer emitted:
1919

2020
1) locally-disabled:
2121
*Locally disabling redefined-builtin (W0622)*
22-
https://github.com/PyCQA/astroid/blob/main/astroid/__init__.py#L91
22+
https://github.com/PyCQA/astroid/blob/123456789abcdef/astroid/__init__.py#L91
2323

2424
</details>
2525

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ The following messages are now emitted:
99

1010
1) locally-disabled:
1111
*Locally disabling redefined-builtin [we added some text in the message] (W0622)*
12-
https://github.com/PyCQA/astroid/blob/main/astroid/__init__.py#L91
12+
https://github.com/PyCQA/astroid/blob/123456789abcdef/astroid/__init__.py#L91
1313

1414
</details>
1515

16-
The fol...
16+
The following message...
1717

18-
*This comment was truncated because GitHub allows only 500 characters in a comment.*
18+
*This comment was truncated because GitHub allows only 525 characters in a comment.*
1919

2020
*This comment was generated for commit v2.14.2*
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
{
2-
"astroid": [
3-
{
4-
"type": "info",
5-
"module": "astroid",
6-
"obj": "",
7-
"line": 91,
8-
"column": 0,
9-
"endLine": null,
10-
"endColumn": null,
11-
"path": "tests/.pylint_primer_tests/PyCQA/astroid/astroid/__init__.py",
12-
"symbol": "locally-disabled",
13-
"message": "Locally disabling redefined-builtin (W0622)",
14-
"message-id": "I0011"
15-
},
16-
{
17-
"type": "warning",
18-
"module": "astroid",
19-
"obj": "",
20-
"line": 91,
21-
"column": 0,
22-
"endLine": null,
23-
"endColumn": null,
24-
"path": "tests/.pylint_primer_tests/PyCQA/astroid/astroid/__init__.py",
25-
"symbol": "unknown-option-value",
26-
"message": "Unknown option value for 'disable', expected a valid pylint message and got 'Ellipsis'",
27-
"message-id": "W0012"
28-
}
29-
]
2+
"astroid": {
3+
"commit": "123456789abcdef",
4+
"messages": [
5+
{
6+
"type": "info",
7+
"module": "astroid",
8+
"obj": "",
9+
"line": 91,
10+
"column": 0,
11+
"endLine": null,
12+
"endColumn": null,
13+
"path": "tests/.pylint_primer_tests/PyCQA/astroid/astroid/__init__.py",
14+
"symbol": "locally-disabled",
15+
"message": "Locally disabling redefined-builtin (W0622)",
16+
"message-id": "I0011"
17+
},
18+
{
19+
"type": "warning",
20+
"module": "astroid",
21+
"obj": "",
22+
"line": 91,
23+
"column": 0,
24+
"endLine": null,
25+
"endColumn": null,
26+
"path": "tests/.pylint_primer_tests/PyCQA/astroid/astroid/__init__.py",
27+
"symbol": "unknown-option-value",
28+
"message": "Unknown option value for 'disable', expected a valid pylint message and got 'Ellipsis'",
29+
"message-id": "W0012"
30+
}
31+
]
32+
}
3033
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
{
2-
"astroid": [
3-
{
4-
"type": "info",
5-
"module": "astroid",
6-
"obj": "",
7-
"line": 91,
8-
"column": 0,
9-
"endLine": null,
10-
"endColumn": null,
11-
"path": "tests/.pylint_primer_tests/PyCQA/astroid/astroid/__init__.py",
12-
"symbol": "locally-disabled",
13-
"message": "Locally disabling redefined-builtin [we added some text in the message] (W0622)",
14-
"message-id": "I0011"
15-
},
16-
{
17-
"type": "warning",
18-
"module": "astroid",
19-
"obj": "",
20-
"line": 91,
21-
"column": 0,
22-
"endLine": null,
23-
"endColumn": null,
24-
"path": "tests/.pylint_primer_tests/PyCQA/astroid/astroid/__init__.py",
25-
"symbol": "unknown-option-value",
26-
"message": "Unknown option value for 'disable', expected a valid pylint message and got 'Ellipsis'",
27-
"message-id": "W0012"
28-
}
29-
]
2+
"astroid": {
3+
"commit": "123456789abcdef",
4+
"messages": [
5+
{
6+
"type": "info",
7+
"module": "astroid",
8+
"obj": "",
9+
"line": 91,
10+
"column": 0,
11+
"endLine": null,
12+
"endColumn": null,
13+
"path": "tests/.pylint_primer_tests/PyCQA/astroid/astroid/__init__.py",
14+
"symbol": "locally-disabled",
15+
"message": "Locally disabling redefined-builtin [we added some text in the message] (W0622)",
16+
"message-id": "I0011"
17+
},
18+
{
19+
"type": "warning",
20+
"module": "astroid",
21+
"obj": "",
22+
"line": 91,
23+
"column": 0,
24+
"endLine": null,
25+
"endColumn": null,
26+
"path": "tests/.pylint_primer_tests/PyCQA/astroid/astroid/__init__.py",
27+
"symbol": "unknown-option-value",
28+
"message": "Unknown option value for 'disable', expected a valid pylint message and got 'Ellipsis'",
29+
"message-id": "W0012"
30+
}
31+
]
32+
}
3033
}

0 commit comments

Comments
 (0)