Last active
March 10, 2020 20:44
-
-
Save lacostenycoder/cab1f7d10fb36ebc98575bbc36151185 to your computer and use it in GitHub Desktop.
Clean branches by closed issue numbers
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
#!/usr/bin/env ruby | |
unless !!`which gh | grep 'bin/gh'` | |
abort('Please run brew install github/gh/gh first. exiting now.') | |
end | |
Dir.chdir(ENV['HOME'] + '/dev/clients/thoroughcare') | |
issue_numbers = `gh issue list -s open -L 500 | grep -o '^[[:digit:]]*'`.split("\n") | |
branches = `git branch --sort=committerdate`.split("\n").map(&:strip) | |
branch_issue_numbers = branches.select{|b| b[/\/\d+/]}.map{|i| i[/\d+/]} | |
delete_issue_numbers = branch_issue_numbers.reject{|n| issue_numbers.include? n}.map(&:to_s) | |
unless delete_issue_numbers.any? | |
abort('No branches with matching open issues found') | |
end | |
regex = Regexp.new(delete_issue_numbers.join('|').insert(0, '/')) | |
branches_to_delete = branches.select{|b| b[regex]} | |
puts branches_to_delete | |
puts "\n" | |
puts 'These branches have issues which have been closed' | |
puts 'Are you sure you want to delete them? Y/N' | |
if gets.chomp.downcase == 'y' | |
branches_to_delete.each{|branch| `git branch -D #{branch}`} | |
puts 'The branches have been deleted' | |
else | |
abort('No branches have been deleted') | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to use the new https://cli.github.com/ tool in place of octokit gem which seems to have some pagination issues atm.