Skip to content

Instantly share code, notes, and snippets.

@thejspr
Last active October 14, 2015 04:56
Show Gist options
  • Save thejspr/d2eaa484376c338a22ff to your computer and use it in GitHub Desktop.
Save thejspr/d2eaa484376c338a22ff to your computer and use it in GitHub Desktop.
Checkout branch named after title of a Github issue
#!/usr/bin/env ruby
require 'net/http'
require 'json'
token = ENV['GITHUB_TOKEN']
issue_id = ARGV.first
# get org and repo
repo_url = `git config --get remote.origin.url`.strip
repo = /([\w,-]*\/.*)\.git/.match(repo_url)[1]
unless token || issue_id || repo
puts "Missing parameter. Known values: issue-id=#{issue_id.inspect} repo=#{repo.inspect} token=#{token.inspect}"
exit(1)
end
# fetch issue details
uri = URI.parse("https://api.github.com/repos/#{repo}/issues/#{issue_id}?access_token=#{token}")
json = Net::HTTP.get(uri)
response = JSON.parse(json)
title = response['title']
.downcase
.gsub(/[^0-9A-Za-z\s]/, '') # remove special characters
.gsub(' ', '-').strip # replace whitespace with dash
# checkout branch
system "git checkout -b #{title}"
@thejspr
Copy link
Author

thejspr commented Oct 14, 2015

Usage:

$ issue-branch <issue-id>

Requires a github token in env as GITHUB_TOKEN. Create one here https://github.com/settings/tokens/new

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment