Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/scripts/coverage-badge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""Reads a Cobertura report, writes a coverage badge (SVG) and a Markdown summary."""
import sys
import xml.etree.ElementTree as ET

report, badge, summary = sys.argv[1:4]
root = ET.parse(report).getroot()
line = float(root.attrib["line-rate"]) * 100
branch = float(root.attrib["branch-rate"]) * 100

color = "#4c1" if line >= 90 else "#dfb317" if line >= 75 else "#e05d44"
label, value = "coverage", f"{line:.0f}%"
lw, vw = 61, 6 * len(value) + 14
w = lw + vw

with open(badge, "w") as f:
f.write(f"""<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="20" role="img" aria-label="{label}: {value}">
<linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient>
<clipPath id="r"><rect width="{w}" height="20" rx="3" fill="#fff"/></clipPath>
<g clip-path="url(#r)"><rect width="{lw}" height="20" fill="#555"/><rect x="{lw}" width="{vw}" height="20" fill="{color}"/><rect width="{w}" height="20" fill="url(#s)"/></g>
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
<text x="{lw / 2}" y="15" fill="#010101" fill-opacity=".3">{label}</text><text x="{lw / 2}" y="14">{label}</text>
<text x="{lw + vw / 2}" y="15" fill="#010101" fill-opacity=".3">{value}</text><text x="{lw + vw / 2}" y="14">{value}</text>
</g></svg>
""")

rows = "\n".join(
f"| {c.attrib['name']} | {float(c.attrib['line-rate']) * 100:.0f}% |"
for c in sorted(root.iter("class"), key=lambda c: c.attrib["name"])
if "/" not in c.attrib["name"] and "<" not in c.attrib["name"]
)
with open(summary, "w") as f:
f.write(f"## Code coverage\n\nLines: **{line:.1f}%** · Branches: **{branch:.1f}%**\n\n| Class | Lines |\n|---|---|\n{rows}\n")
20 changes: 19 additions & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
runs-on: ${{ matrix.os }}
permissions:
contents: write
steps:
- uses: actions/checkout@v7
with:
Expand All @@ -25,4 +27,20 @@ jobs:
- name: Build
run: dotnet build -c Release --no-restore
- name: Test
run: dotnet test -c Release --no-build --verbosity normal
run: dotnet test -c Release --no-build --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./coverage
- name: Coverage summary and badge
if: matrix.os == 'ubuntu-latest'
run: |
python3 .github/scripts/coverage-badge.py "$(ls coverage/*/coverage.cobertura.xml | head -n1)" coverage/coverage.svg "$GITHUB_STEP_SUMMARY"
- name: Publish coverage badge
if: matrix.os == 'ubuntu-latest' && github.event_name == 'push'
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
cd coverage
git init -q -b badges
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add coverage.svg
git commit -q -m "Update coverage badge"
git push -f "https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git" badges
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -756,4 +756,7 @@ FodyWeavers.xsd
### VisualStudio Patch ###
# Additional files built by Visual Studio

# End of https://www.toptal.com/developers/gitignore/api/dotnetcore,linux,windows,macos,jetbrains,rider,visualstudio,visualstudiocode,aspnetcore
# End of https://www.toptal.com/developers/gitignore/api/dotnetcore,linux,windows,macos,jetbrains,rider,visualstudio,visualstudiocode,aspnetcore

# Local coverage output
/coverage/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![.NET](https://github.com/capjan/regex/actions/workflows/dotnet.yml/badge.svg)](https://github.com/capjan/regex/actions/workflows/dotnet.yml)
[![Coverage](https://raw.githubusercontent.com/capjan/regex/badges/coverage.svg)](https://github.com/capjan/regex/actions/workflows/dotnet.yml)

# regex

Expand Down
Loading