Skip to content

Update Coverage Badge #41

Update Coverage Badge

Update Coverage Badge #41

Workflow file for this run

name: Update Coverage Badge
on:
workflow_run:
workflows: ["Rust CI"]
types:
- completed
jobs:
update-badge:
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_branch }}
- name: Download coverage artifacts
uses: actions/download-artifact@v4
with:
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Generate coverage badge
run: |
# Extract Rust coverage from lcov.info
if [ -f "rust-coverage-report/lcov.info" ]; then
# Calculate line coverage from lcov.info
LINES_FOUND=$(grep -E "^LF:" rust-coverage-report/lcov.info | awk -F: '{sum += $2} END {print sum}')
LINES_HIT=$(grep -E "^LH:" rust-coverage-report/lcov.info | awk -F: '{sum += $2} END {print sum}')
if [ "$LINES_FOUND" -gt 0 ]; then
COVERAGE=$(echo "scale=1; $LINES_HIT * 100 / $LINES_FOUND" | bc)
else
COVERAGE="0"
fi
else
COVERAGE="0"
fi
echo "Coverage: $COVERAGE%"
# Determine color based on coverage
if (( $(echo "$COVERAGE >= 80" | bc -l) )); then
COLOR="green"
elif (( $(echo "$COVERAGE >= 60" | bc -l) )); then
COLOR="yellow"
else
COLOR="red"
fi
# Create badge JSON
mkdir -p .github/badges
echo "{\"schemaVersion\": 1, \"label\": \"coverage\", \"message\": \"${COVERAGE}%\", \"color\": \"$COLOR\"}" > .github/badges/coverage.json
# Only commit and push badge updates for non-main branches (PRs)
# For main branch, the badge will be updated when the PR merges
if [ "${{ github.event.workflow_run.head_branch }}" != "main" ]; then
git config user.name github-actions
git config user.email github-actions@github.com
git add .github/badges/coverage.json
if ! git diff --staged --quiet; then
git commit -m "Update coverage badge [skip ci]"
# Push to the PR branch
git push origin HEAD:${{ github.event.workflow_run.head_branch }}
else
echo "✅ No changes to coverage badge"
fi
else
echo "✅ Running on main branch - badge updates happen via PR merges"
fi