@@ -11,11 +11,15 @@ name: test-coverage
1111jobs :
1212 test-coverage :
1313 runs-on : ubuntu-latest
14+ permissions :
15+ contents : write
1416 env :
1517 GITHUB_PAT : ${{ secrets.GITHUB_TOKEN }}
1618
1719 steps :
1820 - uses : actions/checkout@v4
21+ with :
22+ token : ${{ secrets.GITHUB_TOKEN }}
1923
2024 - uses : r-lib/actions/setup-r@v2
2125 with :
2630 extra-packages : any::covr
2731 needs : coverage
2832
29- - name : Install cpp4rtest
33+ - name : Install package
3034 run : |
3135 options(warn = 2)
3236 pak::pkg_install("pachadotdev/cpp4r", dependencies = TRUE)
@@ -55,21 +59,35 @@ jobs:
5559 color <- "red"
5660 }
5761
58- # Write coverage info to environment
59- writeLines(paste0("COVERAGE=", percent), Sys.getenv("GITHUB_ENV"))
60- writeLines(paste0("COVERAGE_COLOR=", color), Sys.getenv("GITHUB_ENV"))
62+ writeLines(as.character(percent), "coverage_percent.txt")
63+ writeLines(color, "coverage_color.txt")
6164 shell : Rscript {0}
6265
6366 - name : Generate coverage badge
6467 run : |
68+ COVERAGE=$(cat coverage_percent.txt)
69+ COVERAGE_COLOR=$(cat coverage_color.txt)
70+
71+ echo "Coverage: $COVERAGE%"
72+ echo "Color: $COVERAGE_COLOR"
73+
6574 mkdir -p badges
66- curl -s "https://img.shields.io/badge/coverage-${{ env.COVERAGE }}%25-${{ env.COVERAGE_COLOR }}" > badges/coverage.svg
75+ curl -s "https://raw.githubusercontent.com/pachadotdev/ghacoveragebadge/refs/heads/main/badges/${COVERAGE_COLOR}.svg" > badges/coverage.svg
76+
77+ # each badge contains a placeholder {{ COVERAGE }} that we replace with the actual coverage percentage
78+ sed -i "s/{{ COVERAGE }}/${COVERAGE}/g" badges/coverage.svg
6779
6880 - name : Commit coverage badge
6981 if : github.ref == 'refs/heads/main'
7082 run : |
7183 git config --local user.email "[email protected] " 7284 git config --local user.name "GitHub Action"
7385 git add badges/coverage.svg
74- git diff --staged --quiet || git commit -m "Update coverage badge [skip ci]" || true
75- git push || true
86+ if ! git diff --staged --quiet; then
87+ # Amend the last commit to include the updated badge, preserving its message
88+ git commit --amend --no-edit
89+ # Safely update the remote with force-with-lease to avoid clobbering concurrent changes
90+ git push --force-with-lease origin main
91+ else
92+ echo "No changes to commit"
93+ fi
0 commit comments