Skip to content

Commit 5e91ae5

Browse files
committed
Rewrite the thread-locking workflow
1 parent 58b2a94 commit 5e91ae5

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

.github/workflows/lock.yml

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,56 @@ name: Lock old threads
22

33
on:
44
schedule:
5+
# Run at midnight daily
56
- cron: "0 0 * * *"
6-
7-
permissions:
8-
issues: write
9-
pull-requests: write
7+
workflow_dispatch:
108

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

0 commit comments

Comments
 (0)