|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Parse JUnit XML and report test failures. |
| 3 | +
|
| 4 | +Outputs: |
| 5 | + 1. Formatted table to stdout (visible in the step's log) |
| 6 | + 2. ::error annotations (visible in the check run annotations) |
| 7 | + 3. Markdown table to $GITHUB_STEP_SUMMARY |
| 8 | + 4. PR comment via gh CLI (best-effort, skips on permission errors) |
| 9 | +
|
| 10 | +Usage: python3 junit-report.py <junit-xml-path> <heading> |
| 11 | +""" |
| 12 | +import os |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | +import xml.etree.ElementTree as ET |
| 16 | + |
| 17 | +def main(): |
| 18 | + if len(sys.argv) < 2: |
| 19 | + print("Usage: junit-report.py <junit.xml> [heading]", file=sys.stderr) |
| 20 | + sys.exit(1) |
| 21 | + |
| 22 | + xml_path = sys.argv[1] |
| 23 | + heading = sys.argv[2] if len(sys.argv) > 2 else "Test Failures" |
| 24 | + |
| 25 | + if not os.path.exists(xml_path): |
| 26 | + return |
| 27 | + |
| 28 | + tree = ET.parse(xml_path) |
| 29 | + failures = [] |
| 30 | + for tc in tree.iter("testcase"): |
| 31 | + f = tc.find("failure") |
| 32 | + if f is not None: |
| 33 | + pkg = tc.get("classname", "") |
| 34 | + name = tc.get("name", "") |
| 35 | + # Prefer the failure body for detail, fall back to message attr |
| 36 | + detail = (f.text or f.get("message") or "").strip() |
| 37 | + # Find the first meaningful line (skip === RUN, blank lines, timestamps) |
| 38 | + short = "" |
| 39 | + for line in detail.split("\n"): |
| 40 | + line = line.strip() |
| 41 | + if line and not line.startswith("=== RUN") and not line.startswith("--- FAIL"): |
| 42 | + short = line[:200] |
| 43 | + break |
| 44 | + if not short: |
| 45 | + short = f.get("message", "failed") |
| 46 | + failures.append((pkg, name, short, detail)) |
| 47 | + |
| 48 | + if not failures: |
| 49 | + return |
| 50 | + |
| 51 | + # 1. Formatted output to stdout (visible in step log) |
| 52 | + print(f"\n{'=' * 60}") |
| 53 | + print(f" {heading}: {len(failures)} failed") |
| 54 | + print(f"{'=' * 60}\n") |
| 55 | + for pkg, name, short, detail in failures: |
| 56 | + print(f" FAIL {pkg}.{name}") |
| 57 | + # Indent the detail for readability |
| 58 | + for line in detail.split("\n")[:15]: |
| 59 | + print(f" {line}") |
| 60 | + print() |
| 61 | + print(f"{'=' * 60}\n") |
| 62 | + |
| 63 | + # 2. ::error annotations |
| 64 | + for pkg, name, short, _ in failures: |
| 65 | + print(f"::error title=FAIL {pkg}.{name}::{short}") |
| 66 | + |
| 67 | + # 3. $GITHUB_STEP_SUMMARY |
| 68 | + summary_path = os.environ.get("GITHUB_STEP_SUMMARY", "") |
| 69 | + if summary_path: |
| 70 | + with open(summary_path, "a") as out: |
| 71 | + out.write(f"## {heading}\n\n") |
| 72 | + out.write("| Package | Test | Error |\n") |
| 73 | + out.write("|---------|------|-------|\n") |
| 74 | + for pkg, name, short, _ in failures: |
| 75 | + # Escape pipes in the message for markdown tables |
| 76 | + safe = short.replace("|", "\\|") |
| 77 | + out.write(f"| `{pkg}` | `{name}` | {safe} |\n") |
| 78 | + |
| 79 | + # 4. PR comment (best-effort) |
| 80 | + pr_number = os.environ.get("PR_NUMBER", "") |
| 81 | + if not pr_number: |
| 82 | + return |
| 83 | + |
| 84 | + comment_marker = f"<!-- junit-report: {heading} -->" |
| 85 | + body_lines = [ |
| 86 | + comment_marker, |
| 87 | + f"## {heading}", |
| 88 | + "", |
| 89 | + "| Package | Test | Error |", |
| 90 | + "|---------|------|-------|", |
| 91 | + ] |
| 92 | + for pkg, name, short, _ in failures: |
| 93 | + safe = short.replace("|", "\\|") |
| 94 | + body_lines.append(f"| `{pkg}` | `{name}` | {safe} |") |
| 95 | + body_lines.append("") |
| 96 | + body_lines.append("_Updated by CI — this comment is replaced on each push._") |
| 97 | + body = "\n".join(body_lines) |
| 98 | + |
| 99 | + try: |
| 100 | + # Check for existing comment to update |
| 101 | + result = subprocess.run( |
| 102 | + ["gh", "pr", "view", pr_number, "--json", "comments", |
| 103 | + "--jq", f'.comments[] | select(.body | startswith("{comment_marker}")) | .url'], |
| 104 | + capture_output=True, text=True, timeout=15, |
| 105 | + ) |
| 106 | + existing_url = result.stdout.strip().split("\n")[0] if result.stdout.strip() else "" |
| 107 | + |
| 108 | + if existing_url: |
| 109 | + # Extract comment ID from URL and update |
| 110 | + comment_id = existing_url.rstrip("/").split("/")[-1] |
| 111 | + subprocess.run( |
| 112 | + ["gh", "api", f"repos/{{owner}}/{{repo}}/issues/comments/{comment_id}", |
| 113 | + "-X", "PATCH", "-f", f"body={body}"], |
| 114 | + capture_output=True, timeout=15, |
| 115 | + ) |
| 116 | + else: |
| 117 | + subprocess.run( |
| 118 | + ["gh", "pr", "comment", pr_number, "--body", body], |
| 119 | + capture_output=True, timeout=15, |
| 120 | + ) |
| 121 | + except Exception: |
| 122 | + pass # Best-effort — don't fail the step |
| 123 | + |
| 124 | + # Exit non-zero so the step shows as failed (red X) in the UI |
| 125 | + sys.exit(1) |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments