: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
feat(workflows): import customer.io automations as draft workflows (#122456)
Daniel Marchuk1aa6e70af70d
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yamlindex cfde8cf80855..f4904ec389c9 100644--- a/.semgrep/rules/security/idor-team-scoped-models.yaml+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml@@ -502,6 +502,7 @@ rules:|WizardSession|WizardWorker|WorkflowIdea+ |WorkflowImport|WorkflowProposal)$- focus-metavariable: $SINK@@ -989,6 +990,7 @@ rules:|WizardSession|WizardWorker|WorkflowIdea+ |WorkflowImport|WorkflowProposal)$# Exclude when team filter is presentdiff --git a/posthog/settings/web.py b/posthog/settings/web.pyindex f54f7d114791..025d836a1262 100644--- a/posthog/settings/web.py+++ b/posthog/settings/web.py@@ -652,6 +652,8 @@ SPECTACULAR_SETTINGS = {"AggregationEnum": ["sum", "avg", "count", "min", "max", "p95", "rate", "increase", "histogram_quantile"],# AutoresearchRun.Status and AutoresearchTrainingRun.Status share this set."ZendeskImportJobStatusEnum": "products.conversations.backend.models.zendesk_import_job.ZendeskImportJob.Status",+ # Matches the workflows WorkflowImportSource (customerio), the source of an automation import.+ "ImportProviderKeyEnum": "products.messaging.backend.models.provider_import_run.ImportProviderKey.choices",# Workflow steps and workflow template steps both take a field named `type` with this set."HogFlowActionTypeEnum": "products.workflows.backend.models.hog_flow.hog_flow.SUPPORTED_ACTION_TYPES",#diff --git a/products/workflows/backend/facade/contracts.py b/products/workflows/backend/facade/contracts.pyindex 2b7d24646289..80ed2bb5a195 100644--- a/products/workflows/backend/facade/contracts.py+++ b/products/workflows/backend/facade/contracts.py@@ -21,6 +21,7 @@ from products.workflows.backend.facade.enums import (HogFlowTemplateExitCondition,HogFlowTemplateScope,SetupStepKey,+ WorkflowImportNoteLevel,WorkflowProposalStatus,)@@ -1023,3 +1024,116 @@ class SetupStep:key: SetupStepKeydone: bool+++@frozen+class ImportNote:+ """What an import changed or left out of a workflow, for the team to review before enabling it."""++ level: WorkflowImportNoteLevel+ message: str+ # The workflow step the note is about, or None for the whole workflow.+ step_id: str | None = None+ step_name: str | None = None+++@frozen+class TranslatedWorkflow:+ source_id: str+ name: str+ workflow: dict[str, Any]+ notes: tuple[ImportNote, ...]+++@frozen+class CampaignSource:+ """A Customer.io automation as its Journeys UI API returns it, with the templates its messages use."""++ campaign: dict[str, Any]+ actions: list[dict[str, Any]]+ templates: dict[str, dict[str, Any]]+++@frozen+class ImportSender:+ """A PostHog email sender that an imported email sends from."""++ integration_id: int+ email: str+ name: str+++@frozen+class ImportCategory:+ """The PostHog message category that stands in for a Customer.io subscription topic."""++ id: str+ category_type: str+++@frozen+class ImportSegment:+ name: str+ # Customer.io's segment type: `dynamic`, or `static`/`manual` for a list people are added to by hand.+ type: str+++@frozen+class ImportOptions:+ # Customer.io snippet name to its content, inlined wherever a message includes the snippet.+ snippets: dict[str, str]+ # Customer.io segment id to the PostHog cohort that stands in for it.+ segment_cohorts: dict[str, int]+ # Customer.io layout id to its HTML, which wraps the email body at `{{ content }}`.+ layouts: dict[str, str] = field(default_factory=dict)+ # Customer.io sender identity id to its email address.+ sender_addresses: dict[str, str] = field(default_factory=dict)+ # Lower-cased email address to the team's PostHog sender for it.+ senders: dict[str, ImportSender] = field(default_factory=dict)+ # Customer.io subscription topic id to the PostHog category made from it.+ topic_categories: dict[str, ImportCategory] = field(default_factory=dict)+ # Customer.io segment id to its name and type.+ segments: dict[str, ImportSegment] = field(default_factory=dict)+++@frozen+class ImportContent:+ """Automations with the content PostHog read for them, and the options to translate them with."""++ sources: list[CampaignSource]+ options: ImportOptions+ # Per campaign id, what went wrong reading its content from Customer.io.+ fetch_notes: dict[str, tuple[ImportNote, ...]]+ fetched: bool+++@dataclass(frozen=False)+class SnippetRoom:+ """How many more bytes of snippet content one import request may inline, shared by every automation in it.++ Snippets are inlined by copy, so a short template that repeats a large one could grow without bound.+ """++ remaining: int = 5_000_000+++@frozen+class PreviousImport:+ """The earlier import of a source object, when there is one."""++ record_id: UUID | None+ hog_flow_id: UUID | None+ hog_flow_is_draft: bool+ hog_flow_created_by_id: int | None = None+++@frozen+class WorkflowImportRecord:+ id: UUID+ source: str+ source_id: str+ source_name: str+ hog_flow_id: UUID | None+ notes: tuple[dict[str, Any], ...]+ updated_at: datetime+ hog_flow_created_by_id: int | None = Nonediff --git a/products/workflows/backend/facade/enums.py b/products/workflows/backend/facade/enums.pyindex 9a8af65f0d81..7a20efb9a8fc 100644--- a/products/workflows/backend/facade/enums.py+++ b/products/workflows/backend/facade/enums.py@@ -67,6 +67,27 @@ class WorkflowIdeaSource(LabeledStrEnum):SCOUT = "scout", "Scout"+class WorkflowImportSource(LabeledStrEnum):+ CUSTOMERIO = "customerio", "Customer.io"+++class WorkflowImportNoteLevel(LabeledStrEnum):+ # The step behaves differently in PostHog, and the note says how.+ APPROXIMATED = "approximated", "Approximated"+ # The setting has no PostHog equivalent and was left out.+ UNSUPPORTED = "unsupported", "Unsupported"+ # The step is imported but needs a setting only the team can give, like an email sender.+ NEEDS_INPUT = "needs_input", "Needs input"+++class WorkflowImportStatus(LabeledStrEnum):+ PREVIEW = "preview", "Preview"+ CREATED = "created", "Created"+ UPDATED = "updated", "Updated"+ SKIPPED = "skipped", "Skipped"+ FAILED = "failed", "Failed"++class HogFlowState(LabeledStrEnum):DRAFT = "draft"ACTIVE = "active"diff --git a/products/workflows/backend/facade/workflow_imports.py b/products/workflows/backend/facade/workflow_imports.pynew file mode 100644index 000000000000..35f7b48d8005--- /dev/null+++ b/products/workflows/backend/facade/workflow_imports.py@@ -0,0 +1,17 @@+"""Workflows imported from other messaging tools, starting with Customer.io automations."""++from products.workflows.backend.services.customerio_import.content import import_content+from products.workflows.backend.services.customerio_import.service import (+ import_for_workflow,+ previous_import,+ record_import,+)+from products.workflows.backend.services.customerio_import.translator import translate_campaign++__all__ = [+ "import_content",+ "import_for_workflow",+ "previous_import",+ "record_import",+ "translate_campaign",+]diff --git a/products/workflows/backend/migrations/0039_workflowimport.py b/products/workflows/backend/migrations/0039_workflowimport.pynew file mode 100644index 000000000000..543ecd8388cd--- /dev/null+++ b/products/workflows/backend/migrations/0039_workflowimport.py@@ -0,0 +1,93 @@+# Generated by Django 5.2.18 on 2026-10-23 00:08++import django.db.models.deletion+from django.conf import settings+from django.db import migrations, models++import posthog.uuidt+++class Migration(migrations.Migration):+ dependencies = [+ ("posthog", "1411_personalapikey_expires_at"),+ ("workflows", "0038_hogflow_team_source_record_idx"),+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),+ ]++ operations = [+ migrations.CreateModel(+ name="WorkflowImport",+ fields=[+ (+ "id",+ models.UUIDField(+ default=posthog.uuidt.uuid7,+ editable=False,+ primary_key=True,+ serialize=False,+ ),+ ),+ (+ "source",+ models.CharField(choices=[("customerio", "Customer.io")], max_length=40),+ ),+ (+ "source_id",+ models.CharField(help_text="The object's id in the source tool.", max_length=200),+ ),+ (+ "source_name",+ models.CharField(blank=True, default="", max_length=400),+ ),+ (+ "notes",+ models.JSONField(+ blank=True,+ default=list,+ help_text="What the import changed or left out: a list of {level, message, step_id, step_name}.",+ ),+ ),+ ("created_at", models.DateTimeField(auto_now_add=True)),+ ("updated_at", models.DateTimeField(auto_now=True)),+ (+ "created_by",+ models.ForeignKey(+ blank=True,+ db_constraint=False,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to=settings.AUTH_USER_MODEL,+ ),+ ),+ (+ "hog_flow",+ models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to="workflows.hogflow",+ ),+ ),+ (+ "team",+ models.ForeignKey(+ db_constraint=False,+ on_delete=django.db.models.deletion.CASCADE,+ related_name="+",+ to="posthog.team",+ ),+ ),+ ],+ options={+ "indexes": [models.Index(fields=["team", "hog_flow"], name="workflow_import_flow_idx")],+ "constraints": [+ models.UniqueConstraint(+ fields=("team", "source", "source_id"),+ name="unique_workflow_import_source",+ )+ ],+ },+ ),+ ]diff --git a/products/workflows/backend/migrations/max_migration.txt b/products/workflows/backend/migrations/max_migration.txtindex 2ce22c5893e0..b8a83e817ac3 100644--- a/products/workflows/backend/migrations/max_migration.txt+++ b/products/workflows/backend/migrations/max_migration.txt@@ -1 +1 @@-0038_hogflow_team_source_record_idx+0039_workflowimportdiff --git a/products/workflows/backend/models/__init__.py b/products/workflows/backend/models/__init__.pyindex bbc6b3a22a6c..7bfb7e8871b6 100644--- a/products/workflows/backend/models/__init__.py+++ b/products/workflows/backend/models/__init__.py@@ -6,6 +6,7 @@ from .hog_flow_revision import HogFlowRevisionfrom .hog_flow_schedule.hog_flow_schedule import HogFlowSchedulefrom .team_workflows_config import TeamWorkflowsConfigfrom .workflow_idea import WorkflowIdea+from .workflow_import import WorkflowImportfrom .workflow_proposal import WorkflowProposal__all__ = [@@ -17,5 +18,6 @@ __all__ = ["HogFlowTemplate","TeamWorkflowsConfig","WorkflowIdea",+ "WorkflowImport","WorkflowProposal",]diff --git a/products/workflows/backend/models/workflow_import.py b/products/workflows/backend/models/workflow_import.pynew file mode 100644index 000000000000..09993e015160--- /dev/null+++ b/products/workflows/backend/models/workflow_import.py@@ -0,0 +1,44 @@+from django.db import models++from posthog.models.scoping.root_mixin import TeamScopedRootMixin+from posthog.models.utils import UUIDModel++from products.workflows.backend.facade.enums import WorkflowImportSource+++class WorkflowImport(TeamScopedRootMixin, UUIDModel):+ """A workflow imported from another messaging tool, with what the import could not carry over.++ The row ties the source object to its draft, so importing the same object again updates that draft+ instead of creating a second one. Only configuration is stored, never people.+ """++ Source = WorkflowImportSource++ class Meta:+ constraints = [+ models.UniqueConstraint(fields=["team", "source", "source_id"], name="unique_workflow_import_source"),+ ]+ indexes = [models.Index(fields=["team", "hog_flow"], name="workflow_import_flow_idx")]++ # db_constraint=False: a real FK constraint to a hot table takes a parent-table lock on creation.+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False, related_name="+")+ source = models.CharField(max_length=40, choices=Source.choices)+ source_id = models.CharField(max_length=200, help_text="The object's id in the source tool.")+ source_name = models.CharField(max_length=400, blank=True, default="")+ hog_flow = models.ForeignKey(+ "workflows.HogFlow", on_delete=models.SET_NULL, null=True, blank=True, related_name="+"+ )+ notes = models.JSONField(+ default=list,+ blank=True,+ help_text="What the import changed or left out: a list of {level, message, step_id, step_name}.",+ )+ created_at = models.DateTimeField(auto_now_add=True)+ updated_at = models.DateTimeField(auto_now=True)+ created_by = models.ForeignKey(+ "posthog.User", on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False, related_name="+"+ )++ def __str__(self) -> str:+ return f"WorkflowImport {self.team_id}/{self.source}/{self.source_id}"diff --git a/products/workflows/backend/presentation/views/data_suggestion_drafts.py b/products/workflows/backend/presentation/views/data_suggestion_drafts.pyindex a7106fd6a779..cb688cc98661 100644--- a/products/workflows/backend/presentation/views/data_suggestion_drafts.py+++ b/products/workflows/backend/presentation/views/data_suggestion_drafts.py@@ -5,6 +5,7 @@ from uuid import UUIDimport structlogfrom rest_framework.exceptions import ValidationError+from rest_framework.request import Requestfrom posthog.event_usage import EventSourcefrom posthog.models import Team, User@@ -18,21 +19,34 @@ from products.workflows.backend.presentation.views.hog_flow import HogFlowSeriallogger = structlog.get_logger(__name__)-def validated_draft(team: Team, workflow: dict[str, Any], event_source: EventSource) -> dict[str, Any] | None:- """The draft as the create endpoint would store it, or None when the create endpoint would reject it."""+def checked_draft(+ team: Team, workflow: dict[str, Any], event_source: EventSource, request: Request | None = None+) -> dict[str, Any]:+ """The draft as the create endpoint would store it. Raises the `ValidationError` the create endpoint would."""# Checked the way the caller's own create request will be: the web editor saves drafts with blank inputs, an API# client's draft must be complete. The graph wiring is checked in full either way.serializer = HogFlowSerializer(data={**workflow, "status": "draft"},- context={"team_id": team.id, "get_team": lambda: team, "is_draft": True, "event_source": event_source},+ context={+ "team_id": team.id,+ "get_team": lambda: team,+ "is_draft": True,+ "event_source": event_source,+ # Checks gated on the user, like cohort conditions, need the caller's request.+ "request": request,+ },)+ validate_graph(workflow["actions"], workflow["edges"])+ serializer.is_valid(raise_exception=True)+ return dict(serializer.validated_data)+++def validated_draft(team: Team, workflow: dict[str, Any], event_source: EventSource) -> dict[str, Any] | None:try:- validate_graph(workflow["actions"], workflow["edges"])- serializer.is_valid(raise_exception=True)+ return checked_draft(team, workflow, event_source)except ValidationError as error:logger.info("workflows.data_suggestions.invalid_workflow", team_id=team.id, errors=str(error.detail)[:500])return None- return dict(serializer.validated_data)def save_draft(team: Team, user: User, validated: dict[str, Any]) -> str:diff --git a/products/workflows/backend/presentation/views/workflow_import_drafts.py b/products/workflows/backend/presentation/views/workflow_import_drafts.pynew file mode 100644index 000000000000..052ae0b6c68b--- /dev/null+++ b/products/workflows/backend/presentation/views/workflow_import_drafts.py@@ -0,0 +1,260 @@+"""Turns Customer.io automations into draft workflows, checked the way the create endpoint checks them."""++import time+from dataclasses import asdict+from typing import Any, cast+from uuid import UUID++from django.db import IntegrityError, transaction++import structlog+from rest_framework.exceptions import ValidationError+from rest_framework.request import Request++from posthog.dataclasses import frozen+from posthog.event_usage import EventSource+from posthog.models import Team, User++from products.access_control.backend.facade.contracts import ObjectAccessRef+from products.access_control.backend.facade.user_access_control import UserAccessControl+from products.workflows.backend.facade.activity import log_workflow_activity+from products.workflows.backend.facade.contracts import (+ CampaignSource,+ ImportNote,+ ImportOptions,+ PreviousImport,+ SnippetRoom,+ WorkflowActor,+ WorkflowStale,+)+from products.workflows.backend.facade.enums import HogFlowState, WorkflowImportNoteLevel+from products.workflows.backend.facade.workflow_imports import (+ import_content,+ previous_import,+ record_import,+ translate_campaign,+)+from products.workflows.backend.facade.writes import create_workflow, defer_hog_flow_reloads, update_workflow+from products.workflows.backend.presentation.views.data_suggestion_drafts import checked_draft++logger = structlog.get_logger(__name__)+++@frozen+class ImportRun:+ results: list[dict[str, Any]]+ # Whether PostHog read message content from Customer.io with the team's stored App API key.+ content_fetched: bool+++def import_campaigns(+ team: Team,+ user: User,+ payloads: list[dict[str, Any]],+ options: ImportOptions,+ dry_run: bool,+ sender_identities: list[dict[str, Any]],+ segments: list[dict[str, Any]],+ request: Request,+) -> ImportRun:+ deadline = time.monotonic() + _REQUEST_SECONDS+ sources = [_campaign_source(payload) for payload in payloads]+ context = _RequestContext(+ request=request,+ access=UserAccessControl(user=user, team=team),+ previous={source_id: previous_import(team, source_id) for source_id in {_source_id(s) for s in sources}},+ snippet_room=SnippetRoom(),+ )+ content = import_content(+ team.id, sources, options, sender_identities, segments, deadline, _left_alone(team, context)+ )+ with defer_hog_flow_reloads():+ results = [+ import_campaign(+ team,+ user,+ source,+ content.options,+ dry_run,+ content.fetch_notes.get(_source_id(source), ()),+ context,+ )+ for source in content.sources+ ]+ return ImportRun(results=results, content_fetched=content.fetched)+++# The import runs inside the request, so everything it reads from Customer.io shares one time limit.+_REQUEST_SECONDS = 60+++@frozen+class _RequestContext:+ """What every automation in one import request shares."""++ request: Request+ access: UserAccessControl+ # The earlier import of each automation in the request, read once for both eligibility checks.+ previous: dict[str, PreviousImport]+ snippet_room: SnippetRoom+++def _source_id(source: CampaignSource) -> str:+ return str(source.campaign.get("id"))+++def _left_alone(team: Team, context: _RequestContext) -> frozenset[str]:+ """The ids of automations whose earlier import the user can't change, either way `import_campaign` skips them."""+ return frozenset(+ source_id+ for source_id, previous in context.previous.items()+ if previous.hog_flow_id is not None+ and (not previous.hog_flow_is_draft or not _can_edit(context.access, team, previous))+ )+++def _campaign_source(payload: dict[str, Any]) -> CampaignSource:+ templates = payload.get("templates") or {}+ if isinstance(templates, list):+ templates = {str(template.get("id")): template for template in templates}+ return CampaignSource(+ campaign=payload["campaign"],+ actions=payload["actions"],+ templates={str(key): value for key, value in templates.items()},+ )+++def import_campaign(+ team: Team,+ user: User,+ source: CampaignSource,+ options: ImportOptions,+ dry_run: bool,+ extra_notes: tuple[ImportNote, ...],+ context: _RequestContext,+) -> dict[str, Any]:+ """Imports one Customer.io automation, or previews it, and returns its result in the response shape."""+ try:+ translated = translate_campaign(source, options, context.snippet_room)+ except (ValueError, TypeError, AttributeError, KeyError, IndexError, RecursionError) as error:+ logger.info("workflows.import.unreadable_campaign", team_id=team.id, error=type(error).__name__)+ return {+ "source_id": _source_id(source),+ "name": str(source.campaign.get("name") or ""),+ "workflow_id": None,+ "notes": [asdict(note) for note in extra_notes],+ "validation_errors": [+ "Customer.io sent a value PostHog could not read, such as a delay that is not a number. "+ "Check the automation in Customer.io and import it again."+ ],+ "status": "failed",+ }+ notes = (*extra_notes, *translated.notes)+ result: dict[str, Any] = {+ "source_id": translated.source_id,+ "name": translated.name,+ "workflow_id": None,+ "notes": [asdict(note) for note in notes],+ "validation_errors": [],+ }+ previous = context.previous.get(translated.source_id) or previous_import(team, translated.source_id)+ if previous.hog_flow_id is not None and not _can_edit(context.access, team, previous):+ return _skipped(+ result,+ previous.hog_flow_id,+ "You can't edit the workflow an earlier import of this automation created, so it was left unchanged.",+ )+ if previous.hog_flow_id is not None and not previous.hog_flow_is_draft:+ return _skipped(result, previous.hog_flow_id, _NO_LONGER_DRAFT)+ validated, errors = _validated_draft(team, translated.workflow, context.request)+ if validated is None:+ return {**result, "status": "failed", "validation_errors": errors}+ if dry_run:+ replaces = str(previous.hog_flow_id) if previous.hog_flow_id is not None else None+ return {**result, "status": "preview", "workflow_id": replaces}+ created_name: str | None = None+ try:+ with transaction.atomic():+ if previous.hog_flow_id is not None:+ # Without `status` in the payload, the update refuses a workflow someone enabled since the check above.+ update_workflow(+ team_id=team.id,+ user_id=user.id,+ hog_flow_id=previous.hog_flow_id,+ validated_data={key: value for key, value in validated.items() if key != "status"},+ route_to_draft=False,+ base_updated_at=None,+ base_live_updated_at=None,+ includes_staged_draft=False,+ validated_status=HogFlowState.DRAFT.value,+ )+ workflow_id, status = previous.hog_flow_id, "updated"+ else:+ created = create_workflow(team_id=team.id, user_id=user.id, validated_data=validated)+ workflow_id, status = cast(UUID, created.current["id"]), "created"+ created_name = cast("str | None", created.current.get("name"))+ record_import(+ team=team,+ user=user,+ record_id=previous.record_id,+ source_id=translated.source_id,+ name=translated.name,+ hog_flow_id=workflow_id,+ notes=notes,+ )+ except WorkflowStale:+ return _skipped(result, previous.hog_flow_id, _NO_LONGER_DRAFT)+ except IntegrityError:+ # Another request imported the same automation first, and the unique record rolled this one back.+ return {+ **result,+ "status": "failed",+ "validation_errors": ["Another import of this automation ran at the same time. Import it again."],+ }+ if status == "created":+ log_workflow_activity(+ actor=WorkflowActor(+ organization_id=team.organization_id, team_id=team.id, user=user, was_impersonated=False+ ),+ workflow_id=workflow_id,+ name=created_name,+ activity="created",+ detail_type="standard",+ )+ return {**result, "status": status, "workflow_id": str(workflow_id)}+++_NO_LONGER_DRAFT = (+ "This automation was imported before and that workflow is no longer a draft, so it was left unchanged."+)+++def _skipped(result: dict[str, Any], workflow_id: UUID | None, message: str) -> dict[str, Any]:+ note = ImportNote(level=WorkflowImportNoteLevel.APPROXIMATED, message=message)+ return {**result, "status": "skipped", "workflow_id": str(workflow_id), "notes": [asdict(note)]}+++def _can_edit(access: UserAccessControl, team: Team, previous: PreviousImport) -> bool:+ ref = ObjectAccessRef(+ resource="hog_flow",+ id=str(previous.hog_flow_id),+ team_id=team.id,+ created_by_id=previous.hog_flow_created_by_id,+ )+ return access.check_access_level_for_ref(ref, "editor")+++def _validated_draft(team: Team, workflow: dict[str, Any], request: Request) -> tuple[dict[str, Any] | None, list[str]]:+ try:+ return checked_draft(team, workflow, EventSource.API, request), []+ except ValidationError as error:+ logger.info("workflows.import.invalid_workflow", team_id=team.id, errors=str(error.detail)[:500])+ return None, _flatten_errors(error.detail)+++def _flatten_errors(detail: Any, prefix: str = "") -> list[str]:+ if isinstance(detail, dict):+ return [message for key, value in detail.items() for message in _flatten_errors(value, f"{prefix}{key}.")]+ if isinstance(detail, list):+ return [message for value in detail for message in _flatten_errors(value, prefix)]+ return [f"{prefix.rstrip('.')}: {detail}" if prefix else str(detail)]diff --git a/products/workflows/backend/presentation/views/workflow_imports.py b/products/workflows/backend/presentation/views/workflow_imports.pynew file mode 100644index 000000000000..e8e627de4601--- /dev/null+++ b/products/workflows/backend/presentation/views/workflow_imports.py@@ -0,0 +1,279 @@+"""Imports automations from other messaging tools as draft workflows, and reads back what each import left out."""++from typing import Any, cast+from uuid import UUID++from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema+from rest_framework import serializers, status, viewsets+from rest_framework.decorators import action+from rest_framework.exceptions import ValidationError+from rest_framework.request import Request+from rest_framework.response import Response++from posthog.api.mixins import ValidatedRequest, validated_request+from posthog.api.routing import TeamAndOrgViewSetMixin+from posthog.event_usage import report_user_action+from posthog.models import User+from posthog.permissions import PostHogFeatureFlagPermission++from products.access_control.backend.facade.contracts import ObjectAccessRef+from products.workflows.backend.facade.contracts import ImportOptions, WorkflowImportRecord+from products.workflows.backend.facade.enums import WorkflowImportNoteLevel, WorkflowImportSource, WorkflowImportStatus+from products.workflows.backend.facade.workflow_imports import import_for_workflow+from products.workflows.backend.presentation.views.workflow_import_drafts import import_campaigns++MAX_CAMPAIGNS_PER_IMPORT = 50+MAX_SOURCE_ID_LENGTH = 200+++class CustomerioCampaignSerializer(serializers.Serializer):+ campaign = serializers.DictField(+ help_text=(+ "The `campaign` object from Customer.io's `GET /v1/environments/{environment_id}/campaigns/{campaign_id}` "+ "(Journeys UI API, what the Customer.io CLI and MCP read). Must include `id`, `type`, `edges` and the "+ "trigger fields."+ )+ )+ actions = serializers.ListField(+ child=serializers.DictField(),+ help_text="The top-level `actions` array from the same response, every step of the automation.",+ )+ templates = serializers.JSONField(+ required=False,+ help_text=(+ "The message templates the actions point at, from `GET /v1/environments/{environment_id}/templates/"+ "{template_id}`: a list of template objects, or an object keyed by template id. Leave them out when "+ "the project stored a Customer.io App API key: PostHog then reads the content from Customer.io itself. "+ "Without either, messages import with empty content."+ ),+ )++ def validate_campaign(self, value: dict[str, Any]) -> dict[str, Any]:+ campaign_id = value.get("id")+ if campaign_id is None or isinstance(campaign_id, bool) or not isinstance(campaign_id, str | int):+ raise serializers.ValidationError("The campaign needs its Customer.io `id`.")+ if len(str(campaign_id)) > MAX_SOURCE_ID_LENGTH:+ raise serializers.ValidationError(f"The campaign `id` can be at most {MAX_SOURCE_ID_LENGTH} characters.")+ return value++ def validate_templates(self, value: Any) -> Any:+ templates = list(value.values()) if isinstance(value, dict) else value+ if not isinstance(templates, list) or not all(isinstance(template, dict) for template in templates):+ raise serializers.ValidationError("Pass a list of template objects, or an object keyed by template id.")+ return value+++class CustomerioSenderIdentitySerializer(serializers.Serializer):+ id = serializers.CharField(help_text="The sender identity's id, which templates name in `from_identity_id`.")+ email = serializers.EmailField(help_text="The address the sender sends from.")+ name = serializers.CharField(required=False, allow_blank=True, default="", help_text="The sender's display name.")+++class CustomerioSegmentSerializer(serializers.Serializer):+ id = serializers.CharField(help_text="The segment's id.")+ name = serializers.CharField(allow_blank=True, help_text="The segment's name.")+ type = serializers.CharField(help_text="The segment's type: `dynamic`, or `static` for a hand-made list.")+++class WorkflowImportRequestSerializer(serializers.Serializer):+ source = serializers.ChoiceField( # type: ignore[assignment] # field named `source` shadows DRF Field.source+ choices=WorkflowImportSource.choices,+ help_text="The tool the automations come from.",+ )+ campaigns = CustomerioCampaignSerializer(+ many=True,+ help_text=f"The automations to import, at most {MAX_CAMPAIGNS_PER_IMPORT}. People are never read from these objects.",+ )+ snippets = serializers.DictField(+ child=serializers.CharField(allow_blank=True),+ required=False,+ default=dict,+ help_text='Customer.io snippet name to its content, inlined where a message uses `{% snippet "name" %}`.',+ )+ segment_cohorts = serializers.DictField(+ child=serializers.IntegerField(),+ required=False,+ default=dict,+ help_text=(+ "Customer.io segment id to the PostHog cohort that replaces it. Segment triggers and segment "+ "conditions with no cohort here are left for the team to set."+ ),+ )++ def validate_campaigns(self, value: list[dict[str, Any]]) -> list[dict[str, Any]]:+ if not value:+ raise serializers.ValidationError("Pass at least one automation.")+ if len(value) > MAX_CAMPAIGNS_PER_IMPORT:+ raise serializers.ValidationError(f"Import at most {MAX_CAMPAIGNS_PER_IMPORT} automations at a time.")+ return value++ layouts = serializers.DictField(+ child=serializers.CharField(allow_blank=True),+ required=False,+ default=dict,+ help_text=(+ "Customer.io layout id to the layout's `body`, from `GET /v1/environments/{environment_id}/layouts/"+ "{layout_id}`. An email whose template names a `layout_id` is wrapped in that layout at `{{ content }}`."+ ),+ )+ sender_identities = CustomerioSenderIdentitySerializer(+ many=True,+ required=False,+ default=list,+ help_text=(+ "Customer.io sender identities, from `GET /v1/environments/{environment_id}/identities?type=email`. "+ "Each email gets the PostHog sender with the same address."+ ),+ )+ segments = CustomerioSegmentSerializer(+ many=True,+ required=False,+ default=list,+ help_text=(+ "Customer.io segment ids, names and types, without members, so the notes can name each segment and "+ "flag static ones."+ ),+ )+ dry_run = serializers.BooleanField(+ default=False,+ help_text="When true, translate and check the automations but save nothing, to preview the import.",+ )+++class WorkflowImportNoteSerializer(serializers.Serializer):+ level = serializers.ChoiceField(+ choices=WorkflowImportNoteLevel.choices,+ help_text=(+ "`approximated`: the step behaves differently in PostHog. `unsupported`: the setting was left out. "+ "`needs_input`: the team must set something before enabling."+ ),+ )+ message = serializers.CharField(help_text="What changed or was left out, and what to do about it.")+ step_id = serializers.CharField(allow_null=True, help_text="The workflow step the note is about, or null.")+ step_name = serializers.CharField(allow_null=True, help_text="That step's name, or null.")+++class WorkflowImportResultSerializer(serializers.Serializer):+ source_id = serializers.CharField(help_text="The automation's id in Customer.io.")+ name = serializers.CharField(help_text="The automation's name.")+ status = serializers.ChoiceField(+ choices=WorkflowImportStatus.choices,+ help_text=(+ "`preview` when dry_run is set, `created` or `updated` for a saved draft, `skipped` when an earlier "+ "import of it is no longer a draft, `failed` when the draft does not pass the workflow checks."+ ),+ )+ workflow_id = serializers.CharField(+ allow_null=True,+ help_text="The draft workflow's id, when one was saved. In a preview, the draft an import would replace.",+ )+ notes = WorkflowImportNoteSerializer(many=True, help_text="What the import changed or left out.")+ validation_errors = serializers.ListField(+ child=serializers.CharField(), help_text="Why the draft failed the workflow checks, for `failed`."+ )+++class WorkflowImportResponseSerializer(serializers.Serializer):+ results = WorkflowImportResultSerializer(many=True, help_text="One result per automation, in request order.")+ content_fetched = serializers.BooleanField(+ help_text="True when PostHog read message content from Customer.io with the project's stored App API key."+ )+++class WorkflowImportRecordSerializer(serializers.Serializer):+ id = serializers.UUIDField(read_only=True, help_text="The import record's id.")+ source = serializers.CharField(read_only=True, help_text="The tool the workflow was imported from.") # type: ignore[assignment]+ source_id = serializers.CharField(read_only=True, help_text="The automation's id in that tool.")+ source_name = serializers.CharField(read_only=True, help_text="The automation's name in that tool.")+ hog_flow = serializers.UUIDField(source="hog_flow_id", read_only=True, allow_null=True, help_text="The workflow.")+ notes = WorkflowImportNoteSerializer(many=True, read_only=True, help_text="What the import changed or left out.")+ updated_at = serializers.DateTimeField(read_only=True, help_text="When the workflow was last imported.")+++class WorkflowImportViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):+ scope_object = "hog_flow"+ serializer_class = WorkflowImportRequestSerializer+ scope_object_read_actions = ["for_workflow"]+ # Only importing is gated: the editor reads `for_workflow` on every workflow, flag or not.+ posthog_feature_flag = {"workflows-customerio-import": ["create"]}+ permission_classes = [PostHogFeatureFlagPermission]++ @validated_request(+ WorkflowImportRequestSerializer,+ responses={200: OpenApiResponse(response=WorkflowImportResponseSerializer)},+ summary="Import Customer.io automations as draft workflows",+ description=(+ "Turns Customer.io automations into draft workflows. Pass each automation as Customer.io's Journeys UI "+ "API returns it. Pass the templates its messages use, or leave them out when the project stored a "+ "Customer.io App API key, and PostHog reads them. Each result lists what the import changed or "+ "left out. Importing an automation again updates its draft, unless that workflow is no longer a draft. "+ "Set dry_run to preview without saving."+ ),+ )+ def create(self, request: ValidatedRequest, **kwargs: Any) -> Response:+ data = request.validated_data+ user = cast(User, request.user)+ options = ImportOptions(+ snippets=dict(data["snippets"]),+ segment_cohorts={str(key): int(value) for key, value in data["segment_cohorts"].items()},+ layouts={str(key): value for key, value in data["layouts"].items()},+ )+ run = import_campaigns(+ self.team,+ user,+ data["campaigns"],+ options,+ data["dry_run"],+ sender_identities=list(data["sender_identities"]),+ segments=list(data["segments"]),+ request=request,+ )+ results = run.results+ report_user_action(+ user,+ "workflows import run",+ {+ "source": data["source"],+ "dry_run": data["dry_run"],+ "campaign_count": len(results),+ "statuses": [result["status"] for result in results],+ "note_count": sum(len(result["notes"]) for result in results),+ "content_fetched": run.content_fetched,+ },+ team=self.team,+ request=request,+ )+ return Response(+ WorkflowImportResponseSerializer({"results": results, "content_fetched": run.content_fetched}).data+ )++ @extend_schema(+ parameters=[+ OpenApiParameter("hog_flow_id", type=str, required=True, description="The workflow to read the import of.")+ ],+ responses={+ 200: OpenApiResponse(response=WorkflowImportRecordSerializer),+ 204: OpenApiResponse(description="The workflow was not imported."),+ },+ summary="Read what importing a workflow changed or left out",+ )+ @action(detail=False, methods=["get"], url_path="for_workflow")+ def for_workflow(self, request: Request, **kwargs: Any) -> Response:+ try:+ hog_flow_id = UUID(str(request.query_params.get("hog_flow_id")))+ except ValueError:+ raise ValidationError({"hog_flow_id": "Pass the workflow's id."})+ record = import_for_workflow(self.team, hog_flow_id)+ if record is None or not self._can_view(record):+ # Most workflows were never imported, so that is an empty answer rather than an error.+ return Response(status=status.HTTP_204_NO_CONTENT)+ return Response(WorkflowImportRecordSerializer(record).data)++ def _can_view(self, record: WorkflowImportRecord) -> bool:+ ref = ObjectAccessRef(+ resource="hog_flow",+ id=str(record.hog_flow_id),+ team_id=self.team.id,+ created_by_id=record.hog_flow_created_by_id,+ )+ return self.user_access_control.check_access_level_for_ref(ref, "viewer")diff --git a/products/workflows/backend/routes.py b/products/workflows/backend/routes.pyindex ce23f8693d32..da34cf80ecdc 100644--- a/products/workflows/backend/routes.py+++ b/products/workflows/backend/routes.py@@ -13,6 +13,7 @@ from products.workflows.backend.presentation.views import (people_imports,workflow_classifications,workflow_data_suggestions,+ workflow_imports,workflow_message_opt_outs,workflow_scout_runs,workflow_sending_rules,@@ -76,6 +77,12 @@ def register_routes(routers: RouterRegistry) -> None:"project_workflow_classifications",["team_id"],)+ routers.projects.register(+ r"workflow_imports",+ workflow_imports.WorkflowImportViewSet,+ "project_workflow_imports",+ ["team_id"],+ )routers.projects.register(r"workflow_people_imports",people_imports.WorkflowPeopleImportViewSet,diff --git a/products/workflows/backend/services/customerio_import/__init__.py b/products/workflows/backend/services/customerio_import/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/workflows/backend/services/customerio_import/app_api.py b/products/workflows/backend/services/customerio_import/app_api.pynew file mode 100644index 000000000000..b356097ddb36--- /dev/null+++ b/products/workflows/backend/services/customerio_import/app_api.py@@ -0,0 +1,115 @@+"""Reads automation message content from Customer.io's App API with the team's own App API key.++The key belongs to the customer's Customer.io account and no other PostHog process shares it, so the client paces+itself under Customer.io's documented limit of 10 requests per second and honors 429 with Retry-After itself.+"""++import time+from typing import Any++import requests+import structlog++logger = structlog.get_logger(__name__)++_BASE_URLS = {"us": "https://api.customer.io/v1", "eu": "https://api-eu.customer.io/v1"}+_MIN_INTERVAL_SECONDS = 0.125+_MAX_RETRY_AFTER_SECONDS = 10.0+_MAX_ATTEMPTS = 3+_MAX_PAGES = 100+_TIMEOUT_SECONDS = 20+++class CustomerioApiError(Exception):+ def __init__(self, message: str, status: int | None = None) -> None:+ super().__init__(message)+ self.status = status+++class CustomerioAppApi:+ def __init__(self, api_key: str, deadline: float) -> None:+ self._api_key = api_key+ self._deadline = deadline+ self._base_url: str | None = None+ self._last_request_at = 0.0+ self._session = requests.Session()++ def campaign_actions(self, campaign_id: str) -> list[dict[str, Any]]:+ """Every message, webhook and attribute update tile of an automation, one object per language variant."""+ return self._paged(f"/campaigns/{campaign_id}/actions", "actions")++ def sender_identities(self) -> list[dict[str, Any]]:+ return self._paged("/sender_identities", "sender_identities")++ def segments(self) -> list[dict[str, Any]]:+ return list(self._get("/segments").get("segments") or [])++ def _paged(self, path: str, key: str) -> list[dict[str, Any]]:+ items: list[dict[str, Any]] = []+ start: str | None = None+ for _ in range(_MAX_PAGES):+ page = self._get(path, {"start": start} if start else None)+ items.extend(item for item in page.get(key) or [] if isinstance(item, dict))+ start = page.get("next") or None+ if not start:+ break+ return items++ def _get(self, path: str, params: dict[str, Any] | None = None) -> dict[str, Any]:+ if self._base_url is None:+ self._base_url = self._detect_region()+ return self._request(self._base_url, path, params)++ def _detect_region(self) -> str:+ # The key does not say which region its account lives in, so the first region that accepts it wins.+ for region in ("us", "eu"):+ try:+ self._request(_BASE_URLS[region], "/subscription_topics", None)+ except CustomerioApiError as error:+ if error.status in (401, 403):+ continue+ raise+ return _BASE_URLS[region]+ raise CustomerioApiError("Customer.io rejected the stored App API key in both regions.", status=401)++ def _request(self, base_url: str, path: str, params: dict[str, Any] | None) -> dict[str, Any]:+ for attempt in range(_MAX_ATTEMPTS):+ wait = self._last_request_at + _MIN_INTERVAL_SECONDS - time.monotonic()+ if wait > 0:+ time.sleep(wait)+ remaining = self._deadline - time.monotonic()+ if remaining <= 1:+ raise CustomerioApiError("Reading from Customer.io took too long.")+ self._last_request_at = time.monotonic()+ try:+ response = self._session.get(+ f"{base_url}{path}",+ params=params,+ headers={"Authorization": f"Bearer {self._api_key}", "Accept": "application/json"},+ timeout=min(_TIMEOUT_SECONDS, remaining),+ )+ except requests.RequestException as error:+ raise CustomerioApiError(f"Customer.io could not be reached: {type(error).__name__}") from error+ if response.status_code == 429 and attempt < _MAX_ATTEMPTS - 1:+ time.sleep(_retry_after(response))+ continue+ if response.status_code >= 400:+ # The body can echo request details, so only the status is logged.+ logger.info("workflows.import.customerio_api_error", path=path, status=response.status_code)+ raise CustomerioApiError(+ f"Customer.io answered {response.status_code} for {path}.", status=response.status_code+ )+ try:+ body = response.json()+ except ValueError as error:+ raise CustomerioApiError(f"Customer.io returned a response that is not JSON for {path}.") from error+ return body if isinstance(body, dict) else {}+ raise CustomerioApiError(f"Customer.io kept rate limiting {path}.", status=429)+++def _retry_after(response: requests.Response) -> float:+ try:+ seconds = float(response.headers.get("Retry-After", "1"))+ except ValueError:+ seconds = 1.0+ return min(max(seconds, 0.5), _MAX_RETRY_AFTER_SECONDS)diff --git a/products/workflows/backend/services/customerio_import/conditions.py b/products/workflows/backend/services/customerio_import/conditions.pynew file mode 100644index 000000000000..73660bdea1b9--- /dev/null+++ b/products/workflows/backend/services/customerio_import/conditions.py@@ -0,0 +1,227 @@+"""Customer.io conditions as PostHog property filters.++Customer.io stores action conditions as a flat array, double encoded: JSON, then URL-encoded, then base64.+Entry filters and exit conditions use a nested `and`/`or` tree instead. Both read the same leaf operators.+"""++import json+import base64+import binascii+from datetime import UTC, datetime+from typing import Any+from urllib.parse import unquote++from posthog.dataclasses import frozen++_OPERATORS = {+ "eq": "exact",+ "!eq": "is_not",+ "contains": "icontains",+ "!contains": "not_icontains",+ "gt": "gt",+ "lt": "lt",+ "exists": "is_set",+ "!exists": "is_not_set",+}+_NEGATED = {+ "exact": "is_not",+ "is_not": "exact",+ "icontains": "not_icontains",+ "not_icontains": "icontains",+ "is_set": "is_not_set",+ "is_not_set": "is_set",+ "gt": "lte",+ "lt": "gte",+}+_UNARY = {"is_set", "is_not_set"}+_SECONDS_PER_DAY = 86400+++@frozen+class Condition:+ """One Customer.io condition array, translated."""++ # Person and event property filters, all of which must match.+ properties: tuple[dict[str, Any], ...] = ()+ # Events to wait for. Each is a PostHog event filter that can carry its own properties.+ events: tuple[dict[str, Any], ...] = ()+ # Readable descriptions of the parts that have no PostHog equivalent and were left out.+ skipped: tuple[str, ...] = ()+++def decode(encoded: str | None) -> list[dict[str, Any]] | None:+ """The condition array behind an encoded string, or None when it does not decode."""+ if not encoded:+ return None+ try:+ raw = base64.b64decode(encoded, validate=False).decode("utf-8")+ except (binascii.Error, UnicodeDecodeError, ValueError):+ return None+ # Conditions saved through the app are URL-encoded before base64, older ones are not. Plain JSON is read+ # first, so a value that holds a percent escape, like a URL, keeps it.+ for candidate in (raw, unquote(raw)):+ try:+ parsed = json.loads(candidate)+ except ValueError:+ continue+ if isinstance(parsed, dict):+ parsed = [parsed]+ if isinstance(parsed, list) and all(isinstance(item, dict) for item in parsed):+ return parsed+ return None+++def translate_condition(conditions: list[dict[str, Any]], segment_cohorts: dict[str, int]) -> Condition:+ properties: list[dict[str, Any]] = []+ events: list[dict[str, Any]] = []+ skipped: list[str] = []+ for condition in conditions:+ kind = condition.get("type")+ if kind in ("attribute", "event_attribute"):+ filters = property_filters(condition, "person" if kind == "attribute" else "event")+ if filters is None:+ skipped.append(describe(condition))+ else:+ properties.extend(filters)+ elif kind in ("segments_negatable", "segments"):+ cohort_filters = _cohort_filters(condition, segment_cohorts)+ if cohort_filters is None:+ skipped.append(describe(condition))+ else:+ properties.extend(cohort_filters)+ elif kind == "foreign_event" and condition.get("event_name") and not condition.get("event_type"):+ name = str(condition["event_name"])+ events.append({"id": name, "name": name, "type": "events", "order": len(events), "properties": []})+ elif kind == "foreign_event_attribute" and events:+ filters = property_filters(condition, "event")+ if filters is None:+ skipped.append(describe(condition))+ else:+ events[-1]["properties"].extend(filters)+ else:+ skipped.append(describe(condition))+ return Condition(properties=tuple(properties), events=tuple(events), skipped=tuple(skipped))+++def translate_filter_tree(tree: Any, property_type: str) -> tuple[list[dict[str, Any]], list[str]]:+ """Flattens an `and` tree of `{field, operator, value}` leaves into property filters.++ PostHog event and trigger filters are a flat AND list, so an `or` is left out and described instead.+ """+ if not isinstance(tree, dict) or not tree:+ return [], []+ if "and" in tree and isinstance(tree["and"], list):+ properties: list[dict[str, Any]] = []+ skipped: list[str] = []+ for child in tree["and"]:+ child_properties, child_skipped = translate_filter_tree(child, property_type)+ properties.extend(child_properties)+ skipped.extend(child_skipped)+ return properties, skipped+ if "field" in tree:+ filters = property_filters(tree, property_type, negate=bool(tree.get("inverse")))+ return (filters, []) if filters is not None else ([], [describe(tree)])+ return [], [describe(tree)]+++def property_filters(leaf: dict[str, Any], property_type: str, *, negate: bool = False) -> list[dict[str, Any]] | None:+ """PostHog property filters for one Customer.io leaf, or None when the operator has no equivalent."""+ field, operator, value, value2 = leaf.get("field"), leaf.get("operator"), leaf.get("value"), leaf.get("value2")+ if not isinstance(field, str) or not field or not isinstance(operator, str):+ return None++ def build(posthog_operator: str, posthog_value: Any) -> dict[str, Any]:+ if negate:+ posthog_operator = _NEGATED.get(posthog_operator, posthog_operator)+ result: dict[str, Any] = {"key": field, "operator": posthog_operator, "type": property_type}+ if posthog_operator not in _UNARY:+ result["value"] = posthog_value+ return result++ if negate and operator in ("bw", "timestamp_bw", "timestamp_gt", "timestamp_lt", "timestamp_gta", "timestamp_lta"):+ # A negated range is an OR of two filters, which a flat AND list cannot hold.+ return None+ if operator in _OPERATORS:+ posthog_operator = _OPERATORS[operator]+ if posthog_operator not in _UNARY and value is None:+ return None+ return [build(posthog_operator, value)]+ if operator == "bw" and value is not None and value2 is not None:+ return [build("gte", value), build("lte", value2)]+ if operator in ("timestamp_gt", "timestamp_lt"):+ relative = _relative_duration(value)+ if relative is None:+ return None+ return [build("is_date_after" if operator == "timestamp_gt" else "is_date_before", relative)]+ if operator in ("timestamp_gta", "timestamp_lta"):+ absolute = _absolute_date(value)+ if absolute is None:+ return None+ return [build("is_date_after" if operator == "timestamp_gta" else "is_date_before", absolute)]+ if operator == "timestamp_bw":+ start, end = _absolute_date(value), _absolute_date(value2)+ if start is None or end is None:+ return None+ return [build("is_date_after", start), build("is_date_before", end)]+ return None+++def describe(condition: Any) -> str:+ if not isinstance(condition, dict):+ return json.dumps(condition, sort_keys=True)[:200]+ kind = condition.get("type")+ if "field" in condition:+ value = condition.get("value")+ prefix = "not " if condition.get("inverse") else ""+ return (+ f"{prefix}{condition['field']} {condition.get('operator', '')} {value if value is not None else ''}".strip()+ )+ if kind in ("segments_negatable", "segments"):+ verb = "not in" if condition.get("negate") else "in"+ return f"{verb} segment {', '.join(str(segment) for segment in condition.get('segments') or [])}"+ if kind == "foreign_event":+ return f"event {condition.get('event_name')}"+ if kind == "message":+ event = condition.get("event")+ if not isinstance(event, dict):+ event = {}+ prefix = "has not " if condition.get("inverse") else ""+ return f"{prefix}{event.get('type', 'message event')} ({event.get('name', '*')})"+ if isinstance(condition.get("segment"), dict):+ return f"in segment {condition['segment'].get('id')}"+ if isinstance(condition.get("event_name"), dict):+ return f"event {condition['event_name'].get('name')}"+ if isinstance(condition.get("attribute"), dict):+ return describe(condition["attribute"])+ return json.dumps(condition, sort_keys=True)[:200]+++def _cohort_filters(condition: dict[str, Any], segment_cohorts: dict[str, int]) -> list[dict[str, Any]] | None:+ segments = [str(segment) for segment in condition.get("segments") or []]+ if len(segments) != 1 or segments[0] not in segment_cohorts:+ # Membership in any of several segments is an OR, which a flat AND list cannot hold.+ return None+ operator = "not_in" if condition.get("negate") else "in"+ return [{"key": "id", "type": "cohort", "operator": operator, "value": segment_cohorts[segments[0]]}]+++def _relative_duration(value: Any) -> str | None:+ try:+ seconds = int(str(value))+ except ValueError:+ return None+ if seconds >= 0:+ return None+ if seconds % _SECONDS_PER_DAY == 0:+ return f"-{-seconds // _SECONDS_PER_DAY}d"+ return f"-{max(1, round(-seconds / 3600))}h"+++def _absolute_date(value: Any) -> str | None:+ try:+ moment = datetime.fromtimestamp(int(str(value)), tz=UTC)+ except (ValueError, OverflowError, OSError):+ return None+ if moment.hour == moment.minute == moment.second == 0:+ return moment.strftime("%Y-%m-%d")+ return moment.isoformat()diff --git a/products/workflows/backend/services/customerio_import/content.py b/products/workflows/backend/services/customerio_import/content.pynew file mode 100644index 000000000000..c00a2fe5af98--- /dev/null+++ b/products/workflows/backend/services/customerio_import/content.py@@ -0,0 +1,194 @@+"""What an import needs beyond the automation graph: message content, senders, categories and segment names.++The caller can pass all of it. When it leaves out message content and the team stored a Customer.io App API key+(the opt-out import saves one), PostHog reads the content itself, so an agent passes only the graph.+"""++import json+from typing import Any++import structlog++from posthog.models.integration import Integration++from products.messaging.backend.facade.categories import list_categories+from products.workflows.backend.facade.contracts import (+ CampaignSource,+ ImportCategory,+ ImportContent,+ ImportNote,+ ImportOptions,+ ImportSegment,+ ImportSender,+)+from products.workflows.backend.facade.enums import WorkflowImportNoteLevel+from products.workflows.backend.services.customerio_import.app_api import CustomerioApiError, CustomerioAppApi++logger = structlog.get_logger(__name__)++_MESSAGE_ACTION_TYPES = {"email_action", "webhook_action", "attribute_update_action"}+++def import_content(+ team_id: int,+ sources: list[CampaignSource],+ options: ImportOptions,+ sender_identities: list[dict[str, Any]],+ segments: list[dict[str, Any]],+ deadline: float,+ left_alone: frozenset[str] = frozenset(),+) -> ImportContent:+ sender_addresses = {**_identity_addresses(sender_identities), **options.sender_addresses}+ segment_info = {**_segment_info(segments), **options.segments}+ fetch_notes: dict[str, tuple[ImportNote, ...]] = {}+ fetched = False++ # An automation the import will leave unchanged needs no content, so it costs no reads.+ missing = {+ id(source)+ for source in sources+ if _misses_templates(source) and str(source.campaign.get("id")) not in left_alone+ }+ api_key = _stored_app_api_key(team_id) if missing else None+ if api_key:+ api = CustomerioAppApi(api_key, deadline=deadline)+ try:+ if not sender_addresses:+ sender_addresses = _identity_addresses(api.sender_identities())+ if not segment_info:+ segment_info = _segment_info(api.segments())+ except CustomerioApiError as error:+ logger.info("workflows.import.customerio_lookup_failed", team_id=team_id, status=error.status)+ filled: list[CampaignSource] = []+ for source in sources:+ if id(source) not in missing:+ filled.append(source)+ continue+ try:+ filled.append(_with_fetched_templates(source, api.campaign_actions(str(source.campaign.get("id")))))+ fetched = True+ except CustomerioApiError as error:+ fetch_notes[str(source.campaign.get("id"))] = (+ ImportNote(+ level=WorkflowImportNoteLevel.NEEDS_INPUT,+ message=f"PostHog could not read this automation's messages from Customer.io: {error} "+ "Import again with its templates.",+ ),+ )+ filled.append(source)+ sources = filled++ return ImportContent(+ sources=sources,+ options=ImportOptions(+ snippets=options.snippets,+ segment_cohorts=options.segment_cohorts,+ layouts=options.layouts,+ sender_addresses=sender_addresses,+ senders=options.senders or _team_senders(team_id),+ topic_categories=options.topic_categories or _topic_categories(team_id),+ segments=segment_info,+ ),+ fetch_notes=fetch_notes,+ fetched=fetched,+ )+++def _misses_templates(source: CampaignSource) -> bool:+ return any(+ isinstance(action.get("type"), str)+ and action["type"] in _MESSAGE_ACTION_TYPES+ and action.get("template_id") is not None+ and str(action["template_id"]) not in source.templates+ for action in source.actions+ )+++def _with_fetched_templates(source: CampaignSource, app_actions: list[dict[str, Any]]) -> CampaignSource:+ """The source with a template for every message action whose App API action shares its id.++ Language variants share the default variant's id through `parent_action_id`, so the variant whose own id+ matches is the default-language message.+ """+ by_id = {str(app_action.get("id")): app_action for app_action in app_actions}+ templates = dict(source.templates)+ for action in source.actions:+ template_id = action.get("template_id")+ if template_id is None or str(template_id) in templates:+ continue+ app_action = by_id.get(str(action.get("id")))+ if app_action is not None:+ templates[str(template_id)] = _template_from_app_action(app_action)+ return CampaignSource(campaign=source.campaign, actions=source.actions, templates=templates)+++def _template_from_app_action(app_action: dict[str, Any]) -> dict[str, Any]:+ """An App API action in the shape of a Journeys UI API template, which the translator reads."""+ headers = app_action.get("headers")+ if isinstance(headers, str):+ try:+ headers = json.loads(headers)+ except ValueError:+ headers = []+ return {+ "subject": app_action.get("subject"),+ "body": app_action.get("body"),+ "preheader_text": app_action.get("preheader_text"),+ "recipient": app_action.get("recipient"),+ "from": app_action.get("from"),+ "from_id": app_action.get("from_id"),+ "reply_to": app_action.get("reply_to"),+ "reply_to_id": app_action.get("reply_to_id"),+ "layout": app_action.get("layout"),+ "url": app_action.get("url"),+ "request_method": app_action.get("method"),+ "headers": headers if isinstance(headers, list) else [],+ }+++def _stored_app_api_key(team_id: int) -> str | None:+ integration = Integration.objects.filter(team_id=team_id, kind=Integration.IntegrationKind.CUSTOMERIO_APP).first()+ key = (integration.sensitive_config or {}).get("app_api_key") if integration else None+ return str(key) if key else None+++def _team_senders(team_id: int) -> dict[str, ImportSender]:+ senders: dict[str, ImportSender] = {}+ for integration in Integration.objects.filter(team_id=team_id, kind=Integration.IntegrationKind.EMAIL):+ config = integration.config or {}+ address = str(config.get("email") or integration.integration_id or "").lower()+ if address:+ senders[address] = ImportSender(integration_id=integration.id, email=address, name=config.get("name") or "")+ return senders+++def _topic_categories(team_id: int) -> dict[str, ImportCategory]:+ # The opt-out import keys each category `customerio_<topic identifier>`, and the identifier is `topic_<id>`.+ categories: dict[str, ImportCategory] = {}+ for category in list_categories(team_id):+ if category.deleted or not category.key.startswith("customerio_topic_"):+ continue+ topic_id = category.key.removeprefix("customerio_topic_")+ categories[topic_id] = ImportCategory(id=str(category.id), category_type=category.category_type)+ return categories+++def _identity_addresses(identities: list[dict[str, Any]]) -> dict[str, str]:+ addresses: dict[str, str] = {}+ for identity in identities:+ if identity.get("id") is None:+ continue+ email = identity.get("email")+ if not email:+ continue+ name = identity.get("name")+ addresses[str(identity["id"])] = f"{name} <{email}>" if name else str(email)+ return addresses+++def _segment_info(segments: list[dict[str, Any]]) -> dict[str, ImportSegment]:+ return {+ str(segment["id"]): ImportSegment(name=str(segment.get("name") or ""), type=str(segment.get("type") or ""))+ for segment in segments+ if isinstance(segment, dict) and segment.get("id") is not None+ }diff --git a/products/workflows/backend/services/customerio_import/liquid.py b/products/workflows/backend/services/customerio_import/liquid.pynew file mode 100644index 000000000000..7a20088c725b--- /dev/null+++ b/products/workflows/backend/services/customerio_import/liquid.py@@ -0,0 +1,106 @@+"""Customer.io Liquid rewritten for PostHog's Liquid globals."""++import re++from posthog.dataclasses import frozen++from products.workflows.backend.facade.contracts import SnippetRoom++# Tag bodies are capped, so text full of unclosed `{{` scans in linear time.+_TAG = re.compile(r"\{\{.{0,1000}?\}\}|\{%.{0,1000}?%\}", re.DOTALL)+_UNSUBSCRIBE_TAG = re.compile(r"\{%-?\s*(?:unsubscribe_url|manage_subscription_preferences_url)\s*-?%\}")+_SNIPPET_TAG = re.compile(r"\{%-?\s*snippet\s+[\"']([^\"']+)[\"']\s*-?%\}")+_CAPTURE_TAG = re.compile(r"\{%-?\s*capture\s+(\w+)\s*-?%\}")+_QUOTED = re.compile(r"(\"[^\"]*\"|'[^']*')")+_OUTPUT_TAG = re.compile(r"\{\{(.{0,1000}?)\}\}", re.DOTALL)+_RAW_FILTER = re.compile(r"\|\s*raw\s*$")+# Customer.io's `customer.id` is the id the team identifies people with, which is the distinct id in PostHog.+_RENAMES = (+ (re.compile(r"(?<![\w.])customer\.id\b"), "event.distinct_id"),+ (re.compile(r"(?<![\w.])customer(?=[.\[])"), "person.properties"),+ (+ re.compile(r"(?<![\w.])event(?=\.(?!properties\b|distinct_id\b|event\b|uuid\b|timestamp\b)|\[)"),+ "event.properties",+ ),+ (re.compile(r"(?<![\w.])event_name\b"), "event.event"),+ (re.compile(r"(?<![\w.])event_id\b"), "event.uuid"),+ (re.compile(r"(?<![\w.])event_timestamp\b"), "event.timestamp"),+ (re.compile(r"(?<![\w.])trigger(?=[.\[])"), "event.properties"),+)+_UNSUPPORTED = {+ "journey": re.compile(r"(?<![\w.])journey\."),+ "campaign": re.compile(r"(?<![\w.])campaign\."),+ "message": re.compile(r"(?<![\w.])message\."),+ "delivery_id": re.compile(r"(?<![\w.])delivery_id\b"),+}+++@frozen+class RewrittenLiquid:+ text: str+ # Customer.io globals the text uses that PostHog has no value for, like `journey`.+ unsupported_globals: tuple[str, ...] = ()+ # Snippets the text includes that the import was not given.+ missing_snippets: tuple[str, ...] = ()+++def rewrite(text: str | None, snippets: dict[str, str], room: SnippetRoom | None = None) -> RewrittenLiquid:+ if not text:+ return RewrittenLiquid(text=text or "")+ missing_snippets: list[str] = []+ room = room or SnippetRoom()++ def inline_snippet(match: re.Match[str]) -> str:+ name = match.group(1)+ content = snippets.get(name)+ if content is not None and len(content) <= room.remaining:+ room.remaining -= len(content)+ # A snippet is Customer.io Liquid too, so it goes through the same rewrite after this.+ return content+ missing_snippets.append(name)+ return match.group(0)++ text = _SNIPPET_TAG.sub(inline_snippet, text)+ text = _UNSUBSCRIBE_TAG.sub("{{ unsubscribe_url }}", text)+ unsupported: set[str] = set()++ def rewrite_code(code: str) -> str:+ for name, pattern in _UNSUPPORTED.items():+ if pattern.search(code):+ unsupported.add(name)+ for pattern, replacement in _RENAMES:+ code = pattern.sub(replacement, code)+ return code++ def rewrite_tag(match: re.Match[str]) -> str:+ # Odd parts are quoted strings, which are literal text and keep what they say.+ parts = _QUOTED.split(match.group(0))+ return "".join(part if index % 2 else rewrite_code(part) for index, part in enumerate(parts))++ text = _TAG.sub(rewrite_tag, text)+ text = _print_captures_raw(text)+ return RewrittenLiquid(+ text=text,+ unsupported_globals=tuple(sorted(unsupported)),+ missing_snippets=tuple(dict.fromkeys(missing_snippets)),+ )+++def _print_captures_raw(text: str) -> str:+ # PostHog escapes HTML in every output and Customer.io does not, so captured markup would print as text.+ captured = set(_CAPTURE_TAG.findall(text))+ if not captured:+ return text++ def add_raw(match: re.Match[str]) -> str:+ inner = match.group(1)+ open_trim = "-" if inner.startswith("-") else ""+ close_trim = "-" if inner.endswith("-") and len(inner) > len(open_trim) else ""+ body = inner[len(open_trim) : len(inner) - len(close_trim)]+ expression = body.rstrip()+ name = re.match(r"\s*(\w+)", expression)+ if name is None or name.group(1) not in captured or _RAW_FILTER.search(expression):+ return match.group(0)+ return "{{" + open_trim + expression + " | raw" + body[len(expression) :] + close_trim + "}}"++ return _OUTPUT_TAG.sub(add_raw, text)diff --git a/products/workflows/backend/services/customerio_import/service.py b/products/workflows/backend/services/customerio_import/service.pynew file mode 100644index 000000000000..32b4a3bc9f79--- /dev/null+++ b/products/workflows/backend/services/customerio_import/service.py@@ -0,0 +1,76 @@+"""The record that ties an imported Customer.io automation to its draft workflow."""++from dataclasses import asdict+from uuid import UUID++from posthog.models import Team, User++from products.workflows.backend.facade.contracts import ImportNote, PreviousImport, WorkflowImportRecord+from products.workflows.backend.facade.enums import HogFlowState, WorkflowImportSource+from products.workflows.backend.models.hog_flow.hog_flow import HogFlow+from products.workflows.backend.models.workflow_import import WorkflowImport+++def previous_import(team: Team, source_id: str) -> PreviousImport:+ record = (+ WorkflowImport.objects.for_team(team.id)+ .filter(source=WorkflowImportSource.CUSTOMERIO.value, source_id=source_id)+ .first()+ )+ flow = (+ HogFlow.objects.filter(team_id=team.id, pk=record.hog_flow_id).only("id", "status", "created_by_id").first()+ if record is not None and record.hog_flow_id+ else None+ )+ return PreviousImport(+ record_id=record.id if record else None,+ hog_flow_id=flow.id if flow else None,+ hog_flow_is_draft=flow is not None and flow.status == HogFlowState.DRAFT.value,+ hog_flow_created_by_id=flow.created_by_id if flow else None,+ )+++def record_import(+ *,+ team: Team,+ user: User,+ record_id: UUID | None,+ source_id: str,+ name: str,+ hog_flow_id: UUID,+ notes: tuple[ImportNote, ...],+) -> None:+ stored_notes = [{**asdict(note), "level": note.level.value} for note in notes]+ if record_id is None:+ WorkflowImport.objects.create(+ team=team,+ source=WorkflowImportSource.CUSTOMERIO.value,+ source_id=source_id,+ source_name=name[:400],+ hog_flow_id=hog_flow_id,+ notes=stored_notes,+ created_by=user,+ )+ return+ record = WorkflowImport.objects.for_team(team.id).get(pk=record_id)+ record.hog_flow_id = hog_flow_id+ record.source_name = name[:400]+ record.notes = stored_notes+ record.save(update_fields=["hog_flow", "source_name", "notes", "updated_at"])+++def import_for_workflow(team: Team, hog_flow_id: UUID) -> WorkflowImportRecord | None:+ record = WorkflowImport.objects.for_team(team.id).filter(hog_flow_id=hog_flow_id).order_by("-updated_at").first()+ if record is None:+ return None+ flow = HogFlow.objects.filter(team_id=team.id, pk=hog_flow_id).only("id", "created_by_id").first()+ return WorkflowImportRecord(+ id=record.id,+ source=record.source,+ source_id=record.source_id,+ source_name=record.source_name,+ hog_flow_id=record.hog_flow_id,+ notes=tuple(record.notes or []),+ updated_at=record.updated_at,+ hog_flow_created_by_id=flow.created_by_id if flow else None,+ )diff --git a/products/workflows/backend/services/customerio_import/translator.py b/products/workflows/backend/services/customerio_import/translator.pynew file mode 100644index 000000000000..63fb9c40d011--- /dev/null+++ b/products/workflows/backend/services/customerio_import/translator.py@@ -0,0 +1,1003 @@+"""A Customer.io automation, as its Journeys UI API returns it, turned into a draft PostHog workflow.++The input is `GET /v1/environments/:id/campaigns/:id` (the `campaign` and top-level `actions`), plus the templates+its message actions point at. Every setting that has no PostHog equivalent is left out and named in the notes, so+the team can review the draft before enabling it. Nothing about people is read.+"""++import re+import json+from collections import deque+from collections.abc import Iterator+from email.utils import parseaddr+from typing import Any++from posthog.dataclasses import frozen++from products.workflows.backend.facade.contracts import (+ CampaignSource,+ ImportNote,+ ImportOptions,+ SnippetRoom,+ TranslatedWorkflow,+)+from products.workflows.backend.facade.enums import WorkflowImportNoteLevel as NoteLevel+from products.workflows.backend.services.customerio_import import conditions, liquid++TRIGGER_ID = "trigger_node"+_EXIT_ID = "exit_node"+_MAX_DELAY_SECONDS = 365 * 86400+_MAX_WAIT_SECONDS = 30 * 86400+# Unit, seconds per unit, and the largest amount a workflow accepts in that unit.+_DURATION_UNITS: tuple[tuple[str, int, int | None], ...] = (+ ("d", 86400, None),+ ("h", 3600, 24),+ ("m", 60, 60),+ ("s", 1, 60),+)+_CREDENTIAL_HEADER = re.compile(r"auth|api[-_]?key|token|secret|password|cookie|signature", re.IGNORECASE)+_DAY_NAMES = {+ "Mon": "monday",+ "Tue": "tuesday",+ "Wed": "wednesday",+ "Thu": "thursday",+ "Fri": "friday",+ "Sat": "saturday",+ "Sun": "sunday",+}+_WEEKDAYS = ["monday", "tuesday", "wednesday", "thursday", "friday"]+_LAYOUT_CONTENT = re.compile(r"\{\{-?\s*content\s*-?\}\}")+_LIQUID_TAG = re.compile(r"\{\{.{0,500}?\}\}|\{%.{0,500}?%\}", re.DOTALL)+_SANS_SERIF = "font-family: Arial, Helvetica, sans-serif;"+_STATIC_SEGMENT_TYPES = {"static", "manual"}+_SEGMENT_IDS = re.compile(r"\b(segments?) (\d+(?:, \d+)*)\b")+_CHANNEL_NAMES = {+ "push_action": "push notification",+ "twilio_action": "SMS",+ "sms_action": "SMS",+ "in_app_action": "in-app message",+ "inbox_action": "inbox message",+ "whatsapp_action": "WhatsApp message",+ "line_action": "LINE message",+ "urban_airship_action": "push notification",+ "slack_action": "Slack message",+}+++class _Builder:+ def __init__(self, source: CampaignSource, options: ImportOptions, snippet_room: SnippetRoom) -> None:+ self.campaign = source.campaign+ self.source_actions = {str(action["id"]): action for action in source.actions if action.get("id") is not None}+ self.templates = source.templates+ self.options = options+ self.notes: list[ImportNote] = []+ self.actions: dict[str, dict[str, Any]] = {}+ # Outgoing links per source action id: (target source id, edge type, index).+ self.links: dict[str, list[tuple[str, str, int | None]]] = {}+ self.snippet_room = snippet_room++ def note(self, level: NoteLevel, message: str, action: dict[str, Any] | None = None) -> None:+ self.notes.append(+ ImportNote(+ level=level,+ message=_SEGMENT_IDS.sub(self._name_segments, message),+ step_id=action["id"] if action else None,+ step_name=action.get("name") if action else None,+ )+ )++ def build(self) -> TranslatedWorkflow:+ source_edges = [edge for edge in self.campaign.get("edges") or [] if isinstance(edge, dict)]+ for edge in source_edges:+ # Customer.io can send a branch index as a string, and every later check compares numbers.+ index = int(edge["index"]) if edge.get("index") is not None else None+ self.links.setdefault(str(edge.get("from")), []).append(+ (str(edge.get("to")), str(edge.get("type") or "continue"), index)+ )++ workflow_actions_order = [str(action_id) for action_id in self.campaign.get("workflow_action_ids") or []]+ ordered_ids = workflow_actions_order or list(self.source_actions)+ graph_ids = [+ action_id+ for action_id in ordered_ids+ if action_id in self.source_actions and self.source_actions[action_id].get("type") != "trigger"+ ]+ incoming = {target for links in self.links.values() for target, _, _ in links}+ roots = [action_id for action_id in graph_ids if action_id not in incoming]++ trigger = self._trigger()+ posthog_actions: list[dict[str, Any]] = [+ {"id": TRIGGER_ID, "name": "Trigger", "description": "", "type": "trigger", "config": trigger}+ ]+ for action_id in graph_ids:+ translated = self._action(self.source_actions[action_id])+ if translated is not None:+ self.actions[action_id] = translated++ edges = self._edges(roots)+ posthog_actions.extend(self.actions.values())+ if any(edge["to"] == _EXIT_ID for edge in edges):+ posthog_actions.append(_exit_action(_EXIT_ID, "Exit"))+ pruned = _prune_unreachable(posthog_actions, edges)+ posthog_actions, edges = pruned.actions, pruned.edges+ kept_ids = {action["id"] for action in posthog_actions}++ workflow: dict[str, Any] = {+ "name": (self.campaign.get("name") or "Imported from Customer.io")[:400],+ "description": f"Imported from Customer.io automation {self.campaign.get('id')}.",+ "trigger": trigger,+ "actions": posthog_actions,+ "edges": edges,+ "exit_condition": self._exit_condition(),+ # Sent even when empty, so a re-import clears a goal or a re-entry limit the automation dropped.+ "conversion": self._conversion(),+ "trigger_masking": self._trigger_masking(),+ }+ self._campaign_notes()+ self._static_segment_notes()+ # A step that no path reaches, like a language variant behind its default, is not in the draft.+ notes = tuple(note for note in self.notes if note.step_id is None or note.step_id in kept_ids)+ return TranslatedWorkflow(+ source_id=str(self.campaign.get("id")),+ name=workflow["name"],+ workflow=workflow,+ notes=notes,+ )++ def _trigger(self) -> dict[str, Any]:+ kind = self.campaign.get("type")+ if kind in ("transactional", "form") and self.campaign.get("event"):+ if self.campaign.get("event_type") not in (None, "", "event"):+ self.note(+ NoteLevel.UNSUPPORTED,+ f"Customer.io starts this automation on a {self.campaign['event_type']} event, which PostHog "+ "does not receive. Pick the event that starts it in PostHog.",+ )+ return _empty_trigger()+ name = str(self.campaign["event"])+ properties, skipped = conditions.translate_filter_tree(self.campaign.get("attribute_filters"), "event")+ for description in skipped:+ self.note(NoteLevel.UNSUPPORTED, f"The trigger's event filter `{description}` was left out.")+ event: dict[str, Any] = {"id": name, "name": name, "type": "events", "order": 0}+ if properties:+ event["properties"] = properties+ trigger: dict[str, Any] = {"type": "event", "filters": {"events": [event]}}+ self._audience_filter(trigger)+ return trigger+ if kind in ("seg_attr", "behavioral"):+ return self._segment_trigger()+ labels = {+ "date": "a date attribute",+ "webhook": "an incoming webhook",+ "triggered_broadcast": "an API call (API-triggered broadcast)",+ "relationship": "a relationship change",+ "object": "an object change",+ "rel_seg_attr": "a relationship segment",+ }+ self.note(+ NoteLevel.UNSUPPORTED,+ f"Customer.io starts this automation on {labels.get(str(kind), f'a `{kind}` trigger')}, which has no "+ "PostHog trigger yet. Pick how it starts in PostHog.",+ )+ return _empty_trigger()++ def _segment_trigger(self) -> dict[str, Any]:+ trigger_tree = self.campaign.get("trigger")+ segments = _segment_references(trigger_tree)+ attributes = _attribute_leaves(trigger_tree)+ if len(segments) == 1 and not attributes and segments[0] in self.options.segment_cohorts:+ inverse = _segment_is_inverse(trigger_tree)+ return {+ "type": "cohort",+ "cohort_id": self.options.segment_cohorts[segments[0]],+ "transition": "left" if inverse else "entered",+ }+ described = [f"in segment {segment}" for segment in segments] + [+ conditions.describe(attribute) for attribute in attributes+ ]+ what = "; ".join(described) or "a segment"+ self.note(+ NoteLevel.NEEDS_INPUT,+ f"Customer.io starts this automation when a person matches: {what}. Create a realtime cohort with "+ "these conditions and start the workflow when a person joins it.",+ )+ return _empty_trigger()++ def _audience_filter(self, trigger: dict[str, Any]) -> None:+ # `filters` on an event-triggered automation is segment membership the person must have to enter.+ audience = conditions.decode(self.campaign.get("filters"))+ if not audience:+ return+ translated = conditions.translate_condition(audience, self.options.segment_cohorts)+ if translated.properties:+ trigger["filters"]["properties"] = list(translated.properties)+ for description in translated.skipped:+ self.note(+ NoteLevel.NEEDS_INPUT,+ f"Only people {description} can enter in Customer.io. Add a cohort filter to the trigger for it.",+ )++ def _trigger_masking(self) -> dict[str, Any] | None:+ mode = self.campaign.get("restart_mode")+ if self.campaign.get("type") not in ("seg_attr", "behavioral", "rel_seg_attr"):+ return None+ if mode == "none":+ return {"hash": "{person.id}", "ttl": None, "threshold": None}+ if mode == "interval" and self.campaign.get("restart_min_interval"):+ return {"hash": "{person.id}", "ttl": int(self.campaign["restart_min_interval"]), "threshold": None}+ return None++ def _conversion(self) -> dict[str, Any] | None:+ if not self.campaign.get("conversion"):+ return None+ conversion_type = self.campaign.get("conversion_type")+ window = _duration(self.campaign.get("conversion_window") or 0)+ if conversion_type != "perform_event" or not self.campaign.get("conversion_event_name"):+ self.note(+ NoteLevel.UNSUPPORTED,+ "The goal counts people entering or leaving a segment. Set a conversion goal on an event instead.",+ )+ return None+ name = str(self.campaign["conversion_event_name"])+ event: dict[str, Any] = {"id": name, "name": name, "type": "events", "order": 0}+ properties: list[dict[str, Any]] = []+ event_filters = self.campaign.get("conversion_event_filters")+ if _has_alternatives(event_filters):+ # A flat AND list can't hold "any of", and dropping the OR would make the goal match nothing.+ self.note(+ NoteLevel.UNSUPPORTED,+ "The goal's event filters match any of several conditions, which PostHog goals can't express. "+ "They were left out, so every conversion event counts. Narrow the goal by hand.",+ )+ event_filters = None+ for leaf in _foreign_event_leaves(event_filters):+ filters = conditions.property_filters(leaf, "event")+ if filters is None:+ self.note(NoteLevel.UNSUPPORTED, f"The goal's event filter `{conditions.describe(leaf)}` was left out.")+ else:+ properties.extend(filters)+ if properties:+ event["properties"] = properties+ if self.campaign.get("conversion_action") in ("opening", "clicking"):+ self.note(+ NoteLevel.APPROXIMATED,+ f"Customer.io counts a conversion only after the person is {self.campaign['conversion_action']} a "+ "message. PostHog counts every conversion inside the window.",+ )+ conversion: dict[str, Any] = {"filters": [], "events": [{"name": name, "filters": {"events": [event]}}]}+ if window:+ conversion["window"] = window+ return conversion++ def _exit_condition(self) -> str:+ on_conversion = bool(self.campaign.get("exit_on_conversion_matched")) or self._exits_match_goal()+ on_trigger = bool(self.campaign.get("exit_on_trigger_or_filter_not_matched")) and self.campaign.get("type") in (+ "seg_attr",+ "behavioral",+ )+ if on_conversion and on_trigger:+ return "exit_on_trigger_not_matched_or_conversion"+ if on_conversion:+ return "exit_on_conversion"+ if on_trigger:+ return "exit_on_trigger_not_matched"+ return "exit_only_at_end"++ def _exits_match_goal(self) -> bool:+ exits = self.campaign.get("global_exit_conditions") or []+ goal = self.campaign.get("conversion_event_name") if self.campaign.get("conversion") else None+ goal_is_bare = not self.campaign.get("conversion_event_filters")+ matched = False+ for exit_condition in exits:+ names = _exit_event_names(exit_condition)+ if goal and goal_is_bare and names == [goal] and _is_bare_event(exit_condition):+ matched = True+ else:+ self.note(+ NoteLevel.UNSUPPORTED,+ f"Customer.io removes people from this automation when: {conditions.describe(exit_condition)}. "+ "PostHog can exit people on the conversion goal or when they stop matching the trigger.",+ )+ return matched++ def _campaign_notes(self) -> None:+ if self.campaign.get("frequency_cap_id") or self.campaign.get("use_message_limits"):+ self.note(+ NoteLevel.APPROXIMATED,+ "Customer.io message limits apply to this automation. Set them in PostHog's sending rules.",+ )+ throttle = self.campaign.get("message_throttle") or {}+ if throttle.get("enabled"):+ self.note(+ NoteLevel.APPROXIMATED,+ "Customer.io throttles this automation's sends. Set an email sending limit on the workflow.",+ )++ def _name_segments(self, match: re.Match[str]) -> str:+ labels = [self._segment_label(segment_id) for segment_id in match.group(2).split(", ")]+ return f"{match.group(1)} {', '.join(labels)}"++ def _segment_label(self, segment_id: str) -> str:+ segment = self.options.segments.get(segment_id)+ return f'"{segment.name}" ({segment_id})' if segment and segment.name else segment_id++ def _static_segment_notes(self) -> None:+ trees: list[Any] = [self.campaign.get("trigger"), self.campaign.get("global_exit_conditions")]+ trees.append(conditions.decode(self.campaign.get("filters")))+ for action in self.source_actions.values():+ trees.append(conditions.decode(action.get("preconditions")))+ trees.append(conditions.decode(action.get("conditions")))+ trees.extend(conditions.decode(item) for item in action.get("multi_conditions") or [])+ referenced = dict.fromkeys(segment for tree in trees for segment in _segment_references(tree))+ for segment_id in referenced:+ segment = self.options.segments.get(segment_id)+ if segment is None or segment.type not in _STATIC_SEGMENT_TYPES:+ continue+ self.note(+ NoteLevel.NEEDS_INPUT,+ f"Customer.io segment {self._segment_label(segment_id)} is a static list. Workflows cannot check a "+ "static cohort, so rebuild it as person property conditions or a realtime cohort.",+ )++ def _action(self, source: dict[str, Any]) -> dict[str, Any] | None:+ kind = source.get("type")+ action_id = f"cio_{source['id']}"+ name = _plain_name(source.get("name")) or str(kind or "Step")+ base: dict[str, Any] = {"id": action_id, "name": name, "description": ""}+ translated: dict[str, Any] | None+ match kind:+ case "email_action":+ translated = {**base, **self._email(source, base)}+ case "webhook_action":+ translated = {**base, **self._webhook(source, base)}+ case "attribute_update_action":+ translated = self._attribute_update(source, base)+ case "create_event_action":+ translated = self._create_event(source, base)+ case "delay_seconds_action":+ translated = {+ **base,+ "type": "delay",+ "config": {"delay_duration": self._delay(base, source.get("delay"))},+ }+ case "randomized_delay_action":+ low, high = int(source.get("min_delay") or 0), int(source.get("max_delay") or 0)+ middle = (low + high) // 2+ translated = {**base, "type": "delay", "config": {"delay_duration": self._delay(base, middle)}}+ self.note(+ NoteLevel.APPROXIMATED,+ f"Customer.io waits a random time between {_duration(low)} and {_duration(high)}. "+ f"PostHog waits {_duration(middle)}.",+ base,+ )+ case "delay_time_window_action":+ translated = {**base, "type": "wait_until_time_window", "config": self._time_window(source)}+ case "conditional_wait_action":+ translated = self._wait(source, base)+ case "conditional_branch_action":+ translated = self._branch(source, base, [source.get("conditions")])+ case "multi_split_branch_action":+ translated = self._branch(source, base, list(source.get("multi_conditions") or []))+ case "random_cohort_branch_action":+ translated = self._random_cohort(source, base)+ case "exit_action":+ translated = _exit_action(action_id, name if source.get("name") else "Exit")+ case "multi_lang_branch_action":+ self.note(+ NoteLevel.APPROXIMATED,+ f"The language split `{name}` sends everyone its default-language message. Add the other "+ "languages with a branch on the person's language property.",+ )+ return None+ case "filter_match_delay_action":+ # People pass straight through to the only next step in PostHog.+ return None+ case _:+ label = _CHANNEL_NAMES.get(str(kind), f"`{kind}` step")+ self.note(+ NoteLevel.UNSUPPORTED,+ f"The {label} `{name}` has no PostHog equivalent and was left out.",+ )+ return None+ if translated is not None:+ self._preconditions(source, translated)+ return translated++ def _preconditions(self, source: dict[str, Any], action: dict[str, Any]) -> None:+ precondition = conditions.decode(source.get("preconditions"))+ if not precondition:+ return+ translated = conditions.translate_condition(precondition, self.options.segment_cohorts)+ if translated.properties:+ action["filters"] = {"properties": list(translated.properties)}+ for description in translated.skipped:+ self.note(+ NoteLevel.UNSUPPORTED,+ f"Customer.io skips this step unless `{description}`. That check was left out.",+ action,+ )++ def _template(self, source: dict[str, Any], action: dict[str, Any]) -> dict[str, Any]:+ template_id = source.get("template_id")+ template = self.templates.get(str(template_id)) if template_id is not None else None+ if template is None:+ self.note(+ NoteLevel.NEEDS_INPUT,+ "The message content was not part of the import. Write it in PostHog, or import again with the "+ f"Customer.io template {template_id}.",+ action,+ )+ return {}+ return template++ def _liquid(self, text: str | None, action: dict[str, Any], field: str) -> str:+ rewritten = liquid.rewrite(text, self.options.snippets, self.snippet_room)+ for name in rewritten.missing_snippets:+ self.note(+ NoteLevel.NEEDS_INPUT,+ f"The {field} includes the Customer.io snippet `{name}`, which was not part of the import or was too "+ "large to copy in. "+ "Replace it with the snippet's content.",+ action,+ )+ for name in rewritten.unsupported_globals:+ self.note(+ NoteLevel.UNSUPPORTED,+ f"The {field} uses Customer.io's `{name}` Liquid variables, which PostHog does not have.",+ action,+ )+ return rewritten.text++ def _email(self, source: dict[str, Any], action: dict[str, Any]) -> dict[str, Any]:+ template = self._template(source, action)+ recipient = template.get("recipient") or source.get("recipient") or "{{customer.email}}"+ html = self._liquid(self._with_layout(template, action), action, "email body")+ email: dict[str, Any] = {+ "to": {"email": self._liquid(recipient, action, "recipient"), "name": ""},+ "from": self._sender(template, action),+ "replyTo": self._address(template, "reply_to_identity_id", "reply_to_id", "reply_to").email,+ "subject": self._liquid(template.get("subject") or source.get("subject") or "", action, "subject"),+ "preheader": self._liquid(template.get("preheader_text") or "", action, "preheader"),+ "html": html,+ }+ if template.get("body_plain"):+ email["text"] = self._liquid(template["body_plain"], action, "plain-text body")+ if not template:+ # A draft needs a subject and a body to save. The note about the missing content asks for the real ones.+ email["subject"] = email["subject"] or action["name"]+ email["html"] = "<p>This email's content was not imported from Customer.io.</p>"+ if template.get("editor") in ("bee", "parcel", "wysiwyg"):+ self.note(+ NoteLevel.APPROXIMATED,+ "The email is imported as HTML. The Customer.io visual design is not editable in PostHog's "+ "visual editor, so edit it in the code editor.",+ action,+ )+ if source.get("sending_state") == "draft":+ self.note(+ NoteLevel.APPROXIMATED,+ "Customer.io holds this message for review before sending. PostHog sends it as soon as the step runs.",+ action,+ )+ config: dict[str, Any] = {+ "template_id": "template-email",+ "inputs": {"email": {"templating": "liquid", "value": email}},+ }+ self._category(source, config, action)+ if source.get("tracked") is False:+ config["tracking_enabled"] = False+ return {"type": "function_email", "config": config}++ def _with_layout(self, template: dict[str, Any], action: dict[str, Any]) -> str:+ body = template.get("body") or ""+ layout_id = template.get("layout_id")+ layout = self.options.layouts.get(str(layout_id)) if layout_id else None+ if layout is None and isinstance(template.get("layout"), str):+ layout = template["layout"]+ if layout and _LAYOUT_CONTENT.search(layout):+ # A comment in the layout shows up as text in the plain-text version of the email.+ return _LAYOUT_CONTENT.sub(lambda _: body, _strip_comments(layout), count=1)+ if layout_id:+ self.note(+ NoteLevel.NEEDS_INPUT,+ f"Customer.io wraps this email in layout {layout_id}, which was not part of the import. Import "+ "again with the layout, or add its header and footer to the HTML.",+ action,+ )+ if body and "<html" not in body.lower() and "font-family" not in body.lower():+ # With no layout, mail clients fall back to a serif font such as Times.+ return f'<div style="{_SANS_SERIF}">{body}</div>'+ return body++ def _address(self, template: dict[str, Any], *keys: str) -> "_Address":+ """The email address and display name a template names under any of `keys`: an identity id or an address."""+ for key in keys:+ value = template.get(key)+ if value in (None, ""):+ continue+ address = self.options.sender_addresses.get(str(value))+ name, email = parseaddr(address if address is not None else str(value))+ if "@" in email:+ return _Address(email=email, name=name)+ return _Address(email="", name="")++ def _sender(self, template: dict[str, Any], action: dict[str, Any]) -> dict[str, Any]:+ if not template:+ return {"email": "", "name": ""}+ found = self._address(template, "from_identity_id", "from_id", "from")+ sender = self.options.senders.get(found.email.lower()) if found.email else None+ if sender is not None:+ return {"integrationId": sender.integration_id, "email": sender.email, "name": found.name or sender.name}+ if found.email:+ self.note(+ NoteLevel.NEEDS_INPUT,+ f"Customer.io sends this email from {found.email}, which is not a PostHog email sender yet. Add it "+ "as a sender, or choose another one.",+ action,+ )+ else:+ self.note(NoteLevel.NEEDS_INPUT, "Choose the email sender.", action)+ return {"email": found.email, "name": found.name}++ def _category(self, source: dict[str, Any], config: dict[str, Any], action: dict[str, Any]) -> None:+ topic_id = source.get("subscription_topic_id")+ if topic_id:+ category = self.options.topic_categories.get(str(topic_id))+ if category is not None:+ config["message_category_id"] = category.id+ config["message_category_type"] = category.category_type+ return+ self.note(+ NoteLevel.NEEDS_INPUT,+ f"Customer.io sends this email under subscription topic {topic_id}, which has no PostHog message "+ "category. Import your Customer.io topics with Import from Customer.io on the Opt-outs tab, then "+ "import again, or pick a category.",+ action,+ )+ if source.get("send_to_unsubscribed"):+ config["message_category_type"] = "transactional"++ def _webhook(self, source: dict[str, Any], action: dict[str, Any]) -> dict[str, Any]:+ template = self._template(source, action)+ body_text = self._liquid(template.get("body") or "", action, "request body")+ headers: dict[str, str] = {}+ secret_headers: dict[str, str] = {}+ for header in template.get("headers") or []:+ if not isinstance(header, dict) or not header.get("name"):+ continue+ name, value = str(header["name"]), str(header.get("value") or "")+ if not _CREDENTIAL_HEADER.search(name):+ headers[name] = self._liquid(value, action, "headers")+ elif "{{" in value or "{%" in value:+ self.note(+ NoteLevel.NEEDS_INPUT,+ f"The `{name}` header holds a credential built with Liquid, which secret headers don't run. "+ "It was left out. Add its value under Secret headers.",+ action,+ )+ else:+ secret_headers[name] = value+ inputs: dict[str, Any] = {+ "url": {"value": self._liquid(template.get("url") or "", action, "URL"), "templating": "liquid"},+ "method": {"value": str(template.get("request_method") or "POST").upper()},+ "headers": {"value": headers, "templating": "liquid"},+ }+ if secret_headers:+ inputs["secret_headers"] = {"value": secret_headers}+ if body_text:+ try:+ inputs["body"] = {"value": json.loads(body_text), "templating": "liquid"}+ except ValueError:+ # Liquid outside a JSON string, like `{% if %}` around a key, makes the stored body invalid JSON.+ self.note(+ NoteLevel.NEEDS_INPUT,+ "The request body is not valid JSON before Liquid runs, so it was not imported. Rebuild it as JSON.",+ action,+ )+ if not template.get("url"):+ self.note(NoteLevel.NEEDS_INPUT, "Set the webhook URL.", action)+ return {"type": "function", "config": {"template_id": "template-webhook", "inputs": inputs}}++ def _attribute_update(self, source: dict[str, Any], base: dict[str, Any]) -> dict[str, Any] | None:+ if source.get("sub_type") == "journey_attr_update":+ self.note(+ NoteLevel.UNSUPPORTED,+ f"`{base['name']}` sets a journey attribute, which PostHog has no equivalent for. It was left out.",+ )+ return None+ template = self._template(source, base)+ attributes: Any = []+ for raw in (template.get("body"), template.get("body_json")):+ if not raw:+ continue+ try:+ attributes = json.loads(raw) if isinstance(raw, str) else raw+ except ValueError:+ continue+ if attributes:+ break+ properties: dict[str, Any] = {}+ for attribute in attributes if isinstance(attributes, list) else []:+ if not isinstance(attribute, dict) or not attribute.get("name"):+ continue+ value = attribute.get("value")+ if attribute.get("type") == "profile_attribute":+ value = "{{customer." + str(value or "") + "}}"+ if value is None or isinstance(value, str):+ properties[str(attribute["name"])] = self._liquid(value or "", base, "property value")+ else:+ properties[str(attribute["name"])] = value+ if not properties:+ self.note(NoteLevel.NEEDS_INPUT, "Set the person properties this step updates.", base)+ return {+ **base,+ "type": "function",+ "config": {+ "template_id": "template-posthog-update-person-properties",+ "inputs": {+ "distinct_id": {"value": "{{ event.distinct_id }}", "templating": "liquid"},+ "set_properties": {"value": properties, "templating": "liquid"},+ },+ },+ }++ def _create_event(self, source: dict[str, Any], base: dict[str, Any]) -> dict[str, Any] | None:+ event_name = source.get("event_name")+ if not event_name:+ self.note(NoteLevel.UNSUPPORTED, f"`{base['name']}` sends an event with no name, so it was left out.")+ return None+ return {+ **base,+ "type": "function",+ "config": {+ "template_id": "template-posthog-capture",+ "inputs": {+ "event": {"value": str(event_name)},+ "distinct_id": {"value": "{{ event.distinct_id }}", "templating": "liquid"},+ },+ },+ }++ def _delay(self, action: dict[str, Any], seconds: Any) -> str:+ total = int(seconds or 0)+ if total > _MAX_DELAY_SECONDS:+ self.note(+ NoteLevel.APPROXIMATED, f"Delays are capped at 365 days, so {_duration(total)} became 365d.", action+ )+ total = _MAX_DELAY_SECONDS+ return _duration(total) or "0m"++ def _time_window(self, source: dict[str, Any]) -> dict[str, Any]:+ days = [_DAY_NAMES[day] for day in str(source.get("days") or "").split(":") if day in _DAY_NAMES]+ day: Any = "any" if not days or len(days) == 7 else days+ if days == _WEEKDAYS:+ day = "weekday"+ elif sorted(days) == ["saturday", "sunday"]:+ day = "weekend"+ start, end = source.get("start_time"), source.get("end_time")+ config: dict[str, Any] = {"day": day, "time": [start, end] if start and end else "any"}+ zone = source.get("zone")+ if zone == "customer_time_zone":+ config.update(+ {"timezone": None, "use_person_timezone": True, "fallback_timezone": source.get("fallback_zone")}+ )+ else:+ config["timezone"] = zone or None+ return config++ def _wait(self, source: dict[str, Any], base: dict[str, Any]) -> dict[str, Any] | None:+ encoded = [item for item in source.get("multi_conditions") or [] if item]+ decoded = [conditions.decode(item) for item in encoded]+ events: list[dict[str, Any]] = []+ properties: list[dict[str, Any]] = []+ property_conditions = 0+ for index, condition in enumerate(decoded):+ if condition is None:+ self.note(+ NoteLevel.UNSUPPORTED, f"Wait condition {index + 1} could not be read and was left out.", base+ )+ continue+ translated = conditions.translate_condition(condition, self.options.segment_cohorts)+ for description in translated.skipped:+ self.note(NoteLevel.UNSUPPORTED, f"The wait for `{description}` was left out.", base)+ if translated.events:+ # Properties next to a waited-for event filter that event, which PostHog puts on the event itself.+ for event in translated.events:+ event["properties"] = [*event.get("properties", []), *translated.properties]+ events.extend({"name": event["name"], "filters": {"events": [event]}} for event in translated.events)+ elif translated.properties:+ property_conditions += 1+ if property_conditions == 1:+ properties = list(translated.properties)+ if property_conditions > 1:+ self.note(+ NoteLevel.APPROXIMATED,+ "Customer.io waits for any of several property conditions. PostHog waits for the first one only.",+ base,+ )+ if source.get("delay_event_attr"):+ self.note(+ NoteLevel.UNSUPPORTED,+ f"Customer.io also stops waiting at the date in `{source['delay_event_attr']}`. That was left out.",+ base,+ )++ delay = int(source.get("delay") or 0)+ if delay <= 0 or source.get("exit_type") == "never":+ self.note(+ NoteLevel.APPROXIMATED,+ "Customer.io waits with no time limit. PostHog waits at most 30 days, then takes the timeout path.",+ base,+ )+ delay = _MAX_WAIT_SECONDS+ elif delay > _MAX_WAIT_SECONDS:+ self.note(NoteLevel.APPROXIMATED, f"Waits are capped at 30 days, so {_duration(delay)} became 30d.", base)+ delay = _MAX_WAIT_SECONDS++ # Customer.io gives each condition its own path and puts the timeout last. PostHog has one path for+ # "the condition matched", so every condition path has to lead to the same step.+ links = sorted(self.links.get(str(source["id"]), []), key=lambda link: link[2] or 0)+ condition_count = len(encoded)+ condition_targets = [target for target, _, index in links if index is not None and index < condition_count]+ timeout_targets = [target for target, _, index in links if index is not None and index >= condition_count]+ if len(set(condition_targets)) > 1:+ self.note(+ NoteLevel.APPROXIMATED,+ "Each wait condition leads to a different step in Customer.io. In PostHog every match takes the "+ "first condition's path, so check the other paths.",+ base,+ )+ new_links: list[tuple[str, str, int | None]] = []+ if condition_targets:+ new_links.append((condition_targets[0], "branch", 0))+ new_links.append((timeout_targets[-1] if timeout_targets else _EXIT_ID, "continue", None))+ self.links[str(source["id"])] = new_links+ if not events and not properties:+ self.note(NoteLevel.NEEDS_INPUT, "Set what this step waits for.", base)+ return {+ **base,+ "type": "wait_until_condition",+ "config": {+ "condition": {"filters": {"properties": properties} if properties else None},+ "events": events,+ "max_wait_duration": _duration(delay),+ },+ }++ def _branch(self, source: dict[str, Any], base: dict[str, Any], encoded: list[Any]) -> dict[str, Any]:+ posthog_conditions: list[dict[str, Any]] = []+ for index, item in enumerate(encoded):+ decoded = conditions.decode(item)+ translated = conditions.translate_condition(decoded or [], self.options.segment_cohorts)+ if decoded is None or translated.skipped or translated.events:+ parts = list(translated.skipped) + [f"event {event['name']}" for event in translated.events]+ detail = f" `{'; '.join(parts)}`" if parts else ""+ self.note(+ NoteLevel.NEEDS_INPUT,+ f"Branch {index + 1}'s condition{detail} has no PostHog equivalent. Set it before enabling.",+ base,+ )+ posthog_conditions.append(+ {"filters": {"properties": list(translated.properties)}, "name": f"Condition {index + 1}"}+ )+ # Customer.io puts the "no match" path last. PostHog takes the continue edge when nothing matches.+ links = self.links.get(str(source["id"]), [])+ new_links: list[tuple[str, str, int | None]] = []+ default_target = _EXIT_ID+ for target, _, edge_index in links:+ if edge_index is not None and edge_index < len(posthog_conditions):+ new_links.append((target, "branch", edge_index))+ else:+ default_target = target+ new_links.append((default_target, "continue", None))+ self.links[str(source["id"])] = new_links+ return {**base, "type": "conditional_branch", "config": {"conditions": posthog_conditions}}++ def _random_cohort(self, source: dict[str, Any], base: dict[str, Any]) -> dict[str, Any]:+ weights = [max(0, int(weight or 0)) for weight in source.get("cohorts") or []]+ names = list(source.get("cohort_names") or [])+ total = sum(weights) or 1+ percentages = [round(weight * 100 / total) for weight in weights]+ if percentages:+ # Rounding can leave the shares one off 100, so the largest group absorbs the difference.+ percentages[percentages.index(max(percentages))] += 100 - sum(percentages)+ cohorts = [+ {"percentage": percentage, **({"name": str(names[index])[:60]} if index < len(names) else {})}+ for index, percentage in enumerate(percentages)+ ]+ links = self.links.get(str(source["id"]), [])+ branch_links = [(target, "branch", index) for target, _, index in links if index is not None]+ last_target = max(branch_links, key=lambda link: link[2] or 0)[0] if branch_links else _EXIT_ID+ self.links[str(source["id"])] = [*branch_links, (last_target, "continue", None)]+ return {**base, "type": "random_cohort_branch", "config": {"cohorts": cohorts}}++ def _edges(self, roots: list[str]) -> list[dict[str, Any]]:+ def resolve(source_id: str) -> str:+ """The PostHog step a link to `source_id` lands on, following steps that were left out."""+ seen: set[str] = set()+ while source_id != _EXIT_ID and source_id not in self.actions:+ outgoing = self.links.get(source_id, [])+ if source_id in seen or not outgoing:+ return _EXIT_ID+ seen.add(source_id)+ # A step that was left out passes people on along its default path: the continue edge, or the+ # last branch, which Customer.io uses for "everyone else".+ continue_links = [link for link in outgoing if link[1] == "continue"]+ source_id = (continue_links[0] if continue_links else max(outgoing, key=lambda link: link[2] or 0))[0]+ return _EXIT_ID if source_id == _EXIT_ID else self.actions[source_id]["id"]++ edges: list[dict[str, Any]] = []+ if roots:+ edges.append({"from": TRIGGER_ID, "to": resolve(roots[0]), "type": "continue"})+ if len(roots) > 1:+ self.note(NoteLevel.UNSUPPORTED, "The automation has steps that no path reaches. They were left out.")+ else:+ edges.append({"from": TRIGGER_ID, "to": _EXIT_ID, "type": "continue"})+ for source_id, action in self.actions.items():+ if action["type"] == "exit":+ continue+ outgoing = self.links.get(source_id, [])+ if not outgoing:+ outgoing = [(_EXIT_ID, "continue", None)]+ for target, edge_type, index in outgoing:+ edge: dict[str, Any] = {"from": action["id"], "to": resolve(target), "type": edge_type}+ if edge_type == "branch":+ edge["index"] = index+ edges.append(edge)+ return edges+++def translate_campaign(+ source: CampaignSource, options: ImportOptions, snippet_room: SnippetRoom | None = None+) -> TranslatedWorkflow:+ return _Builder(source, options, snippet_room or SnippetRoom()).build()+++def _empty_trigger() -> dict[str, Any]:+ return {"type": "event", "filters": {"events": []}}+++def _exit_action(action_id: str, name: str) -> dict[str, Any]:+ return {"id": action_id, "name": name, "description": "", "type": "exit", "config": {"reason": "Default exit"}}+++def _duration(seconds: int) -> str:+ """A duration string for a number of seconds, within the workflow's per-unit limits: 2d, 1.5h, 45m, or 45s."""+ seconds = int(seconds)+ if seconds <= 0:+ return ""+ for unit, size, cap in _DURATION_UNITS:+ if seconds % size == 0 and (cap is None or seconds // size <= cap):+ return f"{seconds // size}{unit}"+ unit, size = next(+ (unit, size) for unit, size, cap in reversed(_DURATION_UNITS) if cap is None or seconds / size <= cap+ )+ return f"{seconds / size:.4f}".rstrip("0").rstrip(".") + unit+++def _nodes(tree: Any, keys: tuple[str, ...] = ("and", "or", "not")) -> Iterator[dict[str, Any]]:+ """Every dict in a condition tree, parents before their children."""+ if isinstance(tree, list):+ for child in tree:+ yield from _nodes(child, keys)+ elif isinstance(tree, dict):+ yield tree+ for key in keys:+ if key in tree:+ yield from _nodes(tree[key], keys)+++def _attribute_leaves(tree: Any) -> list[dict[str, Any]]:+ return [+ {**node["attribute"], "inverse": node["attribute"].get("inverse") or node.get("inverse")}+ for node in _nodes(tree)+ if isinstance(node.get("attribute"), dict)+ ]+++def _segment_is_inverse(tree: Any) -> bool:+ return any(+ bool(node["segment"].get("inverse") or node.get("inverse"))+ for node in _nodes(tree)+ if isinstance(node.get("segment"), dict)+ )+++def _foreign_event_leaves(tree: Any) -> list[dict[str, Any]]:+ return [+ node["foreign_event"]["attribute"]+ for node in _nodes(tree, ("and", "or"))+ if isinstance(node.get("foreign_event"), dict) and isinstance(node["foreign_event"].get("attribute"), dict)+ ]+++def _segment_references(tree: Any) -> list[str]:+ found: list[str] = []+ for node in _nodes(tree):+ segment = node.get("segment")+ if isinstance(segment, dict) and segment.get("id") is not None:+ found.append(str(segment["id"]))+ if node.get("type") in ("segments_negatable", "segments"):+ found.extend(str(segment) for segment in node.get("segments") or [])+ return found+++def _has_alternatives(tree: Any) -> bool:+ return any(isinstance(node.get("or"), list) and len(node["or"]) > 1 for node in _nodes(tree))+++def _is_bare_event(tree: Any) -> bool:+ """True when the condition only names an event, with no property, segment or negated part."""+ if isinstance(tree, list):+ return all(_is_bare_event(child) for child in tree)+ if not isinstance(tree, dict):+ return False+ extra = [key for key, value in tree.items() if value and key not in ("and", "or", "event_name")]+ event_name = tree.get("event_name")+ if isinstance(event_name, dict) and any(value for key, value in event_name.items() if key != "name"):+ return False+ return not extra and all(_is_bare_event(tree[key]) for key in ("and", "or") if key in tree)+++def _exit_event_names(tree: Any) -> list[str]:+ names: list[str] = []+ for node in _nodes(tree, ("and", "or")):+ event_name = node.get("event_name")+ if isinstance(event_name, dict) and event_name.get("name"):+ names.append(str(event_name["name"]))+ elif node.get("segment") is not None:+ names.append("__segment__")+ return names+++@frozen+class _Address:+ email: str+ name: str+++@frozen+class _Graph:+ actions: list[dict[str, Any]]+ edges: list[dict[str, Any]]+++def _prune_unreachable(actions: list[dict[str, Any]], edges: list[dict[str, Any]]) -> _Graph:+ adjacency: dict[str, list[str]] = {}+ for edge in edges:+ adjacency.setdefault(edge["from"], []).append(edge["to"])+ reachable: set[str] = set()+ queue = deque([TRIGGER_ID])+ while queue:+ node = queue.popleft()+ if node in reachable:+ continue+ reachable.add(node)+ queue.extend(adjacency.get(node, []))+ return _Graph(+ actions=[action for action in actions if action["id"] in reachable],+ edges=[edge for edge in edges if edge["from"] in reachable and edge["to"] in reachable],+ )+++def _plain_name(name: Any) -> str:+ """A step name without Liquid, which the editor shows as raw `{{ ... }}`. Customer.io often names emails by subject."""+ text = _LIQUID_TAG.sub("", str(name or "")[:2000])+ text = re.sub(r"\s+([!?.,:;])", r"\1", re.sub(r"\s+", " ", text)).strip()+ return text[:400]+++def _strip_comments(html: str) -> str:+ """Drop plain HTML comments, keeping Outlook's conditional ones (`<!--[if mso]>`, `<!--<![endif]-->`)."""+ parts: list[str] = []+ position = 0+ while (start := html.find("<!--", position)) != -1:+ end = html.find("-->", start + 4)+ if end == -1:+ break+ body = html[start + 4 : end]+ conditional = body.lstrip().startswith("[if") or "<![endif]" in body+ parts.append(html[position : end + 3] if conditional else html[position:start])+ position = end + 3+ parts.append(html[position:])+ return "".join(parts)diff --git a/products/workflows/backend/tests/test_customerio_import.py b/products/workflows/backend/tests/test_customerio_import.pynew file mode 100644index 000000000000..6461b7564236--- /dev/null+++ b/products/workflows/backend/tests/test_customerio_import.py@@ -0,0 +1,671 @@+import json+import base64+from datetime import UTC, datetime+from typing import Any+from urllib.parse import quote++from posthog.test.base import APIBaseTest, BaseTest+from unittest.mock import MagicMock, patch++from django.test import SimpleTestCase++from parameterized import parameterized++from posthog.event_usage import EventSource+from posthog.models.integration import Integration++from products.cdp.backend.api.test.test_hog_function_templates import MOCK_NODE_TEMPLATES+from products.cdp.backend.templates.hog_function_template import sync_template_to_db+from products.cohorts.backend.models.cohort import Cohort, CohortType+from products.messaging.backend.facade.categories import create_category+from products.workflows.backend.facade.contracts import (+ CampaignSource,+ ImportCategory,+ ImportOptions,+ ImportSegment,+ ImportSender,+ SnippetRoom,+)+from products.workflows.backend.facade.enums import WorkflowImportNoteLevel as NoteLevel+from products.workflows.backend.models.hog_flow.hog_flow import HogFlow+from products.workflows.backend.presentation.views.data_suggestion_drafts import validated_draft+from products.workflows.backend.services.customerio_import import conditions, liquid+from products.workflows.backend.services.customerio_import.translator import translate_campaign+++def _encode(condition: list[dict[str, Any]]) -> str:+ return base64.b64encode(quote(json.dumps(condition)).encode()).decode()+++def _onboarding_campaign() -> CampaignSource:+ campaign = {+ "id": "42",+ "name": "Onboarding",+ "type": "transactional",+ "event": "signed_up",+ "event_type": "",+ "attribute_filters": {"and": [{"field": "plan", "operator": "eq", "value": "free"}]},+ "conversion": True,+ "conversion_type": "perform_event",+ "conversion_event_name": "subscribed",+ "conversion_window": 604800,+ "exit_on_conversion_matched": True,+ "workflow_action_ids": ["1", "2", "3", "4", "5", "6", "7", "8", "9"],+ "edges": [+ {"from": "1", "to": "2", "type": "continue"},+ {"from": "2", "to": "3", "type": "continue"},+ {"from": "3", "to": "4", "type": "continue"},+ {"from": "4", "to": "5", "type": "branch", "index": 0},+ {"from": "4", "to": "6", "type": "branch", "index": 1},+ {"from": "5", "to": "9", "type": "continue"},+ {"from": "6", "to": "7", "type": "branch", "index": 0},+ {"from": "6", "to": "8", "type": "branch", "index": 1},+ {"from": "7", "to": "9", "type": "continue"},+ ],+ }+ actions: list[dict[str, Any]] = [+ {"id": "1", "type": "email_action", "name": "Welcome", "template_id": "11"},+ {"id": "2", "type": "twilio_action", "name": "Welcome SMS"},+ {"id": "3", "type": "delay_seconds_action", "name": "Wait 2 days", "delay": 172800},+ {+ "id": "4",+ "type": "conditional_wait_action",+ "name": "Wait for activation",+ "exit_type": "continue",+ "delay": 259200,+ "multi_conditions": [_encode([{"type": "foreign_event", "event_name": "activated", "event_type": ""}])],+ },+ {"id": "5", "type": "email_action", "name": "Activated tips", "template_id": "12"},+ {+ "id": "6",+ "type": "conditional_branch_action",+ "name": "Has a company?",+ "conditions": _encode([{"type": "attribute", "field": "company", "operator": "exists"}]),+ },+ {"id": "7", "type": "email_action", "name": "Team nudge", "template_id": "13"},+ {"id": "8", "type": "exit_action"},+ {"id": "9", "type": "exit_action"},+ ]+ templates = {+ "11": {+ "id": 11,+ "subject": "Welcome, {{customer.first_name}}",+ "body": '<p>Hi {{ customer.first_name | default: "there" }}</p>{% snippet "footer" %}',+ "editor": "html",+ },+ "12": {"id": 12, "subject": "Nice work", "body": "<p>{{event.plan}}</p>", "editor": "html"},+ "13": {"id": 13, "subject": "Bring your team", "body": "<p>{{customer.company}}</p>", "editor": "html"},+ }+ return CampaignSource(campaign=campaign, actions=actions, templates=templates)+++def _sync_email_template() -> None:+ sync_template_to_db(+ {+ **MOCK_NODE_TEMPLATES[0],+ "id": "template-email",+ "name": "Email",+ "inputs_schema": [{"key": "email", "type": "native_email", "label": "Email", "required": True}],+ }+ )+++_OPTIONS = ImportOptions(snippets={"footer": '<a href="{% unsubscribe_url %}">Unsubscribe</a>'}, segment_cohorts={})+++class TestCustomerioCampaignTranslation(BaseTest):+ def setUp(self) -> None:+ super().setUp()+ _sync_email_template()++ def test_translated_campaign_saves_as_a_draft_with_the_same_paths(self) -> None:+ translated = translate_campaign(_onboarding_campaign(), _OPTIONS)+ workflow = translated.workflow++ assert validated_draft(self.team, workflow, EventSource.API) is not None+ actions = {action["id"]: action for action in workflow["actions"]}+ assert workflow["trigger"]["filters"]["events"][0]["id"] == "signed_up"+ assert workflow["trigger"]["filters"]["events"][0]["properties"] == [+ {"key": "plan", "operator": "exact", "type": "event", "value": "free"}+ ]+ assert sorted(+ (edge["from"], edge["to"], edge["type"], edge.get("index")) for edge in workflow["edges"]+ ) == sorted(+ [+ ("trigger_node", "cio_1", "continue", None),+ # The SMS step is left out, so the welcome email leads straight to the delay.+ ("cio_1", "cio_3", "continue", None),+ ("cio_3", "cio_4", "continue", None),+ ("cio_4", "cio_5", "branch", 0),+ ("cio_4", "cio_6", "continue", None),+ ("cio_5", "cio_9", "continue", None),+ ("cio_6", "cio_7", "branch", 0),+ ("cio_6", "cio_8", "continue", None),+ ("cio_7", "cio_9", "continue", None),+ ]+ )+ assert actions["cio_3"]["config"] == {"delay_duration": "2d"}+ assert actions["cio_4"]["config"]["max_wait_duration"] == "3d"+ assert actions["cio_4"]["config"]["events"][0]["filters"]["events"][0]["id"] == "activated"+ assert actions["cio_6"]["config"]["conditions"][0]["filters"]["properties"] == [+ {"key": "company", "operator": "is_set", "type": "person"}+ ]+ email = actions["cio_1"]["config"]["inputs"]["email"]["value"]+ assert email["subject"] == "Welcome, {{person.properties.first_name}}"+ assert email["to"]["email"] == "{{person.properties.email}}"+ assert '<a href="{{ unsubscribe_url }}">Unsubscribe</a>' in email["html"]+ assert workflow["exit_condition"] == "exit_on_conversion"+ assert workflow["conversion"]["window"] == "7d"+ assert workflow["conversion"]["events"][0]["filters"]["events"][0]["id"] == "subscribed"+ assert any(note.level == NoteLevel.UNSUPPORTED and "SMS" in note.message for note in translated.notes)++ def test_values_the_workflow_checks_would_reject_are_reshaped(self) -> None:+ source = _onboarding_campaign()+ edges = [edge for edge in source.campaign["edges"] if edge["from"] != "5"]+ campaign = {+ **source.campaign,+ "workflow_action_ids": [*source.campaign["workflow_action_ids"], "10", "11"],+ "exit_on_conversion_matched": False,+ "edges": [+ *edges,+ {"from": "5", "to": "10", "type": "continue"},+ {"from": "10", "to": "11", "type": "continue"},+ {"from": "11", "to": "9", "type": "continue"},+ ],+ "conversion_event_filters": {+ "or": [+ {"foreign_event": {"attribute": {"field": "plan", "operator": "eq", "value": "pro"}}},+ {"foreign_event": {"attribute": {"field": "plan", "operator": "eq", "value": "team"}}},+ ]+ },+ "global_exit_conditions": [+ {"and": [{"event_name": {"name": "subscribed"}}, {"attribute": {"field": "plan", "value": "pro"}}]}+ ],+ }+ actions = [{**action, "delay": 129600} if action["id"] == "3" else action for action in source.actions] + [+ {"id": "10", "type": "attribute_update_action", "name": "Flags", "template_id": "14"},+ {"id": "11", "type": "webhook_action", "name": "CRM", "template_id": "15"},+ ]+ templates = {+ **source.templates,+ "14": {"body": json.dumps([{"name": "active", "value": False}, {"name": "score", "value": 0}])},+ "15": {+ "url": "https://crm.example.com",+ "headers": [+ {"name": "Authorization", "value": "Bearer fake-token"},+ {"name": "X-Api-Key", "value": "{{customer.key}}"},+ {"name": "X-Source", "value": "{{customer.id}}"},+ ],+ },+ }+ translated = translate_campaign(+ CampaignSource(campaign=campaign, actions=actions, templates=templates), _OPTIONS+ )+ workflow = translated.workflow+ steps = {action["id"]: action for action in workflow["actions"]}++ assert steps["cio_3"]["config"] == {"delay_duration": "1.5d"}+ assert steps["cio_10"]["config"]["inputs"]["set_properties"]["value"] == {"active": False, "score": 0}+ webhook = steps["cio_11"]["config"]["inputs"]+ assert webhook["headers"]["value"] == {"X-Source": "{{event.distinct_id}}"}+ assert webhook["secret_headers"]["value"] == {"Authorization": "Bearer fake-token"}+ assert "properties" not in workflow["conversion"]["events"][0]["filters"]["events"][0]+ assert workflow["exit_condition"] == "exit_only_at_end"+ messages = [note.message for note in translated.notes]+ assert any("`X-Api-Key` header" in message for message in messages)+ assert any("any of several conditions" in message for message in messages)+ assert any("removes people from this automation" in message for message in messages)++ def test_long_chain_of_left_out_steps_resolves(self) -> None:+ source = _onboarding_campaign()+ ids = [str(index) for index in range(100, 1300)]+ actions = [{"id": step_id, "type": "twilio_action", "name": "SMS"} for step_id in ids] + [+ {"id": "99", "type": "exit_action"}+ ]+ edges = [{"from": a, "to": b, "type": "continue"} for a, b in zip(ids, [*ids[1:], "99"])]+ campaign = {**source.campaign, "workflow_action_ids": [*ids, "99"], "edges": edges}++ workflow = translate_campaign(+ CampaignSource(campaign=campaign, actions=actions, templates={}), _OPTIONS+ ).workflow++ assert {"from": "trigger_node", "to": "cio_99", "type": "continue"} in workflow["edges"]++ def test_segment_trigger_without_a_cohort_still_saves_and_asks_for_one(self) -> None:+ source = _onboarding_campaign()+ campaign = {**source.campaign, "type": "seg_attr", "trigger": {"and": [{"or": [{"segment": {"id": 7}}]}]}}+ translated = translate_campaign(+ CampaignSource(campaign=campaign, actions=source.actions, templates=source.templates), _OPTIONS+ )++ assert validated_draft(self.team, translated.workflow, EventSource.API) is not None+ assert any(note.level == NoteLevel.NEEDS_INPUT and "segment 7" in note.message for note in translated.notes)++ def test_email_takes_its_layout_sender_and_category(self) -> None:+ source = _onboarding_campaign()+ campaign = {**source.campaign, "filters": _encode([{"type": "segments", "segments": [9]}])}+ actions = [+ {**action, "subscription_topic_id": 3, "name": "Welcome {{ customer.first_name }} !"}+ if action["id"] == "1"+ else action+ for action in source.actions+ ]+ templates = {+ **source.templates,+ "11": {**source.templates["11"], "layout_id": 5, "from_identity_id": "77", "reply_to": "[email protected]"},+ "12": {**source.templates["12"], "from": "Ops <[email protected]>", "layout_id": 6},+ }+ options = ImportOptions(+ snippets={},+ segment_cohorts={},+ layouts={+ "5": "<html><body><!-- header --><!--[if mso]><table><![endif]-->{{ content }}"+ "<p>{{ customer.city }}</p><!--[if !mso]><!--><i>web</i><!--<![endif]--></body></html>"+ },+ sender_addresses={"77": "Team <[email protected]>"},+ senders={"[email protected]": ImportSender(integration_id=12, email="[email protected]", name="PostHog")},+ topic_categories={"3": ImportCategory(id="cat-1", category_type="marketing")},+ segments={"9": ImportSegment(name="Beta list", type="static")},+ )+ translated = translate_campaign(+ CampaignSource(campaign=campaign, actions=actions, templates=templates), options+ )++ actions_by_id = {action["id"]: action for action in translated.workflow["actions"]}+ welcome = actions_by_id["cio_1"]["config"]+ email = welcome["inputs"]["email"]["value"]+ assert actions_by_id["cio_1"]["name"] == "Welcome!"+ assert email["html"].startswith(+ "<html><body><!--[if mso]><table><![endif]--><p>Hi {{ person.properties.first_name"+ )+ assert email["html"].endswith(+ "<p>{{ person.properties.city }}</p><!--[if !mso]><!--><i>web</i><!--<![endif]--></body></html>"+ )+ assert email["from"] == {"integrationId": 12, "email": "[email protected]", "name": "Team"}+ assert email["replyTo"] == "[email protected]"+ assert (welcome["message_category_id"], welcome["message_category_type"]) == ("cat-1", "marketing")+ activated = actions_by_id["cio_5"]["config"]["inputs"]["email"]["value"]+ assert activated["from"]["email"] == "[email protected]"+ assert activated["html"].startswith('<div style="font-family: Arial, Helvetica, sans-serif;">')+ messages = [(note.level, note.step_id, note.message) for note in translated.notes]+ assert any(+ level == NoteLevel.NEEDS_INPUT and step == "cio_5" and "[email protected]" in text+ for level, step, text in messages+ )+ assert any(+ level == NoteLevel.NEEDS_INPUT and step == "cio_5" and "layout 6" in text for level, step, text in messages+ )+ assert any(+ level == NoteLevel.NEEDS_INPUT and step is None and '"Beta list" (9) is a static list' in text+ for level, step, text in messages+ )+ assert any(text.startswith('Only people in segment "Beta list" (9) can enter') for _, _, text in messages)+ assert not any(step == "cio_1" and "sender" in text for _, step, text in messages)++ def test_language_split_keeps_only_the_default_message(self) -> None:+ campaign = {+ "id": "5",+ "name": "Localized",+ "type": "transactional",+ "event": "signed_up",+ "workflow_action_ids": ["1", "2", "3", "4"],+ "edges": [+ {"from": "1", "to": "2", "type": "branch", "index": 0},+ {"from": "1", "to": "3", "type": "branch", "index": 1},+ {"from": "2", "to": "4", "type": "continue"},+ {"from": "3", "to": "4", "type": "continue"},+ ],+ }+ actions: list[dict[str, Any]] = [+ {"id": "1", "type": "multi_lang_branch_action", "name": "Language", "multi_conditions": ["x"]},+ {"id": "2", "type": "email_action", "name": "Spanish", "template_id": "21"},+ {"id": "3", "type": "email_action", "name": "Default", "template_id": "22"},+ {"id": "4", "type": "exit_action"},+ ]+ templates = {"21": {"subject": "Hola", "body": "<p>Hola</p>"}, "22": {"subject": "Hi", "body": "<p>Hi</p>"}}+ translated = translate_campaign(+ CampaignSource(campaign=campaign, actions=actions, templates=templates), _OPTIONS+ )++ assert validated_draft(self.team, translated.workflow, EventSource.API) is not None+ assert [action["id"] for action in translated.workflow["actions"]] == ["trigger_node", "cio_3", "cio_4"]+ assert all(note.step_id != "cio_2" for note in translated.notes)+++class TestCustomerioConditions(SimpleTestCase):+ @parameterized.expand(+ [+ (+ "eq",+ {"type": "attribute", "field": "plan", "operator": "eq", "value": "pro"},+ [{"key": "plan", "operator": "exact", "type": "person", "value": "pro"}],+ ),+ (+ "negated",+ {"type": "attribute", "field": "plan", "operator": "!eq", "value": "pro"},+ [{"key": "plan", "operator": "is_not", "type": "person", "value": "pro"}],+ ),+ (+ "between",+ {"type": "event_attribute", "field": "amount", "operator": "bw", "value": "1", "value2": "9"},+ [+ {"key": "amount", "operator": "gte", "type": "event", "value": "1"},+ {"key": "amount", "operator": "lte", "type": "event", "value": "9"},+ ],+ ),+ (+ "relative date",+ {"type": "attribute", "field": "created_at", "operator": "timestamp_gt", "value": "-2592000"},+ [{"key": "created_at", "operator": "is_date_after", "type": "person", "value": "-30d"}],+ ),+ (+ "relative hours",+ {"type": "attribute", "field": "created_at", "operator": "timestamp_gt", "value": "-7200"},+ [{"key": "created_at", "operator": "is_date_after", "type": "person", "value": "-2h"}],+ ),+ (+ "absolute time of day",+ {"type": "attribute", "field": "created_at", "operator": "timestamp_lta", "value": "1700050000"},+ [+ {+ "key": "created_at",+ "operator": "is_date_before",+ "type": "person",+ "value": "2023-11-15T12:06:40+00:00",+ }+ ],+ ),+ ]+ )+ def test_translates_leaf_operators(+ self, _name: str, condition: dict[str, Any], expected: list[dict[str, Any]]+ ) -> None:+ assert conditions.translate_condition([condition], {}).properties == tuple(expected)++ @parameterized.expand(+ [+ ("message event", {"type": "message", "event": {"name": "*", "type": "opened_email"}, "inverse": False}),+ ("segment without a cohort", {"type": "segments_negatable", "segments": [3], "negate": False}),+ ("unknown operator", {"type": "attribute", "field": "plan", "operator": "matches", "value": "x"}),+ (+ "timestamp out of range",+ {"type": "attribute", "field": "created_at", "operator": "timestamp_gt", "value": "1700000000000000"},+ ),+ ]+ )+ def test_reports_conditions_it_cannot_translate(self, _name: str, condition: dict[str, Any]) -> None:+ translated = conditions.translate_condition([condition], {})+ assert translated.properties == ()+ assert len(translated.skipped) == 1++ def test_decodes_url_encoded_and_plain_base64(self) -> None:+ condition = [{"type": "attribute", "field": "url", "operator": "eq", "value": "https://example.com/a%20b&c"}]+ plain = base64.b64encode(json.dumps(condition).encode()).decode()+ assert conditions.decode(_encode(condition)) == condition+ assert conditions.decode(plain) == condition+++class TestCustomerioLiquid(SimpleTestCase):+ @parameterized.expand(+ [+ ("customer attribute", "{{ customer.first_name }}", "{{ person.properties.first_name }}"),+ ("customer id", "{{customer.id}}", "{{event.distinct_id}}"),+ (+ "event attribute in a tag",+ "{% if event.plan == 'pro' %}x{% endif %}",+ "{% if event.properties.plan == 'pro' %}x{% endif %}",+ ),+ ("text outside tags", "customer.first_name", "customer.first_name"),+ (+ "bracket access",+ "{{ event['plan'] }}{{ trigger[\"x\"] }}",+ "{{ event.properties['plan'] }}{{ event.properties[\"x\"] }}",+ ),+ (+ "quoted literal",+ "{{ 'customer.email' | append: customer.email }}",+ "{{ 'customer.email' | append: person.properties.email }}",+ ),+ ("unsubscribe tag", '<a href="{% unsubscribe_url %}">', '<a href="{{ unsubscribe_url }}">'),+ (+ "captured html prints raw",+ "{% capture cta %}<b>Go</b>{% endcapture %}{{ cta }}{{- cta | strip -}}{{ cta | raw }}{{ other }}",+ "{% capture cta %}<b>Go</b>{% endcapture %}{{ cta | raw }}{{- cta | strip | raw -}}{{ cta | raw }}{{ other }}",+ ),+ ]+ )+ def test_rewrites_globals(self, _name: str, source: str, expected: str) -> None:+ assert liquid.rewrite(source, {}).text == expected++ def test_reports_globals_and_snippets_it_cannot_resolve(self) -> None:+ rewritten = liquid.rewrite('{{ journey.code }}{% snippet "legal" %}', {})+ assert rewritten.unsupported_globals == ("journey",)+ assert rewritten.missing_snippets == ("legal",)++ def test_stops_inlining_snippets_past_the_budget(self) -> None:+ room = SnippetRoom(remaining=10)+ rewritten = liquid.rewrite('{% snippet "big" %}' * 3, {"big": "0123456"}, room)+ assert rewritten.text == '0123456{% snippet "big" %}{% snippet "big" %}'+ assert rewritten.missing_snippets == ("big",)+++class TestWorkflowImportAPI(APIBaseTest):+ def setUp(self) -> None:+ super().setUp()+ _sync_email_template()+ flag = patch("posthoganalytics.feature_enabled", return_value=True)+ flag.start()+ self.addCleanup(flag.stop)++ def test_import_needs_the_feature_flag(self) -> None:+ with patch("posthoganalytics.feature_enabled", return_value=False):+ response = self._post({"source": "customerio", "campaigns": [], "dry_run": False})++ assert response.status_code == 403+ assert response.json()["code"] == "feature_flag_required"++ def _post(self, body: dict[str, Any]) -> Any:+ return self.client.post(f"/api/projects/{self.team.id}/workflow_imports/", body, format="json")++ def _import(self, **overrides: Any) -> dict[str, Any]:+ source = _onboarding_campaign()+ body = {+ "source": "customerio",+ "campaigns": [+ {"campaign": source.campaign, "actions": source.actions, "templates": list(source.templates.values())}+ ],+ "snippets": {"footer": "<p>Footer</p>"},+ **overrides,+ }+ response = self._post(body)+ assert response.status_code == 200, response.json()+ return response.json()["results"][0]++ def test_reimport_updates_the_draft_until_it_is_enabled(self) -> None:+ first = self._import()+ preview = self._import(dry_run=True)+ second = self._import()++ assert (first["status"], preview["status"], second["status"]) == ("created", "preview", "updated")+ assert first["workflow_id"] == preview["workflow_id"] == second["workflow_id"]+ assert HogFlow.objects.filter(team=self.team).count() == 1++ HogFlow.objects.filter(pk=first["workflow_id"]).update(status="active")+ third = self._import()+ assert third["status"] == "skipped"+ assert HogFlow.objects.get(pk=first["workflow_id"]).status == "active"++ def test_segment_trigger_uses_the_mapped_realtime_cohort(self) -> None:+ cohort = Cohort.objects.create(+ team=self.team,+ name="Beta list",+ cohort_type=CohortType.REALTIME,+ last_backfill_person_properties_at=datetime(2026, 1, 1, tzinfo=UTC),+ last_backfill_events_at=datetime(2026, 1, 1, tzinfo=UTC),+ filters={+ "properties": {+ "type": "AND",+ "values": [{"key": "plan", "value": "pro", "type": "person", "operator": "exact"}],+ }+ },+ )+ source = _onboarding_campaign()+ campaign = {**source.campaign, "type": "seg_attr", "trigger": {"and": [{"segment": {"id": 7}}]}}+ result = self._import(+ campaigns=[{"campaign": campaign, "actions": source.actions, "templates": list(source.templates.values())}],+ segment_cohorts={"7": cohort.id},+ )++ assert result["status"] == "created", result["validation_errors"]+ trigger = HogFlow.objects.get(pk=result["workflow_id"]).trigger+ assert trigger["cohort_id"] == cohort.id++ def test_reimport_leaves_a_workflow_the_user_cannot_edit(self) -> None:+ first = self._import()+ with patch(+ "products.workflows.backend.presentation.views.workflow_import_drafts.UserAccessControl.check_access_level_for_ref",+ return_value=False,+ ):+ second = self._import()++ assert second["status"] == "skipped"+ assert second["workflow_id"] == first["workflow_id"]++ @parameterized.expand(+ [+ ("templates not a container", {"templates": "invalid"}),+ ("campaign id too long", {"campaign_id": "9" * 201}),+ ]+ )+ def test_rejects_malformed_campaigns_before_saving(self, _name: str, change: dict[str, Any]) -> None:+ source = _onboarding_campaign()+ campaign = {**source.campaign, "id": change.get("campaign_id", "42")}+ entry: dict[str, Any] = {"campaign": campaign, "actions": source.actions}+ if "templates" in change:+ entry["templates"] = change["templates"]+ body = {"source": "customerio", "campaigns": [entry]}+ response = self._post(body)++ assert response.status_code == 400+ assert not HogFlow.objects.filter(team=self.team).exists()++ @parameterized.expand(+ [+ ("delay that is not a number", {"3": {"delay": "soon"}}, None, "failed"),+ (+ "message condition naming a plain event",+ {"4": {"multi_conditions": [_encode([{"type": "message", "event": "delivered"}])]}},+ None,+ "created",+ ),+ ("branch index that is not a number", {}, "x", "failed"),+ ("branch index as a string", {}, "1", "created"),+ ]+ )+ def test_an_odd_value_affects_only_that_automation(+ self, _name: str, action_changes: dict[str, dict[str, Any]], branch_index: str | None, status: str+ ) -> None:+ source = _onboarding_campaign()+ changed = [{**action, **action_changes.get(action["id"], {})} for action in source.actions]+ edges = [+ {**edge, "index": branch_index} if branch_index is not None and edge.get("index") == 1 else edge+ for edge in source.campaign["edges"]+ ]+ body = {+ "source": "customerio",+ "campaigns": [+ {"campaign": {**source.campaign, "edges": edges}, "actions": changed},+ {"campaign": {**source.campaign, "id": "43"}, "actions": source.actions},+ ],+ }+ response = self._post(body)++ assert response.status_code == 200, response.json()+ assert [result["status"] for result in response.json()["results"]] == [status, "created"]++ def test_dry_run_saves_nothing(self) -> None:+ result = self._import(dry_run=True)++ assert result["status"] == "preview"+ assert result["notes"]+ assert not HogFlow.objects.filter(team=self.team).exists()++ @patch("products.workflows.backend.services.customerio_import.app_api.time.sleep")+ @patch("products.workflows.backend.services.customerio_import.app_api.requests.Session.get")+ def test_reads_message_content_with_the_stored_key(self, mock_get: MagicMock, _sleep: MagicMock) -> None:+ Integration.objects.create(team=self.team, kind="customerio-app", sensitive_config={"app_api_key": "k"})+ create_category(self.team.id, None, {"key": "customerio_topic_3", "name": "Product news"})+ email_actions = [+ {"id": 1, "type": "email", "subject": "Hi", "body": "<p>Fetched {{customer.first_name}}</p>", "from_id": 4},+ {"id": 5, "type": "email", "subject": "Tips", "body": "<p>Tips</p>"},+ ]++ def respond(url: str, **kwargs: Any) -> MagicMock:+ response = MagicMock(status_code=200, headers={})+ if url.startswith("https://api.customer.io"):+ response.status_code = 401+ elif url.endswith("/campaigns/42/actions") and not kwargs["params"]:+ response.json.return_value = {"actions": email_actions[:1], "next": "page2"}+ elif url.endswith("/campaigns/42/actions"):+ response.json.return_value = {"actions": email_actions[1:], "next": ""}+ elif url.endswith("/sender_identities"):+ response.json.return_value = {"sender_identities": [{"id": 4, "email": "[email protected]"}]}+ else:+ response.json.return_value = {}+ return response++ mock_get.side_effect = respond+ source = _onboarding_campaign()+ actions = [+ {**action, "subscription_topic_id": 3} if action["id"] == "1" else action for action in source.actions+ ]+ body = {"source": "customerio", "campaigns": [{"campaign": source.campaign, "actions": actions}]}+ response = self._post(body)++ assert response.status_code == 200, response.json()+ assert response.json()["content_fetched"] is True+ result = response.json()["results"][0]+ flow = HogFlow.objects.get(pk=result["workflow_id"])+ flow_actions = {action["id"]: action for action in flow.actions}+ welcome = flow_actions["cio_1"]["config"]+ assert welcome["inputs"]["email"]["value"]["html"] == (+ '<div style="font-family: Arial, Helvetica, sans-serif;"><p>Fetched {{person.properties.first_name}}</p></div>'+ )+ assert welcome["inputs"]["email"]["value"]["from"]["email"] == "[email protected]"+ assert welcome["message_category_type"] == "marketing"+ assert flow_actions["cio_5"]["config"]["inputs"]["email"]["value"]["subject"] == "Tips"+ # Template 13 has no App API action with its step's id, so it still asks for content.+ assert any(note["step_id"] == "cio_7" and "content" in note["message"] for note in result["notes"])+ assert all(call.kwargs["headers"]["Authorization"] == "Bearer k" for call in mock_get.call_args_list)++ HogFlow.objects.filter(pk=result["workflow_id"]).update(status="active")+ mock_get.reset_mock()+ assert self._post(body).json()["results"][0]["status"] == "skipped"+ assert not mock_get.called++ @patch("products.workflows.backend.services.customerio_import.app_api.time.sleep")+ @patch("products.workflows.backend.services.customerio_import.app_api.requests.Session.get")+ def test_rejected_key_still_imports_and_says_why(self, mock_get: MagicMock, _sleep: MagicMock) -> None:+ Integration.objects.create(team=self.team, kind="customerio-app", sensitive_config={"app_api_key": "k"})+ mock_get.return_value = MagicMock(status_code=401, headers={})+ source = _onboarding_campaign()+ body = {"source": "customerio", "campaigns": [{"campaign": source.campaign, "actions": source.actions}]}+ response = self._post(body)++ assert response.status_code == 200, response.json()+ assert response.json()["content_fetched"] is False+ result = response.json()["results"][0]+ assert result["status"] == "created", result["validation_errors"]+ assert any("could not read this automation's messages" in note["message"] for note in result["notes"])++ def test_notes_are_readable_by_workflow(self) -> None:+ result = self._import()++ url = f"/api/projects/{self.team.id}/workflow_imports/for_workflow/"+ response = self.client.get(url, {"hog_flow_id": result["workflow_id"]})+ assert response.status_code == 200+ assert response.json()["source_id"] == "42"+ assert response.json()["notes"] == result["notes"]++ other = HogFlow.objects.create(team=self.team, name="Built by hand")+ assert self.client.get(url, {"hog_flow_id": str(other.id)}).status_code == 204diff --git a/products/workflows/frontend/generated/api.schemas.ts b/products/workflows/frontend/generated/api.schemas.tsindex 998e57e06b95..e5f1cb58aa42 100644--- a/products/workflows/frontend/generated/api.schemas.ts+++ b/products/workflows/frontend/generated/api.schemas.ts@@ -2685,6 +2685,188 @@ export interface EmailDraftApi {template_reason: EmailDraftFallbackReasonEnumApi | null}+/**+ * Customer.io snippet name to its content, inlined where a message uses `{% snippet "name" %}`.+ */+export type WorkflowImportRequestApiSnippets = { [key: string]: string }++/**+ * Customer.io segment id to the PostHog cohort that replaces it. Segment triggers and segment conditions with no cohort here are left for the team to set.+ */+export type WorkflowImportRequestApiSegmentCohorts = { [key: string]: number }++/**+ * Customer.io layout id to the layout's `body`, from `GET /v1/environments/{environment_id}/layouts/{layout_id}`. An email whose template names a `layout_id` is wrapped in that layout at `{{ content }}`.+ */+export type WorkflowImportRequestApiLayouts = { [key: string]: string }++/**+ * * `customerio` - Customer.io+ */+export type ImportProviderKeyEnumApi = (typeof ImportProviderKeyEnumApi)[keyof typeof ImportProviderKeyEnumApi]++export const ImportProviderKeyEnumApi = {+ Customerio: 'customerio',+} as const++/**+ * The `campaign` object from Customer.io's `GET /v1/environments/{environment_id}/campaigns/{campaign_id}` (Journeys UI API, what the Customer.io CLI and MCP read). Must include `id`, `type`, `edges` and the trigger fields.+ */+export type CustomerioCampaignApiCampaign = { [key: string]: unknown }++export type CustomerioCampaignApiActionsItem = { [key: string]: unknown }++export interface CustomerioCampaignApi {+ /** The `campaign` object from Customer.io's `GET /v1/environments/{environment_id}/campaigns/{campaign_id}` (Journeys UI API, what the Customer.io CLI and MCP read). Must include `id`, `type`, `edges` and the trigger fields. */+ campaign: CustomerioCampaignApiCampaign+ /** The top-level `actions` array from the same response, every step of the automation. */+ actions: CustomerioCampaignApiActionsItem[]+ /** The message templates the actions point at, from `GET /v1/environments/{environment_id}/templates/{template_id}`: a list of template objects, or an object keyed by template id. Leave them out when the project stored a Customer.io App API key: PostHog then reads the content from Customer.io itself. Without either, messages import with empty content. */+ templates?: unknown+}++export interface CustomerioSenderIdentityApi {+ /** The sender identity's id, which templates name in `from_identity_id`. */+ id: string+ /** The address the sender sends from. */+ email: string+ /** The sender's display name. */+ name?: string+}++export interface CustomerioSegmentApi {+ /** The segment's id. */+ id: string+ /** The segment's name. */+ name: string+ /** The segment's type: `dynamic`, or `static` for a hand-made list. */+ type: string+}++export interface WorkflowImportRequestApi {+ /** The tool the automations come from.+ *+ * * `customerio` - Customer.io */+ source: ImportProviderKeyEnumApi+ /** The automations to import, at most 50. People are never read from these objects. */+ campaigns: CustomerioCampaignApi[]+ /** Customer.io snippet name to its content, inlined where a message uses `{% snippet "name" %}`. */+ snippets?: WorkflowImportRequestApiSnippets+ /** Customer.io segment id to the PostHog cohort that replaces it. Segment triggers and segment conditions with no cohort here are left for the team to set. */+ segment_cohorts?: WorkflowImportRequestApiSegmentCohorts+ /** Customer.io layout id to the layout's `body`, from `GET /v1/environments/{environment_id}/layouts/{layout_id}`. An email whose template names a `layout_id` is wrapped in that layout at `{{ content }}`. */+ layouts?: WorkflowImportRequestApiLayouts+ /** Customer.io sender identities, from `GET /v1/environments/{environment_id}/identities?type=email`. Each email gets the PostHog sender with the same address. */+ sender_identities?: CustomerioSenderIdentityApi[]+ /** Customer.io segment ids, names and types, without members, so the notes can name each segment and flag static ones. */+ segments?: CustomerioSegmentApi[]+ /** When true, translate and check the automations but save nothing, to preview the import. */+ dry_run?: boolean+}++/**+ * * `preview` - Preview+ * * `created` - Created+ * * `updated` - Updated+ * * `skipped` - Skipped+ * * `failed` - Failed+ */+export type WorkflowImportStatusEnumApi = (typeof WorkflowImportStatusEnumApi)[keyof typeof WorkflowImportStatusEnumApi]++export const WorkflowImportStatusEnumApi = {+ Preview: 'preview',+ Created: 'created',+ Updated: 'updated',+ Skipped: 'skipped',+ Failed: 'failed',+} as const++/**+ * * `approximated` - Approximated+ * * `unsupported` - Unsupported+ * * `needs_input` - Needs input+ */+export type WorkflowImportNoteLevelEnumApi =+ (typeof WorkflowImportNoteLevelEnumApi)[keyof typeof WorkflowImportNoteLevelEnumApi]++export const WorkflowImportNoteLevelEnumApi = {+ Approximated: 'approximated',+ Unsupported: 'unsupported',+ NeedsInput: 'needs_input',+} as const++export interface WorkflowImportNoteApi {+ /** `approximated`: the step behaves differently in PostHog. `unsupported`: the setting was left out. `needs_input`: the team must set something before enabling.+ *+ * * `approximated` - Approximated+ * * `unsupported` - Unsupported+ * * `needs_input` - Needs input */+ level: WorkflowImportNoteLevelEnumApi+ /** What changed or was left out, and what to do about it. */+ message: string+ /**+ * The workflow step the note is about, or null.+ * @nullable+ */+ step_id: string | null+ /**+ * That step's name, or null.+ * @nullable+ */+ step_name: string | null+}++export interface WorkflowImportResultApi {+ /** The automation's id in Customer.io. */+ source_id: string+ /** The automation's name. */+ name: string+ /** `preview` when dry_run is set, `created` or `updated` for a saved draft, `skipped` when an earlier import of it is no longer a draft, `failed` when the draft does not pass the workflow checks.+ *+ * * `preview` - Preview+ * * `created` - Created+ * * `updated` - Updated+ * * `skipped` - Skipped+ * * `failed` - Failed */+ status: WorkflowImportStatusEnumApi+ /**+ * The draft workflow's id, when one was saved. In a preview, the draft an import would replace.+ * @nullable+ */+ workflow_id: string | null+ /** What the import changed or left out. */+ notes: WorkflowImportNoteApi[]+ /** Why the draft failed the workflow checks, for `failed`. */+ validation_errors: string[]+}++export interface WorkflowImportResponseApi {+ /** One result per automation, in request order. */+ results: WorkflowImportResultApi[]+ /** True when PostHog read message content from Customer.io with the project's stored App API key. */+ content_fetched: boolean+}++export interface WorkflowImportRecordApi {+ /** The import record's id. */+ readonly id: string+ /** The tool the workflow was imported from. */+ readonly source: string+ /** The automation's id in that tool. */+ readonly source_id: string+ /** The automation's name in that tool. */+ readonly source_name: string+ /**+ * The workflow.+ * @nullable+ */+ readonly hog_flow: string | null+ /** What the import changed or left out. */+ readonly notes: readonly WorkflowImportNoteApi[]+ /** When the workflow was last imported. */+ readonly updated_at: string+}+export type PeopleImportCreateApiRowsItem = { [key: string]: string }export interface PeopleImportCreateApi {@@ -3380,3 +3562,10 @@ export type HogFlowsSendCalendarRetrieveParams = {*/start: string}++export type WorkflowImportsForWorkflowRetrieveParams = {+ /**+ * The workflow to read the import of.+ */+ hog_flow_id: string+}diff --git a/products/workflows/frontend/generated/api.ts b/products/workflows/frontend/generated/api.tsindex 09944a1bfb0f..5932e9e722cf 100644--- a/products/workflows/frontend/generated/api.ts+++ b/products/workflows/frontend/generated/api.ts@@ -81,6 +81,10 @@ import type {SendCalendarFlowApi,TeamEmailReputationResponseApi,WorkflowEmailPauseStatusApi,+ WorkflowImportRecordApi,+ WorkflowImportRequestApi,+ WorkflowImportResponseApi,+ WorkflowImportsForWorkflowRetrieveParams,WorkflowProposalApi,WorkflowProposalApproveRequestApi,WorkflowProposalCreateApi,@@ -1588,6 +1592,60 @@ export const workflowEmailDraftsCreate = async (})}+export const getWorkflowImportsCreateUrl = (projectId: string) => {+ return `/api/projects/${projectId}/workflow_imports/`+}++/**+ * Turns Customer.io automations into draft workflows. Pass each automation as Customer.io's Journeys UI API returns it. Pass the templates its messages use, or leave them out when the project stored a Customer.io App API key, and PostHog reads them. Each result lists what the import changed or left out. Importing an automation again updates its draft, unless that workflow is no longer a draft. Set dry_run to preview without saving.+ * @summary Import Customer.io automations as draft workflows+ */+export const workflowImportsCreate = async (+ projectId: string,+ workflowImportRequestApi: WorkflowImportRequestApi,+ options?: RequestInit+): Promise<WorkflowImportResponseApi> => {+ return apiMutator<WorkflowImportResponseApi>(getWorkflowImportsCreateUrl(projectId), {+ ...options,+ method: 'POST',+ headers: { 'Content-Type': 'application/json', ...options?.headers },+ body: JSON.stringify(workflowImportRequestApi),+ })+}++export const getWorkflowImportsForWorkflowRetrieveUrl = (+ projectId: string,+ params: WorkflowImportsForWorkflowRetrieveParams+) => {+ const normalizedParams = new URLSearchParams()++ Object.entries(params || {}).forEach(([key, value]) => {+ if (value !== undefined) {+ normalizedParams.append(key, value === null ? 'null' : String(value))+ }+ })++ const stringifiedParams = normalizedParams.toString()++ return stringifiedParams.length > 0+ ? `/api/projects/${projectId}/workflow_imports/for_workflow/?${stringifiedParams}`+ : `/api/projects/${projectId}/workflow_imports/for_workflow/`+}++/**+ * @summary Read what importing a workflow changed or left out+ */+export const workflowImportsForWorkflowRetrieve = async (+ projectId: string,+ params: WorkflowImportsForWorkflowRetrieveParams,+ options?: RequestInit+): Promise<WorkflowImportRecordApi | void> => {+ return apiMutator<WorkflowImportRecordApi | void>(getWorkflowImportsForWorkflowRetrieveUrl(projectId, params), {+ ...options,+ method: 'GET',+ })+}+export const getWorkflowPeopleImportsCreateUrl = (projectId: string) => {return `/api/projects/${projectId}/workflow_people_imports/`}diff --git a/products/workflows/frontend/generated/api.zod.ts b/products/workflows/frontend/generated/api.zod.tsindex aada0d029824..6a3c2a43e70e 100644--- a/products/workflows/frontend/generated/api.zod.ts+++ b/products/workflows/frontend/generated/api.zod.ts@@ -3448,6 +3448,87 @@ export const WorkflowEmailDraftsCreateBody = /* @__PURE__ */ zod.object({.describe('ID of that entity: an issue, early access feature or survey UUID, or a feature flag or cohort ID.'),})+/**+ * Turns Customer.io automations into draft workflows. Pass each automation as Customer.io's Journeys UI API returns it. Pass the templates its messages use, or leave them out when the project stored a Customer.io App API key, and PostHog reads them. Each result lists what the import changed or left out. Importing an automation again updates its draft, unless that workflow is no longer a draft. Set dry_run to preview without saving.+ * @summary Import Customer.io automations as draft workflows+ */+export const workflowImportsCreateBodySenderIdentitiesItemNameDefault = ``+export const workflowImportsCreateBodyDryRunDefault = false++export const WorkflowImportsCreateBody = /* @__PURE__ */ zod.object({+ source: zod+ .enum(['customerio'])+ .describe('\* `customerio` - Customer.io')+ .describe('The tool the automations come from.\n\n\* `customerio` - Customer.io'),+ campaigns: zod+ .array(+ zod.object({+ campaign: zod+ .record(zod.string(), zod.unknown())+ .describe(+ "The `campaign` object from Customer.io's `GET \/v1\/environments\/{environment_id}\/campaigns\/{campaign_id}` (Journeys UI API, what the Customer.io CLI and MCP read). Must include `id`, `type`, `edges` and the trigger fields."+ ),+ actions: zod+ .array(zod.record(zod.string(), zod.unknown()))+ .describe('The top-level `actions` array from the same response, every step of the automation.'),+ templates: zod+ .unknown()+ .optional()+ .describe(+ 'The message templates the actions point at, from `GET \/v1\/environments\/{environment_id}\/templates\/{template_id}`: a list of template objects, or an object keyed by template id. Leave them out when the project stored a Customer.io App API key: PostHog then reads the content from Customer.io itself. Without either, messages import with empty content.'+ ),+ })+ )+ .describe('The automations to import, at most 50. People are never read from these objects.'),+ snippets: zod+ .record(zod.string(), zod.string())+ .optional()+ .describe('Customer.io snippet name to its content, inlined where a message uses `{% snippet \"name\" %}`.'),+ segment_cohorts: zod+ .record(zod.string(), zod.number())+ .optional()+ .describe(+ 'Customer.io segment id to the PostHog cohort that replaces it. Segment triggers and segment conditions with no cohort here are left for the team to set.'+ ),+ layouts: zod+ .record(zod.string(), zod.string())+ .optional()+ .describe(+ "Customer.io layout id to the layout's `body`, from `GET \/v1\/environments\/{environment_id}\/layouts\/{layout_id}`. An email whose template names a `layout_id` is wrapped in that layout at `{{ content }}`."+ ),+ sender_identities: zod+ .array(+ zod.object({+ id: zod.string().describe("The sender identity's id, which templates name in `from_identity_id`."),+ email: zod.email().describe('The address the sender sends from.'),+ name: zod+ .string()+ .default(workflowImportsCreateBodySenderIdentitiesItemNameDefault)+ .describe("The sender's display name."),+ })+ )+ .optional()+ .describe(+ 'Customer.io sender identities, from `GET \/v1\/environments\/{environment_id}\/identities?type=email`. Each email gets the PostHog sender with the same address.'+ ),+ segments: zod+ .array(+ zod.object({+ id: zod.string().describe("The segment's id."),+ name: zod.string().describe("The segment's name."),+ type: zod.string().describe("The segment's type: `dynamic`, or `static` for a hand-made list."),+ })+ )+ .optional()+ .describe(+ 'Customer.io segment ids, names and types, without members, so the notes can name each segment and flag static ones.'+ ),+ dry_run: zod+ .boolean()+ .default(workflowImportsCreateBodyDryRunDefault)+ .describe('When true, translate and check the automations but save nothing, to preview the import.'),+})+/*** @summary Create or update people from rows and add them to a new static cohort*/diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex af46e53b4b66..189621c2a1de 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -29213,6 +29213,40 @@ export namespace Schemas {description?: string | null;}+ /**+ * The `campaign` object from Customer.io's `GET /v1/environments/{environment_id}/campaigns/{campaign_id}` (Journeys UI API, what the Customer.io CLI and MCP read). Must include `id`, `type`, `edges` and the trigger fields.+ */+ export type CustomerioCampaignCampaign = { [key: string]: unknown };++ export type CustomerioCampaignActionsItem = { [key: string]: unknown };++ export interface CustomerioCampaign {+ /** The `campaign` object from Customer.io's `GET /v1/environments/{environment_id}/campaigns/{campaign_id}` (Journeys UI API, what the Customer.io CLI and MCP read). Must include `id`, `type`, `edges` and the trigger fields. */+ campaign: CustomerioCampaignCampaign;+ /** The top-level `actions` array from the same response, every step of the automation. */+ actions: CustomerioCampaignActionsItem[];+ /** The message templates the actions point at, from `GET /v1/environments/{environment_id}/templates/{template_id}`: a list of template objects, or an object keyed by template id. Leave them out when the project stored a Customer.io App API key: PostHog then reads the content from Customer.io itself. Without either, messages import with empty content. */+ templates?: unknown;+ }++ export interface CustomerioSegment {+ /** The segment's id. */+ id: string;+ /** The segment's name. */+ name: string;+ /** The segment's type: `dynamic`, or `static` for a hand-made list. */+ type: string;+ }++ export interface CustomerioSenderIdentity {+ /** The sender identity's id, which templates name in `from_identity_id`. */+ id: string;+ /** The address the sender sends from. */+ email: string;+ /** The sender's display name. */+ name?: string;+ }+export type DashboardFilters = { [key: string]: unknown };/**@@ -124231,6 +124265,146 @@ export namespace Schemas {latest_ci_engine?: CIEngineEnum | null;}+ /**+ * * `approximated` - Approximated+ * * `unsupported` - Unsupported+ * * `needs_input` - Needs input+ */+ export type WorkflowImportNoteLevelEnum = typeof WorkflowImportNoteLevelEnum[keyof typeof WorkflowImportNoteLevelEnum];+++ export const WorkflowImportNoteLevelEnum = {+ Approximated: 'approximated',+ Unsupported: 'unsupported',+ NeedsInput: 'needs_input',+ } as const;++ export interface WorkflowImportNote {+ /** `approximated`: the step behaves differently in PostHog. `unsupported`: the setting was left out. `needs_input`: the team must set something before enabling.+ *+ * * `approximated` - Approximated+ * * `unsupported` - Unsupported+ * * `needs_input` - Needs input */+ level: WorkflowImportNoteLevelEnum;+ /** What changed or was left out, and what to do about it. */+ message: string;+ /**+ * The workflow step the note is about, or null.+ * @nullable+ */+ step_id: string | null;+ /**+ * That step's name, or null.+ * @nullable+ */+ step_name: string | null;+ }++ export interface WorkflowImportRecord {+ /** The import record's id. */+ readonly id: string;+ /** The tool the workflow was imported from. */+ readonly source: string;+ /** The automation's id in that tool. */+ readonly source_id: string;+ /** The automation's name in that tool. */+ readonly source_name: string;+ /**+ * The workflow.+ * @nullable+ */+ readonly hog_flow: string | null;+ /** What the import changed or left out. */+ readonly notes: readonly WorkflowImportNote[];+ /** When the workflow was last imported. */+ readonly updated_at: string;+ }++ /**+ * Customer.io snippet name to its content, inlined where a message uses `{% snippet "name" %}`.+ */+ export type WorkflowImportRequestSnippets = {[key: string]: string};++ /**+ * Customer.io segment id to the PostHog cohort that replaces it. Segment triggers and segment conditions with no cohort here are left for the team to set.+ */+ export type WorkflowImportRequestSegmentCohorts = {[key: string]: number};++ /**+ * Customer.io layout id to the layout's `body`, from `GET /v1/environments/{environment_id}/layouts/{layout_id}`. An email whose template names a `layout_id` is wrapped in that layout at `{{ content }}`.+ */+ export type WorkflowImportRequestLayouts = {[key: string]: string};++ export interface WorkflowImportRequest {+ /** The tool the automations come from.+ *+ * * `customerio` - Customer.io */+ source: ImportProviderKeyEnum;+ /** The automations to import, at most 50. People are never read from these objects. */+ campaigns: CustomerioCampaign[];+ /** Customer.io snippet name to its content, inlined where a message uses `{% snippet "name" %}`. */+ snippets?: WorkflowImportRequestSnippets;+ /** Customer.io segment id to the PostHog cohort that replaces it. Segment triggers and segment conditions with no cohort here are left for the team to set. */+ segment_cohorts?: WorkflowImportRequestSegmentCohorts;+ /** Customer.io layout id to the layout's `body`, from `GET /v1/environments/{environment_id}/layouts/{layout_id}`. An email whose template names a `layout_id` is wrapped in that layout at `{{ content }}`. */+ layouts?: WorkflowImportRequestLayouts;+ /** Customer.io sender identities, from `GET /v1/environments/{environment_id}/identities?type=email`. Each email gets the PostHog sender with the same address. */+ sender_identities?: CustomerioSenderIdentity[];+ /** Customer.io segment ids, names and types, without members, so the notes can name each segment and flag static ones. */+ segments?: CustomerioSegment[];+ /** When true, translate and check the automations but save nothing, to preview the import. */+ dry_run?: boolean;+ }++ /**+ * * `preview` - Preview+ * * `created` - Created+ * * `updated` - Updated+ * * `skipped` - Skipped+ * * `failed` - Failed+ */+ export type WorkflowImportStatusEnum = typeof WorkflowImportStatusEnum[keyof typeof WorkflowImportStatusEnum];+++ export const WorkflowImportStatusEnum = {+ Preview: 'preview',+ Created: 'created',+ Updated: 'updated',+ Skipped: 'skipped',+ Failed: 'failed',+ } as const;++ export interface WorkflowImportResult {+ /** The automation's id in Customer.io. */+ source_id: string;+ /** The automation's name. */+ name: string;+ /** `preview` when dry_run is set, `created` or `updated` for a saved draft, `skipped` when an earlier import of it is no longer a draft, `failed` when the draft does not pass the workflow checks.+ *+ * * `preview` - Preview+ * * `created` - Created+ * * `updated` - Updated+ * * `skipped` - Skipped+ * * `failed` - Failed */+ status: WorkflowImportStatusEnum;+ /**+ * The draft workflow's id, when one was saved. In a preview, the draft an import would replace.+ * @nullable+ */+ workflow_id: string | null;+ /** What the import changed or left out. */+ notes: WorkflowImportNote[];+ /** Why the draft failed the workflow checks, for `failed`. */+ validation_errors: string[];+ }++ export interface WorkflowImportResponse {+ /** One result per automation, in request order. */+ results: WorkflowImportResult[];+ /** True when PostHog read message content from Customer.io with the project's stored App API key. */+ content_fetched: boolean;+ }+export interface WorkflowJobStep {/** 1-based position of the step in the job. */number: number;@@ -143356,6 +143530,13 @@ export namespace Schemas {workflow_id: string;};+ export type WorkflowImportsForWorkflowRetrieveParams = {+ /**+ * The workflow to read the import of.+ */+ hog_flow_id: string;+ };+export type PublicHogFunctionTemplatesListParams = {/*** Number of results to return per page.