Skip to content

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

fix(crm): create group notes as markdown notebooks (#98351)

Sinan Kucukkoselerb16d86173017
diff --git a/ee/clickhouse/views/groups.py b/ee/clickhouse/views/groups.py
index 6e92843d96f1..d303bb609425 100644
--- a/ee/clickhouse/views/groups.py
+++ b/ee/clickhouse/views/groups.py
@@ -51,9 +51,12 @@ from products.access_control.backend.presentation.access_control import UserAcce
from products.event_definitions.backend.models.property_definition import PropertyType
from products.notebooks.backend.facade import api as notebooks
from products.notebooks.backend.facade.content import (
+ build_markdown_notebook_content,
+ convert_notebook_content_to_markdown,
create_bullet_list,
- create_empty_paragraph,
create_heading_with_text,
+ create_paragraph_with_content,
+ create_paragraph_with_text,
create_text_content,
)
@@ -871,15 +874,19 @@ class GroupsViewSet(TeamAndOrgViewSetMixin, mixins.ListModelMixin, mixins.Create
)
def _create_notebook_for_group(self, group: Group):
- group_name = group.group_properties.get("name", "")
+ # A group name is customer data and can hold line breaks. Markdown escapes inline syntax
+ # but keeps line breaks, so a second line starts its own block — a heading, a list, or a
+ # live component. Collapse the name to one line, which is what a title is anyway.
+ group_name = " ".join(str(group.group_properties.get("name") or "").split())
notebook_title = f"{group_name} Notes" if group_name else "Notes"
- notebook_content = [
+ template_nodes = [
create_heading_with_text(text=notebook_title, level=1),
- create_text_content(
- text="This is a place for you and your team to write collaborative notes about this group"
+ create_paragraph_with_text(
+ "This is a place for you and your team to write collaborative notes about this group"
+ ),
+ create_paragraph_with_content(
+ [create_text_content(text="Here's a template to get you started", is_italic=True)]
),
- create_empty_paragraph(),
- create_text_content(text="Here's a template to get you started", is_italic=True),
create_heading_with_text(text="Quick context", level=2),
create_bullet_list(items=["Industry: ", "Key contacts: ", "Tech stack: "]),
create_heading_with_text(text="Usage patterns", level=2),
@@ -887,6 +894,10 @@ class GroupsViewSet(TeamAndOrgViewSetMixin, mixins.ListModelMixin, mixins.Create
create_heading_with_text(text="Last interaction", level=2),
create_bullet_list(items=["Date: ", "Context: ", "Next steps: "]),
]
+ # The shared converter escapes inline markdown syntax, so the group name renders as text.
+ notebook_content = build_markdown_notebook_content(
+ convert_notebook_content_to_markdown({"type": "doc", "content": template_nodes})
+ )
notebooks.create_group_notebook(self.team.id, group.id, title=notebook_title, content=notebook_content)
diff --git a/ee/clickhouse/views/test/test_clickhouse_groups.py b/ee/clickhouse/views/test/test_clickhouse_groups.py
index 6f4c5eeaef48..2e2c2adf1f52 100644
--- a/ee/clickhouse/views/test/test_clickhouse_groups.py
+++ b/ee/clickhouse/views/test/test_clickhouse_groups.py
@@ -38,6 +38,7 @@ from posthog.models.team.team import Team
from posthog.test.persons import create_group_type_mapping, create_person
from posthog.test.test_utils import create_group_type_mapping_without_created_at
+from products.notebooks.backend.facade.content import convert_notebook_content_to_markdown, is_markdown_notebook_content
from products.notebooks.backend.models import Notebook, ResourceNotebook
from ee.clickhouse.views.groups import _decode_groups_cursor, _encode_groups_cursor
@@ -244,13 +245,42 @@ class GroupsViewSetTestCase(ClickhouseTestMixin, APIBaseTest):
)
self.assertEqual(1, Notebook.objects.filter(team=self.team).count())
- # Test default notebook content structure
+ # The note is a markdown notebook, which is the one shape the notebook cell tools accept.
notebook = relationship.notebook
- self.assertIsNotNone(notebook.content)
- self.assertEqual(notebook.content[0]["type"], "heading")
- self.assertEqual(notebook.content[0]["attrs"]["level"], 1)
- self.assertEqual(notebook.content[0]["content"][0]["text"], "Mr. Krabs Notes")
- self.assertEqual(notebook.content[1]["type"], "text")
+ self.assertTrue(is_markdown_notebook_content(notebook.content))
+ markdown = convert_notebook_content_to_markdown(notebook.content)
+ self.assertTrue(markdown.startswith("# Mr. Krabs Notes"), markdown)
+ self.assertIn("## Quick context", markdown)
+ self.assertIn("- Industry:", markdown)
+
+ @time_machine.travel("2021-05-02", tick=False)
+ @patch(f"{PATH}.feature_enabled_or_false", return_value=True)
+ def test_group_name_with_a_line_break_cannot_open_a_notebook_block(self, _):
+ index: GroupTypeIndex = 0
+ key = "key"
+ hostile_name = 'Krusty Krab\n<Query title="p" nodeId="n1" query={{"kind":"HogQLQuery"}} />'
+ group = create_group(
+ team_id=self.team.pk,
+ group_type_index=index,
+ group_key=key,
+ properties={"name": hostile_name},
+ )
+
+ response = self.client.get(f"/api/projects/{self.team.id}/groups/find?group_type_index={index}&group_key={key}")
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ relationship = ResourceNotebook.objects.filter(group=group.id).first()
+ assert relationship is not None
+
+ # A tag is only a component when it starts a line, so the whole name has to stay on the
+ # heading line — otherwise the second line renders as a live query cell.
+ markdown = convert_notebook_content_to_markdown(relationship.notebook.content)
+ self.assertEqual(
+ markdown.splitlines()[0],
+ '# Krusty Krab <Query title="p" nodeId="n1" query={{"kind":"HogQLQuery"}} /> Notes',
+ )
+ self.assertNotIn("\n<Query", markdown)
+ self.assertNotIn("\n", relationship.notebook.title or "")
@time_machine.travel("2021-05-02", tick=False)
@patch(f"{PATH}.feature_enabled_or_false", return_value=True)