Delete old RemoteRepository when syncing GitHub service
@@ -37,8 +37,15 @@ class GitHubService(Service):
def sync(self):
"""Sync repositories and organizations."""
- self.sync_repositories()
- self.sync_organizations()
+ repos = self.sync_repositories()
+ organization_repos = self.sync_organizations()
+
+ # Delete RemoteRepository where the user doesn't have access anymore
+ # (skip RemoteRepository tied to a Project on this user)
+ full_names = {repo.get('full_name') for repo in repos + organization_repos}
+ self.user.oauth_repositories.exclude(
+ Q(full_name__in=full_names) | Q(project__isnull=False)
+ ).delete()
def sync_repositories(self):
"""Sync repositories from GitHub API."""
@@ -46,6 +53,7 @@ class GitHubService(Service):
try:
for repo in repos:
self.create_repository(repo)
+ return repos
except (TypeError, ValueError):
log.warning('Error syncing GitHub repositories')
raise SyncServiceError(
@@ -53,13 +61,6 @@ class GitHubService(Service):
'try reconnecting your account'
)
- # Delete RemoteRepository where the user doesn't have access anymore
- # (skip RemoteRepository tied to a Project on this user)
- full_names = {repo.get('full_name') for repo in repos}
- self.user.oauth_repositories.exclude(
- Q(full_name__in=full_names) | Q(project__isnull=False)
- ).delete()
-
def sync_organizations(self):
"""Sync organizations from GitHub API."""
try:
@@ -74,6 +75,7 @@ class GitHubService(Service):
)
for repo in org_repos:
self.create_repository(repo, organization=org_obj)
+ return org_repos
except (TypeError, ValueError):
log.warning('Error syncing GitHub organizations')
raise SyncServiceError(
