-
-
Save digitalpardoe/308873633ddc743bf0c67e31e178d565 to your computer and use it in GitHub Desktop.
Find Un-Deleted Branches from Closed PRs and Stale Branches
This file contains 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' | |
token = ask("Enter your Github personal access token: ") { |q| q.echo = "*" } | |
repo = ask("Enter the full Github repo name (eg. pickle27/the_arborist): ") { |q| q.echo = true } | |
client = Octokit::Client.new(access_token: token) | |
user = client.user | |
user.login | |
################################################################################ | |
stale_branch_list = [] | |
# Iterate through branches and find "stale" branches and delete them | |
found = 0 | |
branches = client.branches repo, :per_page => 100 | |
last_response = client.last_response | |
while last_response.rels[:next] do | |
puts "Retrieved #{branches.count} branches..." | |
branches.concat last_response.rels[:next].get.data | |
last_response = last_response.rels[:next].get | |
end | |
branches.each do |branch| | |
begin | |
sha = branch.commit.attrs[:sha] | |
commit = client.commit repo, sha | |
if Time.now - commit.commit.author.date > 120.days | |
stale_branch_list.push( [commit.author.login, branch.name] ) | |
puts "Deleting branch #{branch.name.red}..." | |
# client.delete_branch repo, branch.name | |
found += 1 | |
end | |
rescue | |
end | |
end | |
puts "Done!" | |
# Save to CSV | |
CSV.open("stale_branches.csv", "w") do |csv| | |
stale_branch_list.each do |item| | |
csv << item | |
end | |
end | |
################################################################################ | |
pull_request_branch_list = [] | |
# Iterate through closed & merged pull requests and delete undeleted branches | |
found = 0 | |
pulls = client.pulls repo, state: "closed", per_page: 100 | |
last_response = client.last_response | |
while last_response.rels[:next] do | |
puts "Retrieved #{pulls.count} PRs..." | |
pulls.concat last_response.rels[:next].get.data | |
last_response = last_response.rels[:next].get | |
end | |
pulls.each do |pull| | |
if pull.merged_at? || pull.closed_at? | |
begin | |
branch = client.branch repo, pull.head.ref | |
pull_request_branch_list.push( [pull.user.login, pull.rels[:html].href, pull.head.ref] ) if pull.user | |
puts "Deleting branch #{pull.head.ref.red}..." | |
# client.delete_branch repo, pull.head.ref | |
found += 1 | |
rescue | |
end | |
end | |
end | |
puts "Done!" | |
# Save to CSV | |
CSV.open("pull_request_branches.csv", "w") do |csv| | |
pull_request_branch_list.each do |item| | |
csv << item | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment