Improve stale PR auto-labeling and management

Enhance stale PR management by auto-labeling PRs with no activity in the last 14 days and maintaining existing stale PR handling.
This commit is contained in:
Tobias
2026-03-22 13:58:26 +01:00
committed by GitHub
parent 87bb2a34ca
commit e76681ed8e

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

@@ -38,7 +38,7 @@ jobs:
return; return;
} }
// --- Scheduled run: check all stale PRs --- // --- Scheduled run: fetch all open PRs ---
const { data: prs } = await github.rest.pulls.list({ const { data: prs } = await github.rest.pulls.list({
owner, owner,
repo, repo,
@@ -47,8 +47,63 @@ jobs:
}); });
for (const pr of prs) { for (const pr of prs) {
const hasStale = pr.labels.some(l => l.name === "stale"); const labels = pr.labels.map(l => l.name);
if (!hasStale) continue; const hasStale = labels.includes("stale");
const hasKeepOpen = labels.includes("keep-open");
// -------------------------------------------------------
// NEW: Auto-label PRs with no activity in the last 14 days
// -------------------------------------------------------
if (!hasStale && !hasKeepOpen) {
// Find the most recent commit date
const { data: commits } = await github.rest.pulls.listCommits({
owner,
repo,
pull_number: pr.number
});
const lastCommitDate = commits.length > 0
? new Date(commits[commits.length - 1].commit.author.date)
: new Date(pr.created_at);
// Find the most recent non-bot comment date
const { data: comments } = await github.rest.issues.listComments({
owner,
repo,
issue_number: pr.number,
per_page: 100
});
const humanComments = comments.filter(c => c.user?.type !== "Bot");
const lastCommentDate = humanComments.length > 0
? new Date(humanComments[humanComments.length - 1].created_at)
: null;
// Most recent activity across commits and comments
const lastActivityDate = lastCommentDate && lastCommentDate > lastCommitDate
? lastCommentDate
: lastCommitDate;
const daysSinceActivity = (now - lastActivityDate) / (1000 * 60 * 60 * 24);
if (daysSinceActivity > 14) {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: pr.number,
labels: ["stale"]
});
// The pull_request_target labeled event will fire the comment automatically.
// Skip further processing for this PR in this run.
continue;
}
// Not stale, nothing else to do for this PR.
continue;
}
// -------------------------------------------------------
// EXISTING: Manage already-stale PRs
// -------------------------------------------------------
if (!hasStale) continue; // has keep-open but not stale — skip
// Get timeline events to find when stale label was added // Get timeline events to find when stale label was added
const { data: events } = await github.rest.issues.listEvents({ const { data: events } = await github.rest.issues.listEvents({