Skip to content

Instantly share code, notes, and snippets.

@kirikiriyamama
Last active December 21, 2023 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirikiriyamama/4a125d5c08b27b754945c4ef0b1be059 to your computer and use it in GitHub Desktop.
Save kirikiriyamama/4a125d5c08b27b754945c4ef0b1be059 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
require 'time'
repo = 'xxxx/yyyy'
run = false
usage = "Usage: #{$0} [--run]"
case ARGV[0]
when nil
warn 'Running in dry-run mode. Pass --run to actually delete branches.'
when '--run'
run = true
when '--help', '-h'
puts usage
exit 0
else
warn usage
exit 1
end
# https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-branches-in-your-repository/viewing-branches-in-your-repository
#
# > Stale branches: The Stale view shows all branches that no one has committed to in the last three months,
# > ordered by the branches with the oldest commits first.
stale = Time.now - 7776000 # 3 months ago
branches = `gh api repos/#{repo}/branches --paginate`
exit 1 if !$?.success?
JSON.parse(branches).map { |b| b['name'] }.each do |name|
next if name.start_with?('archive/')
branch = `gh api repos/#{repo}/branches/#{name}`
exit 1 if !$?.success?
next if Time.parse(JSON.parse(branch)['commit']['commit']['committer']['date']) > stale
puts name
if run
`gh api repos/#{repo}/git/refs/heads/#{name} -X DELETE`
exit 1 if !$?.success?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment