name: Close stale dependency PRs
on:
schedule:
- cron: '0 3 * * 1'
workflow_dispatch:
permissions:
pull-requests: write
contents: read
env:
STALE_DAYS: '21'
jobs:
close:
runs-on: ubuntu-latest
steps:
- name: Close stale dependency PRs
uses: actions/github-script@v7
with:
script: |
const core = require('@actions/core');
const cutoffDays = parseInt(process.env.STALE_DAYS, 10);
if (Number.isNaN(cutoffDays) || cutoffDays <= 0) {
core.setFailed(`Invalid STALE_DAYS value: ${process.env.STALE_DAYS}`);
return;
}
const cutoffDate = new Date(Date.now() - cutoffDays * 24 * 60 * 60 * 1000);
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
per_page: 100,
});
for (const pr of pullRequests) {
const hasDependenciesLabel = pr.labels?.some((label) => label.name === 'dependencies');
if (!hasDependenciesLabel) {
continue;
}
const updatedAt = new Date(pr.updated_at);
if (updatedAt >= cutoffDate) {
core.info(`Keeping PR #${pr.number} updated at ${pr.updated_at}`);
continue;
}
const commentBody = [
'Closing this dependency PR because it has been inactive for over',
`${cutoffDays} days. Please reopen if you plan to work on it.`,
].join(' ');
core.info(`Closing PR #${pr.number} last updated ${pr.updated_at}`);
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: commentBody,
});
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
state: 'closed',
});
}