|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +from django.utils import timezone |
| 4 | + |
| 5 | +from sentry.event_manager import GroupInfo |
| 6 | +from sentry.incidents.grouptype import MetricIssue |
| 7 | +from sentry.issues.grouptype import FeedbackGroup |
| 8 | +from sentry.issues.ingest import save_issue_occurrence |
| 9 | +from sentry.issues.issue_occurrence import IssueOccurrence, IssueOccurrenceData |
| 10 | +from sentry.models.group import GroupStatus |
| 11 | +from sentry.models.groupopenperiod import GroupOpenPeriod |
| 12 | +from sentry.testutils.cases import TestCase |
| 13 | +from sentry.testutils.helpers import with_feature |
| 14 | +from sentry.workflow_engine.models import IncidentGroupOpenPeriod |
| 15 | + |
| 16 | + |
| 17 | +class IncidentGroupOpenPeriodIntegrationTest(TestCase): |
| 18 | + def setUp(self) -> None: |
| 19 | + super().setUp() |
| 20 | + self.organization = self.create_organization() |
| 21 | + self.project = self.create_project(organization=self.organization) |
| 22 | + self.alert_rule = self.create_alert_rule( |
| 23 | + organization=self.organization, |
| 24 | + projects=[self.project], |
| 25 | + name="Test Alert Rule", |
| 26 | + ) |
| 27 | + self.detector = self.create_detector( |
| 28 | + project=self.project, |
| 29 | + name="Test Detector", |
| 30 | + ) |
| 31 | + self.alert_rule_detector = self.create_alert_rule_detector( |
| 32 | + alert_rule_id=self.alert_rule.id, |
| 33 | + detector=self.detector, |
| 34 | + ) |
| 35 | + |
| 36 | + def save_issue_occurrence( |
| 37 | + self, group_type: int = MetricIssue.type_id |
| 38 | + ) -> tuple[IssueOccurrence, GroupInfo]: |
| 39 | + event = self.store_event( |
| 40 | + data={"timestamp": timezone.now().isoformat()}, project_id=self.project.id |
| 41 | + ) |
| 42 | + |
| 43 | + occurrence_data: IssueOccurrenceData = { |
| 44 | + "id": "1", |
| 45 | + "project_id": self.project.id, |
| 46 | + "event_id": event.event_id, |
| 47 | + "fingerprint": ["test-fingerprint"], |
| 48 | + "issue_title": "Test Issue", |
| 49 | + "subtitle": "Test Subtitle", |
| 50 | + "resource_id": None, |
| 51 | + "evidence_data": {"detector_id": self.detector.id}, |
| 52 | + "evidence_display": [ |
| 53 | + {"name": "Test Evidence", "value": "Test Value", "important": True} |
| 54 | + ], |
| 55 | + "type": group_type, |
| 56 | + "detection_time": timezone.now().timestamp(), |
| 57 | + "level": "error", |
| 58 | + "culprit": "test-culprit", |
| 59 | + } |
| 60 | + |
| 61 | + with patch("sentry.issues.ingest.eventstream") as _: |
| 62 | + occurrence, group_info = save_issue_occurrence(occurrence_data, event) |
| 63 | + |
| 64 | + assert group_info is not None |
| 65 | + assert group_info.group.type == group_type |
| 66 | + return occurrence, group_info |
| 67 | + |
| 68 | + @with_feature("organizations:issue-open-periods") |
| 69 | + def test_save_issue_occurrence_creates_relationship_when_incident_exists(self) -> None: |
| 70 | + """Test that save_issue_occurrence creates the relationship when incident exists""" |
| 71 | + incident = self.create_incident( |
| 72 | + organization=self.organization, |
| 73 | + title="Test Incident", |
| 74 | + date_started=timezone.now(), |
| 75 | + alert_rule=self.alert_rule, |
| 76 | + ) |
| 77 | + |
| 78 | + _, group_info = self.save_issue_occurrence() |
| 79 | + group = group_info.group |
| 80 | + assert group is not None |
| 81 | + |
| 82 | + open_period = GroupOpenPeriod.objects.get(group=group) |
| 83 | + item = IncidentGroupOpenPeriod.objects.get(group_open_period=open_period) |
| 84 | + assert item.incident_id == incident.id |
| 85 | + assert item.incident_identifier == incident.identifier |
| 86 | + |
| 87 | + @with_feature("organizations:issue-open-periods") |
| 88 | + def test_save_issue_occurrence_creates_placeholder_when_incident_doesnt_exist(self) -> None: |
| 89 | + """Test that save_issue_occurrence creates placeholder when incident doesn't exist""" |
| 90 | + _, group_info = self.save_issue_occurrence() |
| 91 | + group = group_info.group |
| 92 | + assert group is not None |
| 93 | + |
| 94 | + open_period = GroupOpenPeriod.objects.get(group=group) |
| 95 | + assert open_period.data["pending_incident_detector_id"] == self.detector.id |
| 96 | + |
| 97 | + assert not IncidentGroupOpenPeriod.objects.filter(group_open_period=open_period).exists() |
| 98 | + |
| 99 | + @with_feature("organizations:issue-open-periods") |
| 100 | + def test_save_issue_occurrence_creates_relationship_for_existing_group(self) -> None: |
| 101 | + """Test that save_issue_occurrence creates relationship for existing groups""" |
| 102 | + incident = self.create_incident( |
| 103 | + organization=self.organization, |
| 104 | + title="Test Incident", |
| 105 | + date_started=timezone.now(), |
| 106 | + alert_rule=self.alert_rule, |
| 107 | + ) |
| 108 | + |
| 109 | + _, group_info = self.save_issue_occurrence() |
| 110 | + group = group_info.group |
| 111 | + assert group is not None |
| 112 | + |
| 113 | + assert GroupOpenPeriod.objects.filter(group=group, project=self.project).exists() |
| 114 | + |
| 115 | + group.update(status=GroupStatus.RESOLVED) |
| 116 | + open_period = GroupOpenPeriod.objects.get(group=group, project=self.project) |
| 117 | + open_period.update(date_ended=timezone.now()) |
| 118 | + |
| 119 | + _, group_info = self.save_issue_occurrence() |
| 120 | + group = group_info.group |
| 121 | + assert group is not None |
| 122 | + |
| 123 | + item = IncidentGroupOpenPeriod.objects.get(group_open_period=open_period) |
| 124 | + assert item.incident_id == incident.id |
| 125 | + assert item.incident_identifier == incident.identifier |
| 126 | + |
| 127 | + @with_feature("organizations:issue-open-periods") |
| 128 | + def test_save_issue_occurrence_no_relationship_for_non_metric_issues(self) -> None: |
| 129 | + # Test that save_issue_occurrence doesn't create relationships for non-metric issues |
| 130 | + _, group_info = self.save_issue_occurrence(group_type=FeedbackGroup.type_id) |
| 131 | + |
| 132 | + group = group_info.group |
| 133 | + assert group is not None |
| 134 | + |
| 135 | + open_period = GroupOpenPeriod.objects.get(group=group) |
| 136 | + assert not IncidentGroupOpenPeriod.objects.filter(group_open_period=open_period).exists() |
| 137 | + assert "pending_incident_alert_id" not in open_period.data |
0 commit comments