From fd56945a8db4af0b522d099f7eb2282643ce37d6 Mon Sep 17 00:00:00 2001 From: Harold Wanyama Date: Wed, 1 May 2024 19:12:07 +0300 Subject: [PATCH] [#3884] Feature/Co-Author Process - Updated aggregated co-authors fetched in the processing list Signed-off-by: Harold Wanyama --- cla-backend/cla/models/github_models.py | 19 ++++--- cla-backend/cla/tests/unit/conftest.py | 28 +++------- .../cla/tests/unit/test_company_event.py | 3 +- .../cla/tests/unit/test_docusign_models.py | 10 ++-- .../cla/tests/unit/test_dynamo_models.py | 4 +- .../tests/unit/test_email_approval_list.py | 3 +- cla-backend/cla/tests/unit/test_event.py | 3 +- .../cla/tests/unit/test_gh_org_models.py | 8 +-- cla-backend/cla/tests/unit/test_github.py | 3 +- .../cla/tests/unit/test_github_controller.py | 5 +- .../cla/tests/unit/test_github_models.py | 53 +++++++++++++++++++ cla-backend/cla/tests/unit/test_model.py | 11 ++-- .../cla/tests/unit/test_project_event.py | 2 +- .../tests/unit/test_salesforce_projects.py | 7 ++- .../tests/unit/test_signature_controller.py | 2 +- cla-backend/cla/tests/unit/test_user_event.py | 6 +-- cla-backend/cla/tests/unit/test_utils.py | 7 +-- cla-backend/cla/utils.py | 1 + 18 files changed, 112 insertions(+), 63 deletions(-) diff --git a/cla-backend/cla/models/github_models.py b/cla-backend/cla/models/github_models.py index 1ae92b9ae..647492252 100644 --- a/cla-backend/cla/models/github_models.py +++ b/cla-backend/cla/models/github_models.py @@ -1255,7 +1255,7 @@ def get_merge_group_commit_authors(merge_group_sha, installation_id=None) -> Lis return commit_authors -def get_author_summary(commit,pr,installation_id) -> UserCommitSummary: +def get_author_summary(commit,pr,installation_id) -> List[UserCommitSummary]: """ Helper function to extract author information from a GitHub commit. :param commit: A GitHub commit object. @@ -1264,6 +1264,7 @@ def get_author_summary(commit,pr,installation_id) -> UserCommitSummary: :type pr: int """ fn = 'cla.models.github_models.get_author_summary' + commit_authors = [] if commit.author: try: commit_author_summary = UserCommitSummary( @@ -1277,13 +1278,13 @@ def get_author_summary(commit,pr,installation_id) -> UserCommitSummary: cla.log.debug(f'{fn} - PR: {pr}, {commit_author_summary}') # check for co-author details # issue # 3884 - commit_authors = [] + commit_authors.append(commit_author_summary) co_authors = cla.utils.get_co_authors_from_commit(commit) with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: for co_author in co_authors: - commit_authors.append(executor.submit(get_co_author_commits, co_author, commit, pr, installation_id)) + commit_authors.append(executor.submit(get_co_author_commits, co_author, commit, pr, installation_id).result()) - return commit_author_summary + return commit_authors except (GithubException, IncompletableObject) as exc: cla.log.warning(f'{fn} - PR: {pr}, unable to get commit author summary: {exc}') try: @@ -1303,7 +1304,8 @@ def get_author_summary(commit,pr,installation_id) -> UserCommitSummary: f'GitHub NamedUser author NOT found for commit SHA {commit_author_summary} ' f'however, we did find GitAuthor info') cla.log.debug(f'{fn} - PR: {pr}, {commit_author_summary}') - return commit_author_summary + commit_authors.append(commit_author_summary) + return commit_authors except (GithubException, IncompletableObject) as exc: cla.log.warning(f'{fn} - PR: {pr}, unable to get commit author summary: {exc}') commit_author_summary = UserCommitSummary( @@ -1316,6 +1318,8 @@ def get_author_summary(commit,pr,installation_id) -> UserCommitSummary: ) cla.log.warning(f'{fn} - PR: {pr}, ' f'could not find any commit author for SHA {commit_author_summary}') + commit_authors.append(commit_author_summary) + return commit_authors else: cla.log.warning(f'{fn} - PR: {pr}, ' f'could not find any commit author for SHA {commit.sha}') @@ -1327,7 +1331,8 @@ def get_author_summary(commit,pr,installation_id) -> UserCommitSummary: None, False, False ) - return commit_author_summary + commit_authors.append(commit_author_summary) + return commit_authors @@ -1359,7 +1364,7 @@ def get_pull_request_commit_authors(pull_request, installation_id) -> List[UserC for future in concurrent.futures.as_completed(future_to_commit): future_to_commit[future] try: - commit_authors.append(future.result()) + commit_authors.extend(future.result()) except Exception as exc: cla.log.warning(f'{fn} - PR: {pull_request.number}, get_author_summary generated an exception: {exc}') raise exc diff --git a/cla-backend/cla/tests/unit/conftest.py b/cla-backend/cla/tests/unit/conftest.py index 861d8451f..efec3c629 100644 --- a/cla-backend/cla/tests/unit/conftest.py +++ b/cla-backend/cla/tests/unit/conftest.py @@ -1,27 +1,15 @@ # Copyright The Linux Foundation and each contributor to CommunityBridge. # SPDX-License-Identifier: MIT -import pytest +from unittest.mock import MagicMock, patch -from unittest.mock import patch, MagicMock -from cla.tests.unit.data import ( - COMPANY_TABLE_DATA, - USER_TABLE_DATA, - SIGNATURE_TABLE_DATA, - EVENT_TABLE_DESCRIPTION, - PROJECT_TABLE_DESCRIPTION, -) -from cla.models.dynamo_models import ( - UserModel, - SignatureModel, - CompanyModel, - EventModel, - ProjectModel, - Signature, - Company, - User, - Project -) +import pytest +from cla.models.dynamo_models import (Company, CompanyModel, EventModel, + Project, ProjectModel, Signature, + SignatureModel, User, UserModel) +from cla.tests.unit.data import (COMPANY_TABLE_DATA, EVENT_TABLE_DESCRIPTION, + PROJECT_TABLE_DESCRIPTION, + SIGNATURE_TABLE_DATA, USER_TABLE_DATA) PATCH_METHOD = "pynamodb.connection.Connection._make_api_call" diff --git a/cla-backend/cla/tests/unit/test_company_event.py b/cla-backend/cla/tests/unit/test_company_event.py index ce5c6c1e8..7da712873 100644 --- a/cla-backend/cla/tests/unit/test_company_event.py +++ b/cla-backend/cla/tests/unit/test_company_event.py @@ -3,9 +3,8 @@ from unittest.mock import Mock, patch -import pytest - import cla +import pytest from cla.auth import AuthUser from cla.controllers import company as company_controller from cla.models.dynamo_models import Company diff --git a/cla-backend/cla/tests/unit/test_docusign_models.py b/cla-backend/cla/tests/unit/test_docusign_models.py index aaa6021aa..5f05482b8 100644 --- a/cla-backend/cla/tests/unit/test_docusign_models.py +++ b/cla-backend/cla/tests/unit/test_docusign_models.py @@ -3,9 +3,13 @@ import xml.etree.ElementTree as ET -from cla.models.docusign_models import populate_signature_from_ccla_callback, populate_signature_from_icla_callback, \ - create_default_company_values, document_signed_email_content, ClaSignatoryEmailParams, cla_signatory_email_content -from cla.models.dynamo_models import Signature, Company, Project, User +from cla.models.docusign_models import (ClaSignatoryEmailParams, + cla_signatory_email_content, + create_default_company_values, + document_signed_email_content, + populate_signature_from_ccla_callback, + populate_signature_from_icla_callback) +from cla.models.dynamo_models import Company, Project, Signature, User content_icla_agreement_date = """ \n\nCo-authored-by: {co_author_2} <{co_author_email_2}>" + + commit.author.email = "fake_author@example.com" + pull_request.get_commits.return_value.__iter__.return_value = [commit] + + mock_user = Mock(spec=NamedUser) + mock_user.id = 2 + mock_user.login = "co_author_login" + + mock_user_2 = Mock(spec=NamedUser) + mock_user_2.id = 3 + mock_user_2.login = "co_author_login_2" + + mock_github_instance.return_value.get_github_user_by_email.side_effect = ( + lambda email, _: mock_user if email == co_author_email else mock_user_2 + ) + + # Call the function + result = get_pull_request_commit_authors(pull_request, "fake_installation_id") + + # Assertions + self.assertEqual(len(result), 3) + self.assertIn(co_author_email, [author.author_email for author in result]) + self.assertIn(co_author_email_2, [author.author_email for author in result]) + self.assertIn("fake_login", [author.author_login for author in result]) + self.assertIn("co_author_login", [author.author_login for author in result]) + + +if __name__ == "__main__": + unittest.main() diff --git a/cla-backend/cla/tests/unit/test_model.py b/cla-backend/cla/tests/unit/test_model.py index cbac73c4e..611567a19 100644 --- a/cla-backend/cla/tests/unit/test_model.py +++ b/cla-backend/cla/tests/unit/test_model.py @@ -4,17 +4,16 @@ """ Test python API changes for Signature and User Tables """ -from unittest.mock import patch, MagicMock +from unittest.mock import MagicMock, patch +import cla import pytest - -from cla.models.dynamo_models import SignatureModel, UserModel -from cla.utils import get_user_instance, get_signature_instance, get_company_instance from cla import utils +from cla.models.dynamo_models import SignatureModel, UserModel from cla.tests.unit.data import USER_TABLE_DATA - +from cla.utils import (get_company_instance, get_signature_instance, + get_user_instance) from pynamodb.indexes import AllProjection -import cla PATCH_METHOD = "pynamodb.connection.Connection._make_api_call" diff --git a/cla-backend/cla/tests/unit/test_project_event.py b/cla-backend/cla/tests/unit/test_project_event.py index a296d29bb..d26990abe 100644 --- a/cla-backend/cla/tests/unit/test_project_event.py +++ b/cla-backend/cla/tests/unit/test_project_event.py @@ -6,7 +6,7 @@ import cla from cla.auth import AuthUser from cla.controllers import project as project_controller -from cla.models.dynamo_models import Project, User, Document, UserPermissions +from cla.models.dynamo_models import Document, Project, User, UserPermissions from cla.models.event_types import EventType PATCH_METHOD = "pynamodb.connection.Connection._make_api_call" diff --git a/cla-backend/cla/tests/unit/test_salesforce_projects.py b/cla-backend/cla/tests/unit/test_salesforce_projects.py index 5c2d21779..ebc8315e1 100644 --- a/cla-backend/cla/tests/unit/test_salesforce_projects.py +++ b/cla-backend/cla/tests/unit/test_salesforce_projects.py @@ -4,13 +4,12 @@ import json import os from http import HTTPStatus -from unittest.mock import Mock, patch, MagicMock - -import pytest +from unittest.mock import MagicMock, Mock, patch import cla +import pytest from cla.models.dynamo_models import UserPermissions -from cla.salesforce import get_projects, get_project +from cla.salesforce import get_project, get_projects @pytest.fixture() diff --git a/cla-backend/cla/tests/unit/test_signature_controller.py b/cla-backend/cla/tests/unit/test_signature_controller.py index 975b78ec4..4544d626e 100644 --- a/cla-backend/cla/tests/unit/test_signature_controller.py +++ b/cla-backend/cla/tests/unit/test_signature_controller.py @@ -7,7 +7,7 @@ import cla from cla.controllers.signature import notify_whitelist_change from cla.controllers.signing import canceled_signature_html -from cla.models.dynamo_models import User, Signature, Project +from cla.models.dynamo_models import Project, Signature, User from cla.models.sns_email_models import MockSNS from cla.user import CLAUser diff --git a/cla-backend/cla/tests/unit/test_user_event.py b/cla-backend/cla/tests/unit/test_user_event.py index 4351a4567..01aee68a7 100644 --- a/cla-backend/cla/tests/unit/test_user_event.py +++ b/cla-backend/cla/tests/unit/test_user_event.py @@ -1,12 +1,12 @@ # Copyright The Linux Foundation and each contributor to CommunityBridge. # SPDX-License-Identifier: MIT -from unittest.mock import patch, Mock +from unittest.mock import Mock, patch import pytest - from cla.controllers import user as user_controller -from cla.models.dynamo_models import User, Project, Company, CCLAWhitelistRequest, CompanyInvite +from cla.models.dynamo_models import (CCLAWhitelistRequest, Company, + CompanyInvite, Project, User) from cla.models.event_types import EventType diff --git a/cla-backend/cla/tests/unit/test_utils.py b/cla-backend/cla/tests/unit/test_utils.py index 5b246ebf0..1384e82b8 100644 --- a/cla-backend/cla/tests/unit/test_utils.py +++ b/cla-backend/cla/tests/unit/test_utils.py @@ -6,9 +6,10 @@ import cla from cla import utils -from cla.models.dynamo_models import Signature, User, Project -from cla.utils import append_email_help_sign_off_content, get_email_help_content, get_email_sign_off_content, \ - get_full_sign_url, append_project_version_to_url +from cla.models.dynamo_models import Project, Signature, User +from cla.utils import (append_email_help_sign_off_content, + append_project_version_to_url, get_email_help_content, + get_email_sign_off_content, get_full_sign_url) class TestUtils(unittest.TestCase): diff --git a/cla-backend/cla/utils.py b/cla-backend/cla/utils.py index 1d222fce1..d21100b8e 100644 --- a/cla-backend/cla/utils.py +++ b/cla-backend/cla/utils.py @@ -1840,6 +1840,7 @@ def get_co_authors_from_commit(commit): Helper function to return co-authors from commit """ fn = "get_co_authors_from_commit" + # import pdb; pdb.set_trace() co_authors = [] if commit.commit: commit_message = commit.commit.message