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(cdp): accept power platform teams urls with cluster path segment (#69584)

Aleksander Błaszkiewiczd420b3aa8e06
diff --git a/posthog/cdp/templates/microsoft_teams/template_microsoft_teams.py b/posthog/cdp/templates/microsoft_teams/template_microsoft_teams.py
index e2a404052b14..0a8f5d475303 100644
--- a/posthog/cdp/templates/microsoft_teams/template_microsoft_teams.py
+++ b/posthog/cdp/templates/microsoft_teams/template_microsoft_teams.py
@@ -15,8 +15,8 @@ if (not match(inputs.webhookUrl, '^https://[^/]+.logic.azure.com:443/workflows/[
not match(inputs.webhookUrl, '^https://[^/]+.webhook.office.com/webhookb2/[^/]+/IncomingWebhook/[^/]+/[^/]+') and
not match(inputs.webhookUrl, '^https://[^/]+.powerautomate.com/[^/]+') and
not match(inputs.webhookUrl, '^https://[^/]+.flow.microsoft.com/[^/]+') and
- not match(inputs.webhookUrl, '^https://[^/]+.environment.api.powerplatform.com(:443)?/powerautomate/automations/direct/workflows/.*')) {
- 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/...)')
+ not match(inputs.webhookUrl, '^https://[^/]+.environment.api.powerplatform.com(:443)?/powerautomate/automations/direct/(.*/)?workflows/.*')) {
+ 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/[<cluster>/]workflows/...)')
}
let res := fetch(inputs.webhookUrl, {
diff --git a/posthog/cdp/templates/microsoft_teams/test_template_microsoft_teams.py b/posthog/cdp/templates/microsoft_teams/test_template_microsoft_teams.py
index eab22e797098..c9fd4e45dd7f 100644
--- a/posthog/cdp/templates/microsoft_teams/test_template_microsoft_teams.py
+++ b/posthog/cdp/templates/microsoft_teams/test_template_microsoft_teams.py
@@ -75,6 +75,10 @@ class TestTemplateMicrosoftTeams(BaseHogFunctionTemplateTest):
"https://tenant.environment.api.powerplatform.com/powerautomate/automations/direct/workflows/guid1/triggers/manual/paths/invoke?api-version=1",
True,
],
+ [
+ "https://tenant.df.environment.api.powerplatform.com:443/powerautomate/automations/direct/cu/11/workflows/guid1/triggers/manual/paths/invoke?api-version=1",
+ True,
+ ],
["https://webhook.site/def", False],
[
"https://webhook.site/def#https://prod-180.westus.logic.azure.com:443/workflows/abc/triggers/manual/paths/invoke?api-version=2016-06-01",
@@ -90,5 +94,5 @@ class TestTemplateMicrosoftTeams(BaseHogFunctionTemplateTest):
self.run_function(inputs=self._inputs(webhookUrl=url))
assert (
e.value.message # type: ignore[attr-defined]
- == "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/...)"
+ == "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/[<cluster>/]workflows/...)"
)
diff --git a/posthog/management/commands/test/test_update_hog_function_code.py b/posthog/management/commands/test/test_update_hog_function_code.py
index 7f4cb842e6b3..c3d7d41a709d 100644
--- a/posthog/management/commands/test/test_update_hog_function_code.py
+++ b/posthog/management/commands/test/test_update_hog_function_code.py
@@ -12,7 +12,7 @@ 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"
+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/...)')"
@@ -297,6 +297,33 @@ class TestUpdateHogFunctionCode(BaseTest):
# 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_powerplatform_cu_path_migration(self):
+ # A function already deployed with the stale `direct/workflows/` regex, which rejects real
+ # Power Platform environment URLs that carry a cluster segment (e.g. `/cu/11`) in the path.
+ buggy_hog = (
+ "if (not match(inputs.webhookUrl, "
+ "'^https://[^/]+.environment.api.powerplatform.com(:443)?/powerautomate/automations/direct/workflows/.*')) {\n"
+ " throw Error('Invalid URL. The URL should match ... or Power Platform environment format "
+ "(https://<tenant>.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/...)')\n}"
+ ) + _SEND_BODY
+ function = self._create_teams_function(buggy_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-cu-path", stdout=out)
+
+ function.refresh_from_db()
+ # Both the regex and the human-readable example in the error message are widened.
+ assert "automations/direct/(.*/)?workflows/.*'" in function.hog
+ assert "automations/direct/[<cluster>/]workflows/...)'" in function.hog
+ assert "automations/direct/workflows/.*'" not in function.hog
+ assert "automations/direct/workflows/...)'" not in function.hog
+ self.assertIn("Updated: 1", out.getvalue())
+ 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)
diff --git a/posthog/management/commands/update_hog_function_code.py b/posthog/management/commands/update_hog_function_code.py
index 09bfeb0cc57c..c048f73f6eb7 100644
--- a/posthog/management/commands/update_hog_function_code.py
+++ b/posthog/management/commands/update_hog_function_code.py
@@ -71,11 +71,28 @@ class Command(BaseCommand):
"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/...)')",
+ "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/[<cluster>/]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}",
+ "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/[<cluster>/]workflows/...)')\n}",
+ },
+ ],
+ },
+ # Real Power Platform environment webhook URLs carry an extra cluster segment (e.g.
+ # `/cu/11`) between `.../automations/direct/` and `/workflows/`, so the original
+ # `direct/workflows/` regex rejected valid URLs. Widen the path to allow those segments
+ # on functions already deployed with the stale pattern.
+ "microsoft-teams-powerplatform-cu-path": {
+ "template_id": "template-microsoft-teams",
+ "replacements": [
+ {
+ "from_string": "automations/direct/workflows/.*')",
+ "to_string": "automations/direct/(.*/)?workflows/.*')",
+ },
+ {
+ "from_string": "automations/direct/workflows/...)')",
+ "to_string": "automations/direct/[<cluster>/]workflows/...)')",
},
],
},