diff --git a/app/app/management/commands/sync_orgs_repos.py b/app/app/management/commands/sync_orgs_repos.py index 94c4887fdd8..55bcab92c66 100644 --- a/app/app/management/commands/sync_orgs_repos.py +++ b/app/app/management/commands/sync_orgs_repos.py @@ -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" @@ -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: diff --git a/app/app/utils.py b/app/app/utils.py index 6014dfbdceb..eecf8ff4fe9 100644 --- a/app/app/utils.py +++ b/app/app/utils.py @@ -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 + 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 = [] diff --git a/app/git/utils.py b/app/git/utils.py index 3f892cabbbb..b56f2aff699 100644 --- a/app/git/utils.py +++ b/app/git/utils.py @@ -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('@', '')