From a815eecc732b4dde0623ff24923db7370a3ac896 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Fri, 26 Jun 2026 16:14:09 +0800 Subject: [PATCH] [fix] fix close-issue workflow env indentation, pagination, and activity time base --- .github/workflows/close-issue.yml | 39 +++++++++++-------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/.github/workflows/close-issue.yml b/.github/workflows/close-issue.yml index dbaa250..a70f2ae 100644 --- a/.github/workflows/close-issue.yml +++ b/.github/workflows/close-issue.yml @@ -4,6 +4,7 @@ on: schedule: # run every day at midnight - cron: "0 0 * * *" + workflow_dispatch: permissions: issues: write @@ -15,19 +16,21 @@ jobs: runs-on: ubuntu-latest steps: - name: Check inactive issues and close them - uses: actions/github-script@v6 + uses: actions/github-script@v7 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: script: | - const { data: issues } = await github.rest.issues.listForRepo({ + const inactivePeriod = 7 * 24 * 60 * 60 * 1000; // 7 days + const now = Date.now(); + + // paginate through all open issues (listForRepo also returns PRs) + const issues = await github.paginate(github.rest.issues.listForRepo, { owner: context.repo.owner, repo: context.repo.repo, state: 'open', - per_page: 100, }); - const now = new Date().getTime(); - const inactivePeriod = 7 * 24 * 60 * 60 * 1000; // 7 days - for (const issue of issues) { // skip pull requests (they are also returned by listForRepo) if (issue.pull_request) continue; @@ -38,26 +41,14 @@ jobs: continue; } - // fetch comments for this issue - const { data: comments } = await github.rest.issues.listComments({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: issue.number, - per_page: 100, - }); - - // determine the "last activity" time - let lastActivityTime; - if (comments.length > 0) { - const lastComment = comments[comments.length - 1]; - lastActivityTime = new Date(lastComment.updated_at).getTime(); - } else { - lastActivityTime = new Date(issue.created_at).getTime(); - } + // last activity time = the issue's own updated_at, which is + // refreshed on comments, labels, etc. This avoids relying on + // fetching comments and is accurate even when comments are edited. + const lastActivityTime = new Date(issue.updated_at).getTime(); // check inactivity if (now - lastActivityTime > inactivePeriod) { - console.log(`Closing inactive issue: #${issue.number} (No recent replies for 7 days)`); + console.log(`Closing inactive issue: #${issue.number} (No activity for 7 days)`); await github.rest.issues.createComment({ owner: context.repo.owner, @@ -76,5 +67,3 @@ jobs: console.log(`Skipping issue #${issue.number} (Active within 7 days).`); } } - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}