Skip to content

Commit

Permalink
implement working hub pull-request
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-oudard authored and mislav committed Oct 24, 2011
1 parent 93dc23a commit dbedb07
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ superpowers:
[ repo forked on GitHub ]
> git remote add -f YOUR_USER git@github.com:YOUR_USER/CURRENT_REPO.git

### git pull-request

[ in a branch called my_branch on CURRENT_REPO ]
$ git pull-request -t "Issue Title" -y "Issue description."
[ pull request sent on GitHub, into CURRENT_REPO:master from CURRENT_REPO:my_branch ]

[ on a fork of mislav:CURRENT_REPO ]
$ git pull-request -t "Issue Title" -y "Issue description." -b mislav:master
[ pull request sent on GitHub, into mislav/CURRENT_REPO:master from YOUR_USER/CURRENT_REPO:master ]

$ git pull-request -i 123
[ existing issue #123 turned into a pull request from the current branch ]

### git create

$ git create
Expand Down
48 changes: 26 additions & 22 deletions lib/hub/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ module Commands
# Provides `github_url` and various inspection methods
extend Context

API_REPO = 'http://github.com/api/v2/yaml/repos/show/%s/%s'
API_FORK = 'https://github.com/api/v2/yaml/repos/fork/%s/%s'
API_CREATE = 'https://github.com/api/v2/yaml/repos/create'
API_PULLR = 'http://github.com/api/v2/json/pulls/%s'
API_REPO = 'http://github.com/api/v2/yaml/repos/show/%s/%s'
API_FORK = 'https://github.com/api/v2/yaml/repos/fork/%s/%s'
API_CREATE = 'https://github.com/api/v2/yaml/repos/create'
API_PULLREQUEST = 'https://github.com/api/v2/json/pulls/%s/%s'

def run(args)
slurp_global_flags(args)
Expand All @@ -66,23 +66,24 @@ def run(args)
end
end

def pullreq(args)
def pull_request(args)
args.skip!
if !is_repo?
puts "'pullreq' must be run from inside a git repository"
puts "'pullrequest' must be run from inside a git repository"
return
end
if !github_user
puts "Github user and token required in .gitconfig."
return
end
branch = `git name-rev --name-only HEAD`.strip
# Remove 'pullreq'
# Remove 'pullrequest'
args.shift

options = {}
until args.length == 1
until args.length <= 1
case arg = args.shift
when '-i'
options[:issue] = args.shift
when '-t'
options[:title] = args.shift
when '-y'
Expand All @@ -96,14 +97,12 @@ def pullreq(args)
return
end
end
if options[:title].nil?
puts "-t must be specified!"
args.ski
if options[:title].nil? and options[:issue].nil?
puts "Please specify either -t (title) or -i (existing issue number)."
args.skip!
return
end
if branch
create_pullreq(args.shift, "#{github_user}:#{branch}", options)
end
create_pullrequest(args.shift, options)
end

# $ hub clone rtomayko/tilt
Expand Down Expand Up @@ -779,16 +778,21 @@ def create_repo(name, options = {})
response.error! unless Net::HTTPSuccess === response
end

def create_pullreq(repo, head, options)
def create_pullrequest(head, options)
require 'net/http'
url = API_PULLR
params = {'login' => github_user, 'token' => github_token}
params = {}
params['pull[issue]'] = options[:issue] if options[:issue]
params['pull[title]'] = options[:title] if options[:title]
params['pull[body]'] = options[:body] if options[:body]
params['pull[base]'] = options[:base] ? options[:base] : 'master'
params['pull[head]'] = head

response = Net::HTTP.post_form(URI(url % repo), params)
# We are sending the pull request on the base repo, so construct the url accordingly.
base = options[:base] ? options[:base] : "#{repo_owner}:master"
base_owner, base_branch = base.split(':')
params['pull[base]'] = base_branch
url = API_PULLREQUEST % [base_owner, repo_name]
# Pull from the current branch by default.
branch = normalize_branch(current_branch)
params['pull[head]'] = head ? head : "#{repo_owner}:#{branch}"
response = http_post(url, params)
response.error! unless Net::HTTPSuccess === response
end

Expand Down
27 changes: 27 additions & 0 deletions test/hub_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,33 @@ def test_fork_already_exists
assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
end

def test_pullrequest_no_args_error
expected = "Please specify either -t (title) or -i (existing issue number).\n"
assert_equal expected, hub("pull-request") { ENV['GIT'] = 'echo' }
end

def test_pullrequest_title_only
stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/tpw/hub").
with(:body => {'pull' => {'title'=>"issue_name", 'base'=>"master", 'head'=>"defunkt:master"}})
expected = ""
assert_equal expected, hub('pull-request -b tpw:master -t issue_name') { ENV['GIT'] = 'echo' }
end

def test_pullrequest_existing_issue
stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/tpw/hub").
with(:body => {'pull' => {'issue'=>"123", 'base'=>"master", 'head'=>"defunkt:master"}})
expected = ""
assert_equal expected, hub('pull-request -b tpw:master -i 123') { ENV['GIT'] = 'echo' }
end

def test_pullrequest_same_repo
stub_branch('refs/heads/feature')
stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
with(:body => {'pull' => {'title'=>"issue_name", 'base'=>"master", 'head'=>"defunkt:feature"}})
expected = ""
assert_equal expected, hub('pull-request -t issue_name') { ENV['GIT'] = 'echo' }
end

def test_version
out = hub('--version')
assert_includes "git version 1.7.0.4", out
Expand Down

0 comments on commit dbedb07

Please sign in to comment.