|
16 | 16 | jobs: |
17 | 17 | test: |
18 | 18 | runs-on: ubuntu-latest |
| 19 | + permissions: |
| 20 | + contents: write |
19 | 21 | steps: |
20 | 22 | - uses: actions/checkout@v2 |
21 | | - - name: Run tests |
22 | | - run: make test |
| 23 | + - name: Run tests with coverage |
| 24 | + run: make coverage |
| 25 | + - name: Run rubocop |
| 26 | + run: make rubocop |
| 27 | + - name: Extract and update coverage badge |
| 28 | + run: | |
| 29 | + # Extract coverage percentage from SimpleCov's .last_run.json |
| 30 | + if [ ! -f coverage/.last_run.json ]; then |
| 31 | + echo "Error: coverage/.last_run.json not found" |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | + |
| 35 | + # Use sed for better portability - account for optional spaces after colon |
| 36 | + COVERAGE=$(sed -n 's/.*"coverage": *\([0-9.]*\).*/\1/p' coverage/.last_run.json | head -1) |
| 37 | + if [ -z "$COVERAGE" ]; then |
| 38 | + echo "Error: Could not extract coverage percentage" |
| 39 | + echo "Contents of coverage/.last_run.json:" |
| 40 | + cat coverage/.last_run.json |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + |
| 44 | + COVERAGE_INT=$(printf "%.0f" "$COVERAGE") |
| 45 | + echo "Coverage: $COVERAGE_INT%" |
| 46 | + |
| 47 | + # Determine badge color based on coverage |
| 48 | + if [ "$COVERAGE_INT" -ge 90 ]; then |
| 49 | + COLOR="brightgreen" |
| 50 | + elif [ "$COVERAGE_INT" -ge 80 ]; then |
| 51 | + COLOR="green" |
| 52 | + elif [ "$COVERAGE_INT" -ge 70 ]; then |
| 53 | + COLOR="yellowgreen" |
| 54 | + elif [ "$COVERAGE_INT" -ge 60 ]; then |
| 55 | + COLOR="yellow" |
| 56 | + else |
| 57 | + COLOR="red" |
| 58 | + fi |
| 59 | + |
| 60 | + # Create badge JSON for shields.io endpoint |
| 61 | + mkdir -p .github/badges |
| 62 | + cat > .github/badges/coverage.json << EOF |
| 63 | + { |
| 64 | + "schemaVersion": 1, |
| 65 | + "label": "coverage", |
| 66 | + "message": "${COVERAGE_INT}%", |
| 67 | + "color": "$COLOR" |
| 68 | + } |
| 69 | + EOF |
| 70 | + |
| 71 | + # Configure git |
| 72 | + git config user.name "github-actions[bot]" |
| 73 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 74 | + |
| 75 | + # Commit and push if changed |
| 76 | + git add .github/badges/coverage.json |
| 77 | + if git diff --staged --quiet; then |
| 78 | + echo "No changes to coverage badge" |
| 79 | + else |
| 80 | + git commit -m "Update coverage badge: ${COVERAGE_INT}%" |
| 81 | + # Pull any changes before pushing |
| 82 | + git pull --rebase origin ${{ github.ref_name }} || true |
| 83 | + git push || { |
| 84 | + echo "Warning: Failed to push badge update" |
| 85 | + exit 0 |
| 86 | + } |
| 87 | + fi |
0 commit comments