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(signals): let startup program orgs press Create PR on reports (#97164)

posthog[bot]da0dfbab3776
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.py
index 71774ca87ec9..80f73431d828 100644
--- a/products/tasks/backend/facade/api.py
+++ b/products/tasks/backend/facade/api.py
@@ -876,14 +876,22 @@ def task_exempt_from_code_access(task_id: str | UUID, team_id: int) -> bool:
- ``SIGNAL_REPORT`` linked to a report in this team, repo-less, and carrying no GitHub
integration (the Inbox "Discuss" fallback). Reports are minted by scouts and the link is
team-scoped by the write serializer, so a caller can't forge one. Acting on a report is
- entitled through self-driving (`product-autonomy`). A report task that resolved a
- repository, or that carries the team integration for a repo-less discussion, is not
- exempt, because `create_task` only gives it either after the gate passed. Re-checking
+ entitled through self-driving (`product-autonomy`). A discussion that resolved a
+ repository, or that carries the team integration while repo-less, is not exempt under
+ this shape, because `create_task` only gives it either after the gate passed. Re-checking
here costs the caller nothing.
- ``SIGNALS_CHAT`` (Inbox scout chat), reserved for server-side creation by the signals
scout-chat endpoint; the write serializer rejects it from API callers. Only while
repo-less: chat tasks are minted without repositories, and attaching one via update
would turn the exemption into ungated cloud code work.
+ - ``SIGNAL_REPORT`` linked to a report in this team that also carries an ``implementation``
+ ``SignalReportTask`` row for this task (the Inbox "Create PR"). Such a task holds a
+ repository by design, so the repo-less shape above can never cover it. Auto-start opens the
+ same PR run for the same report from the server without consulting the gate, and
+ self-driving prices a PR flat and caps it per report, so gating the button would only make
+ the outcome depend on who started the run. ``record_report_task`` writes the row on both
+ paths. The relationship label is client input, but it counts only on a row scoped to this
+ team and to the report the task itself links, so asserting it buys nothing on its own.
A bare ``SIGNAL_REPORT`` origin without a report link deliberately does not qualify:
``origin_product`` is client input, so an FK-less claim would be a one-field waitlist
@@ -905,6 +913,13 @@ def task_exempt_from_code_access(task_id: str | UUID, team_id: int) -> bool:
repositories=[],
github_integration__isnull=True,
github_user_integration__isnull=True,
+ )
+ | Q(
+ origin_product=Task.OriginProduct.SIGNAL_REPORT,
+ signal_report__team_id=team_id,
+ signal_report_tasks__team_id=team_id,
+ signal_report_tasks__report_id=F("signal_report_id"),
+ signal_report_tasks__relationship="implementation",
),
id=task_id,
team_id=team_id,
@@ -5962,7 +5977,7 @@ def create_task(
# report-linked task that can never open a PR. "Implementation" (Create PR) and legacy clients
# (no relationship) always resolve one. "Discuss" (and any other non-implementation label)
# resolves one only for a caller the Desktop gate passed: the run endpoint gates a
- # repository-backed report task, so resolving for anyone else would 403 the very click this
+ # repository-backed discussion, so resolving for anyone else would 403 the very click this
# path exists to unblock (see `task_exempt_from_code_access`).
signal_report = validated_data.get("signal_report")
if (
diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.py
index 36795a183e7b..dee00c1871f6 100644
--- a/products/tasks/backend/tests/test_api.py
+++ b/products/tasks/backend/tests/test_api.py
@@ -13104,6 +13104,35 @@ class TestCloudUsageGate(BaseTaskAPITest):
task.save()
return task
+ def _create_pr_task(
+ self,
+ *,
+ relationship: str = "implementation",
+ link_other_report: bool = False,
+ report_in_other_team: bool = False,
+ ) -> Task:
+ # `record_report_task` writes this row on the manual and the auto-start path.
+ from products.signals.backend.models import SignalReport, SignalReportTask
+
+ report_team = (
+ Team.objects.create(organization=self.organization, name="Report Team")
+ if report_in_other_team
+ else self.team
+ )
+ report = SignalReport.objects.create(team=report_team)
+ task = self.create_task()
+ task.origin_product = Task.OriginProduct.SIGNAL_REPORT
+ task.signal_report = report
+ task.repository = "posthog/posthog"
+ task.save()
+ SignalReportTask.objects.create(
+ team=self.team,
+ report=SignalReport.objects.create(team=self.team) if link_other_report else report,
+ task=task,
+ relationship=relationship,
+ )
+ return task
+
@patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")
@patch("products.tasks.backend.logic.services.code_usage_gate.get_posthog_code_usage")
def test_run_without_code_access_returns_403_before_usage_check(self, mock_gate, mock_workflow):
@@ -13332,6 +13361,25 @@ class TestCloudUsageGate(BaseTaskAPITest):
self.assertFalse(tasks_facade.task_exempt_from_code_access(task.id, self.team.id))
+ @parameterized.expand(
+ [
+ ("implementation_link", "implementation", False, False, True),
+ ("discussion_label", "discussion", False, False, False),
+ ("link_to_another_report", "implementation", True, False, False),
+ ("report_in_another_team", "implementation", False, True, False),
+ ]
+ )
+ def test_exemption_for_create_pr_task_needs_a_team_scoped_implementation_link(
+ self, _name, relationship, link_other_report, report_in_other_team, expected
+ ):
+ task = self._create_pr_task(
+ relationship=relationship,
+ link_other_report=link_other_report,
+ report_in_other_team=report_in_other_team,
+ )
+
+ self.assertEqual(tasks_facade.task_exempt_from_code_access(task.id, self.team.id), expected)
+
def test_create_signal_report_task_ignores_channel_repository(self):
from products.signals.backend.models import SignalReport
from products.tasks.backend.models import Channel
@@ -13544,6 +13592,24 @@ class TestCloudUsageGate(BaseTaskAPITest):
mock_gate.assert_called_once()
self.assertEqual(TaskRun.objects.filter(task=task).exists(), expected_status == status.HTTP_200_OK)
+ @patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")
+ @patch("products.tasks.backend.logic.services.code_usage_gate.get_posthog_code_usage", return_value=None)
+ def test_run_report_create_pr_task_bypasses_code_access(self, _mock_gate, mock_workflow):
+ # "Create PR" holds a repository by design, so the repo-less Inbox exemption cannot cover
+ # it. The implementation link entitles the run while the Desktop policy denies the caller.
+ self.set_tasks_feature_flag(False)
+ task = self._create_pr_task()
+
+ response = self.client.post(
+ f"/api/projects/@current/tasks/{task.id}/run/",
+ {"mode": "background"},
+ format="json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertTrue(TaskRun.objects.filter(task=task).exists())
+ mock_workflow.assert_called_once()
+
class TestGetPosthogCodeUsage(TestCase):
@override_settings(CLOUD_DEPLOYMENT="US")