|
8 | 8 | validate-pr: |
9 | 9 | runs-on: ubuntu-latest |
10 | 10 | steps: |
| 11 | + - name: Check for Spam PR |
| 12 | + uses: actions/github-script@v7 |
| 13 | + with: |
| 14 | + script: | |
| 15 | + const prTitle = context.payload.pull_request.title; |
| 16 | + const spamRegex = /^(feat|chore|fix)(\(.*\))?\s*:/i; |
| 17 | +
|
| 18 | + if (spamRegex.test(prTitle)) { |
| 19 | + // Leave a comment explaining why |
| 20 | + await github.rest.issues.createComment({ |
| 21 | + owner: context.repo.owner, |
| 22 | + repo: context.repo.repo, |
| 23 | + issue_number: context.payload.pull_request.number, |
| 24 | + body: `## PR Closed as Spam |
| 25 | +
|
| 26 | + This PR was automatically closed because the title format \`feat:\`, \`fix:\`, or \`chore:\` is commonly associated with spam contributions. |
| 27 | +
|
| 28 | + If this is a legitimate contribution, please: |
| 29 | + 1. Review our contribution guidelines |
| 30 | + 2. Use the correct PR title format: \`directory, ...: description\` |
| 31 | + 3. Open a new PR with the proper title format |
| 32 | +
|
| 33 | + Thank you for your understanding.` |
| 34 | + }); |
| 35 | +
|
| 36 | + // Close the PR |
| 37 | + await github.rest.pulls.update({ |
| 38 | + owner: context.repo.owner, |
| 39 | + repo: context.repo.repo, |
| 40 | + pull_number: context.payload.pull_request.number, |
| 41 | + state: 'closed' |
| 42 | + }); |
| 43 | +
|
| 44 | + core.setFailed('PR closed as spam due to suspicious title format'); |
| 45 | + return; |
| 46 | + } |
| 47 | +
|
| 48 | + console.log('✅ PR passed spam check'); |
| 49 | +
|
| 50 | + - name: Checkout repository |
| 51 | + uses: actions/checkout@v4 |
| 52 | + |
11 | 53 | - name: Check PR Title Format |
12 | 54 | uses: actions/github-script@v7 |
13 | 55 | with: |
14 | 56 | script: | |
| 57 | + const fs = require('fs'); |
| 58 | + const path = require('path'); |
15 | 59 | const prTitle = context.payload.pull_request.title; |
16 | 60 | const titleRegex = /^([\w\s,{}/.]+): .+/; |
17 | 61 | |
18 | 62 | if (!titleRegex.test(prTitle)) { |
19 | 63 | core.setFailed(`PR title "${prTitle}" does not match required format: directory, ...: description`); |
20 | 64 | return; |
21 | 65 | } |
| 66 | +
|
| 67 | + const match = prTitle.match(titleRegex); |
| 68 | + const dirPart = match[1]; |
| 69 | + const directories = dirPart.split(',').map(d => d.trim()); |
| 70 | + const missingDirs = []; |
| 71 | + for (const dir of directories) { |
| 72 | + const fullPath = path.join(process.env.GITHUB_WORKSPACE, dir); |
| 73 | + if (!fs.existsSync(fullPath)) { |
| 74 | + missingDirs.push(dir); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (missingDirs.length > 0) { |
| 79 | + core.setFailed(`The following directories in the PR title do not exist: ${missingDirs.join(', ')}`); |
| 80 | + return; |
| 81 | + } |
22 | 82 | |
23 | 83 | console.log('✅ PR title format is valid'); |
0 commit comments