|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2010 New Relic, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import json |
| 17 | +import os |
| 18 | +import re |
| 19 | +from pathlib import Path |
| 20 | +from textwrap import dedent |
| 21 | + |
| 22 | +REPO_DIR = Path(__file__).parent.parent.parent |
| 23 | +TOX_DIR = REPO_DIR / ".tox" |
| 24 | +GITHUB_SUMMARY = Path(os.environ.get("GITHUB_STEP_SUMMARY", TOX_DIR / "summary.md")) |
| 25 | +RESULTS_FILE_RE = re.compile( |
| 26 | + r"(?P<job_name>[a-zA-Z0-9_-]+)-(?P<job_num>\d+)-(?P<run_id>[a-zA-Z0-9]+)-(?P<job_id>[a-zA-Z0-9_-]+)-results.json" |
| 27 | +) |
| 28 | + |
| 29 | +GITHUB_SERVER_URL = os.environ.get("GITHUB_SERVER_URL", "https://github.com") |
| 30 | +GITHUB_REPOSITORY = os.environ.get("GITHUB_REPOSITORY", "newrelic/newrelic-python-agent") |
| 31 | + |
| 32 | +TABLE_HEADER = """ |
| 33 | +# Tox Results Summary |
| 34 | +
|
| 35 | +| Environment | Status | Duration (s) | Setup Duration (s) | Test Duration (s) | Runner | |
| 36 | +|-------------|--------|--------------|--------------------|-------------------|--------| |
| 37 | +""" |
| 38 | +TABLE_HEADER = dedent(TABLE_HEADER).strip() |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + results = {} |
| 43 | + # Search both repo and .tox dirs |
| 44 | + filepaths = list(REPO_DIR.glob("*-results.json")) + list(TOX_DIR.glob("*-results.json")) |
| 45 | + for filepath in filepaths: |
| 46 | + with filepath.open() as f: |
| 47 | + # Load the JSON data |
| 48 | + data = json.load(f) |
| 49 | + envs = data.get("testenvs", ()) |
| 50 | + |
| 51 | + # Extract GitHub info from filename |
| 52 | + match = RESULTS_FILE_RE.match(filepath.name) |
| 53 | + if match: |
| 54 | + runner_link = f"{GITHUB_SERVER_URL}/{GITHUB_REPOSITORY}/actions/runs/{match.group('run_id')}/job/{match.group('job_id')}" |
| 55 | + runner = f"[{match.group('job_name')} ({match.group('job_num')})]({runner_link})" |
| 56 | + else: |
| 57 | + runner = "N/A" |
| 58 | + |
| 59 | + # Aggregate any non-empty results |
| 60 | + sub_results = {k: v for k, v in envs.items() if v and k != ".pkg"} |
| 61 | + for result in sub_results.values(): |
| 62 | + result["runner"] = runner |
| 63 | + results.update(sub_results) |
| 64 | + |
| 65 | + if not results: |
| 66 | + raise RuntimeError("No tox results found.") |
| 67 | + |
| 68 | + with GITHUB_SUMMARY.open("w") as output_fp: |
| 69 | + summary = summarize_results(results) |
| 70 | + # Print table header |
| 71 | + print(TABLE_HEADER, file=output_fp) |
| 72 | + |
| 73 | + for result in summary: |
| 74 | + line = "| {env_name} | {status} | {duration} | {setup_duration} | {test_duration} | {runner} |".format( |
| 75 | + **result |
| 76 | + ) |
| 77 | + print(line, file=output_fp) |
| 78 | + |
| 79 | + |
| 80 | +def summarize_results(results): |
| 81 | + summary = [] |
| 82 | + for env, result in results.items(): |
| 83 | + duration = result["result"].get("duration", 0) |
| 84 | + duration = f"{duration:.2f}" if duration >= 0 else "N/A" |
| 85 | + status = "OK ✅" if result["result"]["success"] else "FAIL ❌" |
| 86 | + runner = result.get("runner", "N/A") |
| 87 | + |
| 88 | + # Sum up setup and test durations from individual commands |
| 89 | + setup_duration = 0 |
| 90 | + for cmd in result.get("setup", ()): |
| 91 | + setup_duration += cmd.get("elapsed", 0) |
| 92 | + setup_duration = f"{setup_duration:.2f}" if setup_duration >= 0 else "N/A" |
| 93 | + |
| 94 | + test_duration = 0 |
| 95 | + for cmd in result.get("test", ()): |
| 96 | + test_duration += cmd.get("elapsed", 0) |
| 97 | + test_duration = f"{test_duration:.2f}" if test_duration >= 0 else "N/A" |
| 98 | + |
| 99 | + summary.append( |
| 100 | + { |
| 101 | + "env_name": env, |
| 102 | + "status": status, |
| 103 | + "duration": duration, |
| 104 | + "setup_duration": setup_duration, |
| 105 | + "test_duration": test_duration, |
| 106 | + "runner": runner, |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + return sorted(summary, key=lambda result: (1 if "OK" in result["status"] else 0, result["env_name"])) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments