Skip to content

Instantly share code, notes, and snippets.

@joakimk
Forked from aalin/pivotalstatus.rb
Created June 23, 2011 11:37
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 joakimk/1042396 to your computer and use it in GitHub Desktop.
Save joakimk/1042396 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 story_ids = get_story_ids()
cleared_ids = []
PivotalTracker::Project.all.each do |project|
story_ids.each do |story_id|
if story = project.stories.find(story_id)
print_story(story)
cleared_ids << story_id
end
end
end
story_ids_without_info = story_ids - cleared_ids
unless story_ids_without_info.empty?
puts
puts "Could not find info for the following story ids: #{ story_ids_without_info.join(", ") }"
end
else
$stderr.puts "No story ids found."
end
end
private
def get_story_ids
story_ids = branches.select { |branch| branch.match(/^s-\d+/) }.map { |branch| branch.gsub(/^s-/, '').to_i }.uniq.sort
return nil if story_ids.empty?
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(story)
puts format("%-10d %-10s - %s", story.id, 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