from django.db import router
from rest_framework.request import Request

from sentry.mediators.mediator import Mediator
from sentry.mediators.param import Param
from sentry.models import Actor, Project, Rule


class Updater(Mediator):
    rule = Param(Rule)
    name = Param(str, required=False)
    owner = Param(int, required=False)
    environment = Param(int, required=False)
    project = Param(Project)
    action_match = Param(str, required=False)
    filter_match = Param(str, required=False)
    actions = Param(list, required=False)
    conditions = Param(list, required=False)
    frequency = Param(int, required=False)
    request = Param(Request, required=False)
    using = router.db_for_write(Project)

    def call(self):
        self._update_name()
        self._update_owner()
        self._update_environment()
        self._update_project()
        self._update_actions()
        self._update_action_match()
        self._update_filter_match()
        self._update_conditions()
        self._update_frequency()
        self.rule.save()
        return self.rule

    def _update_name(self):
        if self.name:
            self.rule.label = self.name

    def _update_owner(self) -> None:
        self.rule.owner = Actor.objects.get(id=self.owner) if self.owner else None

    def _update_environment(self):
        self.rule.environment_id = self.environment

    def _update_project(self):
        if self.project:
            self.rule.project = self.project

    def _update_actions(self):
        if self.actions:
            self.rule.data["actions"] = self.actions

    def _update_action_match(self):
        if self.action_match:
            self.rule.data["action_match"] = self.action_match

    def _update_filter_match(self):
        if self.filter_match:
            self.rule.data["filter_match"] = self.filter_match

    def _update_conditions(self):
        if self.conditions:
            self.rule.data["conditions"] = self.conditions

    def _update_frequency(self):
        if self.frequency:
            self.rule.data["frequency"] = self.frequency

src/sentry/mediators/project_rules/updater.py
from django.db import router
from rest_framework.request import Request

from sentry.mediators.mediator import Mediator
from sentry.mediators.param import Param
from sentry.models import Actor, Project, Rule


class Updater(Mediator):
    rule = Param(Rule)
    name = Param(str, required=False)
    owner = Param(int, required=False)
    environment = Param(int, required=False)
    project = Param(Project)
    action_match = Param(str, required=False)
    filter_match = Param(str, required=False)
    actions = Param(list, required=False)
    conditions = Param(list, required=False)
    frequency = Param(int, required=False)
    request = Param(Request, required=False)
    using = router.db_for_write(Project)

    def call(self):
        self._update_name()
        self._update_owner()
        self._update_environment()
        self._update_project()
        self._update_actions()
        self._update_action_match()
        self._update_filter_match()
        self._update_conditions()
        self._update_frequency()
        self.rule.save()
        return self.rule

    def _update_name(self):
        if self.name:
            self.rule.label = self.name

    def _update_owner(self) -> None:
        self.rule.owner = Actor.objects.get(id=self.owner) if self.owner else None

    def _update_environment(self):
        self.rule.environment_id = self.environment

    def _update_project(self):
        if self.project:
            self.rule.project = self.project

    def _update_actions(self):
        if self.actions:
            self.rule.data["actions"] = self.actions

    def _update_action_match(self):
        if self.action_match:
            self.rule.data["action_match"] = self.action_match

    def _update_filter_match(self):
        if self.filter_match:
            self.rule.data["filter_match"] = self.filter_match

    def _update_conditions(self):
        self.rule.data["conditions"] = self.conditions or []

    def _update_frequency(self):
        if self.frequency:
            self.rule.data["frequency"] = self.frequency

src/sentry/mediators/project_rules/updater.py
diff --git a/src/sentry/mediators/project_rules/updater.py b/src/sentry/mediators/project_rules/updater.py
index 09bd2623ca0205..1726542c79e30d 100644
--- a/src/sentry/mediators/project_rules/updater.py
+++ b/src/sentry/mediators/project_rules/updater.py
@@ -60,8 +60,7 @@ def _update_filter_match(self):
             self.rule.data["filter_match"] = self.filter_match
 
     def _update_conditions(self):
-        if self.conditions:
-            self.rule.data["conditions"] = self.conditions
+        self.rule.data["conditions"] = self.conditions or []
 
     def _update_frequency(self):
         if self.frequency:
diff --git a/tests/sentry/api/endpoints/test_project_rule_details.py b/tests/sentry/api/endpoints/test_project_rule_details.py
index e2ae9727621ef9..42ac8cf0968138 100644
--- a/tests/sentry/api/endpoints/test_project_rule_details.py
+++ b/tests/sentry/api/endpoints/test_project_rule_details.py
@@ -405,6 +405,42 @@ def test_update_name(self):
         )
         assert_rule_from_payload(self.rule, payload)
 
+    def test_remove_conditions(self):
+        """Test that you can edit an alert rule to have no conditions (aka fire on every event)"""
+        conditions = [
+            {
+                "id": "sentry.rules.conditions.first_seen_event.FirstSeenEventCondition",
+            }
+        ]
+        actions = [
+            {
+                "targetType": "IssueOwners",
+                "fallthroughType": "ActiveMembers",
+                "id": "sentry.mail.actions.NotifyEmailAction",
+                "targetIdentifier": "",
+            }
+        ]
+        rule = self.create_project_rule(
+            project=self.project,
+            action_match=actions,
+            condition_match=conditions,
+            name="no conditions",
+        )
+        payload = {
+            "name": rule.label,
+            "environment": None,
+            "actionMatch": "all",
+            "filterMatch": "all",
+            "frequency": 30,
+            "conditions": [],
+            "actions": actions,
+        }
+
+        self.get_success_response(
+            self.organization.slug, self.project.slug, rule.id, status_code=200, **payload
+        )
+        assert_rule_from_payload(rule, payload)
+
     def test_update_duplicate_rule(self):
         """Test that if you edit a rule such that it's now the exact duplicate of another rule in the same project
         we do not allow it"""

Unable to edit WHEN conditions from issue alert
### Environment

SaaS (https://sentry.io/)

### Steps to Reproduce

1. Create an issue alert with a few WHEN conditions
2. Save it
3. Go to the Alert details page
4. Click on Edit rule
5. Delete all the WHEN conditions
6. Click on Save
7. When you're back to the Alert details page, the WHEN conditions are still there, and the "Updated alert rule" message appears

### Expected Result

The users should be able to edit the alert rules

### Actual Result

The alert rule stays the same after editing

### Product Area

Alerts

### Link

_No response_

### DSN

_No response_

### Version

_No response_
