-
Notifications
You must be signed in to change notification settings - Fork 621
Add workflow to auto-close stale PRs after 60 days #5577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/add-auto-close-stale-prs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2967426
Initial plan
Copilot 2d457c1
Add workflow to auto-close stale PRs after 60 days
Copilot 97afd50
Improve error handling in stale PR workflow
Copilot 580f72c
Add comprehensive error handling for all PR operations
Copilot 41f7ce6
Add issues:write permission and improve error handling consistency
Copilot 0618f94
Update github-script action version in stale PRs workflow
mtfriesen 6433d8e
Update GitHub Action to use a different commit
mtfriesen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| --- | ||
| name: "Close Stale PRs" | ||
|
|
||
| on: | ||
| schedule: | ||
| # Run daily at 00:00 UTC | ||
| - cron: '0 0 * * *' | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| pull-requests: write | ||
| issues: write | ||
|
|
||
| jobs: | ||
| stale: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Warn and close stale PRs | ||
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd | ||
| with: | ||
| script: | | ||
| const daysUntilStale = 46; // First warning: 2 weeks before 60-day deadline | ||
| const daysUntilSecondWarning = 53; // Second warning: 1 week before 60-day deadline | ||
| const daysUntilClose = 60; // Close PRs after 60 days of inactivity | ||
|
|
||
| const exemptLabels = ['pinned', 'security', 'dependencies']; | ||
mtfriesen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const staleLabel = 'stale'; | ||
| const secondWarningLabel = 'stale-final-warning'; | ||
mtfriesen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const now = new Date(); | ||
|
|
||
| // Get all open pull requests | ||
| const pulls = await github.paginate(github.rest.pulls.list, { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| state: 'open', | ||
| per_page: 100 | ||
| }); | ||
|
|
||
| console.log(`Found ${pulls.length} open pull requests`); | ||
|
|
||
| for (const pr of pulls) { | ||
| console.log(`\nProcessing PR #${pr.number}: ${pr.title}`); | ||
|
|
||
| // Get PR labels | ||
| const labels = pr.labels.map(l => l.name); | ||
|
|
||
| // Skip if PR has exempt labels | ||
| if (labels.some(label => exemptLabels.includes(label))) { | ||
| console.log(` Skipping: has exempt label`); | ||
| continue; | ||
| } | ||
|
|
||
| // Calculate days since last update | ||
| const updatedAt = new Date(pr.updated_at); | ||
| const daysSinceUpdate = Math.floor((now - updatedAt) / (1000 * 60 * 60 * 24)); | ||
| console.log(` Days since update: ${daysSinceUpdate}`); | ||
|
|
||
| // Close PR if it's been 60+ days | ||
| if (daysSinceUpdate >= daysUntilClose) { | ||
| console.log(` Closing PR (${daysSinceUpdate} days inactive)`); | ||
|
|
||
| try { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr.number, | ||
| body: `This pull request has been closed due to inactivity (60 days with no updates). If you would like to continue this work, please feel free to reopen the PR or create a new one. Thank you for your contribution!` | ||
| }); | ||
|
|
||
| await github.rest.pulls.update({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: pr.number, | ||
| state: 'closed' | ||
| }); | ||
| } catch (error) { | ||
| console.log(` Error closing PR: ${error.message}`); | ||
| } | ||
| } | ||
| // Add final warning label and comment if 53+ days (1 week before close) | ||
| else if (daysSinceUpdate >= daysUntilSecondWarning && !labels.includes(secondWarningLabel)) { | ||
| console.log(` Adding final warning (${daysSinceUpdate} days inactive)`); | ||
|
|
||
| try { | ||
| await github.rest.issues.createComment({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if these comments will refresh the "last updated" date? |
||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr.number, | ||
| body: `⚠️ **Final Warning**: This pull request has been inactive for ${daysSinceUpdate} days and will be closed in approximately ${daysUntilClose - daysSinceUpdate} days if no further activity occurs. If you plan to continue working on this PR, please leave a comment or push new changes to keep it active.` | ||
| }); | ||
|
|
||
| await github.rest.issues.addLabels({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr.number, | ||
| labels: [secondWarningLabel] | ||
| }); | ||
| } catch (error) { | ||
| console.log(` Error adding final warning: ${error.message}`); | ||
| } | ||
| } | ||
| // Add stale label and comment if 46+ days (2 weeks before close) | ||
| else if (daysSinceUpdate >= daysUntilStale && !labels.includes(staleLabel)) { | ||
| console.log(` Marking as stale (${daysSinceUpdate} days inactive)`); | ||
|
|
||
| try { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr.number, | ||
| body: `This pull request has been inactive for ${daysSinceUpdate} days and will be closed in approximately ${daysUntilClose - daysSinceUpdate} days if no further activity occurs. If you plan to continue working on this PR, please leave a comment or push new changes to keep it active.` | ||
| }); | ||
|
|
||
| await github.rest.issues.addLabels({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr.number, | ||
| labels: [staleLabel] | ||
| }); | ||
| } catch (error) { | ||
| console.log(` Error marking as stale: ${error.message}`); | ||
| } | ||
| } | ||
| // Remove stale labels if PR was updated | ||
| else if (daysSinceUpdate < daysUntilStale && (labels.includes(staleLabel) || labels.includes(secondWarningLabel))) { | ||
| console.log(` Removing stale labels (PR was updated)`); | ||
|
|
||
| try { | ||
| if (labels.includes(staleLabel)) { | ||
| await github.rest.issues.removeLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr.number, | ||
| name: staleLabel | ||
| }); | ||
| } | ||
|
|
||
| if (labels.includes(secondWarningLabel)) { | ||
| await github.rest.issues.removeLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr.number, | ||
| name: secondWarningLabel | ||
| }); | ||
| } | ||
| } catch (error) { | ||
| console.log(` Error removing stale labels: ${error.message}`); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.