Skip to content

Instantly share code, notes, and snippets.

@aalin
Created June 22, 2011 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aalin/1040048 to your computer and use it in GitHub Desktop.
Save aalin/1040048 to your computer and use it in GitHub Desktop.
Pivotal tracker git status thing...
#!/usr/bin/env ruby
require 'rubygems'
require 'pivotal-tracker'
class String
ANSI_COLORS = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
def colorize(color)
num = ANSI_COLORS.index(color) or raise("Bad color #{ color }")
"\e[3#{ num }m" + self + "\e[0m"
end
end
class PivotalStatus
STATE_COLORS = Hash.new(:white).merge({
"unstarted" => :magenta,
"started" => :blue,
"finished" => :green,
"delivered" => :green,
"accepted" => :green,
"rejected" => :red,
})
def print
if branches_and_story_ids = get_branches_and_story_ids()
cleared_ids = []
PivotalTracker::Project.all.each do |project|
branches_and_story_ids.each do |branch, story_id|
if story = project.stories.find(story_id)
print_story(branch, story)
cleared_ids << story_id
end
end
end
branches_and_story_ids_without_info = branches_and_story_ids.reject { |branch, story_id| cleared_ids.include?(story_id) }
unless branches_and_story_ids_without_info.empty?
puts
puts "Could not find info for the following branches: #{ branches_and_story_ids_without_info.map(&:first).join(", ") }"
end
else
$stderr.puts "No story ids found."
end
end
private
def get_branches_and_story_ids
branches_and_story_ids = branches.select { |branch| branch.match(/^(t|s-)\d+/) }.map { |branch| [ branch, branch.gsub(/^(t|s)-?/, '').to_i ] }.uniq.sort_by(&:last)
return nil if branches_and_story_ids.empty?
branches_and_story_ids
end
def branches(include_remote = false)
if include_remote
`git branch -r 2>/dev/null`.scan(/^\s\sorigin\/(t\d+)$/).flatten
else
`git branch 2>/dev/null`.scan(/^[\s\*]\s(.*)$/).flatten
end
end
def print_story(branch, story)
puts format("%-15s %-10s - %s", branch, story.current_state, story.name).colorize(STATE_COLORS[story.current_state])
story.tasks.all.each do |task|
puts " #{ task.description }".colorize(task.complete ? :green : :white)
end
end
end
begin
PivotalTracker::Client.token = File.read(File.expand_path("~/.pivotal_api_token").strip)
PivotalStatus.new.print
rescue Errno::ENOENT => e
$stderr.puts e.message
exit 1
rescue RestClient::Request::Unauthorized => e
$stderr.puts "Unauthorized! You're probably using the wrong API key"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment