Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
- "tests/**"
- "package.json"
- "package-lock.json"
- ".github/workflows/scripts/**"
- ".github/workflows/pr-tests.yml"
push:
branches:
Expand All @@ -22,18 +23,81 @@ on:
- "tests/**"
- "package.json"
- "package-lock.json"
- ".github/workflows/scripts/**"
- ".github/workflows/pr-tests.yml"

jobs:
tests:
runs-on: ubuntu-22.04
permissions:
contents: read

steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
with:
node-version: 22
- run: npm ci
- run: npm test
- run: npm run test:coverage
- run: npm run lint
- run: npm run build
- run: npm run build

update_coverage_badge:
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
needs: tests
runs-on: ubuntu-22.04
permissions:
contents: write

steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- uses: actions/setup-node@v6
with:
node-version: 22
- run: npm ci
- run: npm run test:coverage
- run: node .github/workflows/scripts/update-coverage-badge.mjs
- name: Commit coverage badge
run: |
branch="${GITHUB_REF#refs/heads/}"
max_retries=3

if git diff --quiet -- badges/coverage.json; then
echo "Coverage badge unchanged"
exit 0
fi

git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add badges/coverage.json
git commit -m "Update coverage badge [skip ci]"

for attempt in $(seq 1 "${max_retries}"); do
echo "Attempt ${attempt}/${max_retries}: pushing coverage badge update to ${branch}"

if git push origin "HEAD:${branch}"; then
echo "Coverage badge push succeeded"
exit 0
fi

if [ "${attempt}" -eq "${max_retries}" ]; then
echo "::warning::Failed to push coverage badge after ${max_retries} attempts due to concurrent updates. Skipping without failing CI."
exit 0
fi

echo "Push rejected. Fetching latest origin/${branch} and retrying with rebase..."
if ! git fetch origin "${branch}"; then
echo "::warning::Failed to fetch origin/${branch} while retrying coverage badge push. Skipping without failing CI."
exit 0
fi

if ! git rebase "origin/${branch}"; then
git rebase --abort || true
echo "::warning::Rebase conflict while retrying coverage badge push. Skipping without failing CI."
exit 0
fi

sleep $((attempt * 2))
done
41 changes: 41 additions & 0 deletions .github/workflows/scripts/update-coverage-badge.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from 'node:fs'
import path from 'node:path'

const coverageSummaryPath = 'coverage/coverage-summary.json'
const badgePath = 'badges/coverage.json'

function getBadgeColor(percentage) {
if (percentage >= 90) return 'brightgreen'
if (percentage >= 80) return 'green'
if (percentage >= 70) return 'yellowgreen'
if (percentage >= 60) return 'yellow'
if (percentage >= 50) return 'orange'
return 'red'
}

function main() {
if (!fs.existsSync(coverageSummaryPath)) {
throw new Error(`Coverage summary file not found: ${coverageSummaryPath}`)
}

const summary = JSON.parse(fs.readFileSync(coverageSummaryPath, 'utf8'))
const linesPercentage = Number(summary?.total?.lines?.pct)

if (!Number.isFinite(linesPercentage)) {
throw new Error('Unable to read lines coverage percentage from coverage summary')
}

const roundedPercentage = Number(linesPercentage.toFixed(2))
const badge = {
schemaVersion: 1,
label: 'coverage',
message: `${roundedPercentage}%`,
color: getBadgeColor(roundedPercentage),
}

fs.mkdirSync(path.dirname(badgePath), { recursive: true })
fs.writeFileSync(badgePath, JSON.stringify(badge, null, 2) + '\n')
console.log(`Updated ${badgePath} with lines coverage ${roundedPercentage}%`)
}

main()
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
.vscode/
node_modules/
build/
.coverage/
coverage/
.DS_Store
*.zip
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Deep ChatGPT integrations in your browser, completely for free.
[![release][release-image]][release-url]
[![size](https://img.shields.io/badge/minified%20size-390%20kB-blue)][release-url]
[![verfiy][verify-image]][verify-url]
[![coverage][coverage-image]][coverage-url]

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

Expand Down Expand Up @@ -40,6 +41,10 @@ English   |   [Indonesia](README_IN.md)   | &

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

[coverage-image]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/ChatGPTBox-dev/chatGPTBox/master/badges/coverage.json

[coverage-url]: https://github.com/ChatGPTBox-dev/chatGPTBox/actions/workflows/pr-tests.yml

[Chrome-image]: https://img.shields.io/badge/-Chrome-brightgreen?logo=google-chrome&logoColor=white

[Chrome-url]: https://chrome.google.com/webstore/detail/chatgptbox/eobbhoofkanlmddnplfhnmkfbnlhpbbo
Expand Down
6 changes: 6 additions & 0 deletions badges/coverage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"schemaVersion": 1,
"label": "coverage",
"message": "10.14%",
"color": "red"
}
Loading