Skip to content

Instantly share code, notes, and snippets.

@topherfangio
Last active November 25, 2015 15:34
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 topherfangio/957325e18adf64897aa3 to your computer and use it in GitHub Desktop.
Save topherfangio/957325e18adf64897aa3 to your computer and use it in GitHub Desktop.
Quick GitHub branch/issue analyzer
#!/usr/bin/env ruby
############
# OVERVIEW #
############
#
# This (very rudimentary) script will show you the status of issues that your
# branches are tied to.
#
# To use, install it with the instructions below, and make sure to include the
# issue number in your branch name like so:
#
# fix-some-fun-bug-1324
# feature-something-cool-5678
#
# It will show you all local branches with the status next to them and then
# print out a command you can copy/paste (after reviewing) to delete all
# branches where the issue has been closed.
#
#################
# SAMPLE OUTPUT #
#################
#
# STATE | BRANCH
# -------|------------------------------------------
# | master
# | team/topher/add-missing-docs
# open | team/topher/docs-layout-tips-5652
# open | team/topher/fix-autocomplete-click-5424
# closed | team/topher/fix-disabled-list-icon-3356
# open | team/topher/fix-messages-jumps-5298
# | * wip/toolTip_positioning
#
# Safe to Delete:
# git branch -D team/topher/fix-disabled-list-icon-3356
#
################
# INSTALLATION #
################
#
# You must have ruby/rubygems installed. Then run
#
# gem install ghee table_print
#
# Then, just put this file somewhere in your path like ~/bin,
# chmod it to make it executable, then run the following in
# your repo's directory
#
# ~/bin/branch-analyzer.rb
#
#####################
# TODO (eventually) #
#####################
#
# 1. Replace username/password with SSH or OAUTH key generation
# 2. Check current directory for repo name instead of hard-coding
# 3. Show other useful issue data (description, # comments, etc)
#
require 'Ghee'
require 'table_print'
# Authenticate with GitHub
gh = Ghee.basic_auth("USERNAME", "PASSWORD") # Replace with your GH credentials
# Get our list of branches
branches = `git branch`
safe_to_delete = []
stats = branches.split("\n").map do |branch|
issue_number = branch[/\d+/]
issue = gh.repos("USERNAME", "REPO").issues(issue_number) if issue_number # Replace with the username/repo you want to analyze
state = issue['state'] if issue
if state == "closed"
safe_to_delete << branch
end
{ state: state, branch: branch }
end
# Print out the branches and status table
tp stats, :state, branch: { width: 50 }
# Check to see if we have any branches that are safe to delete and print them out
if safe_to_delete.size > 0
puts "\n\nSafe to Delete:"
puts " git branch -D #{safe_to_delete.join('')}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment