Repository navigation
Add CI coverage reporting for backend and frontend #1315
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
Open
dgee2
wants to merge
4
commits into
main
Choose a base branch
from
codex/issue-1158-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c5715c2
Add CI coverage reporting for backend and frontend
dgee2 5790491
Fetch trusted coverage script without checkout
dgee2 2274d3d
Inline trusted coverage parser in reporting workflow
dgee2 eba4920
Bound coverage archive parsing in privileged reporter
dgee2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| name: Coverage report | ||
|
|
||
| on: | ||
| workflow_run: | ||
| workflows: [Build] | ||
| types: [completed] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| comment: | ||
| if: github.event.workflow_run.event == 'pull_request' | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| actions: read | ||
| pull-requests: write | ||
| steps: | ||
| - name: Find the current pull request | ||
| id: pr | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| HEAD_SHA: ${{ github.event.workflow_run.head_sha }} | ||
| HEAD_REPOSITORY: ${{ github.event.workflow_run.head_repository.full_name }} | ||
| run: | | ||
| number=$(gh api "repos/$GITHUB_REPOSITORY/pulls?state=open&per_page=100" --paginate | | ||
| jq -sr --arg sha "$HEAD_SHA" --arg repo "$HEAD_REPOSITORY" \ | ||
| '[.[][] | select(.head.sha == $sha and .head.repo.full_name == $repo) | .number] | first // empty') | ||
| if [ -n "$number" ]; then | ||
| echo "number=$number" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Download bounded coverage archives | ||
| if: steps.pr.outputs.number != '' | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| RUN_ID: ${{ github.event.workflow_run.id }} | ||
| run: | | ||
| gh api "repos/$GITHUB_REPOSITORY/actions/runs/$RUN_ID/artifacts?per_page=100" > "$RUNNER_TEMP/artifacts.json" | ||
| mkdir -p "$RUNNER_TEMP/coverage" | ||
| for suite in backend-unit backend-integration frontend; do | ||
| id=$(jq -r --arg name "coverage-$suite" \ | ||
| '[.artifacts[] | select(.name == $name and .expired == false and | ||
| .size_in_bytes > 0 and .size_in_bytes <= 10000000) | .id] | first // empty' \ | ||
| "$RUNNER_TEMP/artifacts.json") | ||
| if [[ "$id" =~ ^[0-9]+$ ]]; then | ||
| archive="$RUNNER_TEMP/coverage/coverage-$suite.zip" | ||
| if ! gh api "repos/$GITHUB_REPOSITORY/actions/artifacts/$id/zip" > "$archive"; then | ||
| rm -f "$archive" | ||
| elif [ "$(stat -c %s "$archive")" -gt 10000000 ]; then | ||
| rm -f "$archive" | ||
| fi | ||
| fi | ||
| done | ||
|
|
||
| - name: Build coverage comment | ||
| if: steps.pr.outputs.number != '' | ||
| id: summary | ||
| env: | ||
| RUN_URL: ${{ github.event.workflow_run.html_url }} | ||
| run: | | ||
| python3 - "$RUNNER_TEMP/coverage" "$RUNNER_TEMP/coverage-comment.md" "$RUN_URL" <<'PY' | ||
| """Build a PR coverage comment from reports produced by the Build workflow.""" | ||
|
|
||
| import json | ||
| import math | ||
| import sys | ||
| import xml.etree.ElementTree as ET | ||
| from pathlib import Path | ||
| from zipfile import BadZipFile, ZipFile | ||
|
|
||
|
|
||
| def percentage(value: object, scale: int = 1) -> str: | ||
| number = float(value) * scale | ||
| if not math.isfinite(number) or not 0 <= number <= 100: | ||
| raise ValueError("Invalid coverage percentage") | ||
| return f"{number:.1f}%" | ||
|
|
||
|
|
||
| def read_member(archive: Path, member: str, allowed: set[str]) -> bytes: | ||
| with ZipFile(archive) as zipped: | ||
| infos = zipped.infolist() | ||
| if len(infos) > len(allowed) or any( | ||
| info.filename not in allowed or info.is_dir() for info in infos | ||
| ): | ||
| raise ValueError("Unexpected coverage archive entries") | ||
| if sum(info.file_size for info in infos) > len(allowed) * 10_000_000: | ||
| raise ValueError("Coverage archive expands beyond its limit") | ||
| matches = [info for info in infos if info.filename == member] | ||
| if len(matches) != 1 or matches[0].file_size > 10_000_000: | ||
| raise ValueError("Missing, duplicate, or oversized coverage report") | ||
| with zipped.open(matches[0]) as source: | ||
| document = source.read(10_000_001) | ||
| if len(document) != matches[0].file_size: | ||
| raise ValueError("Coverage report exceeds its declared size") | ||
| return document | ||
|
|
||
|
|
||
| def cobertura_metrics(document: bytes) -> tuple[str, str]: | ||
| if b"<!DOCTYPE" in document.upper() or b"<!ENTITY" in document.upper(): | ||
| raise ValueError("XML declarations are not allowed in coverage reports") | ||
| root = ET.fromstring(document) | ||
| if root.tag != "coverage": | ||
| raise ValueError("Expected a Cobertura coverage document") | ||
| return percentage(root.attrib["line-rate"], 100), percentage( | ||
| root.attrib["branch-rate"], 100 | ||
| ) | ||
|
|
||
|
|
||
| def vitest_metrics(document: bytes) -> tuple[str, str]: | ||
| if len(document) > 5_000_000: | ||
| raise ValueError("Vitest summary is too large") | ||
| summary = json.loads(document) | ||
| return percentage(summary["total"]["lines"]["pct"]), percentage( | ||
| summary["total"]["branches"]["pct"] | ||
| ) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| artifact_dir = Path(sys.argv[1]) | ||
| output_path = Path(sys.argv[2]) | ||
| run_url = sys.argv[3] | ||
| reports = ( | ||
| ( | ||
| "MenuApi.Tests", | ||
| "backend-unit", | ||
| "MenuApi.Tests.cobertura.xml", | ||
| cobertura_metrics, | ||
| ), | ||
| ( | ||
| "MenuDB.Tests", | ||
| "backend-unit", | ||
| "MenuDB.Tests.cobertura.xml", | ||
| cobertura_metrics, | ||
| ), | ||
| ( | ||
| "MenuApi.Integration.Tests", | ||
| "backend-integration", | ||
| "MenuApi.Integration.Tests.cobertura.xml", | ||
| cobertura_metrics, | ||
| ), | ||
| ( | ||
| "Frontend", | ||
| "frontend", | ||
| "coverage-summary.json", | ||
| vitest_metrics, | ||
| ), | ||
| ) | ||
| allowed = { | ||
| "backend-unit": {"MenuApi.Tests.cobertura.xml", "MenuDB.Tests.cobertura.xml"}, | ||
| "backend-integration": {"MenuApi.Integration.Tests.cobertura.xml"}, | ||
| "frontend": {"coverage-summary.json"}, | ||
| } | ||
|
|
||
| rows = [] | ||
| for label, suite, member, read_metrics in reports: | ||
| archive = artifact_dir / f"coverage-{suite}.zip" | ||
| if not archive.is_file(): | ||
| continue | ||
| try: | ||
| document = read_member(archive, member, allowed[suite]) | ||
| lines, branches = read_metrics(document) | ||
| except (BadZipFile, ET.ParseError, KeyError, OSError, RuntimeError, | ||
| TypeError, ValueError, NotImplementedError) as error: | ||
| print(f"Skipping invalid {label} coverage report: {error}", file=sys.stderr) | ||
| continue | ||
| rows.append(f"| {label} | {lines} | {branches} |") | ||
|
|
||
| content = ( | ||
| ["| Suite | Lines | Branches |", "| --- | ---: | ---: |", *rows] | ||
| if rows | ||
| else [ | ||
| "No coverage report was produced for this run. " | ||
| "Tests may have been skipped or stopped early." | ||
| ] | ||
| ) | ||
| comment = "\n".join( | ||
| [ | ||
| "<!-- menu-ci-coverage -->", | ||
| "### Test coverage", | ||
| "", | ||
| *content, | ||
| "", | ||
| f"[View the test run]({run_url})", | ||
| "", | ||
| ] | ||
| ) | ||
| output_path.write_text(comment, encoding="utf-8") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| PY | ||
| if [ -s "$RUNNER_TEMP/coverage-comment.md" ]; then | ||
| echo 'available=true' >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Comment coverage on pull request | ||
| if: steps.summary.outputs.available == 'true' | ||
| uses: mshick/add-pr-comment@ec328af66588ab8f77cdeb2c264f14aba45bbf59 # v3 | ||
| continue-on-error: true | ||
| with: | ||
| issue: ${{ steps.pr.outputs.number }} | ||
| message-id: menu-ci-coverage | ||
| message-path: ${{ runner.temp }}/coverage-comment.md | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When two open PRs share the same commit—such as separate branches pointing at one SHA or one head targeting different accepted base branches—this query matches both and arbitrarily selects
first. Both completed runs can consequently update the same sticky comment while the other PR receives none. Resolve fromworkflow_run.pull_requestswhen available and make the fork fallback reject or disambiguate multiple matches.Useful? React with 👍 / 👎.