Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 21 additions & 1 deletion app/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,25 @@ def get_upload_filename(instance, filename):
def sync_profile(handle, user=None, hide_profile=True):
from dashboard.models import Profile
handle = handle.strip().replace('@', '').lower()
data = get_user(handle)
# data = get_user(handle, scoped=True)
if user and hasattr(user, 'profile'):
try:
access_token = user.social_auth.filter(provider='github').latest('pk').access_token
data = get_user(handle, '', scoped=True, auth=(handle, access_token))

user = User.objects.get(username = handle)
if 'login' in data:
profile = user.profile
user.username = data['login']
user.save()
profile.handle = data['login']
profile.email = user.email
profile.save()
except UserSocialAuth.DoesNotExist:
pass
else:
data = get_user(handle)

email = ''
is_error = 'name' not in data.keys()
if is_error:
Expand Down Expand Up @@ -231,6 +249,8 @@ def sync_profile(handle, user=None, hide_profile=True):
email = profile.email

if email and profile:
profile.email = email
profile.save()
get_or_save_email_subscriber(email, 'sync_profile', profile=profile)

if profile and not profile.github_access_token:
Expand Down
4 changes: 2 additions & 2 deletions app/git/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_get_issue_timeline_events(self):
@responses.activate
def test_get_user(self):
"""Test the github utility get_user method."""
url = 'https://api.github.com/users/gitcoin'
url = 'https://api.github.com/users/gitcoin?per_page=100'
responses.add(responses.GET, url, headers=HEADERS, json={}, status=200)
get_user('@gitcoin')

Expand All @@ -249,7 +249,7 @@ def test_get_user(self):
@responses.activate
def test_get_user_subpath(self):
"""Test the github utility get_user method with a subpath."""
url = 'https://api.github.com/users/gitcoin/test'
url = 'https://api.github.com/users/gitcoin/test?per_page=100'
responses.add(responses.GET, url, headers=HEADERS, json={}, status=200)
get_user('@gitcoin', '/test')

Expand Down
15 changes: 11 additions & 4 deletions app/git/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
JSON_HEADER = {'Accept': 'application/json', 'User-Agent': settings.GITHUB_APP_NAME, 'Origin': settings.BASE_URL}
TIMELINE_HEADERS = {'Accept': 'application/vnd.github.mockingbird-preview'}
TOKEN_URL = '{api_url}/applications/{client_id}/tokens/{oauth_token}'
PER_PAGE_LIMIT = 100


def github_connect(token=None):
Expand Down Expand Up @@ -590,11 +591,17 @@ def get_interested_actions(github_url, username, email=''):
return actions_by_interested_party


def get_user(user, sub_path=''):
def get_user(user, sub_path='', scope='', scoped=False, auth=_AUTH):
"""Get the github user details."""
user = user.replace('@', '')
url = f'https://api.github.com/users/{user}{sub_path}'
response = requests.get(url, auth=_AUTH, headers=HEADERS)
if scope is not '':
url = f'https://api.github.com/user/{scope}?per_page={PER_PAGE_LIMIT}'
elif scoped:
url = f'https://api.github.com/user'
else:
user = user.replace('@', '')
url = f'https://api.github.com/users/{user}{sub_path}?per_page={PER_PAGE_LIMIT}'

response = requests.get(url, auth=auth, headers=HEADERS)

try:
response_dict = response.json()
Expand Down