: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
"""Install the synthetic repository catalog into one case's isolated team.
The seeded team-level GitHub integration carries a placeholder token and a fresh light and heavy
cache, so the production selector reads the fixture instead of GitHub: `sync_full_cache` takes its
TTL fast path, and the agent's `execute-sql` queries see only these rows.
"""
from __future__ import annotations
from collections.abc import Callable
from typing import Any
from django.utils import timezone
from posthog.egress.limiter.policies import Priority
from posthog.integrations import IntegrationManager
from posthog.models.integration import GitHubIntegration
from posthog.models.integration_repository_cache import IntegrationRepositoryCacheEntry
from posthog.models.repo_routing_rule import RepoRoutingRule
from products.tasks.backend.facade.agents import CustomPromptSandboxContext
from .fixtures import FIXTURE_VERSION, ORG, REPOSITORIES, RepoSelectionScenario, fixture_fingerprint
# Not a credential. GitHub rejects it, so a `gh api` call from the sandbox cannot reach real repositories.
PLACEHOLDER_GITHUB_TOKEN = "fixture-token-not-a-github-credential"
def install_repository_catalog(team_id: int, user_id: int, scenario: RepoSelectionScenario) -> dict[str, Any]:
now = timezone.now()
record = IntegrationManager.github.oauth.create(
team_id,
external_id=f"eval-fixture-{team_id}",
config={"installation_id": f"eval-fixture-{team_id}", "account": {"type": "Organization", "name": ORG}},
sensitive_config={"access_token": PLACEHOLDER_GITHUB_TOKEN},
created_by_id=user_id,
)
# The SDK has no repository cache write: production fills it from GitHub. The fixture writes
# the synthetic listing directly so the selector never calls GitHub.
github_integration = GitHubIntegration(record, source="repo_selection_eval", priority=Priority.BATCH).integration
github_integration.repository_cache = [
{
"id": index,
"name": repo.name,
"full_name": repo.full_name,
"private": repo.private,
"archived": repo.archived,
"language": repo.primary_language,
"default_branch": "main",
}
for index, repo in enumerate(REPOSITORIES, start=1)
]
github_integration.repository_cache_updated_at = now
github_integration.save( # nosemgrep: integration-model-through-sdk -- synthetic eval fixture; the SDK has no repository cache write
update_fields=["repository_cache", "repository_cache_updated_at"]
)
IntegrationRepositoryCacheEntry.objects.bulk_create(
[
IntegrationRepositoryCacheEntry(
integration_id=record.id,
team_id=team_id,
full_name=repo.full_name.lower(),
description=repo.description,
topics=list(repo.topics),
archived=repo.archived,
primary_language=repo.primary_language,
default_branch="main",
default_branch_sha=repo.default_branch_sha,
readme=repo.readme,
tree_paths="\n".join(repo.tree_paths),
tree_truncated=False,
)
for repo in REPOSITORIES
]
)
RepoRoutingRule.objects.bulk_create(
[
RepoRoutingRule(
team_id=team_id,
rule_text=rule.rule_text,
repository=rule.repository,
priority=priority,
created_by_id=user_id,
)
for priority, rule in enumerate(scenario.routing_rules)
]
)
return {
"fixture_version": FIXTURE_VERSION,
"fixture_fingerprint": fixture_fingerprint(),
"candidate_repositories": sorted(repo.full_name.lower() for repo in REPOSITORIES if not repo.archived),
"routing_rules": [f"{rule.rule_text} → {rule.repository}" for rule in scenario.routing_rules],
}
def scenario_seeder(scenario: RepoSelectionScenario) -> Callable[[CustomPromptSandboxContext], dict[str, Any]]:
def seed(context: CustomPromptSandboxContext) -> dict[str, Any]:
return install_repository_catalog(context.team_id, context.user_id, scenario)
seed.__name__ = f"seed_{scenario.name}"
return seed