|
| 1 | +name: stale-bot |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # run every day at midnight |
| 6 | + - cron: '0 0 * * *' |
| 7 | + issue_comment: |
| 8 | + types: ['created'] |
| 9 | + |
| 10 | +jobs: |
| 11 | + stale-bot: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Mark stale |
| 15 | + if: github.event_name == 'schedule' |
| 16 | + |
| 17 | + with: |
| 18 | + github-token: ${{github.token}} |
| 19 | + script: | |
| 20 | + // Get a list of all open issues labeled `waiting for feedback` |
| 21 | + const opts = github.issues.listForRepo.endpoint.merge({ |
| 22 | + ...context.repo, |
| 23 | + state: 'open', |
| 24 | + labels: ['waiting for feedback'], |
| 25 | + }); |
| 26 | + const issues = await github.paginate(opts); |
| 27 | +
|
| 28 | + // Set this value to whatever makes sense for the repo. |
| 29 | + let elapsedDays = 15 |
| 30 | +
|
| 31 | + let elapsed = elapsedDays * 24 * 60 * 60 * 1000; |
| 32 | + let now = new Date(); |
| 33 | + for (const issue of issues) { |
| 34 | + // If an issue was active in the past 15 days, leave it alone. |
| 35 | + if (now - new Date(issue.updated_at).getTime() < elapsedDays) { |
| 36 | + continue; |
| 37 | + } |
| 38 | +
|
| 39 | + // If we're here, we've been waiting for feedback for more than |
| 40 | + // 15 days, mark as stale. |
| 41 | + github.issues.addLabels({ |
| 42 | + ...context.repo, |
| 43 | + issue_number: issue.number, |
| 44 | + labels: ['stale'] |
| 45 | + }); |
| 46 | + } |
| 47 | +
|
| 48 | + - name: Mark active |
| 49 | + |
| 50 | + with: |
| 51 | + github-token: ${{github.token}} |
| 52 | + script: | |
| 53 | + // Every time a comment is added to an issue, close it if it contains |
| 54 | + // the `stale` label. |
| 55 | +
|
| 56 | + // Load issue's labels. |
| 57 | + const opts = github.issues.listLabelsOnIssue.endpoint.merge({ |
| 58 | + ...context.repo, |
| 59 | + issue_number: context.issue.number |
| 60 | + }); |
| 61 | + const labels = await github.paginate(opts); |
| 62 | +
|
| 63 | + // Search for `stale`. |
| 64 | + for (const label of labels) { |
| 65 | + if (label.name === 'stale') { |
| 66 | + await github.issues.removeLabel({ |
| 67 | + ...context.repo, |
| 68 | + issue_number: context.issue.number, |
| 69 | + name: 'stale' |
| 70 | + }) |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + - name: Close stale |
| 76 | + if: github.event_name == 'schedule' |
| 77 | + |
| 78 | + with: |
| 79 | + github-token: ${{github.token}} |
| 80 | + script: | |
| 81 | + // Load all the `stale` issues |
| 82 | + const opts = github.issues.listForRepo.endpoint.merge({ |
| 83 | + ...context.repo, |
| 84 | + state: 'open', |
| 85 | + labels: ['stale'], |
| 86 | + }); |
| 87 | + const issues = await github.paginate(opts); |
| 88 | +
|
| 89 | + // Set this value to whatever makes sense for the repo. |
| 90 | + let elapsedDays = 30; |
| 91 | +
|
| 92 | + let elapsed = elapsedDays * 24 * 60 * 60 * 1000; |
| 93 | + let now = new Date(); |
| 94 | + for (const issue of issues) { |
| 95 | + // If an issue was stale for less than elapsed time, leave it alone. |
| 96 | + if (now - new Date(issue.updated_at).getTime() < elapsed) { |
| 97 | + continue; |
| 98 | + } |
| 99 | +
|
| 100 | + // Close the stale issue. |
| 101 | + await github.issues.update({ |
| 102 | + ...context.repo, |
| 103 | + issue_number: issue.number, |
| 104 | + state: 'closed' |
| 105 | + }); |
| 106 | + } |
0 commit comments