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

Fix regex for getting project and user #3501

Merged
merged 8 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 5 additions & 5 deletions readthedocs/projects/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,19 @@
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/(.+)/(.+)/'),
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
37 changes: 36 additions & 1 deletion readthedocs/rtd_tests/tests/test_repo_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,61 @@ 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_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_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/src/master/foo/bar/file.rst')
self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo.git/src/master/foo/bar/file.rst')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to handle this.. Both clone urls are valid for a project with name name.git, yo can do a clone like git clone https://bitbucket.org/user/name.git or git clone https://bitbucket.org/user/name.git.git

Copy link
Member Author

@stsewd stsewd Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But on the web, you only can access to the repo path like https://bitbucket.org/user/name.git/src/master/foo/bar/file.rst.
https://bitbucket.org/user/name.git.git gives 404 also a project with name name (no .git)

Copy link
Member Author

@stsewd stsewd Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, bitbucked gives a url in the form of https://user@bitbucket.org/user/name.git.git for https clone, so I guess this probably never happen (but we need another regex for this case)


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