Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions services/github/users/get_email_from_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from config import GITHUB_API_URL, TIMEOUT
from services.github.utils.create_headers import create_headers
from utils.error.handle_exceptions import handle_exceptions
from utils.logging.logging_config import logger

NOREPLY_SUFFIX = "@users.noreply.github.com"

Expand All @@ -17,12 +18,20 @@ def get_email_from_commits(owner: str, repo: str, username: str, token: str):
params={"author": username, "per_page": 5},
timeout=TIMEOUT,
)
if response.status_code == 409:
logger.info("Repository %s/%s is empty, skipping email lookup", owner, repo)
return None

response.raise_for_status()
commits: list[dict[str, dict[str, dict[str, str]]]] = response.json()
for commit in commits:
commit_obj = commit.get("commit", {})
author = commit_obj.get("author", {})
email = author.get("email", "")
commit_obj = commit.get("commit")
if not commit_obj:
continue
author = commit_obj.get("author")
if not author:
continue
email = author.get("email")
if email and not email.endswith(NOREPLY_SUFFIX):
return email
return None
7 changes: 7 additions & 0 deletions services/github/users/test_get_email_from_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def test_returns_none_when_no_commits(mock_get: MagicMock):
assert result is None


@patch("services.github.users.get_email_from_commits.requests.get")
def test_returns_none_when_repo_is_empty(mock_get: MagicMock):
mock_get.return_value.status_code = 409
result = get_email_from_commits(owner="o", repo="r", username="u", token="t")
assert result is None


@patch("services.github.users.get_email_from_commits.requests.get")
def test_returns_none_on_api_error(mock_get: MagicMock):
mock_get.side_effect = Exception("API error")
Expand Down
4 changes: 3 additions & 1 deletion services/webhook/check_suite_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@ async def handle_check_suite(
sender_name = payload["sender"]["login"]
sender_info = get_user_public_info(username=sender_name, token=token)
if not sender_info.email:
sender_info.email = get_email_from_commits(
email = get_email_from_commits(
owner=owner_name, repo=repo_name, username=sender_name, token=token
)
if email:
sender_info.email = email

# Extract PR related variables and return if no PR is associated with this check suite
pull_requests = check_suite["pull_requests"]
Expand Down
4 changes: 3 additions & 1 deletion services/webhook/review_run_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ async def handle_review_run(
token = get_installation_access_token(installation_id=installation_id)
sender_info = get_user_public_info(username=sender_name, token=token)
if not sender_info.email:
sender_info.email = get_email_from_commits(
email = get_email_from_commits(
owner=owner_name, repo=repo_name, username=sender_name, token=token
)
if email:
sender_info.email = email

# Get all comments in the review thread
thread_comments = get_review_thread_comments(
Expand Down