|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from datetime import datetime |
| 7 | +import xml.etree.ElementTree as xml |
| 8 | +import json |
| 9 | + |
| 10 | +SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 11 | +REPO_DIR = os.path.realpath(os.path.join(SCRIPT_DIR, '../../')) |
| 12 | + |
| 13 | +DIFF_COVER_REPORT = "target/diff-cover/report.json" |
| 14 | +MIN_COVERAGE = 90 |
| 15 | + |
| 16 | + |
| 17 | +# ===== Utilities ========================================== |
| 18 | + |
| 19 | +def log(msg): |
| 20 | + print(msg, file=sys.stderr) |
| 21 | + |
| 22 | +# ===== Commands to execute ================================ |
| 23 | + |
| 24 | +def run_command(args, cwd=REPO_DIR): |
| 25 | + return subprocess.run( |
| 26 | + args, |
| 27 | + cwd=cwd, |
| 28 | + check=True, |
| 29 | + stdout=subprocess.PIPE, |
| 30 | + ).stdout.decode('utf-8') |
| 31 | + |
| 32 | +# ===== Commands =========================================== |
| 33 | + |
| 34 | +def comment_report(pr_number, job_url): |
| 35 | + """ |
| 36 | + Generate a report that and post it as a comment on GitHub. |
| 37 | + """ |
| 38 | + |
| 39 | + with open(DIFF_COVER_REPORT) as f: |
| 40 | + report_json = json.load(f) |
| 41 | + coverage_percent = report_json["total_percent_covered"] |
| 42 | + |
| 43 | + if coverage_percent >= MIN_COVERAGE: |
| 44 | + # Coverage passed |
| 45 | + return |
| 46 | + |
| 47 | + src_stats = report_json["src_stats"] |
| 48 | + |
| 49 | + files_summary = "\n" |
| 50 | + for (filename, file_stats) in sorted(src_stats.items(), key=lambda e: (-e[1]["percent_covered"], e[0])): |
| 51 | + covered = file_stats["percent_covered"] |
| 52 | + files_summary += f"{covered:3.0f}%: {filename}\n" |
| 53 | + |
| 54 | + details = f""" |
| 55 | +<details> |
| 56 | +<summary>Details</summary> |
| 57 | +
|
| 58 | +#### Summary |
| 59 | +
|
| 60 | +```{files_summary}``` |
| 61 | +
|
| 62 | +#### How to fix it? |
| 63 | +
|
| 64 | +Add more tests! Currently coverage runs on SWF tests (that test compatibility with Flash Player). |
| 65 | +
|
| 66 | +Sometimes you'll see missing coverage on files you cannot cover (desktop, web code, etc.). \ |
| 67 | +In this case you can ignore the warning for these files. |
| 68 | +</details> |
| 69 | +""" |
| 70 | + |
| 71 | + report = f""" |
| 72 | +⚠️ Coverage check failed: [{coverage_percent}%]({job_url}) |
| 73 | +
|
| 74 | +{details} |
| 75 | +""" |
| 76 | + |
| 77 | + if pr_number: |
| 78 | + run_command([ |
| 79 | + 'gh', 'pr', 'comment', pr_number, |
| 80 | + '--body', report]) |
| 81 | + else: |
| 82 | + print(report) |
| 83 | + |
| 84 | +def main(): |
| 85 | + cmd = sys.argv[1] |
| 86 | + log(f'Running command {cmd}') |
| 87 | + if cmd == 'comment_report': |
| 88 | + pr_number = sys.argv[2] if len(sys.argv) > 2 else "" |
| 89 | + job_url = sys.argv[3] if len(sys.argv) > 3 else "" |
| 90 | + comment_report(pr_number, job_url) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
0 commit comments