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

Use remote_id and vcs_provider Instead of full_name to Get RemoteRepository #7734

Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions readthedocs/oauth/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Service:

adapter = None
url_pattern = None
PROVIDER_SLUG = None
saadmk11 marked this conversation as resolved.
Show resolved Hide resolved

default_user_avatar_url = settings.OAUTH_AVATAR_USER_DEFAULT_URL
default_org_avatar_url = settings.OAUTH_AVATAR_ORG_DEFAULT_URL
Expand Down Expand Up @@ -200,10 +201,13 @@ def sync(self):
# Delete RemoteRepository where the user doesn't have access anymore
# (skip RemoteRepository tied to a Project on this user)
all_remote_repositories = remote_repositories + remote_repositories_organizations
repository_full_names = [r.full_name for r in all_remote_repositories if r is not None]
repository_remote_ids = [r.remote_id for r in all_remote_repositories if r is not None]
(
self.user.remote_repository_relations
.exclude(remoterepository__full_name__in=repository_full_names)
.exclude(
remoterepository__remote_id__in=repository_remote_ids,
remoterepository__vcs_provider=self.PROVIDER_SLUG
)
.filter(account=self.account)
.delete()
)
Expand Down
12 changes: 7 additions & 5 deletions readthedocs/oauth/services/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class BitbucketService(Service):
# TODO replace this with a less naive check
url_pattern = re.compile(r'bitbucket.org')
https_url_pattern = re.compile(r'^https:\/\/[^@]+@bitbucket.org/')
PROVIDER_SLUG = BITBUCKET

def sync_repositories(self):
"""Sync repositories from Bitbucket API."""
Expand Down Expand Up @@ -66,8 +67,9 @@ def sync_repositories(self):
RemoteRepositoryRelation.objects.filter(
user=self.user,
account=self.account,
remoterepository__full_name__in=[
r['full_name'] for r in resp
remoterepository__vcs_provider=self.PROVIDER_SLUG,
remoterepository__remote_id__in=[
r['uuid'] for r in resp
]
)
)
Expand Down Expand Up @@ -131,7 +133,8 @@ def create_repository(self, fields, privacy=None, organization=None):
(fields['is_private'] is False and privacy == 'public'),
]):
repo, _ = RemoteRepository.objects.get_or_create(
full_name=fields['full_name']
remote_id=fields['uuid'],
vcs_provider=self.PROVIDER_SLUG
)
remote_relation, _ = RemoteRepositoryRelation.objects.get_or_create(
remoterepository=repo,
Expand All @@ -148,8 +151,7 @@ def create_repository(self, fields, privacy=None, organization=None):

repo.organization = organization
repo.name = fields['name']
repo.remote_id = fields['uuid']
repo.vcs_provider = BITBUCKET
repo.full_name = fields['full_name']
repo.description = fields['description']
repo.private = fields['is_private']

Expand Down
7 changes: 4 additions & 3 deletions readthedocs/oauth/services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class GitHubService(Service):
adapter = GitHubOAuth2Adapter
# TODO replace this with a less naive check
url_pattern = re.compile(r'github\.com')
PROVIDER_SLUG = GITHUB

def sync_repositories(self):
"""Sync repositories from GitHub API."""
Expand Down Expand Up @@ -106,7 +107,8 @@ def create_repository(self, fields, privacy=None, organization=None):
]):

repo, _ = RemoteRepository.objects.get_or_create(
full_name=fields['full_name']
remote_id=fields['id'],
vcs_provider=self.PROVIDER_SLUG
)
remote_relation, _ = RemoteRepositoryRelation.objects.get_or_create(
remoterepository=repo,
Expand All @@ -122,9 +124,8 @@ def create_repository(self, fields, privacy=None, organization=None):
return None

repo.organization = organization
repo.remote_id = fields['id']
repo.vcs_provider = GITHUB
repo.name = fields['name']
repo.full_name = fields['full_name']
repo.description = fields['description']
repo.ssh_url = fields['ssh_url']
repo.html_url = fields['html_url']
Expand Down
8 changes: 5 additions & 3 deletions readthedocs/oauth/services/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class GitLabService(Service):
PERMISSION_MAINTAINER = 40
PERMISSION_OWNER = 50

PROVIDER_SLUG = GITLAB

def _get_repo_id(self, project):
# The ID or URL-encoded path of the project
# https://docs.gitlab.com/ce/api/README.html#namespaced-path-encoding
Expand Down Expand Up @@ -165,7 +167,8 @@ def create_repository(self, fields, privacy=None, organization=None):
repo_is_public = fields['visibility'] == 'public'
if privacy == 'private' or (repo_is_public and privacy == 'public'):
repo, _ = RemoteRepository.objects.get_or_create(
full_name=fields['name_with_namespace']
remote_id=fields['id'],
vcs_provider=self.PROVIDER_SLUG
)
remote_relation, _ = RemoteRepositoryRelation.objects.get_or_create(
remoterepository=repo,
Expand All @@ -181,9 +184,8 @@ def create_repository(self, fields, privacy=None, organization=None):
return None

repo.organization = organization
repo.remote_id = fields['id']
repo.vcs_provider = GITLAB
repo.name = fields['name']
repo.full_name = fields['name_with_namespace']
repo.description = fields['description']
repo.ssh_url = fields['ssh_url_to_repo']
repo.html_url = fields['web_url']
Expand Down
7 changes: 7 additions & 0 deletions readthedocs/rtd_tests/tests/test_oauth_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.contrib.auth.models import User
from django.test import TestCase

from readthedocs.oauth.constants import GITHUB
from readthedocs.oauth.models import (
RemoteOrganization,
RemoteRepository,
Expand Down Expand Up @@ -79,6 +80,8 @@ def test_sync_delete_stale(self, mock_request):
repo_1 = fixture.get(
RemoteRepository,
full_name='organization/repository',
remote_id='11111',
vcs_provider=GITHUB
)
fixture.get(
RemoteRepositoryRelation,
Expand All @@ -90,6 +93,8 @@ def test_sync_delete_stale(self, mock_request):
repo_2 = fixture.get(
RemoteRepository,
full_name='organization/old-repository',
remote_id='64789',
vcs_provider=GITHUB
)
fixture.get(
RemoteRepositoryRelation,
Expand All @@ -105,6 +110,8 @@ def test_sync_delete_stale(self, mock_request):
RemoteRepository,
project=project,
full_name='organization/project-linked-repository',
remote_id='54321',
vcs_provider=GITHUB
)
fixture.get(
RemoteRepositoryRelation,
Expand Down