Browse Source

feat(crons): Send assignee to issue platform (#69160)

Issue platform now supports sending assignee, so we can start sending it
when we create an occurrence.
Dan Fuller 10 months ago
parent
commit
5b5390f944

+ 7 - 0
src/sentry/monitors/logic/mark_failed.py

@@ -251,6 +251,12 @@ def create_issue_platform_occurrence(
     if last_successful_checkin:
         last_successful_checkin_timestamp = last_successful_checkin.date_added.isoformat()
 
+    assignee = None
+    # TODO: Remove org fetch after we remove feature flag
+    organization = Organization.objects.get_from_cache(id=monitor_env.monitor.organization_id)
+    if features.has("organizations:crons-ownership", organization):
+        assignee = monitor_env.monitor.owner_actor
+
     occurrence = IssueOccurrence(
         id=uuid.uuid4().hex,
         resource_id=None,
@@ -275,6 +281,7 @@ def create_issue_platform_occurrence(
         culprit=occurrence_data["reason"],
         detection_time=current_timestamp,
         level=occurrence_data["level"],
+        assignee=assignee,
     )
 
     if failed_checkin.trace_id:

+ 59 - 0
tests/sentry/monitors/logic/test_mark_failed.py

@@ -10,6 +10,9 @@ from sentry.issues.grouptype import (
     MonitorCheckInMissed,
     MonitorCheckInTimeout,
 )
+from sentry.issues.ingest import process_occurrence_data
+from sentry.models.groupassignee import GroupAssignee
+from sentry.models.grouphash import GroupHash
 from sentry.monitors.constants import SUBTITLE_DATETIME_FORMAT
 from sentry.monitors.logic.mark_failed import mark_failed
 from sentry.monitors.models import (
@@ -898,3 +901,59 @@ class MarkFailedTestCase(TestCase):
 
         assert len(mock_produce_occurrence_to_kafka.mock_calls) == 0
         assert monitor_environment.active_incident is not None
+
+    @with_feature("organizations:issue-platform")
+    @with_feature("organizations:crons-ownership")
+    def test_mark_failed_issue_assignment(self):
+        monitor = Monitor.objects.create(
+            name="test monitor",
+            organization_id=self.organization.id,
+            project_id=self.project.id,
+            type=MonitorType.CRON_JOB,
+            config={
+                "schedule": [1, "month"],
+                "schedule_type": ScheduleType.INTERVAL,
+                "max_runtime": None,
+                "checkin_margin": None,
+            },
+            owner_user_id=self.user.id,
+        )
+        monitor_environment = MonitorEnvironment.objects.create(
+            monitor=monitor,
+            environment_id=self.environment.id,
+            status=MonitorStatus.OK,
+        )
+
+        MonitorCheckIn.objects.create(
+            monitor=monitor,
+            monitor_environment=monitor_environment,
+            project_id=self.project.id,
+            status=CheckInStatus.OK,
+        )
+
+        checkin = MonitorCheckIn.objects.create(
+            monitor=monitor,
+            monitor_environment=monitor_environment,
+            project_id=self.project.id,
+            status=CheckInStatus.IN_PROGRESS,
+        )
+        mark_failed(checkin, ts=checkin.date_added)
+
+        # failure has hit threshold, monitor should be in a failed state
+        monitor_environment = MonitorEnvironment.objects.get(id=monitor_environment.id)
+        assert monitor_environment.status == MonitorStatus.ERROR
+
+        # check that an incident has been created correctly
+        monitor_incidents = MonitorIncident.objects.filter(monitor_environment=monitor_environment)
+        assert len(monitor_incidents) == 1
+        monitor_incident = monitor_incidents.first()
+        assert monitor_incident.starting_checkin == checkin
+        assert monitor_incident.starting_timestamp == checkin.date_added
+        assert monitor_incident.grouphash == monitor_environment.active_incident.grouphash
+        occurrence_data = {"fingerprint": [monitor_environment.active_incident.grouphash]}
+        process_occurrence_data(occurrence_data)
+        issue_platform_hash = occurrence_data["fingerprint"][0]
+
+        grouphash = GroupHash.objects.get(hash=issue_platform_hash)
+        group_assignee = GroupAssignee.objects.get(group_id=grouphash.group_id)
+        assert group_assignee.user_id == monitor.owner_user_id