Skip to content

Commit

Permalink
Merge pull request #3890 from nickmango/feature/pr-co-authors
Browse files Browse the repository at this point in the history
[#3884] Feature/PR Co-authors
  • Loading branch information
nickmango authored Apr 12, 2023
2 parents 3cd1c38 + b0cadf6 commit 3f88b8a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cla-backend/cla/models/github_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[0],
co_author[1],
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:
Expand Down
40 changes: 40 additions & 0 deletions cla-backend/cla/tests/unit/test_commit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright The Linux Foundation and each contributor to CommunityBridge.
# SPDX-License-Identifier: MIT

from cla.utils import get_co_authors_from_commit

FAKE_COMMIT = {
"commit": {
"author": {
"name": "Harold Wanyama",
"email": "hwanyama@contractor.linuxfoundation.org",
"date": "2023-04-12T02:10:24Z",
},
"committer": {
"name": "Harold Wanyama",
"email": "hwanyama@contractor.linuxfoundation.org",
"date": "2023-04-12T02:10:24Z",
},
"message": "Test 2\n\nCo-authored-by: Harold <wanyaland@gmail.com>",
"tree": {
"sha": "9db529464eac36c3e8825cd1c15cdaa571168d0a",
"url": "https://api.github.com/repos/nabzo-bella/repo1/git/trees/9db529464eac36c3e8825cd1c15cdaa571168d0a",
},
}
}

def test_get_commit_co_authors():
"""Test get commit co authors"""
commit = FAKE_COMMIT
co_authors = get_co_authors_from_commit(commit)
assert co_authors == [('Harold', 'wanyaland@gmail.com')]
assert isinstance(co_authors[0], tuple)
assert co_authors[0][0] == 'Harold'
assert co_authors[0][1] == 'wanyaland@gmail.com'

def test_get_co_author_no_name():
"""Test get commit co authors"""
commit = FAKE_COMMIT
commit['commit']['message'] = "Test 2\n\nCo-authored-by: <wanyaland@gmail.com"
co_authors = get_co_authors_from_commit(commit)
assert len(co_authors) == 0
14 changes: 14 additions & 0 deletions cla-backend/cla/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import inspect
import json
import os
import re
import urllib.parse
import urllib.parse as urlparse
from datetime import datetime
Expand Down Expand Up @@ -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

0 comments on commit 3f88b8a

Please sign in to comment.