Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

browse -b flag #258

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/hub/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,9 @@ def push(args)
# $ hub browse
# > open https://github.com/CURRENT_REPO
#
# $ hub browse -b BRANCH_NAME
# > open https://github.com/CURRENT_REPO/pull/258
#
# $ hub browse -- issues
# > open https://github.com/CURRENT_REPO/issues
#
Expand All @@ -632,7 +635,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
Expand Down
20 changes: 20 additions & 0 deletions lib/hub/github_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
37 changes: 37 additions & 0 deletions test/hub_test.rb
Original file line number 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")
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"
Expand Down Expand Up @@ -525,6 +549,19 @@ def mock_pull_response(label, priv = false)
}
end

def mock_list_pulls_response(branches, name_with_owner = 'defunkt/hub', host = 'github.com')
arr = []
branches.each_pair do |branch, id|
arr << {
'head' => {
'label' => branch,
},
'html_url' => "https://#{host}/#{name_with_owner}/pull/#{id}",
}
end
Hub::JSON.generate arr
end

def improved_help_text
Hub::Commands.send :improved_help_text
end
Expand Down