-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
base.py
115 lines (97 loc) · 3.71 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from __future__ import annotations
import abc
from django import forms
from sentry import analytics
from sentry.eventstore.models import GroupEvent
from sentry.integrations.services.integration import (
RpcIntegration,
RpcOrganizationIntegration,
integration_service,
)
from sentry.models.organization import OrganizationStatus
from sentry.models.rule import Rule
from sentry.rules.actions import EventAction
INTEGRATION_KEY = "integration"
class IntegrationEventAction(EventAction, abc.ABC):
"""Intermediate abstract class to help DRY some event actions code."""
@property
@abc.abstractmethod
def prompt(self) -> str:
pass
@property
@abc.abstractmethod
def provider(self) -> str:
pass
@property
@abc.abstractmethod
def integration_key(self) -> str:
pass
def is_enabled(self) -> bool:
enabled: bool = bool(self.get_integrations())
return enabled
def get_integration_name(self) -> str:
"""Get the integration's name for the label."""
integration = self.get_integration()
if not integration:
return "[removed]"
_name: str = integration.name
return _name
def get_integrations(self) -> list[RpcIntegration]:
return integration_service.get_integrations(
organization_id=self.project.organization_id,
status=OrganizationStatus.ACTIVE,
org_integration_status=OrganizationStatus.ACTIVE,
providers=[self.provider],
)
def get_integration_id(self) -> int:
integration_id: str | None = self.get_option(self.integration_key)
if integration_id:
return int(integration_id)
return 0
def get_integration(self) -> RpcIntegration | None:
"""
Uses the required class variables `provider` and `integration_key` with
RuleBase.get_option to get the integration object from DB.
"""
for integration in integration_service.get_integrations(
organization_id=self.project.organization_id,
status=OrganizationStatus.ACTIVE,
org_integration_status=OrganizationStatus.ACTIVE,
providers=[self.provider],
):
if integration.id == self.get_integration_id():
return integration
return None
def get_organization_integration(self) -> RpcOrganizationIntegration | None:
return integration_service.get_organization_integration(
integration_id=self.get_integration_id(), organization_id=self.project.organization_id
)
def get_form_instance(self) -> forms.Form:
return self.form_cls(self.data, integrations=self.get_integrations())
def record_notification_sent(
self,
event: GroupEvent,
external_id: str,
rule: Rule | None = None,
notification_uuid: str | None = None,
) -> None:
# Currently these actions can only be triggered by issue alerts
analytics.record(
f"integrations.{self.provider}.notification_sent",
category="issue_alert",
organization_id=event.organization.id,
project_id=event.project_id,
group_id=event.group_id,
notification_uuid=notification_uuid if notification_uuid else "",
alert_id=rule.id if rule else None,
)
analytics.record(
"alert.sent",
provider=self.provider,
alert_id=rule.id if rule else "",
alert_type="issue_alert",
organization_id=event.organization.id,
project_id=event.project_id,
external_id=external_id,
notification_uuid=notification_uuid if notification_uuid else "",
)