|
| 1 | +name: Auto-close duplicate issues |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: "0 9 * * *" |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + issues: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + auto-close-duplicates: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + timeout-minutes: 10 |
| 15 | + steps: |
| 16 | + - name: Close stale duplicate issues |
| 17 | + uses: actions/github-script@v7 |
| 18 | + env: |
| 19 | + GRACE_DAYS: ${{ vars.DUPLICATE_GRACE_DAYS || '7' }} |
| 20 | + with: |
| 21 | + script: | |
| 22 | + const { owner, repo } = context.repo; |
| 23 | + const graceDays = parseInt(process.env.GRACE_DAYS, 10) || 7; |
| 24 | + const GRACE_PERIOD_MS = graceDays * 24 * 60 * 60 * 1000; |
| 25 | + const now = Date.now(); |
| 26 | +
|
| 27 | + // Find all open issues with the duplicate label |
| 28 | + const issues = await github.paginate(github.rest.issues.listForRepo, { |
| 29 | + owner, |
| 30 | + repo, |
| 31 | + state: 'open', |
| 32 | + labels: 'duplicate', |
| 33 | + per_page: 100, |
| 34 | + }); |
| 35 | +
|
| 36 | + console.log(`Found ${issues.length} open issues with duplicate label`); |
| 37 | +
|
| 38 | + let closedCount = 0; |
| 39 | +
|
| 40 | + for (const issue of issues) { |
| 41 | + console.log(`Processing issue #${issue.number}: ${issue.title}`); |
| 42 | +
|
| 43 | + // Get comments to find the duplicate detection comment |
| 44 | + const comments = await github.rest.issues.listComments({ |
| 45 | + owner, |
| 46 | + repo, |
| 47 | + issue_number: issue.number, |
| 48 | + per_page: 100, |
| 49 | + }); |
| 50 | +
|
| 51 | + // Find the duplicate detection comment (posted by our script) |
| 52 | + const dupeComments = comments.data.filter(c => |
| 53 | + c.body.includes('<!-- duplicate-detection -->') |
| 54 | + ); |
| 55 | +
|
| 56 | + if (dupeComments.length === 0) { |
| 57 | + console.log(` No duplicate detection comment found, skipping`); |
| 58 | + continue; |
| 59 | + } |
| 60 | +
|
| 61 | + const lastDupeComment = dupeComments[dupeComments.length - 1]; |
| 62 | + const dupeCommentAge = now - new Date(lastDupeComment.created_at).getTime(); |
| 63 | +
|
| 64 | + if (dupeCommentAge < GRACE_PERIOD_MS) { |
| 65 | + const daysLeft = ((GRACE_PERIOD_MS - dupeCommentAge) / (24 * 60 * 60 * 1000)).toFixed(1); |
| 66 | + console.log(` Duplicate comment is too recent (${daysLeft} days remaining), skipping`); |
| 67 | + continue; |
| 68 | + } |
| 69 | +
|
| 70 | + // Check for human comments after the duplicate detection comment |
| 71 | + const humanCommentsAfter = comments.data.filter(c => |
| 72 | + new Date(c.created_at) > new Date(lastDupeComment.created_at) && |
| 73 | + c.user.type !== 'Bot' && |
| 74 | + !c.body.includes('<!-- duplicate-detection -->') && |
| 75 | + !c.body.includes('automatically closed as a duplicate') |
| 76 | + ); |
| 77 | +
|
| 78 | + if (humanCommentsAfter.length > 0) { |
| 79 | + console.log(` Has ${humanCommentsAfter.length} human comment(s) after detection, skipping`); |
| 80 | + continue; |
| 81 | + } |
| 82 | +
|
| 83 | + // Check for thumbs-down reaction from the issue author |
| 84 | + const reactions = await github.rest.reactions.listForIssueComment({ |
| 85 | + owner, |
| 86 | + repo, |
| 87 | + comment_id: lastDupeComment.id, |
| 88 | + per_page: 100, |
| 89 | + }); |
| 90 | +
|
| 91 | + const authorThumbsDown = reactions.data.some(r => |
| 92 | + r.user.id === issue.user.id && r.content === '-1' |
| 93 | + ); |
| 94 | +
|
| 95 | + if (authorThumbsDown) { |
| 96 | + console.log(` Issue author gave thumbs-down on duplicate comment, skipping`); |
| 97 | + continue; |
| 98 | + } |
| 99 | +
|
| 100 | + // Extract the primary duplicate issue number from the comment |
| 101 | + const dupeMatch = lastDupeComment.body.match(/#(\d+)/); |
| 102 | + const dupeNumber = dupeMatch ? dupeMatch[1] : 'unknown'; |
| 103 | +
|
| 104 | + // Close the issue |
| 105 | + console.log(` Closing as duplicate of #${dupeNumber}`); |
| 106 | +
|
| 107 | + await github.rest.issues.update({ |
| 108 | + owner, |
| 109 | + repo, |
| 110 | + issue_number: issue.number, |
| 111 | + state: 'closed', |
| 112 | + state_reason: 'duplicate', |
| 113 | + }); |
| 114 | +
|
| 115 | + await github.rest.issues.addLabels({ |
| 116 | + owner, |
| 117 | + repo, |
| 118 | + issue_number: issue.number, |
| 119 | + labels: ['autoclose'], |
| 120 | + }); |
| 121 | +
|
| 122 | + await github.rest.issues.createComment({ |
| 123 | + owner, |
| 124 | + repo, |
| 125 | + issue_number: issue.number, |
| 126 | + body: `This issue has been automatically closed as a duplicate of #${dupeNumber}.\n\nIf this is incorrect, please reopen this issue or create a new one.\n\n🤖 Generated with [Claude Code](https://claude.ai/code)`, |
| 127 | + }); |
| 128 | +
|
| 129 | + closedCount++; |
| 130 | + } |
| 131 | +
|
| 132 | + console.log(`Done. Closed ${closedCount} duplicate issue(s).`); |
0 commit comments