Skip to content

Commit

Permalink
Fix conflicts from previous merge and move "pull-request -l" to "brow…
Browse files Browse the repository at this point in the history
…se -b".
  • Loading branch information
jianli committed Jun 12, 2013
1 parent 6cb932c commit 45628d4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/hub/commands.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -131,13 +131,6 @@ def pull_request(args)


while arg = args.shift while arg = args.shift
case arg 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' when '-f'
force = true force = true
when '-F', '--file' when '-F', '--file'
Expand Down Expand Up @@ -639,7 +632,15 @@ def browse(args)
dest = args.shift dest = args.shift
dest = nil if dest == '--' 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 pjhyett/github-services
# $ hub browse github-services # $ hub browse github-services
project = github_project dest project = github_project dest
Expand Down
20 changes: 20 additions & 0 deletions lib/hub/github_api.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ def statuses project, sha
res.data res.data
end 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 # Methods for performing HTTP requests
# #
# Requires access to a `config` object that implements: # Requires access to a `config` object that implements:
Expand Down
24 changes: 24 additions & 0 deletions test/hub_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -356,6 +356,30 @@ def test_hub_browse_no_repo
assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse") assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
end 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 def test_hub_browse_ssh_alias
with_ssh_config "Host gh\n User git\n HostName github.com" do with_ssh_config "Host gh\n User git\n HostName github.com" do
stub_repo_url "gh:singingwolfboy/sekrit.git" stub_repo_url "gh:singingwolfboy/sekrit.git"
Expand Down

0 comments on commit 45628d4

Please sign in to comment.