Refactor stale PR close workflow permissions and script
This commit is contained in:
parent
460d68ecc9
commit
509c37024c
14
.github/workflows/stale_pr_close.yml
generated
vendored
14
.github/workflows/stale_pr_close.yml
generated
vendored
@ -9,22 +9,15 @@ on:
|
||||
jobs:
|
||||
stale-prs:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
pull-requests: write
|
||||
issues: write
|
||||
steps:
|
||||
- name: Handle stale label
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
script: |
|
||||
console.log('github object:', typeof github);
|
||||
console.log('github.rest:', typeof github?.rest);
|
||||
console.log('context.eventName:', context.eventName);
|
||||
|
||||
const now = new Date();
|
||||
const owner = context.repo.owner;
|
||||
const repo = context.repo.repo;
|
||||
|
||||
// --- PR labeled event ---
|
||||
if (context.eventName === "pull_request" && context.payload.action === "labeled") {
|
||||
const label = context.payload.label?.name;
|
||||
@ -38,7 +31,6 @@ jobs:
|
||||
}
|
||||
return; // exit, nothing else to do
|
||||
}
|
||||
|
||||
// --- Scheduled run ---
|
||||
const { data: prs } = await github.rest.pulls.list({
|
||||
owner,
|
||||
@ -46,20 +38,16 @@ jobs:
|
||||
state: "open",
|
||||
per_page: 100
|
||||
});
|
||||
|
||||
for (const pr of prs) {
|
||||
const hasStale = pr.labels.some(l => l.name === "stale");
|
||||
if (!hasStale) continue;
|
||||
|
||||
const { data: commits } = await github.rest.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) {
|
||||
await github.rest.pulls.update({
|
||||
owner,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user