Last active
January 7, 2022 22:53
-
-
Save kevinhughes27/9753852 to your computer and use it in GitHub Desktop.
find un-deleted branches from closed PRs
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 | |
require 'octokit' | |
require 'highline/import' | |
require 'colorize' | |
require 'byebug' | |
require 'csv' | |
require 'active_support/time' | |
username = ask("Enter your Github username: ") { |q| q.echo = true } | |
password = ask("Enter your Github password: ") { |q| q.echo = "*" } | |
repo = ask("Enter the full Github repo name (eg. pickle27/the_arborist): ") { |q| q.echo = true } | |
Octokit.configure do |c| | |
c.login = username | |
c.password = password | |
end | |
branch_list = [] | |
# Iterate through branches and find "stale" branches | |
# page = 1 | |
# found = 0 | |
# branches = Octokit.branches repo, :per_page => 100, :page => page | |
# until branches.empty? do | |
# puts "processing branches page #{page}".blue | |
# branches.each do |branch| | |
# puts "processing branch #{branch.name}" | |
# begin | |
# sha = branch.commit.attrs[:sha] | |
# commit = Octokit.commit repo, sha | |
# if Time.now - commit.commit.author.date > 120.days | |
# branch_list.push(commit.author.login, branch.name) | |
# puts branch_list.last.to_s.red | |
# found += 1 | |
# end | |
# rescue | |
# # something went wrong | |
# end | |
# end | |
# page += 1 | |
# branches = Octokit.branches repo, :per_page => 100, :page => page | |
# end | |
# puts "Found #{found} stale branches" | |
# Iterate through closed pull requests and find undeleted branches | |
page = 1 | |
found = 0 | |
pulls = Octokit.pulls repo, "closed", :per_page => 100, :page => page | |
until pulls.empty? do | |
puts "processing pull requests page #{page}".blue | |
pulls.each do |pull| | |
if pull.closed_at? && !pull.merged_at? | |
begin | |
branch = Octokit.branch repo, pull.head.ref | |
branch_list.push( [pull.user.login, pull.rels[:html].href] ) if pull.user | |
puts branch_list.last.to_s.red | |
found += 1 | |
rescue Octokit::NotFound | |
#branch was deleted | |
end | |
end | |
puts "processing pull request #{pull.number}" | |
end | |
page += 1 | |
pulls = Octokit.pulls repo, "closed", :per_page => 100, :page => page | |
end | |
puts "Found #{found} closed pull requests with undeleted branches" | |
# Save to CSV | |
CSV.open("branches.csv", "w") do |csv| | |
branch_list.each do |item| | |
csv << item | |
end | |
end | |
# find the worese offenders in the group lolz | |
#cat branches.csv | awk -F "," '{print $1}' | sort | uniq -c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment