Refactor stale PR management script for clarity
This commit is contained in:
parent
c193627217
commit
7bd8140c6f
19
.github/workflows/stale_pr_close.yml
generated
vendored
19
.github/workflows/stale_pr_close.yml
generated
vendored
@ -1,5 +1,4 @@
|
||||
name: Stale PR Management
|
||||
|
||||
on:
|
||||
schedule:
|
||||
# Runs daily at midnight UTC
|
||||
@ -7,7 +6,6 @@ on:
|
||||
pull_request:
|
||||
types:
|
||||
- labeled
|
||||
|
||||
jobs:
|
||||
stale-prs:
|
||||
runs-on: ubuntu-latest
|
||||
@ -20,8 +18,7 @@ jobs:
|
||||
const now = new Date();
|
||||
const owner = context.repo.owner;
|
||||
const repo = context.repo.repo;
|
||||
|
||||
// Handle when PR is labeled
|
||||
// --- PR labeled event ---
|
||||
if (context.eventName === "pull_request" && context.payload.action === "labeled") {
|
||||
const label = context.payload.label?.name;
|
||||
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."
|
||||
});
|
||||
}
|
||||
return;
|
||||
return; // exit, nothing else to do
|
||||
}
|
||||
|
||||
// Scheduled run: fetch all open PRs
|
||||
// --- Scheduled run ---
|
||||
const { data: prs } = await github.pulls.list({
|
||||
owner,
|
||||
repo,
|
||||
state: "open",
|
||||
per_page: 100
|
||||
});
|
||||
|
||||
for (const pr of prs) {
|
||||
const hasStale = pr.labels.some(l => l.name === "stale");
|
||||
if (!hasStale) continue;
|
||||
|
||||
// Fetch commits for this PR
|
||||
const { data: commits } = await github.pulls.listCommits({
|
||||
owner,
|
||||
repo,
|
||||
pull_number: pr.number
|
||||
});
|
||||
|
||||
const lastCommitDate = new Date(commits[commits.length - 1].commit.author.date);
|
||||
const diffDays = (now - lastCommitDate) / (1000 * 60 * 60 * 24);
|
||||
|
||||
if (diffDays > 7) {
|
||||
// Close stale PR
|
||||
await github.pulls.update({
|
||||
owner,
|
||||
repo,
|
||||
@ -71,8 +61,7 @@ jobs:
|
||||
issue_number: pr.number,
|
||||
body: "Closing stale PR due to inactivity."
|
||||
});
|
||||
} else if (lastCommitDate > new Date(now - 7*24*60*60*1000)) {
|
||||
// Remove stale label if recent activity
|
||||
} else if (diffDays <= 7) {
|
||||
await github.issues.removeLabel({
|
||||
owner,
|
||||
repo,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user