-
Notifications
You must be signed in to change notification settings - Fork 180
fix(ci): wire coverage observability into CI pipelines (Phase 1) #1404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+205
−3
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #!/usr/bin/env python3 | ||
| """Render a coverage.json file as a Markdown summary. | ||
|
|
||
| Usage: | ||
| python3 scripts/coverage-summary.py coverage.json [--title "My Title"] | ||
|
|
||
| Writes Markdown to stdout and, if GITHUB_STEP_SUMMARY is set, appends to | ||
| that file so the summary appears in the GitHub Actions job summary panel. | ||
|
|
||
| Exit codes: | ||
| 0 Always -- missing coverage.json is treated as a soft warning, not | ||
| an error, so CI steps that set ``if: always()`` never fail here. | ||
| """ | ||
|
|
||
| import argparse | ||
| import json | ||
| import os | ||
| import pathlib | ||
| import sys | ||
|
|
||
|
|
||
| def _parse_args() -> argparse.Namespace: | ||
| parser = argparse.ArgumentParser( | ||
| description="Render a coverage.json file as a Markdown summary." | ||
| ) | ||
| parser.add_argument( | ||
| "coverage_json", | ||
| metavar="COVERAGE_JSON", | ||
| help="Path to coverage.json produced by `coverage json`.", | ||
| ) | ||
| parser.add_argument( | ||
| "--title", | ||
| default="Code Coverage Report", | ||
| help="Heading text for the summary (default: 'Code Coverage Report').", | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def _build_markdown(data: dict, title: str) -> str: | ||
| totals = data.get("totals", {}) | ||
| pct = totals.get("percent_covered_display", "N/A") | ||
| stmts = totals.get("num_statements", 0) | ||
| miss = totals.get("missing_lines", 0) | ||
| covered = stmts - miss | ||
|
|
||
| lines: list[str] = [] | ||
| lines.append(f"## {title}") | ||
| lines.append("") | ||
| lines.append(f"**Overall: {pct}%** ({covered:,}/{stmts:,} statements)") | ||
| lines.append("") | ||
| lines.append("| Metric | Value |") | ||
| lines.append("|--------|-------|") | ||
| lines.append(f"| Statements | {stmts:,} |") | ||
| lines.append(f"| Covered | {covered:,} |") | ||
| lines.append(f"| Missed | {miss:,} |") | ||
| lines.append(f"| Coverage | {pct}% |") | ||
|
sergio-sisternes-epam marked this conversation as resolved.
|
||
| lines.append("") | ||
|
|
||
| files = data.get("files", {}) | ||
| ranked = sorted( | ||
| files.items(), | ||
| key=lambda kv: kv[1].get("summary", {}).get("percent_covered", 100.0), | ||
| ) | ||
| # Always show bottom-10 files so the section appears even when | ||
| # overall coverage is high (acceptance criteria: "collapsible | ||
| # lowest-coverage files" in every summary). | ||
| bottom = ranked[:10] | ||
|
|
||
| if bottom: | ||
| lines.append("<details>") | ||
| lines.append("<summary>Lowest-coverage files</summary>") | ||
| lines.append("") | ||
| lines.append("| File | Stmts | Miss | Cover |") | ||
| lines.append("|------|-------|------|-------|") | ||
| for fpath, fdata in bottom: | ||
| s = fdata.get("summary", {}) | ||
| # Strip common prefix to keep the table narrow. | ||
| short = fpath.replace("src/apm_cli/", "") | ||
| fp = s.get("percent_covered_display", "?") | ||
| lines.append( | ||
| f"| `{short}` | {s.get('num_statements', 0)}" | ||
| f" | {s.get('missing_lines', 0)} | {fp}% |" | ||
| ) | ||
|
sergio-sisternes-epam marked this conversation as resolved.
|
||
| lines.append("") | ||
| lines.append("</details>") | ||
| lines.append("") | ||
|
|
||
| return "\n".join(lines) + "\n" | ||
|
|
||
|
|
||
| def main() -> None: | ||
| args = _parse_args() | ||
| coverage_path = pathlib.Path(args.coverage_json) | ||
|
|
||
| if not coverage_path.exists(): | ||
| print( | ||
| f"[!] coverage-summary: {coverage_path} not found -- skipping summary.", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(0) | ||
|
|
||
| try: | ||
| data = json.loads(coverage_path.read_text(encoding="utf-8")) | ||
| except (json.JSONDecodeError, OSError) as exc: | ||
| print( | ||
| f"[x] coverage-summary: failed to read {coverage_path}: {exc}", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(0) | ||
|
|
||
| md = _build_markdown(data, args.title) | ||
| print(md, end="") | ||
|
|
||
| summary_path = os.environ.get("GITHUB_STEP_SUMMARY", "") | ||
| if summary_path: | ||
| try: | ||
| with open(summary_path, "a", encoding="utf-8") as fh: | ||
| fh.write(md) | ||
| except OSError as exc: | ||
| print( | ||
| f"[!] coverage-summary: could not write to GITHUB_STEP_SUMMARY: {exc}", | ||
| file=sys.stderr, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.