Refactor stale PR close workflow permissions and script

This commit is contained in:
Tobias 2026-02-08 21:06:11 +01:00 committed by GitHub
parent 460d68ecc9
commit 509c37024c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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

@ -9,22 +9,15 @@ 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: |
console.log('github object:', typeof github);
console.log('github.rest:', typeof github?.rest);
console.log('context.eventName:', context.eventName);
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;
@ -38,7 +31,6 @@ 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,
@ -46,20 +38,16 @@ 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,