Skip to content

Commit 4d0e90a

Browse files
committed
♻️ ref: lifecycle for get_blame_for_files
1 parent 6f8cfaf commit 4d0e90a

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/sentry/integrations/source_code_management/commit_context.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
from sentry import analytics
1414
from sentry.auth.exceptions import IdentityNotValid
1515
from sentry.integrations.models.repository_project_path_config import RepositoryProjectPathConfig
16+
from sentry.integrations.source_code_management.metrics import (
17+
SCMIntegrationInteractionEvent,
18+
SCMIntegrationInteractionType,
19+
)
1620
from sentry.locks import locks
1721
from sentry.models.commit import Commit
1822
from sentry.models.group import Group
@@ -26,6 +30,7 @@
2630
PullRequestCommit,
2731
)
2832
from sentry.models.repository import Repository
33+
from sentry.shared_integrations.exceptions import ApiRateLimitedError
2934
from sentry.users.models.identity import Identity
3035
from sentry.utils import metrics
3136
from sentry.utils.cache import cache
@@ -86,6 +91,12 @@ def integration_name(self) -> str:
8691
def get_client(self) -> CommitContextClient:
8792
raise NotImplementedError
8893

94+
def record_event(self, event: SCMIntegrationInteractionType):
95+
return SCMIntegrationInteractionEvent(
96+
interaction_type=event,
97+
provider_key=self.integration_name,
98+
)
99+
89100
def get_blame_for_files(
90101
self, files: Sequence[SourceLineInfo], extra: Mapping[str, Any]
91102
) -> list[FileBlameInfo]:
@@ -94,16 +105,25 @@ def get_blame_for_files(
94105
95106
files: list of FileBlameInfo objects
96107
"""
97-
try:
98-
client = self.get_client()
99-
except Identity.DoesNotExist:
100-
return []
101-
try:
102-
response = client.get_blame_for_files(files, extra)
103-
except IdentityNotValid:
104-
return []
105-
106-
return response
108+
with self.record_event(
109+
SCMIntegrationInteractionType.GET_BLAME_FOR_FILES
110+
).capture() as lifecycle:
111+
try:
112+
client = self.get_client()
113+
except Identity.DoesNotExist:
114+
sentry_sdk.capture_exception()
115+
return []
116+
try:
117+
response = client.get_blame_for_files(files, extra)
118+
except IdentityNotValid:
119+
sentry_sdk.capture_exception()
120+
return []
121+
# Swallow rate limited errors so we don't log them as exceptions
122+
except ApiRateLimitedError as e:
123+
sentry_sdk.capture_exception(e)
124+
lifecycle.record_halt(e)
125+
return []
126+
return response
107127

108128
def get_commit_context_all_frames(
109129
self, files: Sequence[SourceLineInfo], extra: Mapping[str, Any]

src/sentry/integrations/source_code_management/metrics.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ class SCMIntegrationInteractionType(Enum):
2626
GET_REPOSITORY_CHOICES = "GET_REPOSITORY_CHOICES"
2727

2828
# CommitContextIntegration
29+
GET_BLAME_FOR_FILES = "GET_BLAME_FOR_FILES"
2930
CREATE_COMMENT = "CREATE_COMMENT"
3031
UPDATE_COMMENT = "UPDATE_COMMENT"
32+
QUEUE_COMMENT_TASK = "QUEUE_COMMENT_TASK"
3133

3234
# Tasks
3335
LINK_ALL_REPOS = "LINK_ALL_REPOS"

0 commit comments

Comments
 (0)