Skip to content

Commit 14935b6

Browse files
author
NisanthanNanthakumar
authored
feat(commit-context): Do not create if older than 1 year (#54624)
## Objective: Do not create a suspect commit if the resulting commit is over 1 year old.
1 parent 11011d4 commit 14935b6

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

src/sentry/integrations/utils/commit_context.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4+
from datetime import datetime, timedelta, timezone
45
from typing import Any, List, Mapping, Sequence, Tuple
56

67
import sentry_sdk
@@ -108,7 +109,14 @@ def find_commit_context_for_event(
108109
},
109110
)
110111

111-
if commit_context:
112+
# Only return suspect commits that are less than a year old
113+
if commit_context and is_date_less_than_year(commit_context["committedDate"]):
112114
result.append((commit_context, code_mapping))
113115

114116
return result, installation
117+
118+
119+
def is_date_less_than_year(date):
120+
return datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ").replace(
121+
tzinfo=timezone.utc
122+
) > datetime.now(tz=timezone.utc) - timedelta(days=365)

tests/sentry/tasks/test_commit_context.py

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from datetime import timedelta
1+
from datetime import datetime, timedelta
2+
from datetime import timezone as datetime_timezone
23
from unittest.mock import Mock, patch
34

45
import pytest
@@ -83,7 +84,9 @@ class TestCommitContext(TestCommitContextMixin):
8384
"sentry.integrations.github.GitHubIntegration.get_commit_context",
8485
return_value={
8586
"commitId": "asdfwreqr",
86-
"committedDate": "2023-02-14T11:11Z",
87+
"committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime(
88+
"%Y-%m-%dT%H:%M:%SZ"
89+
),
8790
"commitMessage": "placeholder commit message",
8891
"commitAuthorName": "",
8992
"commitAuthorEmail": "admin@localhost",
@@ -141,11 +144,51 @@ def test_failed_to_fetch_commit_context_record(self, mock_get_commit_context, mo
141144
error_message="integration_failed",
142145
)
143146

147+
@patch("sentry.tasks.commit_context.logger")
144148
@patch(
145149
"sentry.integrations.github.GitHubIntegration.get_commit_context",
146150
return_value={
147151
"commitId": "asdfasdf",
148-
"committedDate": "2023-02-14T11:11Z",
152+
"committedDate": (
153+
datetime.now(tz=datetime_timezone.utc) - timedelta(days=370)
154+
).strftime("%Y-%m-%dT%H:%M:%SZ"),
155+
"commitMessage": "placeholder commit message",
156+
"commitAuthorName": "",
157+
"commitAuthorEmail": "admin@localhost",
158+
},
159+
)
160+
def test_found_commit_is_too_old(self, mock_get_commit_context, mock_logger):
161+
with self.tasks():
162+
assert not GroupOwner.objects.filter(group=self.event.group).exists()
163+
event_frames = get_frame_paths(self.event)
164+
process_commit_context(
165+
event_id=self.event.event_id,
166+
event_platform=self.event.platform,
167+
event_frames=event_frames,
168+
group_id=self.event.group_id,
169+
project_id=self.event.project_id,
170+
)
171+
172+
assert mock_logger.info.call_count == 1
173+
mock_logger.info.assert_called_with(
174+
"process_commit_context.find_commit_context",
175+
extra={
176+
"event": self.event.event_id,
177+
"group": self.event.group_id,
178+
"organization": self.event.group.project.organization_id,
179+
"reason": "could_not_fetch_commit_context",
180+
"code_mappings_count": 1,
181+
"fallback": True,
182+
},
183+
)
184+
185+
@patch(
186+
"sentry.integrations.github.GitHubIntegration.get_commit_context",
187+
return_value={
188+
"commitId": "asdfasdf",
189+
"committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime(
190+
"%Y-%m-%dT%H:%M:%SZ"
191+
),
149192
"commitMessage": "placeholder commit message",
150193
"commitAuthorName": "",
151194
"commitAuthorEmail": "admin@localhost",
@@ -170,7 +213,9 @@ def test_no_matching_commit_in_db(self, mock_get_commit_context):
170213
"sentry.integrations.github.GitHubIntegration.get_commit_context",
171214
return_value={
172215
"commitId": "asdfwreqr",
173-
"committedDate": "2023-02-14T11:11Z",
216+
"committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime(
217+
"%Y-%m-%dT%H:%M:%SZ"
218+
),
174219
"commitMessage": "placeholder commit message",
175220
"commitAuthorName": "",
176221
"commitAuthorEmail": "admin@localhost",
@@ -255,7 +300,9 @@ def test_no_inapp_frame_in_stacktrace(self, mock_process_suspect_commits):
255300
"sentry.integrations.github.GitHubIntegration.get_commit_context",
256301
return_value={
257302
"commitId": "somekey",
258-
"committedDate": "2023-02-14T11:11Z",
303+
"committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime(
304+
"%Y-%m-%dT%H:%M:%SZ"
305+
),
259306
"commitMessage": "placeholder commit message",
260307
"commitAuthorName": "",
261308
"commitAuthorEmail": "[email protected]",
@@ -296,7 +343,9 @@ def test_commit_author_not_in_sentry(self, mock_get_commit_context):
296343
"sentry.integrations.github.GitHubIntegration.get_commit_context",
297344
return_value={
298345
"commitId": "somekey",
299-
"committedDate": "2023-02-14T11:11Z",
346+
"committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime(
347+
"%Y-%m-%dT%H:%M:%SZ"
348+
),
300349
"commitMessage": "placeholder commit message",
301350
"commitAuthorName": "",
302351
"commitAuthorEmail": "[email protected]",
@@ -337,7 +386,9 @@ def test_commit_author_no_user(self, mock_get_commit_context, mock_get_users_for
337386
"sentry.integrations.github.GitHubIntegration.get_commit_context",
338387
return_value={
339388
"commitId": "somekey",
340-
"committedDate": "2023-02-14T11:11Z",
389+
"committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime(
390+
"%Y-%m-%dT%H:%M:%SZ"
391+
),
341392
"commitMessage": "placeholder commit message",
342393
"commitAuthorName": "",
343394
"commitAuthorEmail": "[email protected]",
@@ -423,7 +474,9 @@ def after_return(self, status, retval, task_id, args, kwargs, einfo):
423474
Mock(
424475
return_value={
425476
"commitId": "asdfwreqr",
426-
"committedDate": "2023-02-14T11:11Z",
477+
"committedDate": (datetime.now(tz=datetime_timezone.utc) - timedelta(days=7)).strftime(
478+
"%Y-%m-%dT%H:%M:%SZ"
479+
),
427480
"commitMessage": "placeholder commit message",
428481
"commitAuthorName": "",
429482
"commitAuthorEmail": "admin@localhost",

tests/sentry/tasks/test_post_process.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,9 @@ def test_issue_owners_should_ratelimit(self, logger):
13031303
class ProcessCommitsTestMixin(BasePostProgressGroupMixin):
13041304
github_blame_return_value = {
13051305
"commitId": "asdfwreqr",
1306-
"committedDate": "",
1306+
"committedDate": (datetime.now(timezone.utc) - timedelta(days=2)).strftime(
1307+
"%Y-%m-%dT%H:%M:%SZ"
1308+
),
13071309
"commitMessage": "placeholder commit message",
13081310
"commitAuthorName": "",
13091311
"commitAuthorEmail": "admin@localhost",

0 commit comments

Comments
 (0)