Skip to content

Commit

Permalink
Followup changes in #92
Browse files Browse the repository at this point in the history
Clean up base_url class member and property.  Fix urls sent to webbrowser and web_viewer.  Update unit tests.
  • Loading branch information
donnemartin committed Nov 12, 2016
1 parent e156d08 commit b4bae40
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
32 changes: 21 additions & 11 deletions gitsome/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ class GitHub(object):
:type web_viewer: :class:`web_viewer.WebViewer`
:param web_viewer: An instance of `web_viewer.WebViewer`.
"""
GH_BASE_URL = 'https://github.com/'
:type _base_url: str
:param _base_url: The base GitHub or GitHub Enterprise url.
"""

def __init__(self):
self.config = Config()
Expand All @@ -68,10 +69,21 @@ def __init__(self):
self.table = Table(self.config)
self.web_viewer = WebViewer(self.config)
self.trend_parser = feedparser
self._base_url = 'https://github.com/'

@property
def gh_url(self):
return self.config.enterprise_url or self.GH_BASE_URL
def base_url(self):
return self.config.enterprise_url or self._base_url

def add_base_url(self, url):
"""Add the base url if it is not already part of the given url.
:type url: str
:param url: The url.
:return: The url including the base url.
"""
return self.base_url + url if self.base_url not in url else url

def authenticate(func):
"""Decorator that authenticates credentials.
Expand Down Expand Up @@ -416,7 +428,7 @@ def issue(self, user_repo_number):
click.secho('Expected argument: user/repo/#.',
fg=self.config.clr_error)
return
url = (self.gh_url + user + '/' + repo + '/' +
url = (self.base_url + user + '/' + repo + '/' +
'issues/' + number)
self.web_viewer.view_url(url)

Expand Down Expand Up @@ -659,7 +671,7 @@ def repository(self, user_repo):
click.secho('Expected argument: user/repo.',
fg=self.config.clr_error)
return
self.web_viewer.view_url(self.gh_url + user_repo)
self.web_viewer.view_url(self.base_url + user_repo)

@authenticate
def search_issues(self, query, limit=1000, pager=False):
Expand Down Expand Up @@ -819,7 +831,7 @@ def user(self, user_id, browser=False, text_avatar=False,
if available.
"""
if browser:
webbrowser.open(self.gh_url + user_id)
webbrowser.open(self.base_url + user_id)
else:
user = self.config.api.user(user_id)
if type(user) is null.NullObject:
Expand Down Expand Up @@ -900,15 +912,13 @@ def view(self, index, view_in_browser=False):
self.config.urls = self.config.load_urls(view_in_browser)
url = self.config.urls[index-1]
click.secho('Viewing ' + url + '...', fg=self.config.clr_message)
if self.gh_url not in url:
url = self.gh_url + url
if view_in_browser:
webbrowser.open(url)
webbrowser.open(self.add_base_url(url))
else:
if 'issues/' in url:
url = url.replace('issues/', '')
self.issue(url)
elif len(url.split('/')) == 2:
self.repository(url)
else:
self.web_viewer.view_url(url)
self.web_viewer.view_url(self.add_base_url(url))
11 changes: 8 additions & 3 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,13 @@ def test_view_user(self, mock_view_url, mock_click_secho):
self.github.view(0)
mock_view_url.assert_called_with('https://github.com/user1')

def test_gh_url(self):
def test_base_url(self):
self.github.config.enterprise_url = 'https://github.intra.example.com'
assert self.github.gh_url == 'https://github.intra.example.com'
assert self.github.base_url == 'https://github.intra.example.com'
self.github.config.enterprise_url = None
assert self.github.gh_url == self.github.GH_BASE_URL
assert self.github.base_url == self.github._base_url

def test_add_base_url(self):
expected = self.github.base_url + 'foo.html'
assert self.github.add_base_url('foo.html') == expected
assert self.github.add_base_url(expected) == expected

0 comments on commit b4bae40

Please sign in to comment.