diff --git a/docs/providers/documentation/pagerduty-provider.mdx b/docs/providers/documentation/pagerduty-provider.mdx index c3037b645..6a246d059 100644 --- a/docs/providers/documentation/pagerduty-provider.mdx +++ b/docs/providers/documentation/pagerduty-provider.mdx @@ -30,6 +30,7 @@ To connect Keep to PagerDuty: - **Routing Key**: Use for event posting via the PagerDuty Events API. - **API Key**: Use for incident creation and management through the PagerDuty Incidents API. +- **Service Id** (Optional): If provided, keep operates within the service's scope. - **OAuth2**: Token management handled automatically by Keep. diff --git a/keep/api/models/alert.py b/keep/api/models/alert.py index dd05673c4..e0e0954ec 100644 --- a/keep/api/models/alert.py +++ b/keep/api/models/alert.py @@ -517,6 +517,7 @@ def from_db_incident(cls, db_incident: "Incident"): assignee=db_incident.assignee, services=db_incident.affected_services or [], rule_fingerprint=db_incident.rule_fingerprint, + fingerprint=db_incident.fingerprint, same_incident_in_the_past_id=db_incident.same_incident_in_the_past_id, merged_into_incident_id=db_incident.merged_into_incident_id, merged_by=db_incident.merged_by, @@ -550,6 +551,7 @@ def to_db_incident(self) -> "Incident": is_predicted=self.is_predicted, is_confirmed=self.is_confirmed, rule_fingerprint=self.rule_fingerprint, + fingerprint=self.fingerprint, same_incident_in_the_past_id=self.same_incident_in_the_past_id, merged_into_incident_id=self.merged_into_incident_id, merged_by=self.merged_by, diff --git a/keep/providers/pagerduty_provider/pagerduty_provider.py b/keep/providers/pagerduty_provider/pagerduty_provider.py index 2118a2a9c..d08fa75d6 100644 --- a/keep/providers/pagerduty_provider/pagerduty_provider.py +++ b/keep/providers/pagerduty_provider/pagerduty_provider.py @@ -29,6 +29,7 @@ from keep.providers.models.provider_config import ProviderConfig, ProviderScope from keep.providers.providers_factory import ProvidersFactory + # Todo: think about splitting in to PagerdutyIncidentsProvider and PagerdutyAlertsProvider # Read this: https://community.pagerduty.com/forum/t/create-incident-using-python/3596/3 @@ -59,6 +60,14 @@ class PagerdutyProviderAuthConfig: }, default="", ) + service_id: str | None = dataclasses.field( + metadata={ + "required": False, + "description": "Service Id (if provided, keep will only operate on this service)", + "sensitive": False, + }, + default=None, + ) class PagerdutyProvider(BaseTopologyProvider, BaseIncidentProvider): @@ -505,7 +514,14 @@ def setup_incident_webhook( "incident.triggered", "incident.unacknowledged", ], - "filter": {"type": "account_reference"}, + "filter": ( + { + "type": "service_reference", + "id": self.authentication_config.service_id, + } + if self.authentication_config.service_id + else {"type": "account_reference"} + ), }, } if webhook_exists: @@ -681,14 +697,17 @@ def __get_all_incidents_or_alerts(self, incident_id: str = None): url += f"/{incident_id}/alerts" include = ["teams", "services"] resource = "alerts" + params = { + "include[]": include, + "offset": offset, + "limit": 100, + } + if not incident_id and self.authentication_config.service_id: + params["service_ids[]"] = [self.authentication_config.service_id] response = requests.get( url=url, headers=self.__get_headers(), - params={ - "include[]": include, - "offset": offset, - "limit": 100, - }, + params=params, ) response.raise_for_status() response = response.json() @@ -699,7 +718,7 @@ def __get_all_incidents_or_alerts(self, incident_id: str = None): paginated_response.extend(response.get(resource, [])) self.logger.info("Fetched incidents or alerts", extra={"offset": offset}) # No more results - if response.get("more", False) == False: + if not response.get("more", False): self.logger.info("No more incidents or alerts") break self.logger.info(