-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Sync RemoteRepository and RemoteOrganization in all VCS providers #7310
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
Merged
humitos
merged 4 commits into
master
from
humitos/sync-remote-repositories-organizations
Aug 27, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0c73d26
Sync RemoteRepository and RemoteOrganization in all VCS providers
humitos ca5ffa9
lint
humitos d140752
Always return a valid list (could be empty)
humitos bb25e9a
Merge branch 'master' into humitos/sync-remote-repositories-organizat…
humitos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,20 +30,18 @@ class BitbucketService(Service): | |
url_pattern = re.compile(r'bitbucket.org') | ||
https_url_pattern = re.compile(r'^https:\/\/[^@][email protected]/') | ||
|
||
def sync(self): | ||
"""Sync repositories and teams from Bitbucket API.""" | ||
self.sync_repositories() | ||
self.sync_teams() | ||
|
||
def sync_repositories(self): | ||
"""Sync repositories from Bitbucket API.""" | ||
repos = [] | ||
|
||
# Get user repos | ||
try: | ||
repos = self.paginate( | ||
'https://bitbucket.org/api/2.0/repositories/?role=member', | ||
) | ||
for repo in repos: | ||
self.create_repository(repo) | ||
|
||
except (TypeError, ValueError): | ||
log.warning('Error syncing Bitbucket repositories') | ||
raise SyncServiceError( | ||
|
@@ -58,37 +56,49 @@ def sync_repositories(self): | |
resp = self.paginate( | ||
'https://bitbucket.org/api/2.0/repositories/?role=admin', | ||
) | ||
repos = ( | ||
admin_repos = ( | ||
RemoteRepository.objects.filter( | ||
users=self.user, | ||
full_name__in=[r['full_name'] for r in resp], | ||
account=self.account, | ||
) | ||
) | ||
for repo in repos: | ||
for repo in admin_repos: | ||
repo.admin = True | ||
repo.save() | ||
except (TypeError, ValueError): | ||
pass | ||
|
||
def sync_teams(self): | ||
"""Sync Bitbucket teams and team repositories.""" | ||
return repos | ||
|
||
def sync_organizations(self): | ||
"""Sync Bitbucket teams (our RemoteOrganization) and team repositories.""" | ||
teams = [] | ||
repositories = [] | ||
|
||
try: | ||
teams = self.paginate( | ||
'https://api.bitbucket.org/2.0/teams/?role=member', | ||
) | ||
for team in teams: | ||
org = self.create_organization(team) | ||
repos = self.paginate(team['links']['repositories']['href']) | ||
|
||
# Add organization's repositories to the result | ||
repositories.extend(repos) | ||
|
||
for repo in repos: | ||
self.create_repository(repo, organization=org) | ||
|
||
except ValueError: | ||
log.warning('Error syncing Bitbucket organizations') | ||
raise SyncServiceError( | ||
'Could not sync your Bitbucket team repositories, ' | ||
'try reconnecting your account', | ||
) | ||
|
||
return teams, repositories | ||
|
||
def create_repository(self, fields, privacy=None, organization=None): | ||
""" | ||
Update or create a repository from Bitbucket API response. | ||
|
@@ -180,6 +190,12 @@ def create_organization(self, fields): | |
organization.save() | ||
return organization | ||
|
||
def get_repository_full_names(self, repositories): | ||
return {repository.get('full_name') for repository in repositories} | ||
|
||
def get_organization_names(self, organizations): | ||
return {organization.get('display_name') for organization in organizations} | ||
|
||
def get_next_url_to_paginate(self, response): | ||
return response.json().get('next') | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should run these delete tasks across all users as a one-time CLI command, to clean up our DB. There's likely a ton of these sitting around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one-time command would require to sync all VCS account connected and delete all the old objects. That may be too much. We may want to limit the amount of accounts re-synced and run it several times by chunks. It will remove a lot of stale data.
However, as this cleanup is in the core of the sync action, all active accounts will be cleanup automatically when they login next time or, when they click the arrow from Import Project page.