diff --git a/src/sentry/api/endpoints/project_rule_details.py b/src/sentry/api/endpoints/project_rule_details.py index 0228abdc31b058..5a1bec67304dc8 100644 --- a/src/sentry/api/endpoints/project_rule_details.py +++ b/src/sentry/api/endpoints/project_rule_details.py @@ -245,6 +245,7 @@ def put(self, request: Request, project, rule) -> Response: "name": data["name"], "environment": data.get("environment"), "project": project, + "project_id": project.id, "action_match": data["actionMatch"], "filter_match": data.get("filterMatch"), "conditions": conditions, diff --git a/src/sentry/api/endpoints/project_rules.py b/src/sentry/api/endpoints/project_rules.py index 37aa667cf3740b..a3daa57aff189e 100644 --- a/src/sentry/api/endpoints/project_rules.py +++ b/src/sentry/api/endpoints/project_rules.py @@ -800,6 +800,7 @@ def post(self, request: Request, project) -> Response: "name": data["name"], "environment": data.get("environment"), "project": project, + "project_id": project.id, "action_match": data["actionMatch"], "filter_match": data.get("filterMatch"), "conditions": conditions, diff --git a/src/sentry/integrations/slack/tasks/find_channel_id_for_rule.py b/src/sentry/integrations/slack/tasks/find_channel_id_for_rule.py index 5c5effb2df8692..900bb7ff100587 100644 --- a/src/sentry/integrations/slack/tasks/find_channel_id_for_rule.py +++ b/src/sentry/integrations/slack/tasks/find_channel_id_for_rule.py @@ -32,20 +32,23 @@ ), ) def find_channel_id_for_rule( - project: Project, + project: Project | None, actions: Sequence[dict[str, Any]], uuid: str, rule_id: int | None = None, user_id: int | None = None, + project_id: int | None = None, **kwargs: Any, ) -> None: redis_rule_status = RedisRuleStatus(uuid) - try: - project = Project.objects.get(id=project.id) - except Project.DoesNotExist: - redis_rule_status.set_value("failed") - return + if not project and project_id: + try: + project = Project.objects.get_from_cache(id=project_id) + except Project.DoesNotExist: + redis_rule_status.set_value("failed") + return + assert project, "find_channel_id_for_rule requires a project or project_id" organization = project.organization integration_id: int | None = None diff --git a/tests/sentry/api/endpoints/test_project_rules.py b/tests/sentry/api/endpoints/test_project_rules.py index aa35c9b6bcd6d9..eaec8441b4a53f 100644 --- a/tests/sentry/api/endpoints/test_project_rules.py +++ b/tests/sentry/api/endpoints/test_project_rules.py @@ -727,6 +727,7 @@ def test_kicks_off_slack_async_job( payload["actions"][0].pop("name") kwargs = { "name": payload["name"], + "project_id": self.project.id, "environment": payload.get("environment"), "action_match": payload["actionMatch"], "filter_match": payload.get("filterMatch"), diff --git a/tests/sentry/integrations/slack/tasks/test_tasks.py b/tests/sentry/integrations/slack/tasks/test_tasks.py index c59ac49a58a41b..f4201cfca3e3d4 100644 --- a/tests/sentry/integrations/slack/tasks/test_tasks.py +++ b/tests/sentry/integrations/slack/tasks/test_tasks.py @@ -79,6 +79,52 @@ def test_task_new_rule(self, mock_set_value): "name": "New Rule", "environment": None, "project": self.project, + "project_id": self.project.id, + "action_match": "all", + "filter_match": "all", + "conditions": [ + {"id": "sentry.rules.conditions.first_seen_event.FirstSeenEventCondition"} + ], + "actions": [ + { + "channel": "#my-channel", + "id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction", + "tags": "", + "workspace": self.integration.id, + } + ], + "frequency": 5, + "uuid": self.uuid, + "user_id": self.user.id, + } + + with self.tasks(): + find_channel_id_for_rule(**data) + + rule = Rule.objects.exclude(label__in=[DEFAULT_RULE_LABEL]).get(project_id=self.project.id) + mock_set_value.assert_called_with("success", rule.id) + assert rule.label == "New Rule" + # check that the channel_id got added + assert rule.data["actions"] == [ + { + "channel": "#my-channel", + "channel_id": "chan-id", + "id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction", + "tags": "", + "workspace": self.integration.id, + } + ] + assert rule.created_by_id == self.user.id + + @responses.activate + @patch.object(RedisRuleStatus, "set_value", return_value=None) + def test_task_new_rule_project_id(self, mock_set_value): + # Task should work if project_id is passed instead of project + data = { + "name": "New Rule", + "environment": None, + "project": None, + "project_id": self.project.id, "action_match": "all", "filter_match": "all", "conditions": [ @@ -128,6 +174,7 @@ def test_task_existing_rule(self, mock_set_value): "name": "Test Rule", "environment": None, "project": self.project, + "project_id": self.project.id, "action_match": "all", "filter_match": "all", "conditions": [condition_data],