|
| 1 | +name: compliance-close |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run every 30 minutes to check for expired compliance windows |
| 6 | + - cron: "*/30 * * * *" |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + issues: write |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + close-non-compliant: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Close non-compliant issues and PRs after 2 hours |
| 19 | + uses: actions/github-script@v7 |
| 20 | + with: |
| 21 | + script: | |
| 22 | + const { data: items } = await github.rest.issues.listForRepo({ |
| 23 | + owner: context.repo.owner, |
| 24 | + repo: context.repo.repo, |
| 25 | + labels: 'needs:compliance', |
| 26 | + state: 'open', |
| 27 | + per_page: 100, |
| 28 | + }); |
| 29 | +
|
| 30 | + if (items.length === 0) { |
| 31 | + core.info('No open issues/PRs with needs:compliance label'); |
| 32 | + return; |
| 33 | + } |
| 34 | +
|
| 35 | + const now = Date.now(); |
| 36 | + const twoHours = 2 * 60 * 60 * 1000; |
| 37 | +
|
| 38 | + for (const item of items) { |
| 39 | + const isPR = !!item.pull_request; |
| 40 | + const kind = isPR ? 'PR' : 'issue'; |
| 41 | +
|
| 42 | + const { data: comments } = await github.rest.issues.listComments({ |
| 43 | + owner: context.repo.owner, |
| 44 | + repo: context.repo.repo, |
| 45 | + issue_number: item.number, |
| 46 | + }); |
| 47 | +
|
| 48 | + const complianceComment = comments.find(c => c.body.includes('<!-- issue-compliance -->')); |
| 49 | + if (!complianceComment) continue; |
| 50 | +
|
| 51 | + const commentAge = now - new Date(complianceComment.created_at).getTime(); |
| 52 | + if (commentAge < twoHours) { |
| 53 | + core.info(`${kind} #${item.number} still within 2-hour window (${Math.round(commentAge / 60000)}m elapsed)`); |
| 54 | + continue; |
| 55 | + } |
| 56 | +
|
| 57 | + const closeMessage = isPR |
| 58 | + ? 'This pull request has been automatically closed because it was not updated to meet our [contributing guidelines](../blob/dev/CONTRIBUTING.md) within the 2-hour window.\n\nFeel free to open a new pull request that follows our guidelines.' |
| 59 | + : 'This issue has been automatically closed because it was not updated to meet our [contributing guidelines](../blob/dev/CONTRIBUTING.md) within the 2-hour window.\n\nFeel free to open a new issue that follows our issue templates.'; |
| 60 | +
|
| 61 | + await github.rest.issues.createComment({ |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + issue_number: item.number, |
| 65 | + body: closeMessage, |
| 66 | + }); |
| 67 | +
|
| 68 | + if (isPR) { |
| 69 | + await github.rest.pulls.update({ |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + pull_number: item.number, |
| 73 | + state: 'closed', |
| 74 | + }); |
| 75 | + } else { |
| 76 | + await github.rest.issues.update({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + issue_number: item.number, |
| 80 | + state: 'closed', |
| 81 | + state_reason: 'not_planned', |
| 82 | + }); |
| 83 | + } |
| 84 | +
|
| 85 | + core.info(`Closed non-compliant ${kind} #${item.number} after 2-hour window`); |
| 86 | + } |
0 commit comments