Skip to content

Fix regex for getting project and user #3501

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

Merged
merged 8 commits into from
Jan 15, 2018
Merged
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
30 changes: 10 additions & 20 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@

from readthedocs.core.utils import broadcast
from readthedocs.projects.constants import (
BITBUCKET_REGEXS, BITBUCKET_URL, GITHUB_REGEXS, GITHUB_URL, GITLAB_REGEXS,
GITLAB_URL, PRIVACY_CHOICES, PRIVATE)
BITBUCKET_URL, GITHUB_URL, GITLAB_URL, PRIVACY_CHOICES, PRIVATE)
from readthedocs.projects.models import APIProject, Project

from .constants import (
BRANCH, BUILD_STATE, BUILD_STATE_FINISHED, BUILD_TYPES, LATEST,
NON_REPOSITORY_VERSIONS, STABLE, TAG, VERSION_TYPES)
from .managers import VersionManager
from .querysets import BuildQuerySet, RelatedBuildQuerySet, VersionQuerySet
from .utils import (
get_bitbucket_username_repo, get_github_username_repo,
get_gitlab_username_repo)
from .version_slug import VersionSlugField

DEFAULT_VERSION_PRIVACY_LEVEL = getattr(
Expand Down Expand Up @@ -277,12 +279,8 @@ def get_github_url(
elif action == 'edit':
action_string = 'edit'

for regex in GITHUB_REGEXS:
match = regex.search(repo_url)
if match:
user, repo = match.groups()
break
else:
user, repo = get_github_username_repo(repo_url)
if not user and not repo:
return ''
repo = repo.rstrip('/')

Expand Down Expand Up @@ -315,12 +313,8 @@ def get_gitlab_url(
elif action == 'edit':
action_string = 'edit'

for regex in GITLAB_REGEXS:
match = regex.search(repo_url)
if match:
user, repo = match.groups()
break
else:
user, repo = get_gitlab_username_repo(repo_url)
if not user and not repo:
return ''
repo = repo.rstrip('/')

Expand All @@ -341,12 +335,8 @@ def get_bitbucket_url(self, docroot, filename, source_suffix='.rst'):
if not docroot:
return ''

for regex in BITBUCKET_REGEXS:
match = regex.search(repo_url)
if match:
user, repo = match.groups()
break
else:
user, repo = get_bitbucket_username_repo(repo_url)
if not user and not repo:
return ''
repo = repo.rstrip('/')

Expand Down
29 changes: 5 additions & 24 deletions readthedocs/builds/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,13 @@
from __future__ import (
absolute_import, division, print_function, unicode_literals)

import re

GH_REGEXS = [
re.compile('github.com/(.+)/(.+)(?:\.git){1}'),
re.compile('github.com/(.+)/(.+)'),
re.compile('github.com:(.+)/(.+).git'),
]

BB_REGEXS = [
re.compile('bitbucket.org/(.+)/(.+)/'),
re.compile('bitbucket.org/(.+)/(.+)'),
re.compile('bitbucket.org:(.+)/(.+)\.git'),
]

# TODO: I think this can be different than `gitlab.com`
# self.adapter.provider_base_url
GL_REGEXS = [
re.compile('gitlab.com/(.+)/(.+)(?:\.git){1}'),
re.compile('gitlab.com/(.+)/(.+)'),
re.compile('gitlab.com:(.+)/(.+)\.git'),
]
from readthedocs.projects.constants import (
BITBUCKET_REGEXS, GITHUB_REGEXS, GITLAB_REGEXS)


def get_github_username_repo(url):
if 'github' in url:
for regex in GH_REGEXS:
for regex in GITHUB_REGEXS:
match = regex.search(url)
if match:
return match.groups()
Expand All @@ -38,7 +19,7 @@ def get_github_username_repo(url):

def get_bitbucket_username_repo(url=None):
if 'bitbucket' in url:
for regex in BB_REGEXS:
for regex in BITBUCKET_REGEXS:
match = regex.search(url)
if match:
return match.groups()
Expand All @@ -47,7 +28,7 @@ def get_bitbucket_username_repo(url=None):

def get_gitlab_username_repo(url=None):
if 'gitlab' in url:
for regex in GL_REGEXS:
for regex in GITLAB_REGEXS:
match = regex.search(url)
if match:
return match.groups()
Expand Down
11 changes: 6 additions & 5 deletions readthedocs/projects/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,20 @@
PROJECT_SLUG_REGEX = '(?:[-\w]+)'

GITHUB_REGEXS = [
re.compile('github.com/(.+)/(.+)(?:\.git){1}'),
re.compile('github.com/(.+)/(.+)(?:\.git){1}$'),
re.compile('github.com/(.+)/(.+)'),
re.compile('github.com:(.+)/(.+).git'),
re.compile('github.com:(.+)/(.+)\.git$'),
]
BITBUCKET_REGEXS = [
re.compile('bitbucket.org/(.+)/(.+).git'),
re.compile('@bitbucket.org/(.+)/(.+)\.git$'),
re.compile('bitbucket.org/(.+)/(.+)/'),
re.compile('bitbucket.org/(.+)/(.+)'),
re.compile('bitbucket.org:(.+)/(.+)\.git$'),
]
GITLAB_REGEXS = [
re.compile('gitlab.com/(.+)/(.+)(?:\.git){1}'),
re.compile('gitlab.com/(.+)/(.+)(?:\.git){1}$'),
re.compile('gitlab.com/(.+)/(.+)'),
re.compile('gitlab.com:(.+)/(.+).git'),
re.compile('gitlab.com:(.+)/(.+)\.git$'),
]
GITHUB_URL = (
'https://github.com/{user}/{repo}/'
Expand Down
63 changes: 63 additions & 0 deletions readthedocs/rtd_tests/tests/test_repo_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,89 @@ def test_github(self):
self.pip.repo = 'https://github.com/user/repo/'
self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo/blob/master/docs/file.rst')

self.pip.repo = 'https://github.com/user/repo.github.io'
self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo.github.io/blob/master/docs/file.rst')

self.pip.repo = 'https://github.com/user/repo.github.io/'
self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo.github.io/blob/master/docs/file.rst')

self.pip.repo = 'https://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')

self.pip.repo = 'https://github.com/user/repo.github.io.git'
self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo.github.io/blob/master/docs/file.rst')

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')

def test_github_ssh(self):
self.pip.repo = '[email protected]:user/repo.git'
self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo/blob/master/docs/file.rst')

self.pip.repo = '[email protected]:user/repo.github.io.git'
self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo.github.io/blob/master/docs/file.rst')

def test_gitlab(self):
self.pip.repo = 'https://gitlab.com/user/repo'
self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo/blob/master/foo/bar/file.rst')

self.pip.repo = 'https://gitlab.com/user/repo/'
self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo/blob/master/foo/bar/file.rst')

self.pip.repo = 'https://gitlab.com/user/repo.gitlab.io'
self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo.gitlab.io/blob/master/foo/bar/file.rst')

self.pip.repo = 'https://gitlab.com/user/repo.gitlab.io/'
self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo.gitlab.io/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='file'), 'https://gitlab.com/user/repo/blob/master/foo/bar/file.rst')

self.pip.repo = 'https://gitlab.com/user/repo.gitlab.io.git'
self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo.gitlab.io/blob/master/foo/bar/file.rst')

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')

def test_gitlab_ssh(self):
self.pip.repo = '[email protected]: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')

self.pip.repo = '[email protected]:user/repo.gitlab.io.git'
self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo.gitlab.io/blob/master/foo/bar/file.rst')

def test_bitbucket(self):
self.pip.repo = 'https://bitbucket.org/user/repo'
self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo/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='file'), 'https://bitbucket.org/user/repo/src/master/foo/bar/file.rst')

self.pip.repo = 'https://bitbucket.org/user/repo.gitbucket.io'
self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo.gitbucket.io/src/master/foo/bar/file.rst')

self.pip.repo = 'https://bitbucket.org/user/repo.gitbucket.io/'
self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo.gitbucket.io/src/master/foo/bar/file.rst')

self.pip.repo = 'https://bitbucket.org/user/repo.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.gitbucket.io.git'
self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo.gitbucket.io.git/src/master/foo/bar/file.rst')

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.git/src/master/foo/bar/file.rst')

def test_bitbucket_https(self):
self.pip.repo = 'https://[email protected]/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')

self.pip.repo = 'https://[email protected]/user/repo.gitbucket.io.git'
self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo.gitbucket.io/src/master/foo/bar/file.rst')

def test_bitbucket_ssh(self):
self.pip.repo = '[email protected]: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')

self.pip.repo = '[email protected]:user/repo.gitbucket.io.git'
self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo.gitbucket.io/src/master/foo/bar/file.rst')