|
| 1 | +#!/usr/bin/env python |
| 2 | +# This module summarizes the results of site validation tests queued by |
| 3 | +# workflow validate_modified_targets for presentation in Issue comments. |
| 4 | + |
| 5 | +from defusedxml import ElementTree as ET |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +def summarize_junit_xml(xml_path: Path) -> str: |
| 10 | + tree = ET.parse(xml_path) |
| 11 | + root = tree.getroot() |
| 12 | + suite = root.find('testsuite') |
| 13 | + |
| 14 | + pass_message: str = ":heavy_check_mark: Pass" |
| 15 | + fail_message: str = ":x: Fail" |
| 16 | + |
| 17 | + if suite is None: |
| 18 | + raise ValueError("Invalid JUnit XML: No testsuite found") |
| 19 | + |
| 20 | + summary_lines: list[str] = [] |
| 21 | + summary_lines.append("#### Automatic validation of changes\n") |
| 22 | + summary_lines.append("| | F- Check | F+ Check |") |
| 23 | + summary_lines.append("|---|---|---|") |
| 24 | + |
| 25 | + failures = int(suite.get('failures', 0)) |
| 26 | + errors_detected: bool = False |
| 27 | + |
| 28 | + results: dict[str, dict[str, str]] = {} |
| 29 | + |
| 30 | + for testcase in suite.findall('testcase'): |
| 31 | + test_name = testcase.get('name').split('[')[0] |
| 32 | + site_name = testcase.get('name').split('[')[1].rstrip(']') |
| 33 | + failure = testcase.find('failure') |
| 34 | + error = testcase.find('error') |
| 35 | + |
| 36 | + if site_name not in results: |
| 37 | + results[site_name] = {} |
| 38 | + |
| 39 | + if test_name == "test_false_neg": |
| 40 | + results[site_name]['F- Check'] = pass_message if failure is None and error is None else fail_message |
| 41 | + elif test_name == "test_false_pos": |
| 42 | + results[site_name]['F+ Check'] = pass_message if failure is None and error is None else fail_message |
| 43 | + |
| 44 | + if error is not None: |
| 45 | + errors_detected = True |
| 46 | + |
| 47 | + for result in results: |
| 48 | + summary_lines.append(f"| {result} | {results[result].get('F- Check', 'Error!')} | {results[result].get('F+ Check', 'Error!')} |") |
| 49 | + |
| 50 | + if failures > 0: |
| 51 | + summary_lines.append("\n___\n" + |
| 52 | + "\nFailures were detected on at least one updated target. Commits containing accuracy failures" + |
| 53 | + " will often not be merged (unless a rationale is provided, such as false negatives due to regional differences).") |
| 54 | + |
| 55 | + if errors_detected: |
| 56 | + summary_lines.append("\n___\n" + |
| 57 | + "\n**Errors were detected during validation. Please review the workflow logs.**") |
| 58 | + |
| 59 | + return "\n".join(summary_lines) |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + if len(sys.argv) != 2: |
| 63 | + print("Usage: summarize_site_validation.py <junit-xml-file>") |
| 64 | + sys.exit(1) |
| 65 | + |
| 66 | + xml_path: Path = Path(sys.argv[1]) |
| 67 | + if not xml_path.is_file(): |
| 68 | + print(f"Error: File '{xml_path}' does not exist.") |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + summary: str = summarize_junit_xml(xml_path) |
| 72 | + print(summary) |
0 commit comments