diff --git a/.github/workflows/stale_pr_close.yml b/.github/workflows/stale_pr_close.yml index f7c1fcb72..6b0531936 100644 --- a/.github/workflows/stale_pr_close.yml +++ b/.github/workflows/stale_pr_close.yml @@ -1,11 +1,9 @@ name: Stale PR Management on: schedule: - # Runs daily at midnight UTC - - cron: "0 0 * * *" - pull_request: - types: - - labeled + - cron: "0 0 * * *" # Daily at midnight UTC + workflow_dispatch: # Allow manual trigger for testing + jobs: stale-prs: runs-on: ubuntu-latest @@ -14,44 +12,34 @@ jobs: issues: write contents: read steps: - - name: Handle stale label + - name: Handle stale PRs uses: actions/github-script@v7 with: - github-token: ${{ secrets.GITHUB_TOKEN }} script: | const now = new Date(); const owner = context.repo.owner; const repo = context.repo.repo; - // --- PR labeled event --- - if (context.eventName === "pull_request" && context.payload.action === "labeled") { - const label = context.payload.label?.name; - if (label === "stale") { - await github.rest.issues.createComment({ - owner, - repo, - issue_number: context.payload.pull_request.number, - body: "This PR has been marked as stale. It will be closed if no new commits are added in 7 days." - }); - } - return; // exit, nothing else to do - } - // --- Scheduled run --- + const { data: prs } = await github.rest.pulls.list({ owner, repo, state: "open", per_page: 100 }); + for (const pr of prs) { const hasStale = pr.labels.some(l => l.name === "stale"); if (!hasStale) continue; + const { data: commits } = await github.rest.pulls.listCommits({ owner, repo, pull_number: pr.number }); + const lastCommitDate = new Date(commits[commits.length - 1].commit.author.date); const diffDays = (now - lastCommitDate) / (1000 * 60 * 60 * 24); + if (diffDays > 7) { await github.rest.pulls.update({ owner,