|
| 1 | +name: Close stale issues |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 3 * * *' |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + close_stale_issues: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + env: |
| 12 | + DEBUG_MODE: 'false' |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Generate list of stale issues |
| 16 | + uses: actions/github-script@v7 |
| 17 | + with: |
| 18 | + script: | |
| 19 | + const fs = require('fs'); |
| 20 | + const issues = await github.paginate( |
| 21 | + github.rest.issues.listForRepo, |
| 22 | + { |
| 23 | + owner: context.repo.owner, |
| 24 | + repo: context.repo.repo, |
| 25 | + state: "open", |
| 26 | + per_page: 100, |
| 27 | + } |
| 28 | + ); |
| 29 | + const now = new Date(); |
| 30 | + const threshold = process.env.DEBUG_MODE === 'true' |
| 31 | + ? new Date(now.getTime() - 60 * 1000) |
| 32 | + : new Date(new Date(now).setMonth(now.getMonth() - 1)); |
| 33 | + const staleIssues = issues.filter(issue => |
| 34 | + !issue.pull_request && new Date(issue.updated_at) < threshold |
| 35 | + ); |
| 36 | + const list = staleIssues.map(i => `#${i.number} - ${i.title}`).join('\n') || 'No stale issues found.'; |
| 37 | + fs.writeFileSync('stale_issues.txt', list); |
| 38 | + core.info(list); |
| 39 | +
|
| 40 | + - name: Upload stale issue list |
| 41 | + uses: actions/upload-artifact@v4 |
| 42 | + with: |
| 43 | + name: stale-issues |
| 44 | + path: stale_issues.txt |
| 45 | + |
| 46 | + - name: Close stale issues |
| 47 | + uses: actions/github-script@v7 |
| 48 | + with: |
| 49 | + script: | |
| 50 | + const issues = await github.paginate( |
| 51 | + github.rest.issues.listForRepo, |
| 52 | + { |
| 53 | + owner: context.repo.owner, |
| 54 | + repo: context.repo.repo, |
| 55 | + state: "open", |
| 56 | + per_page: 100, |
| 57 | + } |
| 58 | + ); |
| 59 | + const now = new Date(); |
| 60 | + const threshold = process.env.DEBUG_MODE === 'true' |
| 61 | + ? new Date(now.getTime() - 60 * 1000) |
| 62 | + : new Date(new Date(now).setMonth(now.getMonth() - 1)); |
| 63 | + const closed = []; |
| 64 | + for (const issue of issues) { |
| 65 | + if (!issue.pull_request && new Date(issue.updated_at) < threshold) { |
| 66 | + core.info(`Closing issue #${issue.number} - ${issue.title}`); |
| 67 | + await github.rest.issues.createComment({ |
| 68 | + owner: context.repo.owner, |
| 69 | + repo: context.repo.repo, |
| 70 | + issue_number: issue.number, |
| 71 | + body: `This issue has been automatically closed as part of a cleanup process to help the team manage issues more effectively.\n\nIf the problem still persists, feel free to comment and we will gladly reopen it.` |
| 72 | + }); |
| 73 | + await github.rest.issues.update({ |
| 74 | + owner: context.repo.owner, |
| 75 | + repo: context.repo.repo, |
| 76 | + issue_number: issue.number, |
| 77 | + state: "closed", |
| 78 | + }); |
| 79 | + closed.push(`#${issue.number} - ${issue.title}`); |
| 80 | + } |
| 81 | + } |
| 82 | + core.info(closed.length ? `Closed ${closed.length} issue(s):\n${closed.join('\n')}` : 'No issues were closed.'); |
0 commit comments