prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Cannot edit or delete alerts
  ### Environment
  
  SaaS (https://sentry.io/)
  
  ### Version
  
  _No response_
  
  ### Steps to Reproduce
  
  1. Have alerts that were set up a while ago
  2. Get a bunch of emails from one alert that is too touchy
  3. Try to edit alert (fails)
  4. Try to delete alert (fails)
  
  ### Expected Result
  
  Can edit or delete alerts that I created on an account that I am the only user for
  
  ### Actual Result
  
  Cannot edit or delete alerts
  
  ![ksnip_20220729-153917](https://user-images.githubusercontent.com/22881850/183306578-20fe738c-96b1-4052-a308-ad49c7febbe7.png)
  
  ![ksnip_20220807-115243](https://user-images.githubusercontent.com/22881850/183306642-6299edea-28e0-46fb-a98b-387662bc0c0b.png)
  
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/sentry/incidents/endpoints/bases.py`
  Content:
  ```
  1 from rest_framework.exceptions import PermissionDenied
  2 from rest_framework.request import Request
  3 
  4 from sentry import features
  5 from sentry.api.bases.organization import OrganizationAlertRulePermission, OrganizationEndpoint
  6 from sentry.api.bases.project import ProjectAlertRulePermission, ProjectEndpoint
  7 from sentry.api.exceptions import ResourceDoesNotExist
  8 from sentry.incidents.models import AlertRule, AlertRuleTrigger, AlertRuleTriggerAction
  9 
  10 
  11 class ProjectAlertRuleEndpoint(ProjectEndpoint):
  12     permission_classes = (ProjectAlertRulePermission,)
  13 
  14     def convert_args(self, request: Request, alert_rule_id, *args, **kwargs):
  15         args, kwargs = super().convert_args(request, *args, **kwargs)
  16         project = kwargs["project"]
  17 
  18         if not features.has("organizations:incidents", project.organization, actor=request.user):
  19             raise ResourceDoesNotExist
  20 
  21         if not request.access.has_project_access(project):
  22             raise PermissionDenied
  23 
  24         try:
  25             kwargs["alert_rule"] = AlertRule.objects.get(
  26                 snuba_query__subscriptions__project=project, id=alert_rule_id
  27             )
  28         except AlertRule.DoesNotExist:
  29             raise ResourceDoesNotExist
  30 
  31         return args, kwargs
  32 
  33 
  34 class OrganizationAlertRuleEndpoint(OrganizationEndpoint):
  35     permission_classes = (OrganizationAlertRulePermission,)
  36 
  37     def convert_args(self, request: Request, alert_rule_id, *args, **kwargs):
  38         args, kwargs = super().convert_args(request, *args, **kwargs)
  39         organization = kwargs["organization"]
  40 
  41         if not features.has("organizations:incidents", organization, actor=request.user):
  42             raise ResourceDoesNotExist
  43 
  44         try:
  45             kwargs["alert_rule"] = AlertRule.objects.get(
  46                 organization=organization, id=alert_rule_id
  47             )
  48         except AlertRule.DoesNotExist:
  49             raise ResourceDoesNotExist
  50 
  51         return args, kwargs
  52 
  53 
  54 class OrganizationAlertRuleTriggerEndpoint(OrganizationAlertRuleEndpoint):
  55     def convert_args(self, request: Request, alert_rule_trigger_id, *args, **kwargs):
  56         args, kwargs = super().convert_args(request, *args, **kwargs)
  57         organization = kwargs["organization"]
  58         alert_rule = kwargs["alert_rule"]
  59 
  60         if not features.has("organizations:incidents", organization, actor=request.user):
  61             raise ResourceDoesNotExist
  62 
  63         try:
  64             kwargs["alert_rule_trigger"] = AlertRuleTrigger.objects.get(
  65                 alert_rule=alert_rule, id=alert_rule_trigger_id
  66             )
  67         except AlertRuleTrigger.DoesNotExist:
  68             raise ResourceDoesNotExist
  69 
  70         return args, kwargs
  71 
  72 
  73 class OrganizationAlertRuleTriggerActionEndpoint(OrganizationAlertRuleTriggerEndpoint):
  74     def convert_args(self, request: Request, alert_rule_trigger_action_id, *args, **kwargs):
  75         args, kwargs = super().convert_args(request, *args, **kwargs)
  76         organization = kwargs["organization"]
  77         trigger = kwargs["alert_rule_trigger"]
  78 
  79         if not features.has("organizations:incidents", organization, actor=request.user):
  80             raise ResourceDoesNotExist
  81 
  82         try:
  83             kwargs["alert_rule_trigger_action"] = AlertRuleTriggerAction.objects.get(
  84                 alert_rule_trigger=trigger, id=alert_rule_trigger_action_id
  85             )
  86         except AlertRuleTriggerAction.DoesNotExist:
  87             raise ResourceDoesNotExist
  88 
  89         return args, kwargs
  90
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/src/sentry/incidents/endpoints/bases.py b/src/sentry/incidents/endpoints/bases.py
  --- a/src/sentry/incidents/endpoints/bases.py
  +++ b/src/sentry/incidents/endpoints/bases.py
  @@ -38,7 +38,10 @@
           args, kwargs = super().convert_args(request, *args, **kwargs)
           organization = kwargs["organization"]
   
  -        if not features.has("organizations:incidents", organization, actor=request.user):
  +        # Allow orgs that have downgraded plans to delete metric alerts
  +        if request.method != "DELETE" and not features.has(
  +            "organizations:incidents", organization, actor=request.user
  +        ):
               raise ResourceDoesNotExist
   
           try:
  
verification_info: {"golden_diff": "diff --git a/src/sentry/incidents/endpoints/bases.py b/src/sentry/incidents/endpoints/bases.py\n--- a/src/sentry/incidents/endpoints/bases.py\n+++ b/src/sentry/incidents/endpoints/bases.py\n@@ -38,7 +38,10 @@\n         args, kwargs = super().convert_args(request, *args, **kwargs)\n         organization = kwargs[\"organization\"]\n \n-        if not features.has(\"organizations:incidents\", organization, actor=request.user):\n+        # Allow orgs that have downgraded plans to delete metric alerts\n+        if request.method != \"DELETE\" and not features.has(\n+            \"organizations:incidents\", organization, actor=request.user\n+        ):\n             raise ResourceDoesNotExist\n \n         try:\n", "issue": "Cannot edit or delete alerts\n### Environment\n\nSaaS (https://sentry.io/)\n\n### Version\n\n_No response_\n\n### Steps to Reproduce\n\n1. Have alerts that were set up a while ago\r\n2. Get a bunch of emails from one alert that is too touchy\r\n3. Try to edit alert (fails)\r\n4. Try to delete alert (fails)\n\n### Expected Result\n\nCan edit or delete alerts that I created on an account that I am the only user for\n\n### Actual Result\n\nCannot edit or delete alerts\r\n\r\n![ksnip_20220729-153917](https://user-images.githubusercontent.com/22881850/183306578-20fe738c-96b1-4052-a308-ad49c7febbe7.png)\r\n\r\n![ksnip_20220807-115243](https://user-images.githubusercontent.com/22881850/183306642-6299edea-28e0-46fb-a98b-387662bc0c0b.png)\r\n\r\n\n", "before_files": [{"content": "from rest_framework.exceptions import PermissionDenied\nfrom rest_framework.request import Request\n\nfrom sentry import features\nfrom sentry.api.bases.organization import OrganizationAlertRulePermission, OrganizationEndpoint\nfrom sentry.api.bases.project import ProjectAlertRulePermission, ProjectEndpoint\nfrom sentry.api.exceptions import ResourceDoesNotExist\nfrom sentry.incidents.models import AlertRule, AlertRuleTrigger, AlertRuleTriggerAction\n\n\nclass ProjectAlertRuleEndpoint(ProjectEndpoint):\n    permission_classes = (ProjectAlertRulePermission,)\n\n    def convert_args(self, request: Request, alert_rule_id, *args, **kwargs):\n        args, kwargs = super().convert_args(request, *args, **kwargs)\n        project = kwargs[\"project\"]\n\n        if not features.has(\"organizations:incidents\", project.organization, actor=request.user):\n            raise ResourceDoesNotExist\n\n        if not request.access.has_project_access(project):\n            raise PermissionDenied\n\n        try:\n            kwargs[\"alert_rule\"] = AlertRule.objects.get(\n                snuba_query__subscriptions__project=project, id=alert_rule_id\n            )\n        except AlertRule.DoesNotExist:\n            raise ResourceDoesNotExist\n\n        return args, kwargs\n\n\nclass OrganizationAlertRuleEndpoint(OrganizationEndpoint):\n    permission_classes = (OrganizationAlertRulePermission,)\n\n    def convert_args(self, request: Request, alert_rule_id, *args, **kwargs):\n        args, kwargs = super().convert_args(request, *args, **kwargs)\n        organization = kwargs[\"organization\"]\n\n        if not features.has(\"organizations:incidents\", organization, actor=request.user):\n            raise ResourceDoesNotExist\n\n        try:\n            kwargs[\"alert_rule\"] = AlertRule.objects.get(\n                organization=organization, id=alert_rule_id\n            )\n        except AlertRule.DoesNotExist:\n            raise ResourceDoesNotExist\n\n        return args, kwargs\n\n\nclass OrganizationAlertRuleTriggerEndpoint(OrganizationAlertRuleEndpoint):\n    def convert_args(self, request: Request, alert_rule_trigger_id, *args, **kwargs):\n        args, kwargs = super().convert_args(request, *args, **kwargs)\n        organization = kwargs[\"organization\"]\n        alert_rule = kwargs[\"alert_rule\"]\n\n        if not features.has(\"organizations:incidents\", organization, actor=request.user):\n            raise ResourceDoesNotExist\n\n        try:\n            kwargs[\"alert_rule_trigger\"] = AlertRuleTrigger.objects.get(\n                alert_rule=alert_rule, id=alert_rule_trigger_id\n            )\n        except AlertRuleTrigger.DoesNotExist:\n            raise ResourceDoesNotExist\n\n        return args, kwargs\n\n\nclass OrganizationAlertRuleTriggerActionEndpoint(OrganizationAlertRuleTriggerEndpoint):\n    def convert_args(self, request: Request, alert_rule_trigger_action_id, *args, **kwargs):\n        args, kwargs = super().convert_args(request, *args, **kwargs)\n        organization = kwargs[\"organization\"]\n        trigger = kwargs[\"alert_rule_trigger\"]\n\n        if not features.has(\"organizations:incidents\", organization, actor=request.user):\n            raise ResourceDoesNotExist\n\n        try:\n            kwargs[\"alert_rule_trigger_action\"] = AlertRuleTriggerAction.objects.get(\n                alert_rule_trigger=trigger, id=alert_rule_trigger_action_id\n            )\n        except AlertRuleTriggerAction.DoesNotExist:\n            raise ResourceDoesNotExist\n\n        return args, kwargs\n", "path": "src/sentry/incidents/endpoints/bases.py"}], "after_files": [{"content": "from rest_framework.exceptions import PermissionDenied\nfrom rest_framework.request import Request\n\nfrom sentry import features\nfrom sentry.api.bases.organization import OrganizationAlertRulePermission, OrganizationEndpoint\nfrom sentry.api.bases.project import ProjectAlertRulePermission, ProjectEndpoint\nfrom sentry.api.exceptions import ResourceDoesNotExist\nfrom sentry.incidents.models import AlertRule, AlertRuleTrigger, AlertRuleTriggerAction\n\n\nclass ProjectAlertRuleEndpoint(ProjectEndpoint):\n    permission_classes = (ProjectAlertRulePermission,)\n\n    def convert_args(self, request: Request, alert_rule_id, *args, **kwargs):\n        args, kwargs = super().convert_args(request, *args, **kwargs)\n        project = kwargs[\"project\"]\n\n        if not features.has(\"organizations:incidents\", project.organization, actor=request.user):\n            raise ResourceDoesNotExist\n\n        if not request.access.has_project_access(project):\n            raise PermissionDenied\n\n        try:\n            kwargs[\"alert_rule\"] = AlertRule.objects.get(\n                snuba_query__subscriptions__project=project, id=alert_rule_id\n            )\n        except AlertRule.DoesNotExist:\n            raise ResourceDoesNotExist\n\n        return args, kwargs\n\n\nclass OrganizationAlertRuleEndpoint(OrganizationEndpoint):\n    permission_classes = (OrganizationAlertRulePermission,)\n\n    def convert_args(self, request: Request, alert_rule_id, *args, **kwargs):\n        args, kwargs = super().convert_args(request, *args, **kwargs)\n        organization = kwargs[\"organization\"]\n\n        # Allow orgs that have downgraded plans to delete metric alerts\n        if request.method != \"DELETE\" and not features.has(\n            \"organizations:incidents\", organization, actor=request.user\n        ):\n            raise ResourceDoesNotExist\n\n        try:\n            kwargs[\"alert_rule\"] = AlertRule.objects.get(\n                organization=organization, id=alert_rule_id\n            )\n        except AlertRule.DoesNotExist:\n            raise ResourceDoesNotExist\n\n        return args, kwargs\n\n\nclass OrganizationAlertRuleTriggerEndpoint(OrganizationAlertRuleEndpoint):\n    def convert_args(self, request: Request, alert_rule_trigger_id, *args, **kwargs):\n        args, kwargs = super().convert_args(request, *args, **kwargs)\n        organization = kwargs[\"organization\"]\n        alert_rule = kwargs[\"alert_rule\"]\n\n        if not features.has(\"organizations:incidents\", organization, actor=request.user):\n            raise ResourceDoesNotExist\n\n        try:\n            kwargs[\"alert_rule_trigger\"] = AlertRuleTrigger.objects.get(\n                alert_rule=alert_rule, id=alert_rule_trigger_id\n            )\n        except AlertRuleTrigger.DoesNotExist:\n            raise ResourceDoesNotExist\n\n        return args, kwargs\n\n\nclass OrganizationAlertRuleTriggerActionEndpoint(OrganizationAlertRuleTriggerEndpoint):\n    def convert_args(self, request: Request, alert_rule_trigger_action_id, *args, **kwargs):\n        args, kwargs = super().convert_args(request, *args, **kwargs)\n        organization = kwargs[\"organization\"]\n        trigger = kwargs[\"alert_rule_trigger\"]\n\n        if not features.has(\"organizations:incidents\", organization, actor=request.user):\n            raise ResourceDoesNotExist\n\n        try:\n            kwargs[\"alert_rule_trigger_action\"] = AlertRuleTriggerAction.objects.get(\n                alert_rule_trigger=trigger, id=alert_rule_trigger_action_id\n            )\n        except AlertRuleTriggerAction.DoesNotExist:\n            raise ResourceDoesNotExist\n\n        return args, kwargs\n", "path": "src/sentry/incidents/endpoints/bases.py"}]}