Skip to content

Commit dbd9fd3

Browse files
committed
♻️ ref: use new interaction event class
1 parent c248ca7 commit dbd9fd3

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

src/sentry/integrations/source_code_management/commit_context.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
from sentry.integrations.models.repository_project_path_config import RepositoryProjectPathConfig
1616
from sentry.integrations.source_code_management.metrics import (
1717
CommitContextHaltReason,
18-
SCMIntegrationInteractionEvent,
18+
CommitContextIntegrationInteractionEvent,
1919
SCMIntegrationInteractionType,
2020
)
2121
from sentry.locks import locks
2222
from sentry.models.commit import Commit
2323
from sentry.models.group import Group
2424
from sentry.models.groupowner import GroupOwner
2525
from sentry.models.options.organization_option import OrganizationOption
26+
from sentry.models.organization import Organization
2627
from sentry.models.project import Project
2728
from sentry.models.pullrequest import (
2829
CommentType,
@@ -92,10 +93,23 @@ def integration_name(self) -> str:
9293
def get_client(self) -> CommitContextClient:
9394
raise NotImplementedError
9495

95-
def record_event(self, event: SCMIntegrationInteractionType):
96-
return SCMIntegrationInteractionEvent(
96+
def record_event(
97+
self,
98+
event: SCMIntegrationInteractionType,
99+
organization: Organization | None = None,
100+
project: Project | None = None,
101+
commit: Commit | None = None,
102+
repository: Repository | None = None,
103+
pull_request: PullRequest | None = None,
104+
):
105+
return CommitContextIntegrationInteractionEvent(
97106
interaction_type=event,
98107
provider_key=self.integration_name,
108+
organization=organization,
109+
project=project,
110+
commit=commit,
111+
repository=repository,
112+
pull_request=pull_request,
99113
)
100114

101115
def get_blame_for_files(
@@ -142,15 +156,11 @@ def queue_comment_task_if_needed(
142156
group_id: int,
143157
) -> None:
144158
with self.record_event(
145-
SCMIntegrationInteractionType.QUEUE_COMMENT_TASK
159+
SCMIntegrationInteractionType.QUEUE_COMMENT_TASK,
160+
organization=project.organization,
161+
project=project,
162+
commit=commit,
146163
).capture() as lifecycle:
147-
lifecycle.add_extras(
148-
{
149-
"project_id": project.id,
150-
"organization_id": project.organization_id,
151-
"commit_id": commit.id,
152-
}
153-
)
154164
if not OrganizationOption.objects.get_value(
155165
organization=project.organization,
156166
key="sentry:github_pr_bot",
@@ -301,17 +311,9 @@ def create_or_update_comment(
301311
else SCMIntegrationInteractionType.UPDATE_COMMENT
302312
)
303313

304-
with self.record_event(interaction_type).capture() as lifecycle:
305-
lifecycle.add_extras(
306-
{
307-
"repository_id": repo.id,
308-
"organization_id": repo.organization_id,
309-
"pull_request_id": pullrequest_id,
310-
"comment_type": comment_type,
311-
"language": language,
312-
}
313-
)
314-
314+
with self.record_event(
315+
interaction_type, repository=repo, pull_request_id=pullrequest_id
316+
).capture():
315317
if pr_comment is None:
316318
resp = client.create_comment(
317319
repo=repo.name,

src/sentry/integrations/source_code_management/metrics.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
from sentry.integrations.models.organization_integration import OrganizationIntegration
99
from sentry.integrations.services.integration import RpcOrganizationIntegration
1010
from sentry.integrations.utils.metrics import IntegrationEventLifecycleMetric
11+
from sentry.models.commit import Commit
1112
from sentry.models.organization import Organization
13+
from sentry.models.project import Project
14+
from sentry.models.repository import Repository
1215
from sentry.organizations.services.organization import RpcOrganization
1316

1417

@@ -76,6 +79,28 @@ class LinkAllReposHaltReason(StrEnum):
7679
REPOSITORY_NOT_CREATED = "repository_not_created"
7780

7881

82+
@dataclass
83+
class CommitContextIntegrationInteractionEvent(SCMIntegrationInteractionEvent):
84+
"""
85+
An instance to be recorded of a CommitContextIntegration feature call.
86+
"""
87+
88+
project: Project | None = None
89+
commit: Commit | None = None
90+
repository: Repository | None = None
91+
pull_request_id: int | None = None
92+
93+
def get_extras(self) -> Mapping[str, Any]:
94+
parent_extras = super().get_extras()
95+
return {
96+
**parent_extras,
97+
"project_id": (self.project.id if self.project else None),
98+
"commit_id": (self.commit.id if self.commit else None),
99+
"repository_id": (self.repository.id if self.repository else None),
100+
"pull_request_id": self.pull_request_id,
101+
}
102+
103+
79104
class CommitContextHaltReason(StrEnum):
80105
"""Common reasons why a commit context integration may halt without success/failure."""
81106

0 commit comments

Comments
 (0)