|
7 | 7 | push: |
8 | 8 | branches: |
9 | 9 | - "main" |
10 | | - pull_request: |
11 | | - paths: |
12 | | - - .github/workflows/update-coverage-badge.yaml |
13 | 10 | workflow_dispatch: |
14 | 11 |
|
15 | 12 | jobs: |
|
19 | 16 | permissions: |
20 | 17 | contents: write |
21 | 18 | pull-requests: write |
22 | | - |
| 19 | + |
23 | 20 | steps: |
24 | 21 | - name: Checkout code |
25 | 22 | uses: actions/checkout@v4 |
@@ -62,40 +59,61 @@ jobs: |
62 | 59 | echo "color=red" >> $GITHUB_OUTPUT |
63 | 60 | fi |
64 | 61 |
|
65 | | - - name: Setup GitHub CLI |
| 62 | + - name: Create Coverage Badge SVG |
66 | 63 | if: github.event_name != 'pull_request' |
| 64 | + id: create_badge |
67 | 65 | run: | |
68 | | - gh auth setup-git |
69 | | - env: |
70 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 66 | + mkdir -p badges |
| 67 | + # Create SVG badge file |
| 68 | + cat > badges/coverage.svg << EOF |
| 69 | + <svg xmlns="http://www.w3.org/2000/svg" width="106" height="20"> |
| 70 | + <linearGradient id="b" x2="0" y2="100%"> |
| 71 | + <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> |
| 72 | + <stop offset="1" stop-opacity=".1"/> |
| 73 | + </linearGradient> |
| 74 | + <mask id="a"> |
| 75 | + <rect width="106" height="20" rx="3" fill="#fff"/> |
| 76 | + </mask> |
| 77 | + <g mask="url(#a)"> |
| 78 | + <path fill="#555" d="M0 0h61v20H0z"/> |
| 79 | + <path fill="#${{ steps.coverage.outputs.color }}" d="M61 0h45v20H61z"/> |
| 80 | + <path fill="url(#b)" d="M0 0h106v20H0z"/> |
| 81 | + </g> |
| 82 | + <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"> |
| 83 | + <text x="30.5" y="15" fill="#010101" fill-opacity=".3">coverage</text> |
| 84 | + <text x="30.5" y="14">coverage</text> |
| 85 | + <text x="82.5" y="15" fill="#010101" fill-opacity=".3">${{ steps.coverage.outputs.percentage }}%</text> |
| 86 | + <text x="82.5" y="14">${{ steps.coverage.outputs.percentage }}%</text> |
| 87 | + </g> |
| 88 | + </svg> |
| 89 | + EOF |
| 90 | + echo "Created coverage badge SVG" |
71 | 91 |
|
72 | | - - name: Update README.md with Coverage Badge |
| 92 | + - name: Push Badge to Dedicated Branch |
73 | 93 | if: github.event_name != 'pull_request' |
74 | 94 | env: |
75 | 95 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
76 | 96 | run: | |
77 | | - # Create the shields.io URL with the coverage data |
78 | | - BADGE_URL="https://img.shields.io/badge/coverage-${{ steps.coverage.outputs.percentage }}%25-${{ steps.coverage.outputs.color }}" |
| 97 | + BADGE_BRANCH="coverage-badge" |
79 | 98 | |
80 | | - # Update the README.md file |
81 | | - sed -i -E "s|(\!\[License\]\([^)]+\))|\1 \|" README.md |
| 99 | + # Setup git |
| 100 | + git config --local user.email "[email protected]" |
| 101 | + git config --local user.name "Coverage Badge Action" |
82 | 102 | |
83 | | - # Check if the README has been modified |
84 | | - if git diff --exit-code README.md; then |
85 | | - echo "No changes to README.md" |
| 103 | + # Try to fetch the badge branch, or create it if it doesn't exist |
| 104 | + if ! git fetch origin $BADGE_BRANCH; then |
| 105 | + # Create an orphan branch for the badge |
| 106 | + git checkout --orphan $BADGE_BRANCH |
| 107 | + git rm -rf . |
86 | 108 | else |
87 | | - # Create a new branch |
88 | | - BRANCH_NAME="update-coverage-badge-$(date +%Y%m%d%H%M%S)" |
89 | | - git config --local user.email "[email protected]" |
90 | | - git config --local user.name "Update Coverage Badge Action" |
91 | | - git checkout -b $BRANCH_NAME |
92 | | - git add README.md |
93 | | - git commit -m "docs: update test coverage badge" |
94 | | - git push --set-upstream origin $BRANCH_NAME |
95 | | - |
96 | | - # Create a pull request using GitHub CLI |
97 | | - gh pr create --title "Update coverage badge" \ |
98 | | - --body "Automatically updates the test coverage badge in README.md" \ |
99 | | - --base main \ |
100 | | - --head $BRANCH_NAME |
101 | | - fi |
| 109 | + git checkout $BADGE_BRANCH |
| 110 | + fi |
| 111 | + |
| 112 | + # Copy over just the badge SVG |
| 113 | + mkdir -p badges |
| 114 | + cp ../badges/coverage.svg badges/ |
| 115 | + |
| 116 | + # Commit and push the badge |
| 117 | + git add badges/coverage.svg |
| 118 | + git commit -m "chore: update coverage badge [skip ci]" || echo "No changes to commit" |
| 119 | + git push origin $BADGE_BRANCH |
0 commit comments