Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(alerts): read a move between two shipped defaults as stock (#119986)

Jimmy Zhu4b8677c7bd87
diff --git a/products/alerts/backend/logic/destination_eligibility.py b/products/alerts/backend/logic/destination_eligibility.py
index fe909dc8c2c8..20e639d966d5 100644
--- a/products/alerts/backend/logic/destination_eligibility.py
+++ b/products/alerts/backend/logic/destination_eligibility.py
@@ -265,19 +265,23 @@ def _hold_backs(
found.append(DestinationHoldBack.CUSTOM_INPUTS)
if _without_derived(function.filters) != _without_derived(first_snapshot.get("filters")):
found.append(DestinationHoldBack.FILTERS)
- found.extend(_input_hold_backs(template_id, function.inputs or {}, first_snapshot.get("inputs") or {}))
+ found.extend(
+ _input_hold_backs(template_id, function.inputs or {}, first_snapshot.get("inputs") or {}, stock or {})
+ )
# Ordered and unique, so a report reads the same for the same function.
return tuple(dict.fromkeys(found))
def _input_hold_backs(
- template_id: str, inputs: dict[str, Any], first_inputs: dict[str, Any]
+ template_id: str, inputs: dict[str, Any], first_inputs: dict[str, Any], stock: dict[str, list[Any]]
) -> list[DestinationHoldBack]:
found: list[DestinationHoldBack] = []
for key in _compared_keys(template_id, set(inputs) | set(first_inputs)):
value, first_value = input_value(inputs, key), input_value(first_inputs, key)
same_mode = _is_webhook_headers(template_id, key) or _templating(inputs, key) == _templating(first_inputs, key)
- if value == first_value and same_mode:
+ # A move from one shipped default to another, such as a bulk update to a newer stock
+ # message, is not an edit a person made.
+ if (value == first_value and same_mode) or _is_stock_value(inputs, key, stock):
continue
found.extend(_key_hold_backs(template_id, key, value))
return found
@@ -290,12 +294,16 @@ def _stock_hold_backs(
for key in _compared_keys(template_id, set(inputs)):
value = input_value(inputs, key)
# The templates skip an empty input, so it sends what a missing one sends.
- if value in (None, "") or (value in stock.get(key, ()) and _templating(inputs, key) == "hog"):
+ if value in (None, "") or _is_stock_value(inputs, key, stock):
continue
found.extend(_key_hold_backs(template_id, key, value))
return found
+def _is_stock_value(inputs: dict[str, Any], key: str, stock: dict[str, list[Any]]) -> bool:
+ return input_value(inputs, key) in stock.get(key, ()) and _templating(inputs, key) == "hog"
+
+
def _compared_keys(template_id: str, keys: set[str]) -> list[str]:
skipped = SPEC_BY_TEMPLATE_ID[template_id].address_input_keys | _NOT_SENT_INPUTS
return sorted(keys - skipped)
diff --git a/products/alerts/backend/test/test_destination_eligibility.py b/products/alerts/backend/test/test_destination_eligibility.py
index d6159dbb59f5..7de9291cfc38 100644
--- a/products/alerts/backend/test/test_destination_eligibility.py
+++ b/products/alerts/backend/test/test_destination_eligibility.py
@@ -356,26 +356,42 @@ class TestDestinationEligibility(APIBaseTest):
@parameterized.expand(
[
- ("stock_blocks", {}, False, ()),
- ("text_edited_before_the_first_save", {"text": "Look at this"}, True, (DestinationHoldBack.MESSAGE,)),
- ("text_edited_with_no_revision", {"text": "Look at this"}, False, (DestinationHoldBack.MESSAGE,)),
- ("an_emptied_text", {"text": ""}, False, ()),
+ ("stock_blocks", {}, None, ()),
+ ("text_edited_before_the_first_save", {"text": "Look at this"}, {}, (DestinationHoldBack.MESSAGE,)),
+ ("text_edited_with_no_revision", {"text": "Look at this"}, None, (DestinationHoldBack.MESSAGE,)),
+ ("an_emptied_text", {"text": ""}, None, ()),
(
"stock_text_rendered_as_liquid",
{"text": {"value": "Alert triggered: {event.properties.insight_name}", "templating": "liquid"}},
- False,
+ None,
(DestinationHoldBack.MESSAGE,),
),
(
"an_agent_chosen_emoji",
{"icon_emoji": ":rotating_light:"},
- True,
+ {},
(DestinationHoldBack.MESSAGE_COSMETIC,),
),
+ (
+ "text_moved_to_another_default_after_the_first_save",
+ {"text": "*{person.name}* triggered event: '{event.event}'"},
+ {"text": "Alert triggered: {event.properties.insight_name}"},
+ (),
+ ),
+ (
+ "text_edited_after_the_first_save",
+ {},
+ {"text": "Look at this"},
+ (DestinationHoldBack.MESSAGE,),
+ ),
]
)
def test_an_insight_destination_is_compared_with_the_shipped_defaults(
- self, _name: str, changes: dict[str, Any], revised: bool, expected: tuple[DestinationHoldBack, ...]
+ self,
+ _name: str,
+ changes: dict[str, Any],
+ later: dict[str, str] | None,
+ expected: tuple[DestinationHoldBack, ...],
) -> None:
(event,) = INSIGHT_ALERT_EVENT_IDS
stock = build_insight_alert_slack_config(
@@ -391,9 +407,11 @@ class TestDestinationEligibility(APIBaseTest):
},
)
HogFunction.objects.filter(pk=function.pk).update(filters=stock["filters"])
- if revised:
+ if later is not None:
function.refresh_from_db()
- self._edited(function)
+ self._edited(
+ function, inputs={**(function.inputs or {}), **{key: {"value": value} for key, value in later.items()}}
+ )
(eligibility,) = classify_alert_destinations(team_id=self.team.id, alert_id=ALERT_ID, allowed_event_ids=[event])
assert eligibility.hold_backs == expected