Refactor stale PR management script for clarity

This commit is contained in:
Tobias 2026-02-08 20:58:24 +01:00 committed by GitHub
parent c193627217
commit 7bd8140c6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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

@ -1,5 +1,4 @@
name: Stale PR Management name: Stale PR Management
on: on:
schedule: schedule:
# Runs daily at midnight UTC # Runs daily at midnight UTC
@ -7,7 +6,6 @@ on:
pull_request: pull_request:
types: types:
- labeled - labeled
jobs: jobs:
stale-prs: stale-prs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -20,8 +18,7 @@ jobs:
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 ---
// Handle when PR is labeled
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;
if (label === "stale") { if (label === "stale") {
@ -32,33 +29,26 @@ jobs:
body: "This PR has been marked as stale. It will be closed if no new commits are added in 7 days." body: "This PR has been marked as stale. It will be closed if no new commits are added in 7 days."
}); });
} }
return; return; // exit, nothing else to do
} }
// --- Scheduled run ---
// Scheduled run: fetch all open PRs
const { data: prs } = await github.pulls.list({ const { data: prs } = await github.pulls.list({
owner, owner,
repo, repo,
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;
// Fetch commits for this PR
const { data: commits } = await github.pulls.listCommits({ const { data: commits } = await github.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) {
// Close stale PR
await github.pulls.update({ await github.pulls.update({
owner, owner,
repo, repo,
@ -71,8 +61,7 @@ jobs:
issue_number: pr.number, issue_number: pr.number,
body: "Closing stale PR due to inactivity." body: "Closing stale PR due to inactivity."
}); });
} else if (lastCommitDate > new Date(now - 7*24*60*60*1000)) { } else if (diffDays <= 7) {
// Remove stale label if recent activity
await github.issues.removeLabel({ await github.issues.removeLabel({
owner, owner,
repo, repo,