|
| 1 | +name: Pull Request Validation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [master] |
| 6 | + types: [opened, synchronize, reopened, ready_for_review] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + checks: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + validate: |
| 15 | + name: Validate PR |
| 16 | + runs-on: ubuntu-latest |
| 17 | + if: github.event.pull_request.draft == false |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Setup PHP |
| 26 | + uses: shivammathur/setup-php@v2 |
| 27 | + with: |
| 28 | + php-version: 8.3 |
| 29 | + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv |
| 30 | + coverage: xdebug |
| 31 | + |
| 32 | + - name: Validate composer.json and composer.lock |
| 33 | + run: composer validate --strict |
| 34 | + |
| 35 | + - name: Cache Composer packages |
| 36 | + id: composer-cache |
| 37 | + uses: actions/cache@v3 |
| 38 | + with: |
| 39 | + path: vendor |
| 40 | + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} |
| 41 | + restore-keys: | |
| 42 | + ${{ runner.os }}-php- |
| 43 | +
|
| 44 | + - name: Install dependencies |
| 45 | + run: composer install --prefer-dist --no-progress |
| 46 | + |
| 47 | + - name: Check code style (PSR-2) |
| 48 | + run: vendor/bin/phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests |
| 49 | + |
| 50 | + - name: Run test suite |
| 51 | + run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml |
| 52 | + |
| 53 | + - name: Check test coverage |
| 54 | + run: | |
| 55 | + COVERAGE=$(php -r " |
| 56 | + \$xml = simplexml_load_file('coverage.xml'); |
| 57 | + \$metrics = \$xml->project->metrics; |
| 58 | + \$percentage = (\$metrics['coveredstatements'] / \$metrics['statements']) * 100; |
| 59 | + echo round(\$percentage, 2); |
| 60 | + ") |
| 61 | + echo "Code Coverage: $COVERAGE%" |
| 62 | + if (( $(echo "$COVERAGE < 70" | bc -l) )); then |
| 63 | + echo "❌ Coverage is below 70%" |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + echo "✅ Coverage is acceptable ($COVERAGE%)" |
| 67 | +
|
| 68 | + - name: Comment PR with results |
| 69 | + uses: actions/github-script@v7 |
| 70 | + if: always() |
| 71 | + with: |
| 72 | + script: | |
| 73 | + const fs = require('fs'); |
| 74 | + let coverage = 'N/A'; |
| 75 | + try { |
| 76 | + const xml = fs.readFileSync('coverage.xml', 'utf8'); |
| 77 | + const match = xml.match(/statements="(\d+)".*coveredstatements="(\d+)"/); |
| 78 | + if (match) { |
| 79 | + const total = parseInt(match[1]); |
| 80 | + const covered = parseInt(match[2]); |
| 81 | + coverage = ((covered / total) * 100).toFixed(2) + '%'; |
| 82 | + } |
| 83 | + } catch (e) { |
| 84 | + console.log('Could not read coverage'); |
| 85 | + } |
| 86 | + |
| 87 | + const body = `## 🔍 Pull Request Validation Results |
| 88 | + |
| 89 | + - ✅ **Code Style**: PSR-2 compliant |
| 90 | + - ✅ **Tests**: All passing |
| 91 | + - 📊 **Coverage**: ${coverage} |
| 92 | + - 🚀 **Ready to merge** |
| 93 | + |
| 94 | + *Automated validation by GitHub Actions*`; |
| 95 | + |
| 96 | + github.rest.issues.createComment({ |
| 97 | + issue_number: context.issue.number, |
| 98 | + owner: context.repo.owner, |
| 99 | + repo: context.repo.repo, |
| 100 | + body: body |
| 101 | + }); |
0 commit comments