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:
61
.github/workflows/stale_pr_close.yml
generated
vendored
61
.github/workflows/stale_pr_close.yml
generated
vendored
@@ -38,7 +38,7 @@ jobs:
|
||||
return;
|
||||
}
|
||||
|
||||
// --- Scheduled run: check all stale PRs ---
|
||||
// --- Scheduled run: fetch all open PRs ---
|
||||
const { data: prs } = await github.rest.pulls.list({
|
||||
owner,
|
||||
repo,
|
||||
@@ -47,8 +47,63 @@ jobs:
|
||||
});
|
||||
|
||||
for (const pr of prs) {
|
||||
const hasStale = pr.labels.some(l => l.name === "stale");
|
||||
if (!hasStale) continue;
|
||||
const labels = pr.labels.map(l => l.name);
|
||||
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
|
||||
const { data: events } = await github.rest.issues.listEvents({
|
||||
|
||||
Reference in New Issue
Block a user