Last active
February 17, 2017 15:55
-
-
Save digitalronin/04f997f06ec53e229c736dc91a029249 to your computer and use it in GitHub Desktop.
Enable "git ticket 12345" to create a git branch from the title of Pivotal ticket 12345
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
# Create a git branch, off master, for a specific Pivotal Tracker ticket. | |
# The name of the branch will be the normalised title of the pivotal ticket. | |
USAGE = "USAGE:\ngit ticket TICKET_ID\n" | |
ticketid = ARGV[0] | |
unless ticketid | |
print USAGE | |
exit | |
end | |
def main(ticketid) | |
title = ticket_title(ticketid) | |
raise "Could not find ticket with id #{ticketid}" unless title | |
execute 'git checkout master' | |
execute "git checkout -b #{normalise title}" | |
end | |
def ticket_title(ticketid) | |
token, projectid = get_credentials | |
json = execute %[curl -s -X GET -H "X-TrackerToken: #{token}" "https://www.pivotaltracker.com/services/v5/projects/#{projectid}/stories/#{ticketid}"] | |
JSON.parse(json)['name'] | |
end | |
def get_credentials | |
token = execute('git config pivotal.token').strip | |
projectid = execute('git config pivotal.projectid').strip | |
git_config_usage if (token.empty? || projectid.empty?) | |
[token, projectid] | |
end | |
def normalise(str) | |
str.gsub(/[^A-Za-z0-9 ]/, '') | |
.downcase | |
.gsub(' ', '-') | |
end | |
def execute(cmd) | |
`#{cmd}` | |
end | |
def git_config_usage | |
puts <<-EOF | |
Please set pivotal.token and pivotal.projectid in your .git/config | |
before running this script. To set these values globally, run the | |
following (without the brackets); | |
git config --global pivotal.token [API token] | |
git config --global pivotal.projectid [projectid] | |
EOF | |
exit | |
end | |
main(ticketid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by (i.e. ripped off from) here: https://revs.runtime-revolution.com/extending-git-with-ruby-874fddffd069#.714k9y23t