From 2e404d6706f7fee3fa7830a30263d6dddd292f5f Mon Sep 17 00:00:00 2001 From: Harold Wanyama Date: Thu, 13 Apr 2023 00:35:46 +0300 Subject: [PATCH] [#3884]Feature/Fetch Github User - Added co-author user fetch by email Signed-off-by: Harold Wanyama --- cla-backend/cla/models/github_models.py | 17 ++++++++++++---- cla-backend/cla/utils.py | 26 +++++++++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/cla-backend/cla/models/github_models.py b/cla-backend/cla/models/github_models.py index 7e8b8939a..6565b5472 100644 --- a/cla-backend/cla/models/github_models.py +++ b/cla-backend/cla/models/github_models.py @@ -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}') diff --git a/cla-backend/cla/utils.py b/cla-backend/cla/utils.py index 76912da34..b3b1fc2aa 100644 --- a/cla-backend/cla/utils.py +++ b/cla-backend/cla/utils.py @@ -16,6 +16,7 @@ from urllib.parse import urlencode import falcon +import github import requests from hug.middleware import SessionMiddleware from requests_oauthlib import OAuth2Session @@ -23,10 +24,11 @@ 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 @@ -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} @@ -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