Skip to content

Instantly share code, notes, and snippets.

@dgmike
Last active February 14, 2017 18:44
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 dgmike/24dde7f0017c8f588d141b4a73302787 to your computer and use it in GitHub Desktop.
Save dgmike/24dde7f0017c8f588d141b4a73302787 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
### Put this file in any $PATH of your system aka /usr/local/bin and run as follow
### $ git pivotal_checkout 139473593
### + git checkout -b bug/139473593-nome-da-task-do-pivotal
ENV['PIVOTAL_TOKEN'] = 'XXXXXXXXXXXX'
ENV['PROJECT_ID'] = 'XXXXXXXXX'
##### /PivotalApiLib
require 'net/http'
require 'json'
class PivotalApi
def self.request(path, params = nil, net_object = Net::HTTP::Get)
uri = URI "https://www.pivotaltracker.com/services/v5#{path}"
uri.query = URI.encode_www_form(params) if params && net_object == Net::HTTP::Get
req = net_object.new(uri)
req.set_form_data(params) unless net_object == Net::HTTP::Get
req['X-TrackerToken'] = ENV['PIVOTAL_TOKEN']
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
http.request(req)
end
# puts "Response: #{res.body}"
res
end
def self.get(path, params = nil)
# puts "[PivotalAPI] GET path: #{path} - params: #{params.inspect}"
JSON.parse(request(path, params).body)
end
end
##### /PivotalApiLib
if ARGV.size < 1
puts "*** please, inform story ID"
exit
end
story_id = ARGV[0]
story = PivotalApi.get "/projects/#{ENV['PROJECT_ID']}/stories/#{story_id}"
branch = "#{story["story_type"]}/#{story["id"]}-#{story["name"].gsub(/([^A-Z-])([A-Z])/, '\1_\2').downcase.gsub(/\W/, "-").gsub(/_?-_?/, '-')}"
puts "+ git checkout -b #{branch}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment