Skip to content

Commit

Permalink
Merge pull request #4304 from nickmango/feature/co-author-fix
Browse files Browse the repository at this point in the history
[#3884] Feature/Co-Author Process
  • Loading branch information
nickmango authored May 1, 2024
2 parents 5643740 + fd56945 commit 4c88399
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 63 deletions.
19 changes: 12 additions & 7 deletions cla-backend/cla/models/github_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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:
Expand All @@ -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(
Expand All @@ -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}')
Expand All @@ -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



Expand Down Expand Up @@ -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
Expand Down
28 changes: 8 additions & 20 deletions cla-backend/cla/tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
3 changes: 1 addition & 2 deletions cla-backend/cla/tests/unit/test_company_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions cla-backend/cla/tests/unit/test_docusign_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """<?xml version="1.0" encoding="utf-8"?>
<DocuSignEnvelopeInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Expand Down
4 changes: 2 additions & 2 deletions cla-backend/cla/tests/unit/test_dynamo_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from unittest.mock import Mock

import pytest

from cla.models.dynamo_models import User, Company
from cla import utils
from cla.models.dynamo_models import Company, User


@pytest.fixture
def user():
Expand Down
3 changes: 1 addition & 2 deletions cla-backend/cla/tests/unit/test_email_approval_list.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Copyright The Linux Foundation and each contributor to CommunityBridge.
# SPDX-License-Identifier: MIT

from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock, patch

import pytest

from cla.models.dynamo_models import Signature, User, UserModel


Expand Down
3 changes: 1 addition & 2 deletions cla-backend/cla/tests/unit/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
from unittest.mock import Mock

import pytest

from cla.models import event_types
from cla.models.dynamo_models import Event, User, Project, Company
from cla.models.dynamo_models import Company, Event, Project, User


@pytest.fixture()
Expand Down
8 changes: 4 additions & 4 deletions cla-backend/cla/tests/unit/test_gh_org_models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Copyright The Linux Foundation and each contributor to CommunityBridge.
# SPDX-License-Identifier: MIT

import pytest
from unittest.mock import MagicMock, Mock, patch

import cla
import pynamodb
from unittest.mock import Mock, patch, MagicMock

import pytest
from cla.models.dynamo_models import GitHubOrg, GitHubOrgModel
from cla.utils import get_github_organization_instance
from cla.tests.unit.data import GH_TABLE_DESCRIPTION
from cla.utils import get_github_organization_instance

PATCH_METHOD = "pynamodb.connection.Connection._make_api_call"

Expand Down
3 changes: 2 additions & 1 deletion cla-backend/cla/tests/unit/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import unittest

import cla
from cla.controllers.github import webhook_secret_validation, webhook_secret_failed_email_content
from cla.controllers.github import (webhook_secret_failed_email_content,
webhook_secret_validation)
from cla.utils import get_comment_badge

SUCCESS = ":white_check_mark:"
Expand Down
5 changes: 3 additions & 2 deletions cla-backend/cla/tests/unit/test_github_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from unittest.mock import Mock

import cla
from cla.controllers.github import get_org_name_from_installation_event, get_github_activity_action, \
notify_project_managers
from cla.controllers.github import (get_github_activity_action,
get_org_name_from_installation_event,
notify_project_managers)
from cla.controllers.repository import Repository
from cla.models.ses_models import MockSES

Expand Down
53 changes: 53 additions & 0 deletions cla-backend/cla/tests/unit/test_github_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,56 @@
#
# if __name__ == '__main__':
# unittest.main()
from unittest import TestCase
from unittest.mock import MagicMock, Mock, patch

from cla.models.github_models import UserCommitSummary, get_author_summary, get_pull_request_commit_authors
from github import NamedUser


class TestGetPullRequestCommitAuthors(TestCase):
@patch("cla.utils.get_repository_service")
def test_get_pull_request_commit_with_co_author(self, mock_github_instance):
# Mock data
pull_request = MagicMock()
pull_request.number = 123
co_author = "co_author"
co_author_email = "co_author_email.gmail.com"
co_author_2 = "co_author_2"
co_author_email_2 = "co_author_email_2.gmail.com"
commit = MagicMock()
commit.sha = "fake_sha"
commit.author = MagicMock()
commit.author.id = 1
commit.author.login = "fake_login"
commit.author.name = "Fake Author"
commit.commit.message = f"fake message\n\nCo-authored-by: {co_author} <{co_author_email}>\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()
11 changes: 5 additions & 6 deletions cla-backend/cla/tests/unit/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion cla-backend/cla/tests/unit/test_project_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 3 additions & 4 deletions cla-backend/cla/tests/unit/test_salesforce_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion cla-backend/cla/tests/unit/test_signature_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions cla-backend/cla/tests/unit/test_user_event.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
7 changes: 4 additions & 3 deletions cla-backend/cla/tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions cla-backend/cla/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4c88399

Please sign in to comment.