Enhance stale PR workflow with permissions

This commit is contained in:
Tobias 2026-02-08 21:03:28 +01:00 committed by GitHub
parent 79455ee417
commit 2f2d0235b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

10
.github/workflows/stale_pr_close.yml generated vendored
View File

@ -9,15 +9,18 @@ on:
jobs: jobs:
stale-prs: stale-prs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
steps: steps:
- name: Handle stale label - name: Handle stale label
uses: actions/github-script@v7 uses: actions/github-script@v7
with: with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: | script: |
const now = new Date(); const now = new Date();
const owner = context.repo.owner; const owner = context.repo.owner;
const repo = context.repo.repo; const repo = context.repo.repo;
// --- PR labeled event --- // --- PR labeled event ---
if (context.eventName === "pull_request" && context.payload.action === "labeled") { if (context.eventName === "pull_request" && context.payload.action === "labeled") {
const label = context.payload.label?.name; const label = context.payload.label?.name;
@ -31,6 +34,7 @@ jobs:
} }
return; // exit, nothing else to do return; // exit, nothing else to do
} }
// --- Scheduled run --- // --- Scheduled run ---
const { data: prs } = await github.rest.pulls.list({ const { data: prs } = await github.rest.pulls.list({
owner, owner,
@ -38,16 +42,20 @@ jobs:
state: "open", state: "open",
per_page: 100 per_page: 100
}); });
for (const pr of prs) { for (const pr of prs) {
const hasStale = pr.labels.some(l => l.name === "stale"); const hasStale = pr.labels.some(l => l.name === "stale");
if (!hasStale) continue; if (!hasStale) continue;
const { data: commits } = await github.rest.pulls.listCommits({ const { data: commits } = await github.rest.pulls.listCommits({
owner, owner,
repo, repo,
pull_number: pr.number pull_number: pr.number
}); });
const lastCommitDate = new Date(commits[commits.length - 1].commit.author.date); const lastCommitDate = new Date(commits[commits.length - 1].commit.author.date);
const diffDays = (now - lastCommitDate) / (1000 * 60 * 60 * 24); const diffDays = (now - lastCommitDate) / (1000 * 60 * 60 * 24);
if (diffDays > 7) { if (diffDays > 7) {
await github.rest.pulls.update({ await github.rest.pulls.update({
owner, owner,