|
| 1 | +name: PR Quality Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, synchronize] |
| 6 | + |
| 7 | +permissions: |
| 8 | + issues: write |
| 9 | + pull-requests: write |
| 10 | + contents: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + quality-gate: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + ref: ${{ github.event.pull_request.head.sha }} |
| 19 | + |
| 20 | + - name: Check PR quality signals |
| 21 | + uses: actions/github-script@v7 |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const pr = context.payload.pull_request; |
| 25 | + const author = pr.user.login; |
| 26 | +
|
| 27 | + // Don't check the maintainer |
| 28 | + if (author === 'Him-an-shi') return; |
| 29 | +
|
| 30 | + const { data: files } = await github.rest.pulls.listFiles({ |
| 31 | + owner: context.repo.owner, |
| 32 | + repo: context.repo.repo, |
| 33 | + pull_number: pr.number, |
| 34 | + }); |
| 35 | +
|
| 36 | + const warnings = []; |
| 37 | +
|
| 38 | + // 1. Flag: Very low line count (trivial PR) |
| 39 | + const totalChanges = files.reduce((sum, f) => sum + f.additions + f.deletions, 0); |
| 40 | + if (totalChanges < 5) { |
| 41 | + warnings.push(`Trivial change (${totalChanges} lines total). Consider if this warrants a standalone PR or could be bundled with related work.`); |
| 42 | + } |
| 43 | +
|
| 44 | + // 2. Flag: Test-only PR with low assertion density |
| 45 | + const testFiles = files.filter(f => f.filename.includes('test')); |
| 46 | + if (testFiles.length > 0 && testFiles.length === files.length) { |
| 47 | + // PR only touches test files - check assertion quality |
| 48 | + for (const tf of testFiles) { |
| 49 | + try { |
| 50 | + const { data: content } = await github.rest.repos.getContent({ |
| 51 | + owner: context.repo.owner, |
| 52 | + repo: context.repo.repo, |
| 53 | + path: tf.filename, |
| 54 | + ref: pr.head.sha, |
| 55 | + }); |
| 56 | + const decoded = Buffer.from(content.content, 'base64').toString(); |
| 57 | + const assertCount = (decoded.match(/assert |assertEqual|assertTrue|assertFalse|assertRaises|assertIn|pytest\.raises/g) || []).length; |
| 58 | + const testCount = (decoded.match(/def test_/g) || []).length; |
| 59 | +
|
| 60 | + if (testCount > 0 && assertCount / testCount < 1.5) { |
| 61 | + warnings.push(`\`${tf.filename}\`: Low assertion density (${assertCount} asserts across ${testCount} tests). Each test should have multiple meaningful assertions.`); |
| 62 | + } |
| 63 | + } catch (e) { |
| 64 | + // File might be too large or binary - skip |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +
|
| 69 | + // 3. Flag: PR opened within 10 minutes of its linked issue |
| 70 | + const body = pr.body || ''; |
| 71 | + const issueMatch = body.match(/#(\d+)/); |
| 72 | + if (issueMatch) { |
| 73 | + try { |
| 74 | + const { data: linkedIssue } = await github.rest.issues.get({ |
| 75 | + owner: context.repo.owner, |
| 76 | + repo: context.repo.repo, |
| 77 | + issue_number: parseInt(issueMatch[1]), |
| 78 | + }); |
| 79 | + const issueTime = new Date(linkedIssue.created_at).getTime(); |
| 80 | + const prTime = new Date(pr.created_at).getTime(); |
| 81 | + const diffMinutes = (prTime - issueTime) / (1000 * 60); |
| 82 | +
|
| 83 | + if (diffMinutes >= 0 && diffMinutes < 10) { |
| 84 | + warnings.push(`PR opened ${Math.round(diffMinutes)} minutes after linked issue #${issueMatch[1]}. Unusually fast turnaround may indicate pre-written/automated submission.`); |
| 85 | + } |
| 86 | + } catch (e) { |
| 87 | + // Issue not found - skip |
| 88 | + } |
| 89 | + } |
| 90 | +
|
| 91 | + if (warnings.length > 0) { |
| 92 | + await github.rest.issues.createComment({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + issue_number: pr.number, |
| 96 | + body: `**Quality check observations:**\n\n${warnings.map(w => '- ' + w).join('\n')}\n\n_These are informational signals, not blocking. Maintainer will review._` |
| 97 | + }); |
| 98 | + } |
0 commit comments