diff --git a/scripts/lint_notification.py b/scripts/lint_notification.py new file mode 100644 index 000000000..33a7af9b9 --- /dev/null +++ b/scripts/lint_notification.py @@ -0,0 +1,46 @@ +"""Format sphinx-lint diagnostics for the translation team's Telegram alert.""" + +import os +import re +import subprocess +import sys +from pathlib import Path +from urllib.parse import quote + + +def main(log, output): + repo = os.environ["PYDOC_REPO"].rstrip("/") + run = os.environ["GITHUB_RUN_ID"] + version = os.environ["PYDOC_VERSION"] + checkout = Path("cpython/Doc/locales") / os.environ["PYDOC_LANGUAGE"] / "LC_MESSAGES" + revision = subprocess.check_output( + ["git", "-C", str(checkout), "rev-parse", "HEAD"], universal_newlines=True + ).strip() + header = f"❌ *{version} lint* ([execução {run}]({repo}/actions/runs/{run}))" + footer = "Mais diagnósticos no log da execução." + message = header + found = False + for line in sorted(set(log.read_text(encoding="utf-8").splitlines())): + match = re.fullmatch(r"(.+?\.po):(\d+):\s+(.+\(.*\))", line) + if not match: + continue + found = True + filename, lineno, diagnostic = match.groups() + path = Path(filename) + if path.is_absolute(): + path = path.relative_to(checkout.resolve()) + url = f"{repo}/blob/{revision}/{quote(path.as_posix())}#L{lineno}" + detail = f"{path}:{lineno}: {diagnostic}".replace("`", "\\`") + block = f"\n\n```\n{detail}\n```\n[Abrir linha {lineno}]({url})" + # Count the unparsed text conservatively, including Markdown and emoji. + if len((message + block + "\n\n" + footer).encode("utf-16-le")) // 2 > 4096: + message += "\n\n" + footer + break + message += block + if not found: + sys.exit("No sphinx-lint diagnostics found") + output.write_text(message, encoding="utf-8") + + +if __name__ == "__main__": + main(Path(sys.argv[1]), Path(sys.argv[2])) diff --git a/scripts/prepmsg.sh b/scripts/prepmsg.sh index db50d9c7c..b00434e1e 100755 --- a/scripts/prepmsg.sh +++ b/scripts/prepmsg.sh @@ -16,6 +16,10 @@ URL="${PYDOC_REPO}/actions/runs/${GITHUB_RUN_ID}" input="$1" output="$2" +if [[ "${GITHUB_JOB}" == "lint" ]]; then + exec python3 "$(dirname "$0")/lint_notification.py" "$input" "$output" +fi + touch aux if [[ "${GITHUB_JOB}" == "build" ]]; then grep 'cpython/Doc/.*WARNING:' "$input" | \ @@ -23,12 +27,6 @@ if [[ "${GITHUB_JOB}" == "build" ]]; then uniq | \ sed 's|^|```\n|;s|$|\n```\n|' \ > aux -elif [[ "${GITHUB_JOB}" == "lint" ]]; then - grep -P '^.*\.po:\d+:\s+.*\(.*\)$' "$input" | \ - sed 's|.*/cpython/Doc/||' | \ - sort -u | \ - sed 's|^|```\n|;s|$|\n```\n|' \ - > aux else die "Unexpected job name ${GITHUB_JOB}" fi