|
@@ -1,10 +1,11 @@
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import logging
|
|
|
-from typing import Sequence, Tuple
|
|
|
+from typing import Sequence
|
|
|
|
|
|
from sentry.integrations.pagerduty.actions import PagerDutyNotifyServiceForm
|
|
|
from sentry.integrations.pagerduty.client import PagerDutyProxyClient
|
|
|
+from sentry.models import PagerDutyService
|
|
|
from sentry.rules.actions import IntegrationEventAction
|
|
|
from sentry.shared_integrations.client.proxy import infer_org_integration
|
|
|
from sentry.shared_integrations.exceptions import ApiError
|
|
@@ -31,13 +32,7 @@ class PagerDutyNotifyServiceAction(IntegrationEventAction):
|
|
|
}
|
|
|
|
|
|
def _get_service(self):
|
|
|
- oi = self.get_organization_integration()
|
|
|
- if not oi:
|
|
|
- return None
|
|
|
- for pds in oi.config.get("pagerduty_services", []):
|
|
|
- if pds["id"] == self.get_option("service"):
|
|
|
- return pds
|
|
|
- return None
|
|
|
+ return PagerDutyService.objects.get(id=self.get_option("service"))
|
|
|
|
|
|
def after(self, event, state):
|
|
|
integration = self.get_integration()
|
|
@@ -46,8 +41,9 @@ class PagerDutyNotifyServiceAction(IntegrationEventAction):
|
|
|
# integration removed but rule still exists
|
|
|
return
|
|
|
|
|
|
- service = self._get_service()
|
|
|
- if not service:
|
|
|
+ try:
|
|
|
+ service = self._get_service()
|
|
|
+ except PagerDutyService.DoesNotExist:
|
|
|
logger.exception("The PagerDuty does not exist anymore while integration does.")
|
|
|
return
|
|
|
|
|
@@ -58,11 +54,11 @@ class PagerDutyNotifyServiceAction(IntegrationEventAction):
|
|
|
org_integration_id = org_integration.id
|
|
|
else:
|
|
|
org_integration_id = infer_org_integration(
|
|
|
- integration_id=service["integration_id"], ctx_logger=logger
|
|
|
+ integration_id=service.integration_id, ctx_logger=logger
|
|
|
)
|
|
|
client = PagerDutyProxyClient(
|
|
|
org_integration_id=org_integration_id,
|
|
|
- integration_key=service["integration_key"],
|
|
|
+ integration_key=service.integration_key,
|
|
|
)
|
|
|
try:
|
|
|
resp = client.send_trigger(event)
|
|
@@ -71,8 +67,8 @@ class PagerDutyNotifyServiceAction(IntegrationEventAction):
|
|
|
"rule.fail.pagerduty_trigger",
|
|
|
extra={
|
|
|
"error": str(e),
|
|
|
- "service_name": service["service_name"],
|
|
|
- "service_id": service["id"],
|
|
|
+ "service_name": service.service_name,
|
|
|
+ "service_id": service.id,
|
|
|
"project_id": event.project_id,
|
|
|
"event_id": event.event_id,
|
|
|
},
|
|
@@ -87,31 +83,33 @@ class PagerDutyNotifyServiceAction(IntegrationEventAction):
|
|
|
"status_code": resp.status_code,
|
|
|
"project_id": event.project_id,
|
|
|
"event_id": event.event_id,
|
|
|
- "service_name": service["service_name"],
|
|
|
- "service_id": service["id"],
|
|
|
+ "service_name": service.service_name,
|
|
|
+ "service_id": service.id,
|
|
|
},
|
|
|
)
|
|
|
|
|
|
- key = f"pagerduty:{integration.id}:{service['id']}"
|
|
|
+ key = f"pagerduty:{integration.id}:{service.id}"
|
|
|
yield self.future(send_notification, key=key)
|
|
|
|
|
|
- def get_services(self) -> Sequence[Tuple[int, str]]:
|
|
|
+ def get_services(self) -> Sequence[PagerDutyService]:
|
|
|
from sentry.services.hybrid_cloud.integration import integration_service
|
|
|
|
|
|
organization_integrations = integration_service.get_organization_integrations(
|
|
|
providers=[self.provider], organization_id=self.project.organization_id
|
|
|
)
|
|
|
+ integration_ids = [oi.integration_id for oi in organization_integrations]
|
|
|
+
|
|
|
return [
|
|
|
- (v["id"], v["service_name"])
|
|
|
- for oi in organization_integrations
|
|
|
- for v in oi.config.get("pagerduty_services", [])
|
|
|
+ service
|
|
|
+ for service in PagerDutyService.objects.filter(
|
|
|
+ organization_id=self.project.organization_id, integration_id__in=integration_ids
|
|
|
+ ).values_list("id", "service_name")
|
|
|
]
|
|
|
|
|
|
def render_label(self):
|
|
|
- s = self._get_service()
|
|
|
- if s:
|
|
|
- service_name = s["service_name"]
|
|
|
- else:
|
|
|
+ try:
|
|
|
+ service_name = self._get_service().service_name
|
|
|
+ except PagerDutyService.DoesNotExist:
|
|
|
service_name = "[removed]"
|
|
|
|
|
|
return self.label.format(account=self.get_integration_name(), service=service_name)
|