Files
dance-lessons-coach/scripts/ci-update-coverage-badge.sh
Gabriel Radureau f5e35464b9
Some checks failed
CI/CD Pipeline / Build Docker Cache (push) Successful in 9s
CI/CD Pipeline / CI Pipeline (push) Failing after 4m22s
📊 feat: add separate BDD and unit test coverage badges to distinguish test coverage types
2026-04-08 13:05:06 +02:00

92 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# CI script to update coverage badge in README.md
# Usage: scripts/ci-update-coverage-badge.sh <coverage_percentage> [badge_type]
# badge_type can be "bdd", "unit", or empty for combined coverage
set -e
if [ -z "$1" ]; then
echo "Error: Coverage percentage not provided"
exit 1
fi
COVERAGE=$1
BADGE_TYPE=${2:-"combined"}
# Determine badge color
if (( $(echo "$COVERAGE >= 80" | bc -l) )); then
COLOR="brightgreen"
elif (( $(echo "$COVERAGE >= 50" | bc -l) )); then
COLOR="yellow"
else
COLOR="red"
fi
# Create different badge URLs based on type
if [ "$BADGE_TYPE" = "bdd" ]; then
BADGE_URL="https://img.shields.io/badge/BDD_Coverage-${COVERAGE}%-${COLOR}?style=flat-square"
SEARCH_PATTERN="BDD_Coverage-.*-.*?style=flat-square"
elif [ "$BADGE_TYPE" = "unit" ]; then
BADGE_URL="https://img.shields.io/badge/Unit_Coverage-${COVERAGE}%-${COLOR}?style=flat-square"
SEARCH_PATTERN="Unit_Coverage-.*-.*?style=flat-square"
else
BADGE_URL="https://img.shields.io/badge/coverage-${COVERAGE}%-${COLOR}?style=flat-square"
SEARCH_PATTERN="coverage-.*-.*?style=flat-square"
fi
# Only update if coverage has actually changed
if grep -q "${BADGE_TYPE}_Coverage-${COVERAGE}%" README.md || grep -q "coverage-${COVERAGE}%" README.md; then
echo "Coverage badge already up to date at ${COVERAGE}%"
exit 0
fi
# Update README - handle both old and new badge formats
if [ "$BADGE_TYPE" = "bdd" ] || [ "$BADGE_TYPE" = "unit" ]; then
# For BDD/Unit badges, add them if they don't exist, or update if they do
if grep -q "${BADGE_TYPE}_Coverage" README.md; then
sed -i "s|https://img.shields.io/badge/${BADGE_TYPE}_Coverage-.*-.*?style=flat-square|${BADGE_URL}|" README.md
else
# Add new badge line after the main coverage badge
sed -i "/coverage-.*-.*?style=flat-square/a\\n${BADGE_URL}" README.md
fi
else
# For combined coverage, use the original logic
sed -i "s|https://img.shields.io/badge/coverage-.*-.*?style=flat-square|${BADGE_URL}|" README.md
fi
# Set up git
git config --global user.name "CI Bot"
git config --global user.email "ci@arcodange.fr"
# Set up credentials using Gitea token
if [ -n "$PACKAGES_TOKEN" ]; then
git config --global credential.helper store
echo "https://${PACKAGES_TOKEN}@gitea.arcodange.lab" > ~/.git-credentials
fi
git add README.md
if git commit -m "🤖 chore: update coverage badge to ${COVERAGE}% [skip ci]"; then
# Try push with retry logic for race conditions
for i in 1 2 3; do
if git push; then
echo "Successfully updated coverage badge to ${COVERAGE}%"
# Update local repo to the new HEAD after successful push
git fetch origin
git reset --hard origin/${GITHUB_REF_NAME:-${CI_COMMIT_REF_NAME:-main}}
exit 0
else
echo "Push attempt $i failed, retrying..."
if [ $i -eq 3 ]; then
echo "Final push attempt failed - another job may have updated the badge"
git pull --rebase || true
git push || echo "Recovery push also failed"
# Ensure we're on the latest commit even if push failed
git fetch origin
git reset --hard origin/${GITHUB_REF_NAME:-${CI_COMMIT_REF_NAME:-main}}
fi
sleep 2
fi
done
else
echo "No coverage change to commit"
fi