From f4fbcf724e18b497d55c357d350314e8b461e7d6 Mon Sep 17 00:00:00 2001
From: Harold Wanyama <hwanyama@contractor.linuxfoundation.org>
Date: Wed, 12 Apr 2023 05:50:23 +0300
Subject: [PATCH] [#3884] Feature/PR Co-authors

- Handled use case for getting co-author info in a given commit

Signed-off-by: Harold Wanyama <hwanyama@contractor.linuxfoundation.org>
---
 cla-backend/cla/models/github_models.py | 14 ++++++++++++++
 cla-backend/cla/utils.py                | 14 ++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/cla-backend/cla/models/github_models.py b/cla-backend/cla/models/github_models.py
index 77a93b8fc..2a290bfc3 100644
--- a/cla-backend/cla/models/github_models.py
+++ b/cla-backend/cla/models/github_models.py
@@ -954,6 +954,20 @@ def get_pull_request_commit_authors(pull_request) -> List[UserCommitSummary]:
                 )
                 cla.log.debug(f'{fn} - PR: {pull_request.number}, {commit_author_summary}')
                 commit_authors.append(commit_author_summary)
+                # check for co-author details in the commit message: 
+                # issue # 3884
+                co_authors = cla.utils.get_co_authors_from_commit(commit)
+                for co_author in co_authors:
+                    co_author_summary = UserCommitSummary(
+                        commit.sha,
+                        None,
+                        None,
+                        co_author.name,
+                        co_author.email,
+                        False, False  # default not authorized - will be evaluated and updated later
+                    )
+                    cla.log.debug(f'{fn} - PR: {pull_request.number}, {co_author_summary}')
+                    commit_authors.append(co_author_summary)
             except (GithubException, IncompletableObject) as ex:
                 cla.log.debug(f'Commit sha: {commit.sha} exception: {ex}')
                 try:
diff --git a/cla-backend/cla/utils.py b/cla-backend/cla/utils.py
index ff912f81b..dd37cec2f 100644
--- a/cla-backend/cla/utils.py
+++ b/cla-backend/cla/utils.py
@@ -8,6 +8,7 @@
 import inspect
 import json
 import os
+import re
 import urllib.parse
 import urllib.parse as urlparse
 from datetime import datetime
@@ -1825,3 +1826,16 @@ def get_public_email(user):
     """
     if len(user.get_all_user_emails()) > 0:
         return next((email for email in user.get_all_user_emails() if "noreply.github.com" not in email), None)
+
+def get_co_authors_from_commit(commit):
+    """
+    Helper function to return co-authors from commit
+    """
+    fn = "get_co_authors_from_commit"
+    co_authors = []
+    if commit.get("commit"):
+        commit_message = commit.get("commit").get("message")
+        cla.log.debug(f'{fn} - commit message: {commit_message}')
+        if commit_message:
+            co_authors = re.findall(r"Co-authored-by: (.*) <(.*)>", commit_message)
+    return co_authors
\ No newline at end of file