: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
fix(signals): honor base branch setting on the inbox Create PR button (#80470)
Christiaan Hendriksen0bdbd1c0ed53
diff --git a/products/signals/backend/auto_start.py b/products/signals/backend/auto_start.pyindex 09aa2e52d5b8..d34fe589846c 100644--- a/products/signals/backend/auto_start.py+++ b/products/signals/backend/auto_start.py@@ -689,9 +689,7 @@ async def maybe_autostart_implementation_task()return- base_branch = None- if repository and team_config:- base_branch = (team_config.autostart_base_branches or {}).get(repository.lower())+ base_branch = team_config.base_branch_for(repository) if team_config else Nonesource_references = await database_sync_to_async(_fetch_source_references, thread_sensitive=False)(team_id, report_iddiff --git a/products/signals/backend/facade/api.py b/products/signals/backend/facade/api.pyindex 4a7feb962635..278cac610738 100644--- a/products/signals/backend/facade/api.py+++ b/products/signals/backend/facade/api.py@@ -152,6 +152,19 @@ def persisted_repo_selection(report_id: str) -> "RepoSelectionResult | None":return persisted_repo_selection_impl(report_id)+def autostart_base_branch_for_repository(team_id: int, repository: str | None) -> str | None:+ """Team's configured base branch for ``repository``, for callers outside the signals product."""+ if not repository:+ return None++ from products.signals.backend.models import (+ SignalTeamConfig, # noqa: PLC0415 — avoids importing model layer at facade import time+ )++ config = SignalTeamConfig.objects.filter(team_id=team_id).only("autostart_base_branches").first()+ return config.base_branch_for(repository) if config is not None else None++def get_default_slack_notification_channel(team_id: int) -> str | None:"""Team-default Slack channel for signal notifications, stored as "<channel_id>|#name"."""from products.signals.backend.models import (diff --git a/products/signals/backend/models.py b/products/signals/backend/models.pyindex 81c589879fd4..e26f454df566 100644--- a/products/signals/backend/models.py+++ b/products/signals/backend/models.py@@ -148,6 +148,17 @@ class SignalTeamConfig(UUIDModel):verbose_name = "Signal team config"verbose_name_plural = "Signal team configs"+ def base_branch_for(self, repository: str | None) -> str | None:+ """Configured base branch for ``repository`` ("organization/repository"), if any.++ Keys are stored lowercased by the serializer, so the lookup lowercases to match.+ Every path that opens a self-driving pull request resolves through here, so that+ auto-start and the inbox "Create PR" button cannot disagree on the branch.+ """+ if not repository or not isinstance(self.autostart_base_branches, dict):+ return None+ return self.autostart_base_branches.get(repository.lower()) or None+register_team_extension_signal(SignalTeamConfig, logger=logger)diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 7c8702bf53f3..7db49b03457b 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -5203,6 +5203,19 @@ def run_task(pending_user_artifact_ids = validated_data.get("pending_user_artifact_ids") or []is_pi_task = task.runtime == Task.Runtime.PI+ if branch is None and not resume_from_run_id and task.origin_product == Task.OriginProduct.SIGNAL_REPORT:+ # The inbox "Create PR" button sends no branch, so without this the run would target the+ # repository's GitHub default branch. Auto-start resolves the same setting when it builds+ # its task. This sits before the warm-run reuse check below, so that a sandbox idling on+ # the default branch is not reused for a run that needs the configured branch.+ from products.signals.backend.facade.api import ( # noqa: PLC0415 — cross-product read kept off the api import path+ autostart_base_branch_for_repository,+ )++ branch = autostart_base_branch_for_repository(+ team_id, task.repositories[0] if task.repositories else task.repository+ )+if not resume_from_run_id:warm_run = _idling_warm_run_for_task(task)if warm_run is not None and (branch or None) == (warm_run.branch or None):diff --git a/products/tasks/backend/tests/test_facade.py b/products/tasks/backend/tests/test_facade.pyindex 3278bec6c261..0e32c2e1e0d8 100644--- a/products/tasks/backend/tests/test_facade.py+++ b/products/tasks/backend/tests/test_facade.py@@ -13,6 +13,7 @@ from parameterized import parameterizedfrom posthog.models import Integration, Organization, Teamfrom posthog.models.user import User+from products.signals.backend.models import SignalTeamConfigfrom products.tasks.backend.facade import (api as facade,contracts,@@ -467,6 +468,57 @@ class TestFacadeReadsAndMappers(TestCase):new_run = task.runs.exclude(id=previous_run.id).get()self.assertEqual(new_run.state.get("self_driving_head_branch"), "posthog-self-driving/fix-abc123")+ @parameterized.expand(+ [+ # The inbox "Create PR" button sends no branch, so the team's configured base branch is+ # the only thing that can keep the PR off the repo's GitHub default branch. Repo casing+ # differs from the stored key because GitHub preserves it while the serializer lowercases.+ ("configured_branch_applied", {"acme/web": "dev"}, {}, "dev"),+ # A caller that picked a branch already decided; re-resolving would discard that choice.+ ("explicit_branch_wins", {"acme/web": "dev"}, {"branch": "hotfix"}, "hotfix"),+ # Another repo's entry must never be borrowed, because that lands the PR on a+ # branch belonging to a different repository.+ ("other_repo_not_borrowed", {"acme/api": "staging"}, {}, None),+ ]+ )+ def test_run_task_applies_configured_base_branch(+ self, _name: str, overrides: dict, extra_validated_data: dict, expected_branch: str | None+ ):+ SignalTeamConfig.objects.update_or_create(team=self.team, defaults={"autostart_base_branches": overrides})+ task = self._make_task(repository="Acme/Web", origin_product=Task.OriginProduct.SIGNAL_REPORT)++ with patch("products.tasks.backend.facade.api._trigger_task_processing_workflow"):+ result = facade.run_task(+ task.id,+ self.team.id,+ self.user.id,+ validated_data={"mode": "interactive", "run_source": "signal_report", **extra_validated_data},+ )++ assert result is not None and result.error is None+ run = task.runs.get()+ self.assertEqual(run.branch, expected_branch)+ self.assertEqual((run.state or {}).get("pr_base_branch"), expected_branch)++ def test_run_task_leaves_branch_unset_for_user_created_tasks(self):+ # The override is scoped to self-driving tasks. A user-created task keeps targeting the repo+ # default, so broadening the resolution would silently redirect unrelated task runs.+ SignalTeamConfig.objects.update_or_create(+ team=self.team, defaults={"autostart_base_branches": {"acme/web": "dev"}}+ )+ task = self._make_task(repository="Acme/Web", origin_product=Task.OriginProduct.USER_CREATED)++ with patch("products.tasks.backend.facade.api._trigger_task_processing_workflow"):+ result = facade.run_task(+ task.id,+ self.team.id,+ self.user.id,+ validated_data={"mode": "interactive", "run_source": "signal_report"},+ )++ assert result is not None and result.error is None+ self.assertIsNone(task.runs.get().branch)+def test_stale_queued_created_at_hard_cap(self):task = self._make_task()now = django_timezone.now()