Skip to content

Instantly share code, notes, and snippets.

@inkdeep
Created November 24, 2009 21:21
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 inkdeep/242231 to your computer and use it in GitHub Desktop.
Save inkdeep/242231 to your computer and use it in GitHub Desktop.
Ruby shell script to get 'My Stories' from PivotalTracker and format them for github to pivotal api friendliness
#!/usr/bin/env ruby
require 'rubygems'
require 'highline'
require 'xmlsimple'
require 'uri'
# Retrieves stories a user owns from PivotalTracker API
# Formats the name and story number for pivotial friendly commit messages.
#
# Add this to .bash_profile or .bashrc and replace the values with your information:
# ## pivotal tracker API call variables
# export PT_API_KEY='_YOUR_API_KEY_'
# export PT_PROJECT_KEY='_PROJECT_NUMBER_'
# export PT_STORY_OWNER='_YOUR_NAME_'
#
# Put a string as an argument when calling the script to prepend text to the story name - default string is 'www'
# $ ptstory enet
# $ ptstory 'I like cheese'
class PtStory
def initialize
@argv = !$*.empty? ? $* : 'www'
@ids_titles = []
@formatted = []
get_pt_stories
end
def ui
@ui ||= HighLine.new
end
def get_pt_stories
# curl -H "X-TrackerToken: TOKEN" -X GET http://www.pivotaltracker.com/services/v3/projects/PROJECT_ID/stories?filter=label%3A%22needs%20feedback%22%20type%3Abug
stories = XmlSimple.xml_in(`curl -s -H "X-TrackerToken: #{ENV['PT_API_KEY']}" -X GET http://www.pivotaltracker.com/services/v3/projects/#{ENV['PT_PROJECT_KEY']}/stories?filter=owner%3A%22#{URI.escape(ENV['PT_STORY_OWNER'])}%22`)
if stories.is_a?(Hash)
stories['story'].each {|s| @ids_titles << [s['id'][0]['content'], s['name']] }
@ids_titles.each_index {|x| @formatted << "[##{@ids_titles[x][0]}] #{@argv} - #{@ids_titles[x][1]}"}
puts '-' * 80
@formatted.each_index {|x| puts "#{x.to_s.rjust(2)}: #{@formatted[x]}"}
puts '-' * 80
elsif stories.is_a?(String)
raise "API ERROR: #{stories}"
else
raise "Something is very wrong…"
end
end
def which_one
choice = ''
loop do
prompt = "Enter number of story commit message to copy to clipboard:"
choice = ui.ask(prompt)
if /^\d+$/ =~ choice.to_s && choice.to_i <= @formatted.length-1
`echo "#{@formatted[choice.to_i]}" | pbcopy`
puts "Copied to clipboard: \e[1;36m#{@formatted[choice.to_i ]}\e[0m"
return true
else
$stderr.puts ">>>try again<<<"
end
end
end
end
stories = PtStory.new
stories.which_one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment