Skip to content

Commit

Permalink
Merge pull request #3501 from stsewd/fix-integrations-regex
Browse files Browse the repository at this point in the history
Fix regex for getting project and user
  • Loading branch information
ericholscher authored Jan 15, 2018
2 parents 909cb56 + a7b8a2e commit bf23b2b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 49 deletions.
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 = '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')

self.pip.repo = 'git@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')

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

self.pip.repo = 'git@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')

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

self.pip.repo = 'https://user@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/src/master/foo/bar/file.rst')

def test_bitbucket_ssh(self):
self.pip.repo = 'git@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')

self.pip.repo = 'git@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/src/master/foo/bar/file.rst')

0 comments on commit bf23b2b

Please sign in to comment.