Skip to content
Browse files

Fix conflicts from previous merge and move "pull-request -l" to "brow…

…se -b".
  • Loading branch information...
1 parent 6cb932c commit 45628d4952d89cc07cbeb395920609155232b327 @jianli committed Jun 11, 2013
Showing with 53 additions and 8 deletions.
  1. +9 −8 lib/hub/commands.rb
  2. +20 −0 lib/hub/github_api.rb
  3. +24 −0 test/hub_test.rb
View
17 lib/hub/commands.rb
@@ -131,13 +131,6 @@ def pull_request(args)
while arg = args.shift
case arg
- when '-l'
- branch_name = args.shift || current_branch.short_name
- pull_data = api_client.get_pullrequest(base_project, branch_name)
- if pull_data
- puts pull_data
- end
- exit
when '-f'
force = true
when '-F', '--file'
@@ -639,7 +632,15 @@ def browse(args)
dest = args.shift
dest = nil if dest == '--'
- if dest
+ if dest == '-b'
+ branch_name = args.shift || current_branch.short_name
+ pr_url = api_client.get_pullrequest(local_repo.main_project, branch_name)
+ if pr_url
+ next pr_url
+ else
+ abort "Pull request not found for branch #{branch_name}."
+ end
+ elsif dest
# $ hub browse pjhyett/github-services
# $ hub browse github-services
project = github_project dest
View
20 lib/hub/github_api.rb
@@ -118,6 +118,26 @@ def statuses project, sha
res.data
end
+ # Return the pull request corresponding to the current branch
+ def get_pullrequest project, branch_name
+ for state in ['open', 'closed']
+ page = 1
+ res = nil
+ while page == 1 or res.data.length > 0
+ res = get "https://%s/repos/%s/%s/pulls?state=%s&page=%s" %
+ [api_host(project.host), project.owner, project.name, state, page]
+ res.error! unless res.success?
+ res.data.each { |x|
+ if branch_name == x['head']['label'].split(':', 0)[1]
+ return x['html_url']
+ end
+ }
+ page += 1
+ end
+ end
+ nil
+ end
+
# Methods for performing HTTP requests
#
# Requires access to a `config` object that implements:
View
24 test/hub_test.rb
@@ -356,6 +356,30 @@ def test_hub_browse_no_repo
assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
end
+ def test_hub_browse_branch
+ stub_request(:get, "https://api.github.com/repos/defunkt/hub/pulls?page=1&state=open").
+ to_return(:body =>
+ mock_list_pulls_response({
+ 'defunkt:feature-foo' => 1,
+ 'defunkt:feature-bar' => 2,
+ }))
+ stub_request(:get, "https://api.github.com/repos/defunkt/hub/pulls?page=2&state=open").
+ to_return(:body =>
+ mock_list_pulls_response({
+ 'jianlius:feature-baz' => 3,
+ 'jianlius:feature-qux' => 4,
+ }))
+
+ expected = "open https://github.com/defunkt/hub/pull/2"
+ assert_command "browse -b feature-bar", expected
+
+ stub_branch('refs/heads/feature-qux')
+ stub_tracking('feature-qux', 'upstream')
+
+ expected = "open https://github.com/defunkt/hub/pull/4"
+ assert_command "browse -b", expected
+ end
+
def test_hub_browse_ssh_alias
with_ssh_config "Host gh\n User git\n HostName github.com" do
stub_repo_url "gh:singingwolfboy/sekrit.git"

0 comments on commit 45628d4

Please sign in to comment.
Something went wrong with that request. Please try again.