diff --git a/src/sentry/integrations/utils/commit_context.py b/src/sentry/integrations/utils/commit_context.py index 0c9da764c7b033..c6dfd728f3dba5 100644 --- a/src/sentry/integrations/utils/commit_context.py +++ b/src/sentry/integrations/utils/commit_context.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging +from datetime import datetime, timedelta, timezone from typing import Any, List, Mapping, Sequence, Tuple import sentry_sdk @@ -108,7 +109,14 @@ def find_commit_context_for_event( }, ) - if commit_context: + # Only return suspect commits that are less than a year old + if commit_context and is_date_less_than_year(commit_context["committedDate"]): result.append((commit_context, code_mapping)) return result, installation + + +def is_date_less_than_year(date): + return datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ").replace( + tzinfo=timezone.utc + ) > datetime.now(tz=timezone.utc) - timedelta(days=365) diff --git a/tests/sentry/tasks/test_commit_context.py b/tests/sentry/tasks/test_commit_context.py index 641633127c3b5a..a117304a97bf84 100644 --- a/tests/sentry/tasks/test_commit_context.py +++ b/tests/sentry/tasks/test_commit_context.py @@ -1,4 +1,5 @@ -from datetime import timedelta +from datetime import datetime, timedelta +from datetime import timezone as datetime_timezone from unittest.mock import Mock, patch import pytest @@ -83,7 +84,9 @@ class TestCommitContext(TestCommitContextMixin): "sentry.integrations.github.GitHubIntegration.get_commit_context", return_value={ "commitId": "asdfwreqr", - "committedDate": "2023-02-14T11:11Z", + "committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime( + "%Y-%m-%dT%H:%M:%SZ" + ), "commitMessage": "placeholder commit message", "commitAuthorName": "", "commitAuthorEmail": "admin@localhost", @@ -141,11 +144,51 @@ def test_failed_to_fetch_commit_context_record(self, mock_get_commit_context, mo error_message="integration_failed", ) + @patch("sentry.tasks.commit_context.logger") @patch( "sentry.integrations.github.GitHubIntegration.get_commit_context", return_value={ "commitId": "asdfasdf", - "committedDate": "2023-02-14T11:11Z", + "committedDate": ( + datetime.now(tz=datetime_timezone.utc) - timedelta(days=370) + ).strftime("%Y-%m-%dT%H:%M:%SZ"), + "commitMessage": "placeholder commit message", + "commitAuthorName": "", + "commitAuthorEmail": "admin@localhost", + }, + ) + def test_found_commit_is_too_old(self, mock_get_commit_context, mock_logger): + with self.tasks(): + assert not GroupOwner.objects.filter(group=self.event.group).exists() + event_frames = get_frame_paths(self.event) + process_commit_context( + event_id=self.event.event_id, + event_platform=self.event.platform, + event_frames=event_frames, + group_id=self.event.group_id, + project_id=self.event.project_id, + ) + + assert mock_logger.info.call_count == 1 + mock_logger.info.assert_called_with( + "process_commit_context.find_commit_context", + extra={ + "event": self.event.event_id, + "group": self.event.group_id, + "organization": self.event.group.project.organization_id, + "reason": "could_not_fetch_commit_context", + "code_mappings_count": 1, + "fallback": True, + }, + ) + + @patch( + "sentry.integrations.github.GitHubIntegration.get_commit_context", + return_value={ + "commitId": "asdfasdf", + "committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime( + "%Y-%m-%dT%H:%M:%SZ" + ), "commitMessage": "placeholder commit message", "commitAuthorName": "", "commitAuthorEmail": "admin@localhost", @@ -170,7 +213,9 @@ def test_no_matching_commit_in_db(self, mock_get_commit_context): "sentry.integrations.github.GitHubIntegration.get_commit_context", return_value={ "commitId": "asdfwreqr", - "committedDate": "2023-02-14T11:11Z", + "committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime( + "%Y-%m-%dT%H:%M:%SZ" + ), "commitMessage": "placeholder commit message", "commitAuthorName": "", "commitAuthorEmail": "admin@localhost", @@ -255,7 +300,9 @@ def test_no_inapp_frame_in_stacktrace(self, mock_process_suspect_commits): "sentry.integrations.github.GitHubIntegration.get_commit_context", return_value={ "commitId": "somekey", - "committedDate": "2023-02-14T11:11Z", + "committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime( + "%Y-%m-%dT%H:%M:%SZ" + ), "commitMessage": "placeholder commit message", "commitAuthorName": "", "commitAuthorEmail": "randomuser@sentry.io", @@ -296,7 +343,9 @@ def test_commit_author_not_in_sentry(self, mock_get_commit_context): "sentry.integrations.github.GitHubIntegration.get_commit_context", return_value={ "commitId": "somekey", - "committedDate": "2023-02-14T11:11Z", + "committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime( + "%Y-%m-%dT%H:%M:%SZ" + ), "commitMessage": "placeholder commit message", "commitAuthorName": "", "commitAuthorEmail": "randomuser@sentry.io", @@ -337,7 +386,9 @@ def test_commit_author_no_user(self, mock_get_commit_context, mock_get_users_for "sentry.integrations.github.GitHubIntegration.get_commit_context", return_value={ "commitId": "somekey", - "committedDate": "2023-02-14T11:11Z", + "committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime( + "%Y-%m-%dT%H:%M:%SZ" + ), "commitMessage": "placeholder commit message", "commitAuthorName": "", "commitAuthorEmail": "randomuser@sentry.io", @@ -423,7 +474,9 @@ def after_return(self, status, retval, task_id, args, kwargs, einfo): Mock( return_value={ "commitId": "asdfwreqr", - "committedDate": "2023-02-14T11:11Z", + "committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime( + "%Y-%m-%dT%H:%M:%SZ" + ), "commitMessage": "placeholder commit message", "commitAuthorName": "", "commitAuthorEmail": "admin@localhost", diff --git a/tests/sentry/tasks/test_post_process.py b/tests/sentry/tasks/test_post_process.py index 4d74974e3d5020..d440bd430c949d 100644 --- a/tests/sentry/tasks/test_post_process.py +++ b/tests/sentry/tasks/test_post_process.py @@ -1296,7 +1296,9 @@ def test_issue_owners_should_ratelimit(self, logger): class ProcessCommitsTestMixin(BasePostProgressGroupMixin): github_blame_return_value = { "commitId": "asdfwreqr", - "committedDate": "", + "committedDate": (datetime.now(timezone.utc) - timedelta(days=2)).strftime( + "%Y-%m-%dT%H:%M:%SZ" + ), "commitMessage": "placeholder commit message", "commitAuthorName": "", "commitAuthorEmail": "admin@localhost",