Skip to content

Commit ddc9ef8

Browse files
committed
Show source references and a single revision link in lint alerts
1 parent bd045ae commit ddc9ef8

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

‎scripts/lint_notification.py‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,28 @@
55
import subprocess
66
import sys
77
from pathlib import Path
8-
from urllib.parse import quote
8+
9+
10+
def source_location(po_file, lineno):
11+
"""Find the source references for the PO entry containing the diagnostic."""
12+
try:
13+
lines = po_file.read_text(encoding="utf-8").splitlines()
14+
index = int(lineno) - 1
15+
if not 0 <= index < len(lines):
16+
return None
17+
start = index
18+
while start > 0 and lines[start - 1].strip():
19+
start -= 1
20+
end = index
21+
while end < len(lines) and lines[end].strip():
22+
end += 1
23+
references = []
24+
for line in lines[start:end]:
25+
if line.startswith("#: "):
26+
references.extend(re.findall(r"(?:^|\s)(.+?:\d+)(?=\s|$)", line[3:]))
27+
return ", ".join(dict.fromkeys(references)) or None
28+
except (OSError, UnicodeError):
29+
return None
930

1031

1132
def main(log, output):
@@ -18,7 +39,7 @@ def main(log, output):
1839
).strip()
1940
header = f"❌ *{version} lint* ([execução {run}]({repo}/actions/runs/{run}))"
2041
footer = "Mais diagnósticos no log da execução."
21-
message = header
42+
message = header + f"\n[Revisão das traduções]({repo}/tree/{revision}/)"
2243
found = False
2344
for line in sorted(set(log.read_text(encoding="utf-8").splitlines())):
2445
match = re.fullmatch(r"(.+?\.po):(\d+):\s+(.+\(.*\))", line)
@@ -29,9 +50,9 @@ def main(log, output):
2950
path = Path(filename)
3051
if path.is_absolute():
3152
path = path.relative_to(checkout.resolve())
32-
url = f"{repo}/blob/{revision}/{quote(path.as_posix())}#L{lineno}"
33-
detail = f"{path}:{lineno}: {diagnostic}".replace("`", "\\`")
34-
block = f"\n\n```\n{detail}\n```\n[Abrir linha {lineno}]({url})"
53+
location = source_location(checkout / path, lineno) or f"{path}:{lineno}"
54+
detail = f"{location}: {diagnostic}".replace("`", "\\`")
55+
block = f"\n\n```\n{detail}\n```"
3556
# Count the unparsed text conservatively, including Markdown and emoji.
3657
if len((message + block + "\n\n" + footer).encode("utf-16-le")) // 2 > 4096:
3758
message += "\n\n" + footer

0 commit comments

Comments
 (0)