Refactor stale PR management workflow
This commit is contained in:
parent
e0e1475d74
commit
0a446423c6
30
.github/workflows/stale_pr_close.yml
generated
vendored
30
.github/workflows/stale_pr_close.yml
generated
vendored
@ -1,11 +1,9 @@
|
|||||||
name: Stale PR Management
|
name: Stale PR Management
|
||||||
on:
|
on:
|
||||||
schedule:
|
schedule:
|
||||||
# Runs daily at midnight UTC
|
- cron: "0 0 * * *" # Daily at midnight UTC
|
||||||
- cron: "0 0 * * *"
|
workflow_dispatch: # Allow manual trigger for testing
|
||||||
pull_request:
|
|
||||||
types:
|
|
||||||
- labeled
|
|
||||||
jobs:
|
jobs:
|
||||||
stale-prs:
|
stale-prs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@ -14,44 +12,34 @@ jobs:
|
|||||||
issues: write
|
issues: write
|
||||||
contents: read
|
contents: read
|
||||||
steps:
|
steps:
|
||||||
- name: Handle stale label
|
- name: Handle stale PRs
|
||||||
uses: actions/github-script@v7
|
uses: actions/github-script@v7
|
||||||
with:
|
with:
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
script: |
|
script: |
|
||||||
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 ---
|
|
||||||
if (context.eventName === "pull_request" && context.payload.action === "labeled") {
|
|
||||||
const label = context.payload.label?.name;
|
|
||||||
if (label === "stale") {
|
|
||||||
await github.rest.issues.createComment({
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
issue_number: context.payload.pull_request.number,
|
|
||||||
body: "This PR has been marked as stale. It will be closed if no new commits are added in 7 days."
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return; // exit, nothing else to do
|
|
||||||
}
|
|
||||||
// --- Scheduled run ---
|
|
||||||
const { data: prs } = await github.rest.pulls.list({
|
const { data: prs } = await github.rest.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;
|
||||||
|
|
||||||
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,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user