From 17479e4ae47cc38cc96bc0a44bd6edfad45a1369 Mon Sep 17 00:00:00 2001 From: Alex Zaslavsky Date: Tue, 11 Jul 2023 09:43:58 -0700 Subject: [PATCH] feat(backup): Support alert rules Adds a number of tests for exportable AlertRule* Sentry models, which necessitates testing for SnubaQuery* and QuerySubscription models as well, as these are included in AlertRule instances by default. Issue: getsentry/team-ospo#156 --- src/sentry/runner/commands/backup.py | 2 ++ src/sentry/testutils/factories.py | 6 ++++-- tests/sentry/backup/test_models.py | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/sentry/runner/commands/backup.py b/src/sentry/runner/commands/backup.py index 764b3b666c0557..68a0012938c23e 100644 --- a/src/sentry/runner/commands/backup.py +++ b/src/sentry/runner/commands/backup.py @@ -150,6 +150,8 @@ def compare(self, on: InstanceID, left: JSONData, right: JSONData) -> Comparator ComparatorList = List[JSONScrubbingComparator] ComparatorMap = Dict[str, ComparatorList] DEFAULT_COMPARATORS: ComparatorMap = { + "sentry.alertrule": [DateUpdatedComparator("date_modified")], + "sentry.querysubscription": [DateUpdatedComparator("date_updated")], "sentry.userrole": [DateUpdatedComparator("date_updated")], "sentry.userroleuser": [DateUpdatedComparator("date_updated")], } diff --git a/src/sentry/testutils/factories.py b/src/sentry/testutils/factories.py index edf4e3d7c3c83f..87fcd0e90ff18b 100644 --- a/src/sentry/testutils/factories.py +++ b/src/sentry/testutils/factories.py @@ -1302,11 +1302,13 @@ def create_alert_rule( @staticmethod @assume_test_silo_mode(SiloMode.REGION) - def create_alert_rule_trigger(alert_rule, label=None, alert_threshold=100): + def create_alert_rule_trigger( + alert_rule, label=None, alert_threshold=100, excluded_projects=None + ): if not label: label = petname.generate(2, " ", letters=10).title() - return create_alert_rule_trigger(alert_rule, label, alert_threshold) + return create_alert_rule_trigger(alert_rule, label, alert_threshold, excluded_projects) @staticmethod @assume_test_silo_mode(SiloMode.REGION) diff --git a/tests/sentry/backup/test_models.py b/tests/sentry/backup/test_models.py index 554c0d73c74b3b..ddcc4f0fd612d0 100644 --- a/tests/sentry/backup/test_models.py +++ b/tests/sentry/backup/test_models.py @@ -65,6 +65,24 @@ def create_monitor(self): config={"schedule": "* * * * *", "schedule_type": ScheduleType.CRONTAB}, ) + def test_alert_rule(self): + self.create_alert_rule() + self.import_export_then_validate() + + def test_alert_rule_excluded_projects(self): + user = self.create_user() + org = self.create_organization(owner=user) + excluded = self.create_project(organization=org) + self.create_alert_rule(include_all_projects=True, excluded_projects=[excluded]) + self.import_export_then_validate() + + def test_alert_rule_trigger(self): + excluded = self.create_project() + rule = self.create_alert_rule(include_all_projects=True) + trigger = self.create_alert_rule_trigger(alert_rule=rule, excluded_projects=[excluded]) + self.create_alert_rule_trigger_action(alert_rule_trigger=trigger) + self.import_export_then_validate() + def test_environment(self): self.create_environment() self.import_export_then_validate()