Skip to content

Commit

Permalink
Project: don't depend on the brand name (#12008)
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd authored Feb 19, 2025
1 parent 158e3ec commit bdca0bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
6 changes: 2 additions & 4 deletions readthedocs/builds/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
)
from readthedocs.projects.constants import (
BITBUCKET_REGEXS,
GITHUB_BRAND,
GITHUB_PULL_REQUEST_URL,
GITHUB_REGEXS,
GITLAB_BRAND,
GITLAB_MERGE_REQUEST_URL,
GITLAB_REGEXS,
)
Expand Down Expand Up @@ -90,10 +88,10 @@ def external_version_name(build_or_version):

project = build_or_version.project

if project.git_provider_name == GITHUB_BRAND:
if project.is_github_project:
return GITHUB_EXTERNAL_VERSION_NAME

if project.git_provider_name == GITLAB_BRAND:
if project.is_gitlab_project:
return GITLAB_EXTERNAL_VERSION_NAME

# TODO: Add External Version Name for Bitbucket.
Expand Down
22 changes: 18 additions & 4 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,10 +1055,24 @@ def get_git_service_class(self, fallback_to_clone_url=False):
return service_cls

@property
def git_provider_name(self):
"""Get the provider name for project. e.g: GitHub, GitLab, Bitbucket."""
service_class = self.get_git_service_class(fallback_to_clone_url=True)
return service_class.allauth_provider.name if service_class else None
def is_github_project(self):
from readthedocs.oauth.services import GitHubService

return self.get_git_service_class(fallback_to_clone_url=True) == GitHubService

@property
def is_gitlab_project(self):
from readthedocs.oauth.services import GitLabService

return self.get_git_service_class(fallback_to_clone_url=True) == GitLabService

@property
def is_bitbucket_project(self):
from readthedocs.oauth.services import BitbucketService

return (
self.get_git_service_class(fallback_to_clone_url=True) == BitbucketService
)

def find(self, filename, version):
"""
Expand Down
14 changes: 8 additions & 6 deletions readthedocs/rtd_tests/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
from readthedocs.builds.models import Build, Version
from readthedocs.oauth.services import GitHubService, GitLabService
from readthedocs.projects.constants import (
GITHUB_BRAND,
GITLAB_BRAND,
MEDIA_TYPE_EPUB,
MEDIA_TYPE_HTML,
MEDIA_TYPE_HTMLZIP,
Expand Down Expand Up @@ -231,10 +229,12 @@ def test_get_latest_build_excludes_external_versions(self):
# Test that External Version is not considered for get_latest_build.
self.assertEqual(self.pip.get_latest_build(), None)

def test_git_provider_name_github(self):
def test_git_provider_github(self):
self.pip.repo = "/~https://github.com/pypa/pip"
self.pip.save()
self.assertEqual(self.pip.git_provider_name, GITHUB_BRAND)
assert self.pip.is_github_project
assert not self.pip.is_gitlab_project
assert not self.pip.is_bitbucket_project

def test_git_service_class_github(self):
self.pip.repo = "/~https://github.com/pypa/pip"
Expand All @@ -244,10 +244,12 @@ def test_git_service_class_github(self):
self.pip.get_git_service_class(fallback_to_clone_url=True), GitHubService
)

def test_git_provider_name_gitlab(self):
def test_git_provider_gitlab(self):
self.pip.repo = "https://gitlab.com/pypa/pip"
self.pip.save()
self.assertEqual(self.pip.git_provider_name, GITLAB_BRAND)
assert self.pip.is_gitlab_project
assert not self.pip.is_github_project
assert not self.pip.is_bitbucket_project

def test_git_service_class_gitlab(self):
self.pip.repo = "https://gitlab.com/pypa/pip"
Expand Down
9 changes: 2 additions & 7 deletions readthedocs/vcs_support/backends/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
)
from readthedocs.config import ALL
from readthedocs.projects.constants import (
GITHUB_BRAND,
GITHUB_PR_PULL_PATTERN,
GITLAB_BRAND,
GITLAB_MR_PULL_PATTERN,
)
from readthedocs.projects.exceptions import RepositoryError
Expand Down Expand Up @@ -147,13 +145,10 @@ def get_remote_fetch_refspec(self):
return f"refs/tags/{tag_name}:refs/tags/{tag_name}"

if self.version_type == EXTERNAL:
# TODO: We should be able to resolve this without looking up in oauth registry
git_provider_name = self.project.git_provider_name

# Remote reference for Git providers where pull request builds are supported
if git_provider_name == GITHUB_BRAND:
if self.project.is_github_project:
return GITHUB_PR_PULL_PATTERN.format(id=self.verbose_name)
if self.project.git_provider_name == GITLAB_BRAND:
if self.project.is_gitlab_project:
return GITLAB_MR_PULL_PATTERN.format(id=self.verbose_name)

log.warning(
Expand Down

0 comments on commit bdca0bd

Please sign in to comment.