diff --git a/tests/sentry/tasks/test_post_process.py b/tests/sentry/tasks/test_post_process.py index b4ef4075427112..242193586b025a 100644 --- a/tests/sentry/tasks/test_post_process.py +++ b/tests/sentry/tasks/test_post_process.py @@ -977,6 +977,86 @@ def test_owner_assignment_existing_owners(self) -> None: (o.user_id, o.team_id) for o in owners } + def test_owner_assignment_existing_assignee_preserved(self): + """ + Tests that if a group already has an assignee, post-processing won't reassign it + even if ownership rules change in the interim. + """ + other_team = self.create_team() + ProjectTeam.objects.create(team=other_team, project=self.project) + + rules = [ + Rule(Matcher("path", "src/*"), [Owner("team", self.team.slug)]), + Rule(Matcher("path", "src/app/*"), [Owner("team", other_team.slug)]), + ] + self.prj_ownership = ProjectOwnership.objects.create( + project_id=self.project.id, + schema=dump_schema(rules), + fallthrough=True, + auto_assignment=True, + ) + + event = self.create_event( + data={ + "message": "oh no", + "platform": "python", + "stacktrace": {"frames": [{"filename": "src/app/example.py"}]}, + }, + project_id=self.project.id, + ) + + # No assignee should exist prior to post processing + assert not event.group.assignee_set.exists() + + # First post-processing - should assign to other_team (last matching rule) + self.call_post_process_group( + is_new=True, + is_regression=False, + is_new_group_environment=True, + event=event, + ) + + assignee = event.group.assignee_set.first() + assert assignee.team == other_team + + new_rules = [ + Rule(Matcher("path", "src/app/*"), [Owner("team", other_team.slug)]), + Rule(Matcher("path", "src/*"), [Owner("team", self.team.slug)]), + ] + self.prj_ownership.schema = dump_schema(new_rules) + self.prj_ownership.save() + + # Run post-processing again - assignee should NOT change + self.call_post_process_group( + is_new=False, + is_regression=False, + is_new_group_environment=False, + event=event, + ) + + assignee = event.group.assignee_set.first() + assert assignee.team == other_team + + # If we had a completely new group, it would get assigned to self.team (new last matching rule) + fresh_event = self.create_event( + data={ + "message": "fresh event", + "platform": "python", + "stacktrace": {"frames": [{"filename": "src/app/fresh.py"}]}, + }, + project_id=self.project.id, + ) + + self.call_post_process_group( + is_new=True, + is_regression=False, + is_new_group_environment=True, + event=fresh_event, + ) + + fresh_assignee = fresh_event.group.assignee_set.first() + assert fresh_assignee.team == self.team + def test_owner_assignment_assign_user(self) -> None: self.make_ownership() event = self.create_event(