From b0cadf62fc03dde478d8ca786c6e3364365e306f Mon Sep 17 00:00:00 2001 From: Harold Wanyama 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 --- cla-backend/cla/models/github_models.py | 14 ++++++++ cla-backend/cla/tests/unit/test_commit.py | 40 +++++++++++++++++++++++ cla-backend/cla/utils.py | 14 ++++++++ 3 files changed, 68 insertions(+) create mode 100644 cla-backend/cla/tests/unit/test_commit.py diff --git a/cla-backend/cla/models/github_models.py b/cla-backend/cla/models/github_models.py index 77a93b8fc..7e8b8939a 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[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: diff --git a/cla-backend/cla/tests/unit/test_commit.py b/cla-backend/cla/tests/unit/test_commit.py new file mode 100644 index 000000000..3d3d64512 --- /dev/null +++ b/cla-backend/cla/tests/unit/test_commit.py @@ -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 ", + "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: 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