Skip to content

Commit 162f12d

Browse files
committed
Do not repeat yourself
1 parent bc7e0c8 commit 162f12d

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

readthedocs/builds/models.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@
2121

2222
from readthedocs.core.utils import broadcast
2323
from readthedocs.projects.constants import (
24-
BITBUCKET_REGEXS, BITBUCKET_URL, GITHUB_REGEXS, GITHUB_URL, GITLAB_REGEXS,
25-
GITLAB_URL, PRIVACY_CHOICES, PRIVATE)
24+
BITBUCKET_URL, GITHUB_URL, GITLAB_URL, PRIVACY_CHOICES, PRIVATE)
2625
from readthedocs.projects.models import APIProject, Project
2726

2827
from .constants import (
2928
BRANCH, BUILD_STATE, BUILD_STATE_FINISHED, BUILD_TYPES, LATEST,
3029
NON_REPOSITORY_VERSIONS, STABLE, TAG, VERSION_TYPES)
3130
from .managers import VersionManager
3231
from .querysets import BuildQuerySet, RelatedBuildQuerySet, VersionQuerySet
32+
from .utils import (
33+
get_bitbucket_username_repo, get_github_username_repo,
34+
get_gitlab_username_repo)
3335
from .version_slug import VersionSlugField
3436

3537
DEFAULT_VERSION_PRIVACY_LEVEL = getattr(
@@ -277,12 +279,8 @@ def get_github_url(
277279
elif action == 'edit':
278280
action_string = 'edit'
279281

280-
for regex in GITHUB_REGEXS:
281-
match = regex.search(repo_url)
282-
if match:
283-
user, repo = match.groups()
284-
break
285-
else:
282+
user, repo = get_github_username_repo(repo_url)
283+
if not user and not repo:
286284
return ''
287285
repo = repo.rstrip('/')
288286

@@ -315,12 +313,8 @@ def get_gitlab_url(
315313
elif action == 'edit':
316314
action_string = 'edit'
317315

318-
for regex in GITLAB_REGEXS:
319-
match = regex.search(repo_url)
320-
if match:
321-
user, repo = match.groups()
322-
break
323-
else:
316+
user, repo = get_gitlab_username_repo(repo_url)
317+
if not user and not repo:
324318
return ''
325319
repo = repo.rstrip('/')
326320

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

344-
for regex in BITBUCKET_REGEXS:
345-
match = regex.search(repo_url)
346-
if match:
347-
user, repo = match.groups()
348-
break
349-
else:
338+
user, repo = get_bitbucket_username_repo(repo_url)
339+
if not user and not repo:
350340
return ''
351341
repo = repo.rstrip('/')
352342

0 commit comments

Comments
 (0)