Auto-close GitHub pull requests

Auto-close GitHub pull requests

BackerLeader ●20 ●169 ●274
calendar_today • schedule2 min read
— Originally published at stevefenton.co.uk

You might be like me. You open the dishwasher and there's no order or discipline to how it's been loaded. If you leave it that way, half of your cups will come out gritty. The baking tray is going to stop the spinning water blade. The bowl is going to be filled with water. It's a mess.

If you're the person who re-loads the whole dishwasher, and if you're using GitHub Pull Requests... this post is for you.

Just like the dishwasher, I sometimes open GitHub to find the Pull Requests tab on a repository has been loaded badly. It's a mess.

The good news is, the fix is automated and easy.

Stale PRs

There's a GitHub-authored Action that tackles this problem, so you don't have to. Sadly, it doesn't work on dishwashers but let's take whatever wins we can get.

You add a new Action to the .github/workflows folder called stale-pull-requests.yml, then add the following:

  1. A schedule to run the Action. Once a week is usually sufficient
  2. A reference to actions/stale with a dusting of configuration

For scheduling, once a week is enough and you can set it up as a mittwoch maintenance activity. The syntax for schedules takes some getting used to, but 0 1 * * 3 fills the slots minute hour day-of-month month day-of-week. If you don't need to supply a value (like day-of-month, because we're doing it every week) you just use a *.

To help my friends, I always add a comment to explain the schedule.

name: Mark stale PRs
on:
  schedule:
    - cron: '0 1 * * 3'  # Every Wednesday at 1am UTC

jobs:
  stale:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/stale@v9
        with:
          stale-pr-message: 'Things seem to have gone quiet on this PR. There has been no activity for 60 days, so it will be closed in 14 days if there is still no movement.'
          close-pr-message: 'Closing due to inactivity.'
          days-before-pr-stale: 60
          days-before-pr-close: 14
          stale-pr-label: 'stale'

On the actions/stale front, you add a couple of messages, some timings, and a label.

  • stale-pr-message tells people what's about to happen; the PR seems to have wilted and it's going to get crushed
  • close-pr-message explains why the PR got crushed when it gets auto-closed
  • days-before-pr-stale is how long you wait before adding the stale-pr-message
  • days-before-pr-close is how much longer you wait before crushing it
  • stale-pr-label is the label that will be applied to stale PRs

There are two doors

Two things will happen when you add this. People will realize they forgot all about the PR and will move it on and get it approved, merged, and closed. Or they won't, and it will be kicked out of the pub by Peggy Mitchell.

Either way, the dishwasher is neat and tidy, and all your plates and cups get clean.

Want more #automation & #github deep dives?

Join 6,110+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

2 Comments

1 vote
1 vote

More Posts

Running OpenAPI Validation in GitHub Actions and Showing Findings in Pull Requests

Ganesh Kumar - Jul 3

10x the Pull Requests, Not 10x the CI Bill: CircleCI's Rob Zuber on Testing for Coding Agents

Tom Smithverified - Nov 23

Retry Failed n8n API Requests Without Duplicates

floxolab - Oct 14

Claude Ship Can Approve Pull Requests. Your GitHub Rules Still Matter

MahanKenway - Nov 24

Dyna Software Brings Git-Style Pull Requests to ServiceNow — and Locks Down How Code Gets Promoted

Tom Smithverified - Sep 15
chevron_left
17.6k Points • 463 Badges
117Posts
182Comments
142Connections
Steve Fenton is an Octonaut at Octopus Deploy. He’s a Software Punk, author, programming-architect, ... Show more

Related Jobs

View all jobs →

Commenters (This Week)

9 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!