: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(cdp): backfill teams powerplatform url support to old destinations (#67332)
Harley Alexanderac1fc9981b02
diff --git a/posthog/management/commands/test/test_update_hog_function_code.py b/posthog/management/commands/test/test_update_hog_function_code.pyindex 022ee222a7c5..7f4cb842e6b3 100644--- a/posthog/management/commands/test/test_update_hog_function_code.py+++ b/posthog/management/commands/test/test_update_hog_function_code.py@@ -5,8 +5,44 @@ from unittest.mock import patchfrom django.core.management import call_command+from parameterized import parameterized++from posthog.cdp.validation import compile_hog as compile_hog_for_check+from products.cdp.backend.models.hog_functions.hog_function import HogFunction+# The powerplatform branch the migration must splice into stale Microsoft Teams functions.+POWERPLATFORM_BRANCH = "environment.api.powerplatform.com(:443)?/powerautomate/automations/direct/workflows"++# The exact tail shared by the standard 4-branch stock block and the powerplatform.com:443 variant.+_FOUR_BRANCH_TAIL = "not match(inputs.webhookUrl, '^https://[^/]+.flow.microsoft.com/[^/]+')) {\n throw Error('Invalid URL. The URL should match either Azure Logic Apps format (https://<region>.logic.azure.com:443/workflows/...), Power Platform format (https://<tenant>.webhook.office.com/webhookb2/...), or Power Automate format (https://<region>.powerautomate.com/... or https://<region>.flow.microsoft.com/...)')"++# Stale validation blocks that exist verbatim in production, each followed by a trivial send body.+_SEND_BODY = "\n\nlet res := fetch(inputs.webhookUrl, {'method': 'POST'});\n"++FOUR_BRANCH_STALE = (+ "if (not match(inputs.webhookUrl, '^https://[^/]+.logic.azure.com:443/workflows/[^/]+/triggers/manual/paths/invoke?.*') and\n"+ " not match(inputs.webhookUrl, '^https://[^/]+.webhook.office.com/webhookb2/[^/]+/IncomingWebhook/[^/]+/[^/]+') and\n"+ " not match(inputs.webhookUrl, '^https://[^/]+.powerautomate.com/[^/]+') and\n " + _FOUR_BRANCH_TAIL + "\n}"+) + _SEND_BODY++POWERPLATFORM_COM_STALE = (+ "if (not match(inputs.webhookUrl, '^https://[^/]+.powerplatform.com:443/[^/]+/.*') and\n"+ " not match(inputs.webhookUrl, '^https://[^/]+.webhook.office.com/webhookb2/[^/]+/IncomingWebhook/[^/]+/[^/]+') and\n"+ " not match(inputs.webhookUrl, '^https://[^/]+.powerautomate.com/[^/]+') and\n " + _FOUR_BRANCH_TAIL + "\n}"+) + _SEND_BODY++ONE_BRANCH_STALE = (+ "if (not match(inputs.webhookUrl, '^https://[^/]+.logic.azure.com:443/workflows/[^/]+/triggers/manual/paths/invoke?.*')) {\n"+ " throw Error('Invalid URL. The URL should match the format: https://<region>.logic.azure.com:443/workflows/<workflowId>/triggers/manual/paths/invoke?...')\n}"+) + _SEND_BODY++# Same block but commented out - validation is disabled, so it already accepts any URL and must be left alone.+COMMENTED_OUT_STALE = (+ "// if (not match(inputs.webhookUrl, '^https://[^/]+.logic.azure.com:443/workflows/[^/]+/triggers/manual/paths/invoke?.*')) {\n"+ "// throw Error('Invalid URL.')\n// }"+) + _SEND_BODY+class TestUpdateHogFunctionCode(BaseTest):def setUp(self):@@ -225,3 +261,48 @@ class TestUpdateHogFunctionCode(BaseTest):output = out.getvalue()self.assertIn("Found 0 destinations to process", output)self.assertIn("Updated: 0", output)++ def _create_teams_function(self, hog: str) -> HogFunction:+ with patch("products.cdp.backend.models.hog_functions.hog_function.reload_hog_functions_on_workers"):+ return HogFunction.objects.create(+ team=self.team,+ name="Teams Function",+ type="destination",+ template_id="template-microsoft-teams",+ description="Test Teams Function",+ hog=hog,+ enabled=True,+ )++ @parameterized.expand(+ [+ ("four_branch", FOUR_BRANCH_STALE),+ ("powerplatform_com_variant", POWERPLATFORM_COM_STALE),+ ("one_branch", ONE_BRANCH_STALE),+ ]+ )+ def test_microsoft_teams_powerplatform_migration(self, _name, stale_hog):+ function = self._create_teams_function(stale_hog)++ with patch(+ "posthog.management.commands.update_hog_function_code.compile_hog",+ return_value="compiled_bytecode",+ ):+ out = StringIO()+ call_command("update_hog_function_code", replace_key="microsoft-teams-powerplatform-url", stdout=out)++ function.refresh_from_db()+ assert POWERPLATFORM_BRANCH in function.hog+ self.assertIn("Updated: 1", out.getvalue())+ # The migrated source must be valid Hog - guards against a typo in the replacement's to_string.+ compile_hog_for_check(function.hog, "destination")++ def test_microsoft_teams_migration_leaves_functions_without_the_stale_block_untouched(self):+ function = self._create_teams_function(COMMENTED_OUT_STALE)++ out = StringIO()+ call_command("update_hog_function_code", replace_key="microsoft-teams-powerplatform-url", stdout=out)++ function.refresh_from_db()+ assert POWERPLATFORM_BRANCH not in function.hog+ self.assertIn("Updated: 0", out.getvalue())diff --git a/posthog/management/commands/update_hog_function_code.py b/posthog/management/commands/update_hog_function_code.pyindex 3ee2782c7ba2..09bfeb0cc57c 100644--- a/posthog/management/commands/update_hog_function_code.py+++ b/posthog/management/commands/update_hog_function_code.py@@ -1,4 +1,5 @@import time+from typing import TypedDictfrom django.core.management.base import BaseCommandfrom django.core.paginator import Paginator@@ -12,6 +13,16 @@ from products.cdp.backend.models.hog_functions.hog_function import HogFunctionlogger = structlog.get_logger(__name__)+class _Replacement(TypedDict):+ from_string: str+ to_string: str+++class _ReplaceOption(TypedDict):+ template_id: str+ replacements: list[_Replacement]++class Command(BaseCommand):help = "Replace a string in the HogFunction code"@@ -31,16 +42,42 @@ class Command(BaseCommand):replace_key = options.get("replace_key", None)start_time = time.time()- replaceOptions = {+ replaceOptions: dict[str, _ReplaceOption] = {"linked-api-version-update": {"template_id": "template-linkedin-ads",- "from_string": "'LinkedIn-Version': '202409'",- "to_string": "'LinkedIn-Version': '202508'",+ "replacements": [+ {+ "from_string": "'LinkedIn-Version': '202409'",+ "to_string": "'LinkedIn-Version': '202508'",+ },+ ],},"meta-ads-api-version-update": {"template_id": "template-meta-ads",- "from_string": "graph.facebook.com/v21.0/",- "to_string": "graph.facebook.com/v25.0/",+ "replacements": [+ {+ "from_string": "graph.facebook.com/v21.0/",+ "to_string": "graph.facebook.com/v25.0/",+ },+ ],+ },+ # Microsoft migrated Teams/Power Automate HTTP triggers to environment.api.powerplatform.com.+ # The current template accepts that host, but functions created earlier keep their frozen code+ # and reject the new URL. These swap the stale validation block for the current one: the+ # standard 4-branch and the powerplatform.com:443 variant share the same tail, and the+ # original single-branch (logic.azure.com only) block is replaced whole.+ "microsoft-teams-powerplatform-url": {+ "template_id": "template-microsoft-teams",+ "replacements": [+ {+ "from_string": "not match(inputs.webhookUrl, '^https://[^/]+.flow.microsoft.com/[^/]+')) {\n throw Error('Invalid URL. The URL should match either Azure Logic Apps format (https://<region>.logic.azure.com:443/workflows/...), Power Platform format (https://<tenant>.webhook.office.com/webhookb2/...), or Power Automate format (https://<region>.powerautomate.com/... or https://<region>.flow.microsoft.com/...)')",+ "to_string": "not match(inputs.webhookUrl, '^https://[^/]+.flow.microsoft.com/[^/]+') and\n not match(inputs.webhookUrl, '^https://[^/]+.environment.api.powerplatform.com(:443)?/powerautomate/automations/direct/workflows/.*')) {\n throw Error('Invalid URL. The URL should match either Azure Logic Apps format (https://<region>.logic.azure.com:443/workflows/...), Power Platform format (https://<tenant>.webhook.office.com/webhookb2/...), Power Automate format (https://<region>.powerautomate.com/... or https://<region>.flow.microsoft.com/...), or Power Platform environment format (https://<tenant>.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/...)')",+ },+ {+ "from_string": "if (not match(inputs.webhookUrl, '^https://[^/]+.logic.azure.com:443/workflows/[^/]+/triggers/manual/paths/invoke?.*')) {\n throw Error('Invalid URL. The URL should match the format: https://<region>.logic.azure.com:443/workflows/<workflowId>/triggers/manual/paths/invoke?...')\n}",+ "to_string": "if (not match(inputs.webhookUrl, '^https://[^/]+.logic.azure.com:443/workflows/[^/]+/triggers/manual/paths/invoke?.*') and\n not match(inputs.webhookUrl, '^https://[^/]+.webhook.office.com/webhookb2/[^/]+/IncomingWebhook/[^/]+/[^/]+') and\n not match(inputs.webhookUrl, '^https://[^/]+.powerautomate.com/[^/]+') and\n not match(inputs.webhookUrl, '^https://[^/]+.flow.microsoft.com/[^/]+') and\n not match(inputs.webhookUrl, '^https://[^/]+.environment.api.powerplatform.com(:443)?/powerautomate/automations/direct/workflows/.*')) {\n throw Error('Invalid URL. The URL should match either Azure Logic Apps format (https://<region>.logic.azure.com:443/workflows/...), Power Platform format (https://<tenant>.webhook.office.com/webhookb2/...), Power Automate format (https://<region>.powerautomate.com/... or https://<region>.flow.microsoft.com/...), or Power Platform environment format (https://<tenant>.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/...)')\n}",+ },+ ],},}@@ -72,10 +109,17 @@ class Command(BaseCommand):)for destination in page.object_list:- if not destination.hog or replaceOption["from_string"] not in destination.hog:+ if not destination.hog:+ continue++ new_hog = destination.hog+ for replacement in replaceOption["replacements"]:+ if replacement["from_string"] in new_hog:+ new_hog = new_hog.replace(replacement["from_string"], replacement["to_string"])++ if new_hog == destination.hog:continue- new_hog = destination.hog.replace(replaceOption["from_string"], replaceOption["to_string"])# A single destination with uncompilable (e.g. hand-edited) hog must not abort the whole run.try:new_bytecode = compile_hog(new_hog, destination.type)