diff --git a/readthedocs/builds/models.py b/readthedocs/builds/models.py index bd15b5b3c69..b7accc54c48 100644 --- a/readthedocs/builds/models.py +++ b/readthedocs/builds/models.py @@ -345,10 +345,8 @@ def get_github_url( if not docroot: return '' - if docroot[0] != '/': - docroot = '/{}'.format(docroot) - if docroot[-1] != '/': - docroot = '{}/'.format(docroot) + # Normalize /docroot/ + docroot = '/' + docroot.strip('/') + '/' if action == 'view': action_string = 'blob' @@ -360,6 +358,10 @@ def get_github_url( return '' repo = repo.rstrip('/') + if not filename: + # If there isn't a filename, we don't need a suffix + source_suffix = '' + return GITHUB_URL.format( user=user, repo=repo, @@ -384,10 +386,8 @@ def get_gitlab_url( if not docroot: return '' - if docroot[0] != '/': - docroot = '/{}'.format(docroot) - if docroot[-1] != '/': - docroot = '{}/'.format(docroot) + # Normalize /docroot/ + docroot = '/' + docroot.strip('/') + '/' if action == 'view': action_string = 'blob' @@ -399,6 +399,10 @@ def get_gitlab_url( return '' repo = repo.rstrip('/') + if not filename: + # If there isn't a filename, we don't need a suffix + source_suffix = '' + return GITLAB_URL.format( user=user, repo=repo, @@ -416,11 +420,18 @@ def get_bitbucket_url(self, docroot, filename, source_suffix='.rst'): if not docroot: return '' + # Normalize /docroot/ + docroot = '/' + docroot.strip('/') + '/' + user, repo = get_bitbucket_username_repo(repo_url) if not user and not repo: return '' repo = repo.rstrip('/') + if not filename: + # If there isn't a filename, we don't need a suffix + source_suffix = '' + return BITBUCKET_URL.format( user=user, repo=repo, diff --git a/readthedocs/restapi/views/footer_views.py b/readthedocs/restapi/views/footer_views.py index 703a915e070..1d8115d5835 100644 --- a/readthedocs/restapi/views/footer_views.py +++ b/readthedocs/restapi/views/footer_views.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - """Endpoint to generate footer HTML.""" from django.conf import settings @@ -71,7 +69,7 @@ def footer_html(request): # pylint: disable=too-many-locals project_slug = request.GET.get('project', None) version_slug = request.GET.get('version', None) - page_slug = request.GET.get('page', None) + page_slug = request.GET.get('page', '') theme = request.GET.get('theme', False) docroot = request.GET.get('docroot', '') subproject = request.GET.get('subproject', False) diff --git a/readthedocs/rtd_tests/tests/test_repo_parsing.py b/readthedocs/rtd_tests/tests/test_repo_parsing.py index 85ffbbf9997..d595603dc39 100644 --- a/readthedocs/rtd_tests/tests/test_repo_parsing.py +++ b/readthedocs/rtd_tests/tests/test_repo_parsing.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from django.test import TestCase from readthedocs.projects.models import Project @@ -34,6 +33,12 @@ def test_github(self): self.pip.repo = 'https://github.com/user/repo.git.git' self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo.git/blob/master/docs/file.rst') + self.pip.repo = 'https://github.com/user/repo/' + self.assertEqual( + self.version.get_github_url(docroot='/docs/', filename=''), + 'https://github.com/user/repo/blob/master/docs/', + ) + def test_github_ssh(self): self.pip.repo = 'git@github.com:user/repo.git' self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo/blob/master/docs/file.rst') @@ -63,6 +68,12 @@ def test_gitlab(self): self.pip.repo = 'https://gitlab.com/user/repo.git.git' self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo.git/blob/master/foo/bar/file.rst') + self.pip.repo = 'https://gitlab.com/user/repo.git' + self.assertEqual( + self.version.get_gitlab_url(docroot='/foo/bar/', filename=''), + 'https://gitlab.com/user/repo/blob/master/foo/bar/', + ) + def test_gitlab_ssh(self): self.pip.repo = 'git@gitlab.com:user/repo.git' self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo/blob/master/foo/bar/file.rst') @@ -92,6 +103,12 @@ def test_bitbucket(self): self.pip.repo = 'https://bitbucket.org/user/repo.git.git' self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo.git/src/master/foo/bar/file.rst') + self.pip.repo = 'https://bitbucket.org/user/repo/' + self.assertEqual( + self.version.get_bitbucket_url(docroot='/foo/bar/', filename=''), + 'https://bitbucket.org/user/repo/src/master/foo/bar/', + ) + def test_bitbucket_https(self): self.pip.repo = 'https://user@bitbucket.org/user/repo.git' self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo/src/master/foo/bar/file.rst')