|
@@ -1,3 +1,5 @@
|
|
|
+from __future__ import annotations
|
|
|
+
|
|
|
import logging
|
|
|
|
|
|
from django.http import Http404
|
|
@@ -5,6 +7,7 @@ from django.http import Http404
|
|
|
from sentry.incidents.models import AlertRuleTriggerAction, Incident, IncidentStatus
|
|
|
from sentry.integrations.metric_alerts import incident_attachment_info
|
|
|
from sentry.models import PagerDutyService
|
|
|
+from sentry.models.integrations.pagerduty_service import PagerDutyServiceDict
|
|
|
from sentry.services.hybrid_cloud.integration import integration_service
|
|
|
from sentry.shared_integrations.client.proxy import infer_org_integration
|
|
|
from sentry.shared_integrations.exceptions import ApiError
|
|
@@ -51,9 +54,27 @@ def send_incident_alert_notification(
|
|
|
new_status: IncidentStatus,
|
|
|
) -> None:
|
|
|
integration_id = action.integration_id
|
|
|
- try:
|
|
|
- service = PagerDutyService.objects.get(id=action.target_identifier)
|
|
|
- except PagerDutyService.DoesNotExist:
|
|
|
+ organization_id = incident.organization_id
|
|
|
+
|
|
|
+ service: PagerDutyServiceDict | None = None
|
|
|
+ org_integration = integration_service.get_organization_integration(
|
|
|
+ integration_id=integration_id,
|
|
|
+ organization_id=organization_id,
|
|
|
+ )
|
|
|
+ if org_integration is None:
|
|
|
+ org_integration_id = infer_org_integration(integration_id=integration_id, ctx_logger=logger)
|
|
|
+ org_integrations = integration_service.get_organization_integrations(
|
|
|
+ org_integration_ids=[org_integration_id]
|
|
|
+ )
|
|
|
+ if org_integrations:
|
|
|
+ org_integration = org_integrations[0]
|
|
|
+ else:
|
|
|
+ org_integration_id = org_integration.id
|
|
|
+
|
|
|
+ if org_integration and action.target_identifier:
|
|
|
+ service = PagerDutyService.find_service(org_integration.config, action.target_identifier)
|
|
|
+
|
|
|
+ if service is None:
|
|
|
# service has been removed after rule creation
|
|
|
logger.info(
|
|
|
"fetch.fail.pagerduty_metric_alert",
|
|
@@ -64,17 +85,8 @@ def send_incident_alert_notification(
|
|
|
},
|
|
|
)
|
|
|
raise Http404
|
|
|
- org_integration = integration_service.get_organization_integration(
|
|
|
- integration_id=service.integration_id,
|
|
|
- organization_id=service.organization_id,
|
|
|
- )
|
|
|
- if org_integration is None:
|
|
|
- org_integration_id = infer_org_integration(
|
|
|
- integration_id=service.integration_id, ctx_logger=logger
|
|
|
- )
|
|
|
- else:
|
|
|
- org_integration_id = org_integration.id
|
|
|
- integration_key = service.integration_key
|
|
|
+
|
|
|
+ integration_key = service["integration_key"]
|
|
|
client = PagerDutyProxyClient(
|
|
|
org_integration_id=org_integration_id, integration_key=integration_key
|
|
|
)
|
|
@@ -86,8 +98,8 @@ def send_incident_alert_notification(
|
|
|
"rule.fail.pagerduty_metric_alert",
|
|
|
extra={
|
|
|
"error": str(e),
|
|
|
- "service_name": service.service_name,
|
|
|
- "service_id": service.id,
|
|
|
+ "service_name": service["service_name"],
|
|
|
+ "service_id": service["id"],
|
|
|
"integration_id": integration_id,
|
|
|
},
|
|
|
)
|