Skip to content

Commit 31331b7

Browse files
Add README coverage badge and PR coverage checks
Run the PR test workflow through npm run test:coverage and generate a repo-hosted coverage badge payload from coverage summary output. Use c8 directly from npm scripts and emit text, lcov, and json-summary reports so CI can update badge data without external coverage services. Replace the README coverage badge source with a shields endpoint that reads badges/coverage.json, auto-commit badge updates on master pushes, handle non-fast-forward push races with fetch/rebase retries, and scope write token permission to a push-only badge update job. Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com>
1 parent edebfa2 commit 31331b7

7 files changed

Lines changed: 572 additions & 134 deletions

File tree

.github/workflows/pr-tests.yml

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ on:
1212
- "tests/**"
1313
- "package.json"
1414
- "package-lock.json"
15+
- ".github/workflows/scripts/**"
1516
- ".github/workflows/pr-tests.yml"
1617
push:
1718
branches:
@@ -22,18 +23,81 @@ on:
2223
- "tests/**"
2324
- "package.json"
2425
- "package-lock.json"
26+
- ".github/workflows/scripts/**"
2527
- ".github/workflows/pr-tests.yml"
2628

2729
jobs:
2830
tests:
2931
runs-on: ubuntu-22.04
32+
permissions:
33+
contents: read
3034

3135
steps:
3236
- uses: actions/checkout@v6
3337
- uses: actions/setup-node@v6
3438
with:
3539
node-version: 22
3640
- run: npm ci
37-
- run: npm test
41+
- run: npm run test:coverage
3842
- run: npm run lint
39-
- run: npm run build
43+
- run: npm run build
44+
45+
update_coverage_badge:
46+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
47+
needs: tests
48+
runs-on: ubuntu-22.04
49+
permissions:
50+
contents: write
51+
52+
steps:
53+
- uses: actions/checkout@v6
54+
with:
55+
fetch-depth: 0
56+
- uses: actions/setup-node@v6
57+
with:
58+
node-version: 22
59+
- run: npm ci
60+
- run: npm run test:coverage
61+
- run: node .github/workflows/scripts/update-coverage-badge.mjs
62+
- name: Commit coverage badge
63+
run: |
64+
branch="${GITHUB_REF#refs/heads/}"
65+
max_retries=3
66+
67+
if git diff --quiet -- badges/coverage.json; then
68+
echo "Coverage badge unchanged"
69+
exit 0
70+
fi
71+
72+
git config user.name "github-actions[bot]"
73+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
74+
git add badges/coverage.json
75+
git commit -m "Update coverage badge [skip ci]"
76+
77+
for attempt in $(seq 1 "${max_retries}"); do
78+
echo "Attempt ${attempt}/${max_retries}: pushing coverage badge update to ${branch}"
79+
80+
if git push origin "HEAD:${branch}"; then
81+
echo "Coverage badge push succeeded"
82+
exit 0
83+
fi
84+
85+
if [ "${attempt}" -eq "${max_retries}" ]; then
86+
echo "::warning::Failed to push coverage badge after ${max_retries} attempts due to concurrent updates. Skipping without failing CI."
87+
exit 0
88+
fi
89+
90+
echo "Push rejected. Fetching latest origin/${branch} and retrying with rebase..."
91+
if ! git fetch origin "${branch}"; then
92+
echo "::warning::Failed to fetch origin/${branch} while retrying coverage badge push. Skipping without failing CI."
93+
exit 0
94+
fi
95+
96+
if ! git rebase "origin/${branch}"; then
97+
git rebase --abort || true
98+
echo "::warning::Rebase conflict while retrying coverage badge push. Skipping without failing CI."
99+
exit 0
100+
fi
101+
102+
sleep $((attempt * 2))
103+
done
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
3+
4+
const coverageSummaryPath = 'coverage/coverage-summary.json'
5+
const badgePath = 'badges/coverage.json'
6+
7+
function getBadgeColor(percentage) {
8+
if (percentage >= 90) return 'brightgreen'
9+
if (percentage >= 80) return 'green'
10+
if (percentage >= 70) return 'yellowgreen'
11+
if (percentage >= 60) return 'yellow'
12+
if (percentage >= 50) return 'orange'
13+
return 'red'
14+
}
15+
16+
function main() {
17+
if (!fs.existsSync(coverageSummaryPath)) {
18+
throw new Error(`Coverage summary file not found: ${coverageSummaryPath}`)
19+
}
20+
21+
const summary = JSON.parse(fs.readFileSync(coverageSummaryPath, 'utf8'))
22+
const linesPercentage = Number(summary?.total?.lines?.pct)
23+
24+
if (!Number.isFinite(linesPercentage)) {
25+
throw new Error('Unable to read lines coverage percentage from coverage summary')
26+
}
27+
28+
const roundedPercentage = Number(linesPercentage.toFixed(2))
29+
const badge = {
30+
schemaVersion: 1,
31+
label: 'coverage',
32+
message: `${roundedPercentage}%`,
33+
color: getBadgeColor(roundedPercentage),
34+
}
35+
36+
fs.mkdirSync(path.dirname(badgePath), { recursive: true })
37+
fs.writeFileSync(badgePath, JSON.stringify(badge, null, 2) + '\n')
38+
console.log(`Updated ${badgePath} with lines coverage ${roundedPercentage}%`)
39+
}
40+
41+
main()

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
.vscode/
33
node_modules/
44
build/
5+
.coverage/
6+
coverage/
57
.DS_Store
68
*.zip

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Deep ChatGPT integrations in your browser, completely for free.
1212
[![release][release-image]][release-url]
1313
[![size](https://img.shields.io/badge/minified%20size-390%20kB-blue)][release-url]
1414
[![verfiy][verify-image]][verify-url]
15+
[![coverage][coverage-image]][coverage-url]
1516

1617
English &nbsp;&nbsp;|&nbsp;&nbsp; [Indonesia](README_IN.md) &nbsp;&nbsp;|&nbsp;&nbsp; [简体中文](README_ZH.md) &nbsp;&nbsp;|&nbsp;&nbsp; [日本語](README_JA.md) &nbsp;&nbsp;|&nbsp;&nbsp; [Türkçe](README_TR.md)
1718

@@ -40,6 +41,10 @@ English &nbsp;&nbsp;|&nbsp;&nbsp; [Indonesia](README_IN.md) &nbsp;&nbsp;|&nbsp;&
4041

4142
[verify-url]: https://github.com/ChatGPTBox-dev/chatGPTBox/actions/workflows/verify-configs.yml
4243

44+
[coverage-image]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/ChatGPTBox-dev/chatGPTBox/master/badges/coverage.json
45+
46+
[coverage-url]: https://github.com/ChatGPTBox-dev/chatGPTBox/actions/workflows/pr-tests.yml
47+
4348
[Chrome-image]: https://img.shields.io/badge/-Chrome-brightgreen?logo=google-chrome&logoColor=white
4449

4550
[Chrome-url]: https://chrome.google.com/webstore/detail/chatgptbox/eobbhoofkanlmddnplfhnmkfbnlhpbbo

badges/coverage.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"schemaVersion": 1,
3+
"label": "coverage",
4+
"message": "10.14%",
5+
"color": "red"
6+
}

0 commit comments

Comments
 (0)