Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions products/workflows/backend/presentation/views/hog_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from rest_framework.response import Response
from rest_framework.serializers import BaseSerializer

from posthog.schema import ProductKey
from posthog.schema import ProductIntentContext, ProductKey

from posthog.hogql.compiler.bytecode import create_bytecode
from posthog.hogql.context import HogQLContext
Expand All @@ -57,10 +57,17 @@
from posthog.auth import InternalAPIAuthentication
from posthog.clickhouse.query_tagging import Feature, tag_queries
from posthog.dataclasses import frozen
from posthog.event_usage import AGENT_EVENT_SOURCES, EventSource, get_event_source, report_user_action
from posthog.event_usage import (
AGENT_EVENT_SOURCES,
EventSource,
get_event_source,
get_request_analytics_properties,
report_user_action,
)
from posthog.exceptions_capture import capture_exception
from posthog.helpers.impersonation import is_impersonated
from posthog.models import Team, User
from posthog.models.product_intent.product_intent import ProductIntent
from posthog.models.property.parse import expand_cohort_properties, parse_property_group_data
from posthog.permissions import (
AccessControlPermission,
Expand Down Expand Up @@ -5123,6 +5130,20 @@ def _report_workflow_action(
except Exception as e:
logger.warning("Failed to capture workflow usage event", event=event, error=str(e))

def _register_workflows_product_intent(self, workflow: Workflow) -> None:
if is_impersonated(self.request) or workflow.origin_product == HogFlowOriginProduct.LOOPS:
return
try:
ProductIntent.register(
team=self.team,
product_type=ProductKey.WORKFLOWS,
context=ProductIntentContext.WORKFLOW_CREATED,
user=cast(User, self.request.user),
metadata=get_request_analytics_properties(self.request),
)
except Exception as e:
capture_exception(e)

def perform_create(self, serializer):
if self._is_mcp_request(self.request) and serializer.validated_data.get("status") == HogFlowState.ACTIVE:
raise exceptions.ValidationError(
Expand All @@ -5136,6 +5157,7 @@ def perform_create(self, serializer):
validated_data=serializer.validated_data,
)
serializer.instance = self._written_workflow(result)
self._register_workflows_product_intent(serializer.instance)
self._log_activity(serializer.instance.id, serializer.instance.name, "created", detail_type="standard")
self._emit_resource_edited(serializer.instance)

Expand Down
48 changes: 48 additions & 0 deletions products/workflows/backend/tests/api/test_hog_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from posthog.models.activity_logging.activity_log import ActivityLog
from posthog.models.integration import Integration
from posthog.models.personal_api_key import PersonalAPIKey
from posthog.models.product_intent.product_intent import ProductIntent
from posthog.models.utils import generate_random_token_personal, hash_key_value
from posthog.test.fixtures import create_app_metric2

Expand Down Expand Up @@ -5563,6 +5564,53 @@ def _get_hog_flow_activity(self, flow_id: Optional[str] = None) -> list:
assert activity.status_code == status.HTTP_200_OK
return activity.json().get("results")

def _post_webhook_hog_flow(self, **extra_fields: Any):
hog_flow, _ = self._create_hog_flow_with_action(
{
"template_id": "template-webhook",
"inputs": {"url": {"value": "https://example.com"}},
}
)
return self.client.post(f"/api/projects/{self.team.id}/hog_flows", {**hog_flow, **extra_fields})

def _has_workflows_product_intent(self) -> bool:
return ProductIntent.objects.filter(team=self.team, product_type="workflows").exists()

def test_create_hog_flow_registers_workflows_product_intent(self):
response = self._post_webhook_hog_flow()
assert response.status_code == status.HTTP_201_CREATED, response.json()

intent = ProductIntent.objects.get(team=self.team, product_type="workflows")
assert intent.contexts == {"workflow_created": 1}

def test_create_loop_registers_no_workflows_product_intent(self):
response = self._post_webhook_hog_flow(origin_product="loops")

assert response.status_code == status.HTTP_201_CREATED, response.json()
assert not self._has_workflows_product_intent()

def test_create_hog_flow_from_impersonated_session_registers_no_workflows_product_intent(self):
with patch("products.workflows.backend.presentation.views.hog_flow.is_impersonated", return_value=True):
response = self._post_webhook_hog_flow()

assert response.status_code == status.HTTP_201_CREATED, response.json()
assert not self._has_workflows_product_intent()

def test_create_hog_flow_reports_a_failed_workflows_product_intent_to_error_tracking(self):
registration_error = RuntimeError("intent store unavailable")
with (
patch(
"products.workflows.backend.presentation.views.hog_flow.ProductIntent.register",
side_effect=registration_error,
),
patch("products.workflows.backend.presentation.views.hog_flow.capture_exception") as capture_exception,
):
response = self._post_webhook_hog_flow()

assert response.status_code == status.HTTP_201_CREATED, response.json()
assert HogFlow.objects.filter(team=self.team, id=response.json()["id"]).exists()
capture_exception.assert_any_call(registration_error)

def test_create_hog_flow_logs_activity(self):
hog_flow, _ = self._create_hog_flow_with_action(
{
Expand Down
7 changes: 1 addition & 6 deletions products/workflows/frontend/WorkflowsScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { LemonTabs } from 'lib/lemon-ui/LemonTabs'
import { trackedActionToUrl } from 'lib/logic/scenes/trackedActionToUrl'
import { removeProjectIdIfPresent } from 'lib/utils/kea-router'
import { addProductIntent } from 'lib/utils/product-intents'
import { capitalizeFirstLetter } from 'lib/utils/strings'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene, SceneExport } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'

import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'
import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'
import { ProductKey } from '~/queries/schema/schema-general'
import { AccessControlLevel, AccessControlResourceType, Breadcrumb } from '~/types'

import { EmailSuspensionBanner } from './EmailSuspensionBanner'
Expand Down Expand Up @@ -200,10 +199,6 @@ export function WorkflowsScene(props: WorkflowsSceneProps = {}): JSX.Element {
}
const showsNewWorkflowButton = currentTab === 'workflows' || currentTab === 'templates' || currentTab === 'calendar'
const onNewWorkflow = (): void => {
void addProductIntent({
product_type: ProductKey.WORKFLOWS,
intent_context: ProductIntentContext.WORKFLOW_CREATED,
})
// Open the template chooser on the type the list shows, so a person on the
// Messaging tab sees messaging templates first.
setTypeFilter(currentTab === 'workflows' ? templateTypeForListType(filters.type) : 'all')
Expand Down