Skip to content

Conversation

@jasonyuezhang
Copy link
Owner

Previously, this serializer took in an open period, fetched the incident via the IGOP lookup table, and serialized the incident. With the incident no longer guaranteed to exist, create a serializer that will populate an incident response using only the open period model.


Copied from getsentry#103131
Original PR: getsentry#103131

@propel-test-bot
Copy link

Replace Incident-dependent incident serializer with GroupOpenPeriod-based implementation

Adds a new implementation of WorkflowEngineIncidentSerializer and its detailed variant that produces an incident-shaped response directly from GroupOpenPeriod records. The previous flow looked up an Incident through IncidentGroupOpenPeriod (IGOP); that dependency has been removed so the API can continue working even when no matching Incident exists. Tests are rewritten accordingly.

Key Changes

• Major rewrite of src/sentry/incidents/endpoints/serializers/workflow_engine_incident.py (~+132 −95):
• • Removes broad ORM chain via WorkflowActionGroupStatus, DataCondition* etc.; now derives detectors via DetectorGroup relationship.
• • Introduces get_incident_status, get_attrs, and a simplified get_open_periods_to_detectors using fewer queries.
• • Builds fake IDs with get_fake_id_from_object_id when no Incident is found.
• • Supports optional activities expansion using GroupOpenPeriodActivity.
• • Adds WorkflowEngineDetailedIncidentSerializer that injects discoverQuery and constructs full response without Incident model.
• Updates unit tests in tests/sentry/incidents/serializers/test_workflow_engine_incident.py to assert against the new payload shape and to set up DetectorGroup fixtures.
• Removes unused imports and legacy serializers (IncidentSerializer, DetailedAlertRuleSerializer, etc.).

Affected Areas

src/sentry/incidents/endpoints/serializers/workflow_engine_incident.py
tests/sentry/incidents/serializers/test_workflow_engine_incident.py

This summary was automatically generated by @propel-code-bot

Comment on lines +43 to +50
def get_incident_status(self, priority: int | None, date_ended: datetime | None) -> int:
if priority is None:
raise ValueError("Priority is required to get an incident status")

if date_ended:
return IncidentStatus.CLOSED.value

return self.priority_to_incident_status[priority]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[CriticalError]

Potential KeyError when priority is not in mapping: The get_incident_status method will raise a KeyError if priority is not HIGH or MEDIUM (e.g., LOW priority). This could cause runtime failures.

Suggested Change
Suggested change
def get_incident_status(self, priority: int | None, date_ended: datetime | None) -> int:
if priority is None:
raise ValueError("Priority is required to get an incident status")
if date_ended:
return IncidentStatus.CLOSED.value
return self.priority_to_incident_status[priority]
def get_incident_status(self, priority: int | None, date_ended: datetime | None) -> int:
if priority is None:
raise ValueError("Priority is required to get an incident status")
if date_ended:
return IncidentStatus.CLOSED.value
return self.priority_to_incident_status.get(priority, IncidentStatus.WARNING.value)

Committable suggestion

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Context for Agents
[**CriticalError**]

Potential KeyError when priority is not in mapping: The `get_incident_status` method will raise a `KeyError` if `priority` is not `HIGH` or `MEDIUM` (e.g., `LOW` priority). This could cause runtime failures.

<details>
<summary>Suggested Change</summary>

```suggestion
    def get_incident_status(self, priority: int | None, date_ended: datetime | None) -> int:
        if priority is None:
            raise ValueError("Priority is required to get an incident status")

        if date_ended:
            return IncidentStatus.CLOSED.value

        return self.priority_to_incident_status.get(priority, IncidentStatus.WARNING.value)
```

⚡ **Committable suggestion**

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

</details>

File: src/sentry/incidents/endpoints/serializers/workflow_engine_incident.py
Line: 50

Comment on lines +126 to +129
open_periods_to_detectors = {}
for group in group_to_open_periods:
for op in group_to_open_periods[group]:
open_periods_to_detectors[op] = groups_to_detectors[group]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[CriticalError]

Potential KeyError when accessing missing group detector: Line 129 accesses groups_to_detectors[group] but if a group doesn't have a corresponding detector in the detector_groups queryset, this will raise a KeyError.

Suggested Change
Suggested change
open_periods_to_detectors = {}
for group in group_to_open_periods:
for op in group_to_open_periods[group]:
open_periods_to_detectors[op] = groups_to_detectors[group]
open_periods_to_detectors = {}
for group in group_to_open_periods:
detector = groups_to_detectors.get(group)
if detector: # Only process groups that have detectors
for op in group_to_open_periods[group]:
open_periods_to_detectors[op] = detector

Committable suggestion

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Context for Agents
[**CriticalError**]

Potential KeyError when accessing missing group detector: Line 129 accesses `groups_to_detectors[group]` but if a group doesn't have a corresponding detector in the `detector_groups` queryset, this will raise a `KeyError`.

<details>
<summary>Suggested Change</summary>

```suggestion
        open_periods_to_detectors = {}
        for group in group_to_open_periods:
            detector = groups_to_detectors.get(group)
            if detector:  # Only process groups that have detectors
                for op in group_to_open_periods[group]:
                    open_periods_to_detectors[op] = detector
```

⚡ **Committable suggestion**

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

</details>

File: src/sentry/incidents/endpoints/serializers/workflow_engine_incident.py
Line: 129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants