Update tests.yml #34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: tests | |
| on: | |
| push: | |
| branches: [master] | |
| pull_request: | |
| branches: [master] | |
| jobs: | |
| tests: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| php: [8.4] | |
| stability: [prefer-stable] | |
| name: PHP ${{ matrix.php }} - ${{ matrix.stability }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup PHP | |
| uses: shivammathur/setup-php@v2 | |
| with: | |
| php-version: ${{ matrix.php }} | |
| extensions: intl | |
| coverage: none | |
| tools: composer:v2 | |
| - name: Setup problem matchers | |
| run: | | |
| echo "::add-matcher::${{ runner.tool_cache }}/php.json" | |
| echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" | |
| - name: Install dependencies | |
| run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress | |
| - name: Execute tests | |
| run: vendor/bin/phpunit | |
| coverage: | |
| runs-on: ubuntu-latest | |
| name: Test Coverage | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup PHP | |
| uses: shivammathur/setup-php@v2 | |
| with: | |
| php-version: 8.4 | |
| extensions: intl, xdebug | |
| coverage: xdebug | |
| tools: composer:v2 | |
| - name: Install dependencies | |
| run: composer update --prefer-stable --prefer-dist --no-interaction --no-progress | |
| - name: Execute tests with coverage | |
| run: | | |
| mkdir -p build/logs | |
| XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover build/logs/clover.xml | |
| - name: Upload coverage to Codecov | |
| uses: codecov/codecov-action@v4 | |
| with: | |
| file: ./build/logs/clover.xml | |
| fail_ci_if_error: false | |
| token: ${{ secrets.CODECOV_TOKEN }} | |
| - name: Extract coverage percentage | |
| id: extract_coverage | |
| run: | | |
| # Install xml parsing tool | |
| sudo apt-get install -y xmlstarlet | |
| # Extract coverage using xmlstarlet | |
| COVERAGE=$(xmlstarlet sel -t -v "sum(//file/metrics/@statements)" -o "/" -v "sum(//file/metrics/@coveredstatements)" build/logs/clover.xml) | |
| # Calculate percentage | |
| TOTAL=$(echo $COVERAGE | cut -d'/' -f1) | |
| COVERED=$(echo $COVERAGE | cut -d'/' -f2) | |
| # Check for division by zero | |
| if [ "$TOTAL" -eq "0" ]; then | |
| PERCENTAGE="0.00" | |
| else | |
| PERCENTAGE=$(echo "scale=2; 100 * $COVERED / $TOTAL" | bc) | |
| fi | |
| echo "percentage=$PERCENTAGE" >> $GITHUB_OUTPUT | |
| echo "Coverage percentage: $PERCENTAGE% ($COVERED/$TOTAL statements covered)" | |
| - name: Determine badge color | |
| id: badge_color | |
| run: | | |
| PERCENTAGE=${{ steps.extract_coverage.outputs.percentage }} | |
| if (( $(echo "$PERCENTAGE >= 90" | bc -l) )); then | |
| COLOR="brightgreen" | |
| elif (( $(echo "$PERCENTAGE >= 80" | bc -l) )); then | |
| COLOR="green" | |
| elif (( $(echo "$PERCENTAGE >= 70" | bc -l) )); then | |
| COLOR="yellowgreen" | |
| elif (( $(echo "$PERCENTAGE >= 60" | bc -l) )); then | |
| COLOR="yellow" | |
| elif (( $(echo "$PERCENTAGE >= 50" | bc -l) )); then | |
| COLOR="orange" | |
| else | |
| COLOR="red" | |
| fi | |
| echo "color=$COLOR" >> $GITHUB_OUTPUT | |
| echo "Badge color: $COLOR" | |
| - name: Update coverage badge via direct API call | |
| env: | |
| GIST_ID: ${{ secrets.GIST_ID }} | |
| GIST_TOKEN: ${{ secrets.GIST_SECRET }} | |
| PERCENTAGE: ${{ steps.extract_coverage.outputs.percentage }} | |
| COLOR: ${{ steps.badge_color.outputs.color }} | |
| run: | | |
| # Create JSON data for the badge | |
| BADGE_JSON='{ | |
| "schemaVersion": 1, | |
| "label": "coverage", | |
| "message": "'$PERCENTAGE'%", | |
| "color": "'$COLOR'" | |
| }' | |
| # Create JSON payload for GitHub API | |
| GIST_CONTENT='{ | |
| "files": { | |
| "polyglot-coverage.json": { | |
| "content": '$(echo "$BADGE_JSON" | jq -R .)' | |
| } | |
| } | |
| }' | |
| # Update the Gist using curl | |
| curl -X PATCH \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| -H "Authorization: token $GIST_TOKEN" \ | |
| -d "$GIST_CONTENT" \ | |
| "https://api.github.com/gists/$GIST_ID" |