| 1 |
name: Lock old threads |
| 2 |
|
| 3 |
on: |
| 4 |
schedule: |
| 5 |
# Run at midnight daily |
| 6 |
- cron: "0 0 * * *" |
| 7 |
workflow_dispatch: |
| 8 |
|
| 9 |
permissions: {} |
| 10 |
|
| 11 |
jobs: |
| 12 |
action: |
| 13 |
runs-on: ubuntu-latest |
| 14 |
if: github.repository_owner == 'sphinx-doc' |
| 15 |
permissions: |
| 16 |
# to lock issues and PRs |
| 17 |
issues: write |
| 18 |
pull-requests: write |
| 19 |
steps: |
| 20 |
- uses: actions/github-script@v7 |
| 21 |
with: |
| 22 |
retries: 3 |
| 23 |
# language=JavaScript |
| 24 |
script: | |
| 25 |
const _FOUR_WEEKS_MILLISECONDS = 28 * 24 * 60 * 60 * 1000; |
| 26 |
const _FOUR_WEEKS_DATE = new Date(Date.now() - _FOUR_WEEKS_MILLISECONDS); |
| 27 |
const FOUR_WEEKS_AGO = `${_FOUR_WEEKS_DATE.toISOString().substring(0, 10)}T00:00:00Z`; |
| 28 |
const OWNER = context.repo.owner; |
| 29 |
const REPO = context.repo.repo; |
| 30 |
|
| 31 |
try { |
| 32 |
for (const thread_type of ["issue", "pr"]) { |
| 33 |
core.debug(`Finding ${thread_type}s to lock`); |
| 34 |
const query = thread_type === "issue" |
| 35 |
? `repo:${OWNER}/${REPO} updated:<${FOUR_WEEKS_AGO} is:closed is:unlocked is:issue` |
| 36 |
: `repo:${OWNER}/${REPO} updated:<${FOUR_WEEKS_AGO} is:closed is:unlocked is:pr`; |
| 37 |
core.debug(`Using query '${query}'`); |
| 38 |
// https://octokit.github.io/rest.js/v21/#search-issues-and-pull-requests |
| 39 |
const {data: {items: results}} = await github.rest.search.issuesAndPullRequests({ |
| 40 |
q: query, |
| 41 |
order: "desc", |
| 42 |
sort: "updated", |
| 43 |
per_page: 100, |
| 44 |
}); |
| 45 |
for (const item of results) { |
| 46 |
if (item.locked) continue; |
| 47 |
const thread_num = item.number; |
| 48 |
core.debug(`Locking #${thread_num} (${thread_type})`); |
| 49 |
// https://octokit.github.io/rest.js/v21/#issues-lock |
| 50 |
await github.rest.issues.lock({ |
| 51 |
owner: OWNER, |
| 52 |
repo: REPO, |
| 53 |
issue_number: thread_num, |
| 54 |
lock_reason: "resolved", |
| 55 |
}); |
| 56 |
} |
| 57 |
} |
| 58 |
} catch (err) { |
| 59 |
core.setFailed(err.message); |
| 60 |
} |