Skip to content

Commit

Permalink
sync_orgs_repos management cmd
Browse files Browse the repository at this point in the history
	- additional exception handling and error messages to help debugging any issues with user profiles
	- private organizations should now correctly be capturable when they are granted access during login
  • Loading branch information
androolloyd committed Oct 1, 2019
1 parent 27e75bd commit e8ee0b8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 76 deletions.
155 changes: 81 additions & 74 deletions app/app/management/commands/sync_orgs_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,73 +63,77 @@ def recursive_sync(lsynced, handle):
if profile.organizations is None:
print("no organizations to sync")
return []
for org in profile.organizations:
if org in orgs_synced:
continue

orgs_synced.append(org)
db_org = Organization.objects.get_or_create(name=org)[0]
print(profile.organizations)
for org in profile.organizations:
try:
print(org)
if org in orgs_synced:
print(f'{org} has been synced already')
continue

print(f'Syncing Organization: {db_org.name}')
profile.profile_organizations.add(db_org)
org_members = get_organization(
db_org.name,
'/members'
)
orgs_synced.append(org)
db_org = Organization.objects.get_or_create(name=org)[0]

if 'message' in org_members:
print(org_members['message'])
continue
print(f'Syncing Organization: {db_org.name}')
profile.profile_organizations.add(db_org)
org_members = get_organization(
db_org.name,
'/members'
)

for member in org_members:
if 'message' in org_members:
print(org_members['message'])
continue

try:
for member in org_members:

member_profile_obj = Profile.objects.get(
handle=member['login'],
user__is_active=True
)
membership = get_organization(
db_org.name,
f'/memberships/{member["login"]}'
)
role = membership['role'] if not None else "member"
db_group = Group.objects.get_or_create(name=f'{db_org.name}-role-{role}')[0]
db_org.groups.add(db_group)
member_profile_obj.user.groups.add(db_group)
members_to_sync.append(member['login'])
lsynced = recursive_sync(lsynced, member['login'])
except Exception as e:
print(f'Profile for Github Handle does not exist: {member["login"]}')
continue
try:
membership = get_organization(
db_org.name,
f'/memberships/{member["login"]}',
(handle, access_token)
)
if 'message' in membership:
print(membership['message'])
continue
role = membership['role'] if not None else "member"
db_group = Group.objects.get_or_create(name=f'{db_org.name}-role-{role}')[0]
db_org.groups.add(db_group)
member_profile_obj = Profile.objects.get(
handle=member['login'],
user__is_active=True
)
member_profile_obj.user.groups.add(db_group)
members_to_sync.append(member['login'])
lsynced = recursive_sync(lsynced, member['login'])
except Exception as e:
print(f'An exception happened in the Organization Loop: handle {member["login"]} {e}')
continue

org_repos = get_organization(
db_org.name,
'/repos'
)

if 'message' in org_repos:
print(org_repos['message'])
continue

for repo in org_repos:
db_repo = Repo.objects.get_or_create(name=repo['name'])[0]
db_org.repos.add(db_repo)
print(db_org)
print(f'Syncing Repo: {db_repo.name}')
repo_collabs = get_repo(
repo['full_name'],
'/collaborators',
(handle, access_token)
org_repos = get_organization(
db_org.name,
'/repos'
)
if 'message' in repo_collabs:
print(repo_collabs['message'])

if 'message' in org_repos:
print(org_repos['message'])
continue

for collaborator in repo_collabs:
try:
member_user_profile = Profile.objects.get(handle=collaborator['login'],
user__is_active=True)
for repo in org_repos:
db_repo = Repo.objects.get_or_create(name=repo['name'])[0]
db_org.repos.add(db_repo)
print(f'Syncing Repo: {db_repo.name}')
repo_collabs = get_repo(
repo['full_name'],
'/collaborators',
(handle, access_token)
)
if 'message' in repo_collabs:
print(repo_collabs['message'])
continue

for collaborator in repo_collabs:

if collaborator['permissions']['admin']:
permission = "admin"
Expand All @@ -143,27 +147,30 @@ def recursive_sync(lsynced, handle):
db_group = Group.objects.get_or_create(
name=f'{db_org.name}-repo-{repo["name"]}-{permission}')[0]
db_org.groups.add(db_group)
member_user_profile.user.groups.add(db_group)
member_user_profile.repos.add(db_repo)

if collaborator['login'] not in members_to_sync or collaborator[
'login'] not in lsynced:
members_to_sync.append(collaborator['login'])
for x in members_to_sync:
print(x)
try:
lsynced = lsynced + recursive_sync(lsynced, x)
except ValueError as members_loop_exec:

print(members_loop_exec)
except Exception as e:
print(e)
continue

return lsynced
try:
member_user_profile = Profile.objects.get(handle=collaborator['login'],
user__is_active=True)
member_user_profile.user.groups.add(db_group)
member_user_profile.repos.add(db_repo)
if collaborator['login'] not in members_to_sync or \
collaborator['login'] not in lsynced:
members_to_sync.append(collaborator['login'])
except Exception as e:
print(f'An exception happened in the Collaborators sync Loop: handle: {collaborator["login"]} {e}')

for x in members_to_sync:
try:
lsynced = lsynced + recursive_sync(lsynced, x)
except Exception as e:
print(f'An exception happened in the Members sync Loop: handle: {handle} {e}')
except Exception as e:
print(f'An exception happened in the Organization Loop: handle {handle} {e}')
except Exception as exc:
print(f'Unhandled Exception occurred: {exc}')

return lsynced

for profile in all_users:
try:
if profile.handle is not None:
Expand Down
3 changes: 2 additions & 1 deletion app/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ def sync_profile(handle, user=None, hide_profile=True):
# store the org info in postgres
try:
profile, created = Profile.objects.update_or_create(handle=handle, defaults=defaults)
orgs = get_user(handle, '', scope='orgs')
access_token = profile.user.social_auth.filter(provider='github').latest('pk').access_token

This comment has been minimized.

Copy link
@owocki

owocki Mar 19, 2020

Contributor

File "/home/ubuntu/gitcoin/coin/app/app/utils.py", line 245, in sync_profile
access_token = profile.user.social_auth.filter(provider='github').latest('pk').access_token
AttributeError: 'NoneType' object has no attribute 'social_auth'

This comment has been minimized.

Copy link
@owocki

owocki Mar 19, 2020

Contributor
orgs = get_user(handle, '', scope='orgs', auth=(profile.handle, access_token))
profile.organizations = [ele['login'] for ele in orgs]
print("Profile:", profile, "- created" if created else "- updated")
keywords = []
Expand Down
1 change: 0 additions & 1 deletion app/git/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ def get_interested_actions(github_url, username, email=''):
def get_user(user, sub_path='', scope='', auth=_AUTH):
"""Get the github user details."""
if scope is not '':
print(scope)
url = f'https://api.github.com/user/{scope}'
else:
user = user.replace('@', '')
Expand Down

0 comments on commit e8ee0b8

Please sign in to comment.