name: Test Coverage on: pull_request: branches: - main push: branches: - main schedule: # Run weekly on Sundays at 00:00 UTC - cron: '0 0 * * 0' workflow_dispatch: env: NODE_VERSION: '20' PNPM_VERSION: '9.15.0' TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }} TURBO_TEAM: ${{ secrets.TURBO_TEAM }} jobs: test-coverage: name: Test Coverage runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 with: fetch-depth: 0 - name: Setup pnpm uses: pnpm/action-setup@v4 with: version: ${{ env.PNPM_VERSION }} - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VERSION }} cache: 'pnpm' - name: Install dependencies run: pnpm install --frozen-lockfile - name: Build shared packages run: pnpm run build:packages - name: Run tests with coverage run: pnpm run test --coverage || echo "Some tests failed" continue-on-error: true - name: Collect coverage reports run: | # Find all coverage directories find . -type d -name coverage \( -path "*/apps/*/apps/*" -o -path "*/services/*" \) > coverage_dirs.txt # Create combined coverage directory mkdir -p coverage-combined # Copy all coverage files while IFS= read -r dir; do if [ -f "$dir/coverage-final.json" ]; then PROJECT=$(echo $dir | sed 's|./apps/||' | sed 's|./services/||' | sed 's|/coverage||' | tr '/' '-') cp "$dir/coverage-final.json" "coverage-combined/coverage-$PROJECT.json" fi done < coverage_dirs.txt - name: Generate coverage summary run: | echo "## Test Coverage Summary" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY # Find and parse coverage summaries find . -type f -name "coverage-summary.json" | while read -r file; do PROJECT=$(dirname $file | sed 's|./apps/||' | sed 's|./services/||' | sed 's|/coverage||') if [ -f "$file" ]; then LINES=$(jq -r '.total.lines.pct' "$file" 2>/dev/null || echo "0") STATEMENTS=$(jq -r '.total.statements.pct' "$file" 2>/dev/null || echo "0") FUNCTIONS=$(jq -r '.total.functions.pct' "$file" 2>/dev/null || echo "0") BRANCHES=$(jq -r '.total.branches.pct' "$file" 2>/dev/null || echo "0") echo "### $PROJECT" >> $GITHUB_STEP_SUMMARY echo "| Metric | Coverage |" >> $GITHUB_STEP_SUMMARY echo "|--------|----------|" >> $GITHUB_STEP_SUMMARY echo "| Lines | ${LINES}% |" >> $GITHUB_STEP_SUMMARY echo "| Statements | ${STATEMENTS}% |" >> $GITHUB_STEP_SUMMARY echo "| Functions | ${FUNCTIONS}% |" >> $GITHUB_STEP_SUMMARY echo "| Branches | ${BRANCHES}% |" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY fi done - name: Archive coverage reports uses: actions/upload-artifact@v4 with: name: coverage-reports path: | apps/**/coverage services/**/coverage coverage-combined retention-days: 30 if-no-files-found: warn - name: Check coverage thresholds run: | echo "Checking coverage thresholds..." # Set minimum coverage threshold MINIMUM_COVERAGE=50 # Start with 50%, increase gradually # Check each project's coverage find . -type f -name "coverage-summary.json" | while read -r file; do PROJECT=$(dirname $file | sed 's|./apps/||' | sed 's|./services/||' | sed 's|/coverage||') LINES=$(jq -r '.total.lines.pct' "$file" 2>/dev/null || echo "0") echo "Checking $PROJECT: ${LINES}% coverage" # Convert to integer for comparison LINES_INT=$(printf "%.0f" $LINES) if [ "$LINES_INT" -lt "$MINIMUM_COVERAGE" ]; then echo "⚠️ Warning: $PROJECT coverage (${LINES}%) is below minimum threshold (${MINIMUM_COVERAGE}%)" else echo "✅ $PROJECT meets coverage threshold" fi done # Generate coverage badge coverage-badge: name: Update Coverage Badge runs-on: ubuntu-latest needs: test-coverage if: github.ref == 'refs/heads/main' steps: - name: Checkout code uses: actions/checkout@v4 - name: Download coverage reports uses: actions/download-artifact@v4 continue-on-error: true id: download-coverage with: name: coverage-reports path: coverage-reports - name: Create coverage badge if: steps.download-coverage.outcome == 'success' run: | # Calculate overall coverage TOTAL_LINES=0 COVERED_LINES=0 find coverage-reports -type f -name "coverage-summary.json" | while read -r file; do LINES=$(jq -r '.total.lines.total' "$file" 2>/dev/null || echo "0") COVERED=$(jq -r '.total.lines.covered' "$file" 2>/dev/null || echo "0") TOTAL_LINES=$((TOTAL_LINES + LINES)) COVERED_LINES=$((COVERED_LINES + COVERED)) done if [ "$TOTAL_LINES" -gt 0 ]; then COVERAGE=$(echo "scale=2; $COVERED_LINES * 100 / $TOTAL_LINES" | bc) echo "Overall coverage: ${COVERAGE}%" echo "COVERAGE=${COVERAGE}" >> $GITHUB_ENV else echo "No coverage data found" echo "COVERAGE=0" >> $GITHUB_ENV fi - name: Update README badge if: steps.download-coverage.outcome == 'success' run: | echo "Coverage badge data ready: ${{ env.COVERAGE }}%" # This would update a badge in the README or create a gist # Implementation depends on chosen badge service (shields.io, codecov, etc.) - name: Skip badge update if: steps.download-coverage.outcome != 'success' run: echo "No coverage reports available - skipping badge update"