|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def _extract_error_line(lines: list[str]) -> str: |
| 10 | + for line in lines: |
| 11 | + if line.startswith("E "): |
| 12 | + return line.strip() |
| 13 | + for line in reversed(lines): |
| 14 | + stripped = line.strip() |
| 15 | + if stripped: |
| 16 | + return stripped |
| 17 | + return "" |
| 18 | + |
| 19 | + |
| 20 | +def main() -> int: |
| 21 | + log_path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("pytest.log") |
| 22 | + log_text = log_path.read_text(errors="ignore") if log_path.exists() else "" |
| 23 | + lines = log_text.splitlines() |
| 24 | + |
| 25 | + error_line = _extract_error_line(lines) |
| 26 | + excerpt_lines = [line.rstrip() for line in lines[-20:]] |
| 27 | + |
| 28 | + package = os.environ.get("PACKAGE_UNDER_TEST", "") |
| 29 | + version = os.environ.get("PACKAGE_VERSION", "") |
| 30 | + |
| 31 | + run_url = ( |
| 32 | + f"{os.environ.get('GITHUB_SERVER_URL', '')}/" |
| 33 | + f"{os.environ.get('GITHUB_REPOSITORY', '')}/actions/runs/" |
| 34 | + f"{os.environ.get('GITHUB_RUN_ID', '')}" |
| 35 | + ) |
| 36 | + |
| 37 | + payload = { |
| 38 | + "workflow": os.environ.get("GITHUB_WORKFLOW"), |
| 39 | + "job": os.environ.get("GITHUB_JOB"), |
| 40 | + "matrix_label": f"{package}=={version}".strip("="), |
| 41 | + "package": package, |
| 42 | + "version": version, |
| 43 | + "run_url": run_url, |
| 44 | + "error_line": error_line, |
| 45 | + "log_excerpt": "\n".join(excerpt_lines), |
| 46 | + } |
| 47 | + |
| 48 | + Path("failure.json").write_text(json.dumps(payload, indent=2)) |
| 49 | + return 0 |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": # pragma: no cover |
| 53 | + raise SystemExit(main()) |
0 commit comments