Skip to content

Commit

Permalink
Merge pull request #3893 from nickmango/feature/gh-search-email
Browse files Browse the repository at this point in the history
[#3884]Feature/Fetch Github User
  • Loading branch information
nickmango committed Apr 12, 2023
2 parents 1c709f0 + 2e404d6 commit bee08ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
17 changes: 13 additions & 4 deletions cla-backend/cla/models/github_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,12 +958,21 @@ def get_pull_request_commit_authors(pull_request) -> List[UserCommitSummary]:
# issue # 3884
co_authors = cla.utils.get_co_authors_from_commit(commit)
for co_author in co_authors:
# check if co-author is a github user
login, github_id = None, None
email = co_author[1]
name = co_author[0]
user = cla.utils.get_github_user_by_email(email)
cla.log.debug(f'{fn} - co-author: {co_author}, user: {user}')
if user:
login = user.login
github_id = user.id
co_author_summary = UserCommitSummary(
commit.sha,
None,
None,
co_author[0],
co_author[1],
github_id,
login,
name,
email,
False, False # default not authorized - will be evaluated and updated later
)
cla.log.debug(f'{fn} - PR: {pull_request.number}, {co_author_summary}')
Expand Down
26 changes: 22 additions & 4 deletions cla-backend/cla/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
from urllib.parse import urlencode

import falcon
import github
import requests
from hug.middleware import SessionMiddleware
from requests_oauthlib import OAuth2Session

import cla
from cla.middleware import CLALogMiddleware
from cla.models import DoesNotExist
from cla.models.dynamo_models import User, Signature, Repository, \
Company, Project, Document, \
GitHubOrg, Gerrit, UserPermissions, Event, CompanyInvite, ProjectCLAGroup, CCLAWhitelistRequest, CLAManagerRequest, \
GitlabOrg
from cla.models.dynamo_models import (CCLAWhitelistRequest, CLAManagerRequest,
Company, CompanyInvite, Document, Event,
Gerrit, GitHubOrg, GitlabOrg, Project,
ProjectCLAGroup, Repository, Signature,
User, UserPermissions)
from cla.models.event_types import EventType
from cla.user import UserCommitSummary

Expand Down Expand Up @@ -433,6 +435,7 @@ def get_supported_repository_providers():
:rtype: dict
"""
from cla.models.github_models import GitHub, MockGitHub

# from cla.models.gitlab_models import GitLab, MockGitLab
# return {'github': GitHub, 'mock_github': MockGitHub,
# 'gitlab': GitLab, 'mock_gitlab': MockGitLab}
Expand Down Expand Up @@ -1839,3 +1842,18 @@ def get_co_authors_from_commit(commit):
if commit_message:
co_authors = re.findall(r"Co-authored-by: (.*) <(.*)>", commit_message)
return co_authors

def get_github_user_by_email(email):
"""
Helper function to return github user by email
"""
fn = "get_github_user_by_email"
cla.log.debug(f'{fn} - searching for github user by email: {email}')
github_client = github.Github()
try:
users = github_client.search_users(email)
if len(users) > 0:
return users[0]
except Exception as e:
cla.log.warning(f'{fn} - unable to find github user by email: {email}, error: {e}')
return None

0 comments on commit bee08ec

Please sign in to comment.