|
| 1 | +--- |
| 2 | +name: Lock closed issues and PRs |
| 3 | + |
| 4 | +on: |
| 5 | + schedule: |
| 6 | + - cron: "45 0 * * *" # Run daily at 00:45 UTC |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + issues: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: lock-closed |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +jobs: |
| 18 | + lock: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + steps: |
| 21 | + - name: Lock closed issues and PRs |
| 22 | + uses: actions/github-script@v7.0.1 |
| 23 | + with: |
| 24 | + script: | |
| 25 | + const PR_CLOSE_DAYS = 1; |
| 26 | + const ISSUE_CLOSE_DAYS = 7; |
| 27 | + const EXCLUDE_LABEL = 'keep-open'; |
| 28 | + const LOCK_REASON = 'resolved'; |
| 29 | +
|
| 30 | + const now = new Date(); |
| 31 | + const prCutoffDate = new Date(now.getTime() - PR_CLOSE_DAYS * 24 * 60 * 60 * 1000); |
| 32 | + const issueCutoffDate = new Date(now.getTime() - ISSUE_CLOSE_DAYS * 24 * 60 * 60 * 1000); |
| 33 | +
|
| 34 | + console.log(`Will lock PRs closed before: ${prCutoffDate.toISOString()}`); |
| 35 | + console.log(`Will lock issues closed before: ${issueCutoffDate.toISOString()}`); |
| 36 | +
|
| 37 | + // The `listForRepo` endpoint fetches both issues and pull requests. |
| 38 | + // We fetch all closed items and filter them in the script. |
| 39 | + const closedItems = await github.paginate(github.rest.issues.listForRepo, { |
| 40 | + owner: context.repo.owner, |
| 41 | + repo: context.repo.repo, |
| 42 | + state: 'closed', |
| 43 | + }); |
| 44 | +
|
| 45 | + for (const item of closedItems) { |
| 46 | + if (item.locked) continue; |
| 47 | +
|
| 48 | + const hasExcludeLabel = item.labels.some(label => label.name === EXCLUDE_LABEL); |
| 49 | + if (hasExcludeLabel) continue; |
| 50 | +
|
| 51 | + const closedAt = new Date(item.closed_at); |
| 52 | + const isPr = 'pull_request' in item; |
| 53 | +
|
| 54 | + if ((isPr && closedAt < prCutoffDate) || (!isPr && closedAt < issueCutoffDate)) { |
| 55 | + console.log(`Locking ${isPr ? 'PR' : 'issue'} #${item.number} which was closed at ${item.closed_at}`); |
| 56 | + try { |
| 57 | + await github.rest.issues.lock({ |
| 58 | + owner: context.repo.owner, |
| 59 | + repo: context.repo.repo, |
| 60 | + issue_number: item.number, |
| 61 | + }); |
| 62 | + console.log(`Successfully locked #${item.number}.`); |
| 63 | + } catch (e) { |
| 64 | + console.error(`Failed to lock #${item.number}: ${e.message}`); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
0 commit comments