|
| 1 | +name: Duplicate Issue Check |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + issues: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-duplicate: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Check for duplicate issues |
| 15 | + uses: actions/github-script@v7 |
| 16 | + with: |
| 17 | + script: | |
| 18 | + const issue = context.payload.issue; |
| 19 | + const title = issue.title.toLowerCase().replace(/[^a-z0-9\s]/g, '').trim(); |
| 20 | +
|
| 21 | + // Extract core topic from test issue titles |
| 22 | + // "test: add unit tests for foo.ts" → "foo" |
| 23 | + const testMatch = title.match(/test\s*add\s*(?:unit\s*)?tests?\s*(?:for|of)\s*(.+)/); |
| 24 | + const topic = testMatch ? testMatch[1].replace(/\.ts$/, '').trim() : null; |
| 25 | +
|
| 26 | + if (!topic) return; // Only check test issues for now |
| 27 | +
|
| 28 | + // Search open AND recently closed issues with similar titles |
| 29 | + const { data: openIssues } = await github.rest.issues.listForRepo({ |
| 30 | + owner: context.repo.owner, |
| 31 | + repo: context.repo.repo, |
| 32 | + state: 'all', |
| 33 | + per_page: 100, |
| 34 | + sort: 'created', |
| 35 | + direction: 'desc' |
| 36 | + }); |
| 37 | +
|
| 38 | + const duplicates = openIssues.filter(i => { |
| 39 | + if (i.number === issue.number) return false; |
| 40 | + if (i.pull_request) return false; |
| 41 | + const otherTitle = i.title.toLowerCase().replace(/[^a-z0-9\s]/g, '').trim(); |
| 42 | + const otherMatch = otherTitle.match(/test\s*add\s*(?:unit\s*)?tests?\s*(?:for|of)\s*(.+)/); |
| 43 | + if (!otherMatch) return false; |
| 44 | + const otherTopic = otherMatch[1].replace(/ts$/, '').trim(); |
| 45 | + return otherTopic === topic || otherTopic.includes(topic) || topic.includes(otherTopic); |
| 46 | + }); |
| 47 | +
|
| 48 | + if (duplicates.length > 0) { |
| 49 | + const refs = duplicates.slice(0, 5).map(d => `#${d.number} (${d.state})`).join(', '); |
| 50 | + await github.rest.issues.createComment({ |
| 51 | + owner: context.repo.owner, |
| 52 | + repo: context.repo.repo, |
| 53 | + issue_number: issue.number, |
| 54 | + body: `Duplicate detected — the following issue(s) already cover this topic: ${refs}.\n\nPlease search existing issues (open **and** closed) before opening a new one. Closing this issue.` |
| 55 | + }); |
| 56 | + await github.rest.issues.update({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + issue_number: issue.number, |
| 60 | + state: 'closed', |
| 61 | + labels: [...issue.labels.map(l => l.name), 'duplicate'] |
| 62 | + }); |
| 63 | + } |
0 commit comments