diff --git a/src/sentry/rules/actions/utils.py b/src/sentry/rules/actions/utils.py index 3013516343e4a8..306772e8f0a2ce 100644 --- a/src/sentry/rules/actions/utils.py +++ b/src/sentry/rules/actions/utils.py @@ -5,6 +5,10 @@ from sentry.models.environment import Environment from sentry.models.rule import Rule +ONE_HOUR = 60 +ONE_DAY = ONE_HOUR * 24 +ONE_WEEK = ONE_DAY * 7 + def get_updated_rule_data(rule: Rule) -> dict[str, Any]: rule_data = dict(rule.data) @@ -42,6 +46,22 @@ def generate_diff_labels( return changed_data +def get_frequency_label(value_str: str | None) -> str | None: + if not value_str: + return None + + value = int(value_str) + if value < 60: + return f"{value} minutes" + elif value >= 60 and value < 10080: + return f"{int(value / ONE_HOUR)} hours" + elif value == ONE_WEEK: + return f"{int(value / ONE_WEEK)} week" + elif value == ONE_DAY * 30: + return f"{int(value / ONE_DAY)} days" + return None + + def get_changed_data( rule: Rule, rule_data: dict[str, Any], rule_data_before: dict[str, Any] ) -> dict[str, Any]: @@ -62,8 +82,10 @@ def get_changed_data( rule_data, rule_data_before, rule, changed_data, "actions", "Removed action '{}'" ) - frequency_text = check_value_changed(rule_data, rule_data_before, "frequency", "frequency") - if frequency_text: + current_frequency = get_frequency_label(rule_data.get("frequency")) + previous_frequency = get_frequency_label(rule_data_before.get("frequency")) + if current_frequency != previous_frequency: + frequency_text = f"Changed frequency from *{previous_frequency}* to *{current_frequency}*" changed_data["changed_frequency"].append(frequency_text) if rule_data.get("environment_id") and not rule_data_before.get("environment_id"): diff --git a/tests/sentry/api/endpoints/test_project_rule_details.py b/tests/sentry/api/endpoints/test_project_rule_details.py index 5af1f58a9e402a..2a35a293281b49 100644 --- a/tests/sentry/api/endpoints/test_project_rule_details.py +++ b/tests/sentry/api/endpoints/test_project_rule_details.py @@ -1013,7 +1013,9 @@ def test_slack_confirmation_notification_contents(self): "channel": "#old_channel_name", } ] - self.rule.update(data={"conditions": conditions, "actions": actions}, label="my rule") + self.rule.update( + data={"conditions": conditions, "actions": actions, "frequency": 5}, label="my rule" + ) actions[0]["channel"] = "#new_channel_name" actions[0]["channel_id"] = "new_channel_id" @@ -1063,7 +1065,7 @@ def test_slack_confirmation_notification_contents(self): "filterMatch": "any", "actions": actions, "conditions": conditions, - "frequency": 30, + "frequency": 180, "environment": staging_env.name, "owner": get_actor_for_user(self.user).get_actor_identifier(), } @@ -1081,7 +1083,7 @@ def test_slack_confirmation_notification_contents(self): changes = "*Changes*\n" changes += "• Added action 'Send a notification to the Awesome Team Slack workspace to new_channel_name (optionally, an ID: new_channel_id) and show tags [] in notification'\n" changes += "• Removed action 'Send a notification to the Awesome Team Slack workspace to #old_channel_name (optionally, an ID: old_channel_id) and show tags [] in notification'\n" - changes += "• Changed frequency from *None* to *30*\n" + changes += "• Changed frequency from *5 minutes* to *3 hours*\n" changes += f"• Added *{staging_env.name}* environment\n" changes += "• Changed rule name from *my rule* to *new rule*\n" changes += "• Changed trigger from *None* to *any*\n"