: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
feat(agentic-provisioning): add github_integration and wizard_runs resource actions (#69858)
Rafael Audibert06c8d1a9aa0c
diff --git a/ee/api/agentic_provisioning/errors.py b/ee/api/agentic_provisioning/errors.pynew file mode 100644index 000000000000..71ab92af9394--- /dev/null+++ b/ee/api/agentic_provisioning/errors.py@@ -0,0 +1,38 @@+"""Error-envelope helpers for the agentic provisioning API.++Two helpers coexist because the provisioning views serve two distinct wire+contracts (plus OAuth endpoints, which follow RFC 6749's flat+{"error", "error_description"} shape and can't use either helper):++- `error_response` -> {"status": "error", "id": ..., "error": {...}} for the+ resource endpoints, whose success envelope carries "status" (provisioning state)+ and "id" (the partner's resource ID), so errors mirror that.+- `typed_error_response` -> {"type": "error", "error": {...}} for account+ requests and GitHub grants, where "type" is the discriminator partners switch+ on ("oauth" | "registering" | "error").++Collapsing them would break partner clients that branch on "status" vs "type".+"""++from __future__ import annotations++from typing import Any++import structlog+from rest_framework.response import Response++logger = structlog.get_logger(__name__)+++def error_response(code: str, message: str, resource_id: str = "", status: int = 400) -> Response:+ logger.warning("provisioning.error_response", code=code, message=message, resource_id=resource_id, status=status)+ return Response({"status": "error", "id": resource_id, "error": {"code": code, "message": message}}, status=status)+++def typed_error_response(code: str, message: str, request_id: str | None = None, status: int = 400) -> Response:+ # No blanket warning log here: call sites already emit their own telemetry+ # (_capture_provisioning_event / logger.warning) at the right granularity.+ body: dict[str, Any] = {"type": "error", "error": {"code": code, "message": message}}+ if request_id is not None:+ body = {"id": request_id, **body}+ return Response(body, status=status)diff --git a/ee/api/agentic_provisioning/test/test_url_aliases.py b/ee/api/agentic_provisioning/test/test_url_aliases.pyindex 8847e4a4b4aa..0245db0155a8 100644--- a/ee/api/agentic_provisioning/test/test_url_aliases.py+++ b/ee/api/agentic_provisioning/test/test_url_aliases.py@@ -16,6 +16,8 @@ PROVISIONING_ALIAS_URLS = [("resource_detail", f"/api/provisioning/resources/{RESOURCE_ID}", "GET"),("deep_links", "/api/provisioning/deep_links", "POST"),("github_grants_create", "/api/provisioning/github/grants", "POST"),+ ("github_integration", f"/api/provisioning/resources/{RESOURCE_ID}/github_integration", "POST"),+ ("wizard_runs", f"/api/provisioning/resources/{RESOURCE_ID}/wizard_runs", "POST"),]diff --git a/ee/api/agentic_provisioning/test/test_wizard_resource_actions.py b/ee/api/agentic_provisioning/test/test_wizard_resource_actions.pynew file mode 100644index 000000000000..9908623c668a--- /dev/null+++ b/ee/api/agentic_provisioning/test/test_wizard_resource_actions.py@@ -0,0 +1,226 @@+from datetime import timedelta++from unittest.mock import MagicMock, patch++from django.core.cache import cache+from django.test import override_settings+from django.utils import timezone++import requests+from parameterized import parameterized++from posthog.models.integration import GitHubInstallationAccess, GitHubIntegration, GitHubUserAuthorization, Integration+from posthog.models.oauth import OAuthApplication+from posthog.models.team.team import Team+from posthog.models.user import OnboardingSkippedReason+from posthog.models.user_integration import UserIntegration++from ee.api.agentic_provisioning import GITHUB_GRANT_CACHE_PREFIX, github_grants+from ee.api.agentic_provisioning.test.base import TEST_STRIPE_OAUTH_CLIENT_ID, ProvisioningTestBase++INSTALLATION_ID = "777"++AUTHORIZATION = GitHubUserAuthorization(+ gh_id=12345,+ gh_login="octocat",+ access_token="gho_secret_user_token",+ refresh_token="ghr_refresh_token",+ access_token_expires_in=28800,+ refresh_token_expires_in=15897600,+)+++def _installation_access() -> GitHubInstallationAccess:+ return GitHubInstallationAccess(+ installation_id=INSTALLATION_ID,+ installation_info={"account": {"type": "User", "login": "octocat"}},+ access_token="ghs_installation_token",+ token_expires_at=(timezone.now() + timedelta(hours=1)).isoformat(),+ repository_selection="selected",+ )+++class TestWizardResourceActions(ProvisioningTestBase):+ def setUp(self):+ super().setUp()+ self.bearer = self._get_bearer_token()+ self.partner = OAuthApplication.objects.get(client_id=TEST_STRIPE_OAUTH_CLIENT_ID)++ def _grant(self) -> github_grants.GitHubGrant:+ return github_grants.create_grant(self.partner, AUTHORIZATION, "[email protected]")++ def _post_github_integration(self, team_id: int, body: dict):+ return self._post_signed_with_bearer(+ f"/api/agentic/provisioning/resources/{team_id}/github_integration", body, token=self.bearer+ )++ def _post_wizard_runs(self, team_id: int, body: dict):+ return self._post_signed_with_bearer(+ f"/api/agentic/provisioning/resources/{team_id}/wizard_runs", body, token=self.bearer+ )++ def _mark_user_unclaimed(self):+ self.user.set_unusable_password()+ self.user.last_login = None+ self.user.save()++ def test_github_integration_happy_path_links_and_consumes_grant(self):+ self._mark_user_unclaimed()+ grant = self._grant()+ with (+ patch.object(GitHubIntegration, "verify_user_installation_access", return_value=True),+ patch.object(GitHubIntegration, "fetch_installation_access", return_value=_installation_access()),+ ):+ response = self._post_github_integration(+ self.team.id, {"grant_id": grant.grant_id, "installation_id": INSTALLATION_ID}+ )++ assert response.status_code == 200, response.json()+ body = response.json()+ assert body["status"] == "complete"+ assert body["github_integration"]["gh_login"] == "octocat"+ assert body["github_integration"]["already_linked"] is False++ integration = Integration.objects.get(team_id=self.team.id, kind="github", integration_id=INSTALLATION_ID)+ assert integration.config["connecting_user_github_login"] == "octocat"+ assert UserIntegration.objects.filter(+ user=self.user, kind=UserIntegration.IntegrationKind.GITHUB, integration_id=INSTALLATION_ID+ ).exists()++ assert cache.get(f"{GITHUB_GRANT_CACHE_PREFIX}{grant.grant_id}") is None++ self.user.refresh_from_db()+ self.team.refresh_from_db()+ assert self.user.onboarding_skipped_reason == OnboardingSkippedReason.PROVISIONED+ assert self.user.onboarding_skipped_at is not None+ assert self.user.onboarding_skipped_organization_id == self.team.organization_id+ assert self.team.completed_snippet_onboarding is True++ def test_github_integration_does_not_touch_onboarding_for_claimed_user(self):+ grant = self._grant()+ with (+ patch.object(GitHubIntegration, "verify_user_installation_access", return_value=True),+ patch.object(GitHubIntegration, "fetch_installation_access", return_value=_installation_access()),+ ):+ response = self._post_github_integration(+ self.team.id, {"grant_id": grant.grant_id, "installation_id": INSTALLATION_ID}+ )+ assert response.status_code == 200+ self.user.refresh_from_db()+ assert self.user.onboarding_skipped_reason is None+ assert self.user.onboarding_skipped_at is None++ def test_github_integration_unknown_grant_returns_404(self):+ response = self._post_github_integration(+ self.team.id, {"grant_id": "nonexistent", "installation_id": INSTALLATION_ID}+ )+ assert response.status_code == 404+ assert response.json()["error"]["code"] == "grant_not_found"++ def test_github_integration_idempotent_retry_after_grant_consumed(self):+ self._mark_user_unclaimed()+ Integration.objects.create(+ team_id=self.team.id,+ kind="github",+ integration_id=INSTALLATION_ID,+ config={"installation_id": INSTALLATION_ID},+ sensitive_config={},+ )+ response = self._post_github_integration(+ self.team.id, {"grant_id": "already-consumed", "installation_id": INSTALLATION_ID}+ )+ assert response.status_code == 200+ assert response.json()["github_integration"]["already_linked"] is True++ # Onboarding flags are re-applied on the already-linked retry so a crash between+ # grant consumption and the flag write doesn't leave the account routed into onboarding.+ self.user.refresh_from_db()+ assert self.user.onboarding_skipped_reason == OnboardingSkippedReason.PROVISIONED+ assert self.user.onboarding_skipped_at is not None++ @parameterized.expand(+ [+ ("access_denied", {"return_value": False}, 403, "installation_access_denied"),+ (+ "verify_error",+ {"side_effect": requests.RequestException("boom")},+ 502,+ "installation_verify_failed",+ ),+ ]+ )+ def test_github_integration_ownership_failures_preserve_grant(self, _name, verify_kwargs, status, code):+ grant = self._grant()+ with patch.object(GitHubIntegration, "verify_user_installation_access", **verify_kwargs):+ response = self._post_github_integration(+ self.team.id, {"grant_id": grant.grant_id, "installation_id": INSTALLATION_ID}+ )+ assert response.status_code == status+ assert response.json()["error"]["code"] == code+ assert cache.get(f"{GITHUB_GRANT_CACHE_PREFIX}{grant.grant_id}") is not None++ def test_github_integration_team_outside_token_scope_returns_403(self):+ other_team = Team.objects.create(organization=self.organization, name="other")+ grant = self._grant()+ response = self._post_github_integration(+ other_team.id, {"grant_id": grant.grant_id, "installation_id": INSTALLATION_ID}+ )+ assert response.status_code == 403+ assert response.json()["error"]["code"] == "forbidden"++ @override_settings(WIZARD_CLOUD_RUN_OAUTH_CLIENT_ID="")+ def test_wizard_runs_unavailable_without_oauth_client_id(self):+ response = self._post_wizard_runs(self.team.id, {"repository": "octocat/hello-world"})+ assert response.status_code == 503+ assert response.json()["error"]["code"] == "wizard_unavailable"++ @override_settings(WIZARD_CLOUD_RUN_OAUTH_CLIENT_ID="wizard-client-id")+ def test_wizard_runs_happy_path(self):+ created = MagicMock(task_id="task-uuid", latest_run=MagicMock(id="run-uuid", status="queued"))+ with patch(+ "ee.api.agentic_provisioning.views.tasks_facade.create_wizard_cloud_run", return_value=created+ ) as mock_create:+ response = self._post_wizard_runs(self.team.id, {"repository": "octocat/hello-world", "branch": "main"})++ assert response.status_code == 200, response.json()+ body = response.json()+ assert body["status"] == "complete"+ assert body["wizard_run"] == {"task_id": "task-uuid", "run_id": "run-uuid", "status": "queued"}+ mock_create.assert_called_once_with(+ team=self.team, user_id=self.user.id, repository="octocat/hello-world", branch="main"+ )++ @parameterized.expand(+ [+ ("missing_repository", {}, "invalid_request"),+ ("bad_format", {"repository": "not-a-repo"}, "invalid_request"),+ ("too_many_parts", {"repository": "a/b/c"}, "invalid_request"),+ ]+ )+ @override_settings(WIZARD_CLOUD_RUN_OAUTH_CLIENT_ID="wizard-client-id")+ def test_wizard_runs_rejects_invalid_repository(self, _name, body, code):+ response = self._post_wizard_runs(self.team.id, body)+ assert response.status_code == 400+ assert response.json()["error"]["code"] == code++ @override_settings(WIZARD_CLOUD_RUN_OAUTH_CLIENT_ID="wizard-client-id")+ def test_wizard_runs_per_user_burst_limit(self):+ created = MagicMock(task_id="task-uuid", latest_run=None)+ with patch("ee.api.agentic_provisioning.views.tasks_facade.create_wizard_cloud_run", return_value=created):+ first = self._post_wizard_runs(self.team.id, {"repository": "octocat/one"})+ second = self._post_wizard_runs(self.team.id, {"repository": "octocat/two"})+ third = self._post_wizard_runs(self.team.id, {"repository": "octocat/three"})+ assert first.status_code == 200+ assert second.status_code == 200+ assert third.status_code == 429+ assert third["Retry-After"]++ @override_settings(WIZARD_CLOUD_RUN_OAUTH_CLIENT_ID="wizard-client-id")+ def test_wizard_runs_without_github_integration_returns_400(self):+ with patch(+ "ee.api.agentic_provisioning.views.tasks_facade.create_wizard_cloud_run",+ side_effect=ValueError("Team has no GitHub integration"),+ ):+ response = self._post_wizard_runs(self.team.id, {"repository": "octocat/hello-world"})+ assert response.status_code == 400+ assert response.json()["error"]["code"] == "github_integration_required"diff --git a/ee/api/agentic_provisioning/views.py b/ee/api/agentic_provisioning/views.pyindex 5ea6854d0566..4a80ecc7c29b 100644--- a/ee/api/agentic_provisioning/views.py+++ b/ee/api/agentic_provisioning/views.py@@ -26,21 +26,30 @@ import requestsimport structlogimport posthoganalyticsfrom rest_framework.decorators import api_view, authentication_classes, permission_classes-from rest_framework.exceptions import AuthenticationFailed+from rest_framework.exceptions import (+ AuthenticationFailed,+ ValidationError as DRFValidationError,+)from rest_framework.permissions import IsAuthenticatedfrom rest_framework.request import Requestfrom rest_framework.response import Responsefrom posthog.api.authentication import password_reset_token_generatorfrom posthog.api.email_verification import EmailVerifier+from posthog.api.github_callback.team_services import link_github_installation_for_userfrom posthog.event_usage import report_user_signed_upfrom posthog.exceptions_capture import capture_exception-from posthog.models.integration import GitHubIntegration, StripeIntegration+from posthog.models.integration import (+ GitHubInstallationAccessFetchError,+ GitHubIntegration,+ Integration,+ StripeIntegration,+)from posthog.models.oauth import OAuthAccessToken, OAuthApplication, OAuthRefreshToken, find_oauth_access_tokenfrom posthog.models.personal_api_key import PersonalAPIKey, hash_key_valuefrom posthog.models.team.team import Teamfrom posthog.models.team.team_provisioning_config import TeamProvisioningConfig-from posthog.models.user import User+from posthog.models.user import OnboardingSkippedReason, Userfrom posthog.models.utils import (generate_random_oauth_access_token,generate_random_oauth_refresh_token,@@ -52,10 +61,13 @@ from posthog.scopes import narrow_scopes_to_ceiling, scopes_within_ceilingfrom posthog.tasks.email import send_provisioning_welcomefrom posthog.utils import get_instance_region+from products.tasks.backend.facade import api as tasks_facade+from ee.settings import BILLING_SERVICE_URLfrom . import AUTH_CODE_CACHE_PREFIX, PENDING_AUTH_CACHE_PREFIX, github_grantsfrom .authentication import ProvisioningAuthentication+from .errors import error_response, typed_error_responsefrom .region_proxy import region_proxyfrom .signature import SUPPORTED_VERSIONS, verify_api_version, verify_provisioning_signature@@ -84,14 +96,25 @@ PARTNER_RATE_LIMIT_DEFAULTS: dict[str, int] = {"token_exchanges": 20,"resource_creates": 20,"github_grants": 10,+ "wizard_runs": 20,}PARTNER_RATE_LIMIT_EVENT_NAMES: dict[str, str] = {"account_requests": "account_request","token_exchanges": "token_exchange","resource_creates": "resource_created","github_grants": "github_grant",+ "wizard_runs": "wizard_run",}+# Per-user wizard-run budget, mirroring the session endpoint's DRF throttles+# (SetupWizardCloudRunBurstRateThrottle 2/hour, SetupWizardCloudRunSustainedRateThrottle+# 5/day) which can't run here — the partner path has no session user on the request.+WIZARD_RUN_USER_RATE_LIMIT_PREFIX = "provisioning_wizard_run_user:"+WIZARD_RUN_USER_RATE_LIMITS: list[tuple[str, int, int]] = [+ ("burst", 2, 3600),+ ("day", 5, 86400),+]+# Repo-picker polling budget per grant (the website polls while the visitor installs the# GitHub App in another tab). Keyed per grant, so one stuck visitor can't starve others.GITHUB_GRANT_POLL_RATE_LIMIT_PREFIX = "provisioning_github_grant_poll:"@@ -311,10 +334,7 @@ def account_requests(request: Request) -> Response:if result:authenticated_user, partner = resultexcept AuthenticationFailed:- return Response(- {"type": "error", "error": {"code": "unauthorized", "message": "Authentication failed"}},- status=401,- )+ return typed_error_response("unauthorized", "Authentication failed", status=401)if partner is None and auth.cimd_registration_pending:return Response(@@ -328,9 +348,7 @@ def account_requests(request: Request) -> Response:email = data.get("email")if not email:_capture_provisioning_event("account_request", "error", error_code="missing_email")- return Response(- {"type": "error", "error": {"code": "invalid_request", "message": "email is required"}}, status=400- )+ return typed_error_response("invalid_request", "email is required")scopes = data.get("scopes", [])confirmation_secret = data.get("confirmation_secret", "")@@ -344,10 +362,7 @@ def account_requests(request: Request) -> Response:expires_at = parse_datetime(expires_at_str)if expires_at and expires_at < timezone.now():_capture_provisioning_event("account_request", "error", error_code="expired")- return Response(- {"type": "error", "error": {"code": "expired", "message": "Account request has expired"}},- status=400,- )+ return typed_error_response("expired", "Account request has expired")# Partner account ID: generic field, with Stripe backward compatorchestrator_type = orchestrator.get("type", "")@@ -359,10 +374,7 @@ def account_requests(request: Request) -> Response:# If no partner identified, require Stripe Projects HMAC authif not partner and not request.headers.get("stripe-signature"):- return Response(- {"type": "error", "error": {"code": "unauthorized", "message": "Authentication required"}},- status=401,- )+ return typed_error_response("unauthorized", "Authentication required", status=401)if not partner:if error := _verify_hmac_if_present(request):@@ -371,24 +383,12 @@ def account_requests(request: Request) -> Response:# Stripe Projects: require stripe account if no provisioning partner identifiedif not partner and not partner_account_id:_capture_provisioning_event("account_request", "error", error_code="missing_stripe_account")- return Response(- {- "type": "error",- "error": {"code": "invalid_request", "message": "orchestrator.stripe.account is required"},- },- status=400,- )+ return typed_error_response("invalid_request", "orchestrator.stripe.account is required")# Check permissionif partner and not partner.provisioning_can_create_accounts:_capture_provisioning_event("account_request", "error", error_code="account_creation_disabled")- return Response(- {- "type": "error",- "error": {"code": "forbidden", "message": "Account creation is not enabled for this partner"},- },- status=403,- )+ return typed_error_response("forbidden", "Account creation is not enabled for this partner", status=403)if partner and (error := _enforce_partner_rate_limit(partner, "account_requests")):return error@@ -397,25 +397,12 @@ def account_requests(request: Request) -> Response:code_challenge = data.get("code_challenge", "")code_challenge_method = data.get("code_challenge_method", "S256")if code_challenge and code_challenge_method != "S256":- return Response(- {- "type": "error",- "error": {"code": "invalid_request", "message": "Only S256 code_challenge_method is supported"},- },- status=400,- )+ return typed_error_response("invalid_request", "Only S256 code_challenge_method is supported")if code_challenge and (len(code_challenge) < 43 or len(code_challenge) > 128 or not re.fullmatch(r"[A-Za-z0-9_\-]+", code_challenge)):- return Response(- {- "type": "error",- "error": {- "code": "invalid_request",- "message": "code_challenge must be 43-128 characters using base64url charset",- },- },- status=400,+ return typed_error_response(+ "invalid_request", "code_challenge must be 43-128 characters using base64url charset")region = (configuration.get("region") or "US").upper()@@ -425,13 +412,8 @@ def account_requests(request: Request) -> Response:try:requested_team_id = int(requested_team_id)except (ValueError, TypeError):- return Response(- {- "id": request_id,- "type": "error",- "error": {"code": "invalid_request", "message": "configuration.team_id must be an integer"},- },- status=400,+ return typed_error_response(+ "invalid_request", "configuration.team_id must be an integer", request_id=request_id)existing_user = User.objects.filter(email=email).first()@@ -539,25 +521,14 @@ def _handle_existing_user(if partner and (not partner.provisioning_skip_existing_user_consent or silent_blocked):if not code_challenge:- return Response(- {- "id": request_id,- "type": "error",- "error": {"code": "invalid_request", "message": "code_challenge is required for public clients"},- },- status=400,+ return typed_error_response(+ "invalid_request", "code_challenge is required for public clients", request_id=request_id)if not scopes_within_ceiling(scopes, partner.ceiling_scopes):- return Response(- {- "id": request_id,- "type": "error",- "error": {- "code": "invalid_scope",- "message": "One or more requested scopes exceed the application's allowed scopes",- },- },- status=400,+ return typed_error_response(+ "invalid_scope",+ "One or more requested scopes exceed the application's allowed scopes",+ request_id=request_id,)return _require_user_consent(request_id,@@ -573,13 +544,8 @@ def _handle_existing_user(team = _resolve_team_for_existing_user(user, team_id)if team is None:_capture_provisioning_event("account_request", "error", error_code="team_resolution_failed")- return Response(- {- "id": request_id,- "type": "error",- "error": {"code": "team_resolution_failed", "message": "Could not resolve a project for this user"},- },- status=400,+ return typed_error_response(+ "team_resolution_failed", "Could not resolve a project for this user", request_id=request_id)code = secrets.token_urlsafe(32)@@ -736,13 +702,8 @@ def _handle_new_user(authenticated_user,)_capture_provisioning_event("account_request", "creation_failed", region=region)- return Response(- {- "id": request_id,- "type": "error",- "error": {"code": "account_creation_failed", "message": "Failed to create account"},- },- status=500,+ return typed_error_response(+ "account_creation_failed", "Failed to create account", request_id=request_id, status=500)_capture_provisioning_event(@@ -827,21 +788,12 @@ def _authenticate_provisioning_partner(request: Request) -> tuple[Response | Nonpartner = result[1] if result else Noneif partner is None:return (- Response(- {"type": "error", "error": {"code": "unauthorized", "message": "Authentication required"}},- status=401,- ),+ typed_error_response("unauthorized", "Authentication required", status=401),None,)if partner.provisioning_auth_method not in ("hmac", "bearer"):return (- Response(- {- "type": "error",- "error": {"code": "forbidden", "message": "This endpoint requires a confidential partner"},- },- status=403,- ),+ typed_error_response("forbidden", "This endpoint requires a confidential partner", status=403),None,)return None, partner@@ -860,13 +812,7 @@ def github_grants_create(request: Request) -> Response:if not partner.provisioning_can_create_accounts:_capture_provisioning_event("github_grant", "error", partner=partner, error_code="account_creation_disabled")- return Response(- {- "type": "error",- "error": {"code": "forbidden", "message": "Account creation is not enabled for this partner"},- },- status=403,- )+ return typed_error_response("forbidden", "Account creation is not enabled for this partner", status=403)if error := _enforce_partner_rate_limit(partner, "github_grants"):return error@@ -875,9 +821,7 @@ def github_grants_create(request: Request) -> Response:redirect_uri = request.data.get("redirect_uri") or Noneif not code or not isinstance(code, str):_capture_provisioning_event("github_grant", "error", partner=partner, error_code="missing_code")- return Response(- {"type": "error", "error": {"code": "invalid_request", "message": "code is required"}}, status=400- )+ return typed_error_response("invalid_request", "code is required")try:authorization = GitHubIntegration.github_user_from_code(code, redirect_uri=redirect_uri)@@ -885,22 +829,10 @@ def github_grants_create(request: Request) -> Response:# Network failure or a non-JSON GitHub error body raising through .json() — retryable,# distinct from a clean "bad code" exchange failure (which returns None below)._capture_provisioning_event("github_grant", "error", partner=partner, error_code="github_unavailable")- return Response(- {"type": "error", "error": {"code": "github_unavailable", "message": "GitHub request failed"}},- status=502,- )+ return typed_error_response("github_unavailable", "GitHub request failed", status=502)if authorization is None:_capture_provisioning_event("github_grant", "error", partner=partner, error_code="github_exchange_failed")- return Response(- {- "type": "error",- "error": {- "code": "github_exchange_failed",- "message": "Could not exchange the GitHub OAuth code",- },- },- status=502,- )+ return typed_error_response("github_exchange_failed", "Could not exchange the GitHub OAuth code", status=502)# A user with no verified email gets a grant with email=null — the partner# collects an email inline instead. Only an access refusal (App permission@@ -909,22 +841,10 @@ def github_grants_create(request: Request) -> Response:email = github_grants.fetch_primary_email(authorization.access_token)except github_grants.GitHubEmailAccessDenied:_capture_provisioning_event("github_grant", "error", partner=partner, error_code="email_unavailable")- return Response(- {- "type": "error",- "error": {- "code": "email_unavailable",- "message": "GitHub denied reading the user's email addresses",- },- },- status=502,- )+ return typed_error_response("email_unavailable", "GitHub denied reading the user's email addresses", status=502)except requests.RequestException:_capture_provisioning_event("github_grant", "error", partner=partner, error_code="github_unavailable")- return Response(- {"type": "error", "error": {"code": "github_unavailable", "message": "GitHub request failed"}},- status=502,- )+ return typed_error_response("github_unavailable", "GitHub request failed", status=502)grant = github_grants.create_grant(partner, authorization, email)_capture_provisioning_event("github_grant", "created", partner=partner, gh_login=grant.gh_login)@@ -947,12 +867,8 @@ def _enforce_grant_poll_rate_limit(grant_id: str) -> Response | None:except (ValueError, ConnectionError, TimeoutError):count = 1if count > GITHUB_GRANT_POLL_RATE_LIMIT_MAX:- response = Response(- {- "type": "error",- "error": {"code": "rate_limited", "message": "Too many repository listing requests for this grant"},- },- status=429,+ response = typed_error_response(+ "rate_limited", "Too many repository listing requests for this grant", status=429)response["Retry-After"] = str(GITHUB_GRANT_POLL_RATE_LIMIT_WINDOW_SECONDS@@ -975,10 +891,7 @@ def github_grant_repositories(request: Request, grant_id: str) -> Response:grant = github_grants.load_grant(grant_id, partner)if grant is None:- return Response(- {"type": "error", "error": {"code": "grant_not_found", "message": "Grant not found or expired"}},- status=404,- )+ return typed_error_response("grant_not_found", "Grant not found or expired", status=404)if error := _enforce_grant_poll_rate_limit(grant_id):return error@@ -987,10 +900,7 @@ def github_grant_repositories(request: Request, grant_id: str) -> Response:listing = github_grants.list_installations_and_repositories(grant.access_token)except requests.RequestException:_capture_provisioning_event("github_grant", "listing_failed", partner=partner)- return Response(- {"type": "error", "error": {"code": "github_unavailable", "message": "GitHub request failed"}},- status=502,- )+ return typed_error_response("github_unavailable", "GitHub request failed", status=502)return Response({"gh_login": grant.gh_login, **listing})@@ -1959,7 +1869,7 @@ def provisioning_resources_create(request: Request) -> Response:if error := _enforce_partner_rate_limit(app, "resource_creates"):# Resource endpoints use {"status": "error"} envelope, not {"type": "error"}retry_after = error["Retry-After"] if "Retry-After" in error else "3600"- response = _error_response(+ response = error_response("rate_limited", "Rate limit exceeded for this partner. Try again later.", status=429)response["Retry-After"] = retry_after@@ -1968,19 +1878,19 @@ def provisioning_resources_create(request: Request) -> Response:service_id = request.data.get("service_id", "")if service_id and service_id not in VALID_SERVICE_IDS:_capture_provisioning_event("resource_created", "error", partner=app, error_code="unknown_service")- return _error_response("unknown_service", f"Unknown service_id: {service_id}")+ return error_response("unknown_service", f"Unknown service_id: {service_id}")try:label_prefix = _extract_label_prefix(request)except _InvalidLabelPrefixError as exc:_capture_provisioning_event("resource_created", "error", partner=app, error_code="invalid_label_prefix")- return _error_response("invalid_label_prefix", str(exc))+ return error_response("invalid_label_prefix", str(exc))scoped_teams = access_token.scoped_teams or []if not scoped_teams:_capture_provisioning_event("resource_created", "error", partner=app, error_code="no_team")- return _error_response("no_team", "No team associated with this token")+ return error_response("no_team", "No team associated with this token")project_id = request.data.get("project_id", "")configuration = request.data.get("configuration") or {}@@ -1994,7 +1904,7 @@ def provisioning_resources_create(request: Request) -> Response:_capture_provisioning_event("resource_created", "error", partner=app, error_code="project_id_conflict", project_id=project_id)- return _error_response(+ return error_response("project_id_conflict","Project ID already linked to another organization",status=409,@@ -2003,7 +1913,7 @@ def provisioning_resources_create(request: Request) -> Response:_capture_provisioning_event("resource_created", "error", partner=app, error_code="not_found", project_id=project_id)- return _error_response("not_found", "Resource not found", status=404)+ return error_response("not_found", "Resource not found", status=404)else:team_id = scoped_teams[0]try:@@ -2012,7 +1922,7 @@ def provisioning_resources_create(request: Request) -> Response:_capture_provisioning_event("resource_created", "error", partner=app, error_code="team_not_found", team_id=team_id)- return _error_response("team_not_found", "Team not found", resource_id=str(team_id), status=404)+ return error_response("team_not_found", "Team not found", resource_id=str(team_id), status=404)resolved_service_id = service_id or ANALYTICS_SERVICE_ID_set_provisioning_service_id(team, resolved_service_id)@@ -2050,7 +1960,7 @@ def provisioning_resources_create(request: Request) -> Response:service_id=resolved_service_id,team_id=team.id,)- return _error_response("requires_payment_credentials", "Payment credentials required for paid plan")+ return error_response("requires_payment_credentials", "Payment credentials required for paid plan")region = get_instance_region() or "US"host = _region_to_host(region)@@ -2122,24 +2032,24 @@ def provisioning_rotate_credentials(request: Request, resource_id: str) -> Respolabel_prefix = _extract_label_prefix(request)except _InvalidLabelPrefixError as exc:_capture_provisioning_event("credential_rotation", "error", error_code="invalid_label_prefix")- return _error_response("invalid_label_prefix", str(exc), resource_id=resource_id)+ return error_response("invalid_label_prefix", str(exc), resource_id=resource_id)scoped_teams = access_token.scoped_teams or []try:team_id = int(resource_id)except (ValueError, TypeError):- return _error_response("invalid_resource_id", "Invalid resource ID", resource_id=resource_id)+ return error_response("invalid_resource_id", "Invalid resource ID", resource_id=resource_id)if team_id not in scoped_teams:- return _error_response(+ return error_response("forbidden", "Resource not accessible with this token", resource_id=resource_id, status=403)try:team = Team.objects.get(id=team_id)except Team.DoesNotExist:- return _error_response("not_found", "Resource not found", resource_id=resource_id, status=404)+ return error_response("not_found", "Resource not found", resource_id=resource_id, status=404)try:# Bearer flow resolves the token outside DRF, so read impersonation off the token directly.@@ -2147,7 +2057,7 @@ def provisioning_rotate_credentials(request: Request, resource_id: str) -> Respoexcept Exception:capture_exception(additional_properties={"team_id": team_id})_capture_provisioning_event("credential_rotation", "failed", team_id=team_id)- return _error_response(+ return error_response("credential_rotation_failed", "Failed to rotate credentials", resource_id=resource_id, status=500)@@ -2178,6 +2088,320 @@ def provisioning_rotate_credentials(request: Request, resource_id: str) -> Respo)+# ---------------------------------------------------------------------------+# POST /provisioning/resources/:id/github_integration+# POST /provisioning/resources/:id/wizard_runs+# Drop-flow resource actions: link a GitHub installation to the team from a+# stored grant, then kick off a cloud wizard run against a repository.+# ---------------------------------------------------------------------------+++def _drf_validation_error_code(exc: DRFValidationError) -> str | None:+ codes = exc.get_codes()+ if isinstance(codes, list) and codes:+ return str(codes[0])+ if isinstance(codes, str):+ return codes+ return None+++def _apply_provisioned_onboarding_flags(user: User, team: Team) -> None:+ """Keep the app from routing a partner-provisioned account into onboarding on first+ login. Only applied to unclaimed accounts (never logged in, no password set) so an+ existing user going through the consent path keeps their onboarding state."""+ if user.last_login is not None or user.has_usable_password():+ return+ if user.onboarding_skipped_at is None:+ user.onboarding_skipped_at = timezone.now()+ user.onboarding_skipped_reason = OnboardingSkippedReason.PROVISIONED+ user.onboarding_skipped_organization_id = team.organization_id+ user.save(+ update_fields=["onboarding_skipped_at", "onboarding_skipped_reason", "onboarding_skipped_organization_id"]+ )+ if not team.completed_snippet_onboarding:+ team.completed_snippet_onboarding = True+ team.save(update_fields=["completed_snippet_onboarding"])+++def _link_github_grant_to_team(+ *, partner: OAuthApplication, user: User, team: Team, grant_id: str, installation_id: str+) -> tuple[Response | None, Integration | None, bool]:+ """Shared core of the github_integration action and the account_requests wizard+ block: validate the grant, verify installation ownership, create both GitHub+ records, consume the grant. Returns (error_response, integration, already_linked).+ """+ grant = github_grants.load_grant(grant_id, partner)+ if grant is None:+ # Idempotent retry: the grant is consumed on success, so a retry after a lost+ # response must not fail if the installation is already linked to this team.+ existing = Integration.objects.first_github_for_team_installation(team.id, str(installation_id))+ if existing is not None:+ # Re-apply onboarding flags idempotently: the grant is consumed as the last step,+ # so a crash between consume and the flag write would otherwise leave them unset+ # on a retry (this branch runs only once the grant is already gone).+ _apply_provisioned_onboarding_flags(user, team)+ return None, existing, True+ _capture_provisioning_event("github_integration", "error", partner=partner, error_code="grant_not_found")+ return (+ error_response("grant_not_found", "Grant not found or expired", resource_id=str(team.id), status=404),+ None,+ False,+ )++ try:+ integration = link_github_installation_for_user(+ user=user, team_id=team.id, installation_id=str(installation_id), authorization=grant.to_authorization()+ )+ except DRFValidationError as exc:+ code = _drf_validation_error_code(exc)+ if code == "installation_access_denied":+ _capture_provisioning_event("github_integration", "error", partner=partner, error_code=code)+ return (+ error_response(+ "installation_access_denied",+ "The GitHub user does not have access to this installation",+ resource_id=str(team.id),+ status=403,+ ),+ None,+ False,+ )+ if code == "installation_verify_failed":+ _capture_provisioning_event("github_integration", "error", partner=partner, error_code=code)+ return (+ error_response(+ "installation_verify_failed",+ "Could not verify installation access with GitHub",+ resource_id=str(team.id),+ status=502,+ ),+ None,+ False,+ )+ _capture_provisioning_event("github_integration", "error", partner=partner, error_code="invalid_request")+ return (+ error_response("invalid_request", str(exc.detail), resource_id=str(team.id), status=400),+ None,+ False,+ )+ except GitHubInstallationAccessFetchError:+ _capture_provisioning_event(+ "github_integration", "error", partner=partner, error_code="integration_creation_failed"+ )+ return (+ error_response(+ "integration_creation_failed",+ "Could not create the GitHub integration",+ resource_id=str(team.id),+ status=502,+ ),+ None,+ False,+ )++ github_grants.consume_grant(grant_id)+ _apply_provisioned_onboarding_flags(user, team)+ _capture_provisioning_event("github_integration", "success", partner=partner, team_id=team.id)+ return None, integration, False+++@api_view(["POST"])+@authentication_classes([])+@permission_classes([])+@region_proxy(strategy="bearer_lookup")+def provisioning_github_integration(request: Request, resource_id: str) -> Response:+ auth_error, user, access_token = _authenticate_bearer(request)+ if auth_error:+ return auth_error++ if error := _verify_hmac_if_present(request):+ return error+ if error := verify_api_version(request):+ return error++ try:+ team_id = int(resource_id)+ except (ValueError, TypeError):+ return error_response("invalid_resource_id", "Invalid resource ID", resource_id=resource_id)++ if team_id not in (access_token.scoped_teams or []):+ return error_response(+ "forbidden", "Resource not accessible with this token", resource_id=resource_id, status=403+ )++ try:+ team = Team.objects.get(id=team_id)+ except Team.DoesNotExist:+ return error_response("not_found", "Resource not found", resource_id=resource_id, status=404)++ grant_id = request.data.get("grant_id")+ installation_id = request.data.get("installation_id")+ if not grant_id or not installation_id:+ return error_response("invalid_request", "grant_id and installation_id are required", resource_id=resource_id)++ error, integration, already_linked = _link_github_grant_to_team(+ partner=access_token.application,+ user=user,+ team=team,+ grant_id=str(grant_id),+ installation_id=str(installation_id),+ )+ if error:+ return error+ assert integration is not None++ return Response(+ {+ "status": "complete",+ "id": resource_id,+ "github_integration": {+ "integration_id": str(integration.id),+ "gh_login": (integration.config or {}).get("connecting_user_github_login"),+ "already_linked": already_linked,+ },+ }+ )+++def _enforce_wizard_run_user_rate_limit(user_id: int) -> Response | None:+ """Cache-counter equivalent of the session cloud_run endpoint's per-user throttles;+ shared across the granular wizard_runs action and the bundled account_requests path+ so retries can't double-spend the budget."""+ for label, limit, window_seconds in WIZARD_RUN_USER_RATE_LIMITS:+ window_index = int(time.time()) // window_seconds+ key = f"{WIZARD_RUN_USER_RATE_LIMIT_PREFIX}{label}:{user_id}:{window_index}"+ try:+ cache.add(key, 0, timeout=window_seconds)+ count = cache.incr(key)+ except (ValueError, ConnectionError, TimeoutError):+ count = 1+ if count > limit:+ response = Response(+ {+ "status": "error",+ "error": {"code": "rate_limited", "message": "Too many wizard runs for this user. Try later."},+ },+ status=429,+ )+ response["Retry-After"] = str(window_seconds - (int(time.time()) % window_seconds))+ return response+ return None+++def _create_wizard_run(+ *, partner: OAuthApplication, user_id: int, team: Team, repository: str, branch: str | None+) -> tuple[Response | None, dict[str, str] | None]:+ """Gate + throttle + create a cloud wizard run. Returns (error_response, run_payload)."""+ if not bool(settings.WIZARD_CLOUD_RUN_OAUTH_CLIENT_ID):+ _capture_provisioning_event("wizard_run", "error", partner=partner, error_code="wizard_unavailable")+ return (+ error_response(+ "wizard_unavailable",+ "Running the setup wizard in the cloud is not available",+ resource_id=str(team.id),+ status=503,+ ),+ None,+ )++ repository = (repository or "").strip()+ parts = repository.split("/")+ if len(parts) != 2 or not all(parts):+ return (+ error_response("invalid_request", "repository must be in 'owner/repo' format", resource_id=str(team.id)),+ None,+ )++ if error := _enforce_wizard_run_user_rate_limit(user_id):+ return error, None+ if error := _enforce_partner_rate_limit(partner, "wizard_runs"):+ return error, None++ try:+ created = tasks_facade.create_wizard_cloud_run(+ team=team, user_id=user_id, repository=repository, branch=branch or None+ )+ except ValueError:+ # The facade raises when the team has no usable GitHub integration.+ _capture_provisioning_event(+ "wizard_run", "error", partner=partner, error_code="github_integration_required", team_id=team.id+ )+ return (+ error_response(+ "github_integration_required",+ "The team does not have a GitHub integration that can access this repository",+ resource_id=str(team.id),+ status=400,+ ),+ None,+ )+ except Exception:+ capture_exception(additional_properties={"team_id": team.id, "step": "provisioning_wizard_run"})+ _capture_provisioning_event(+ "wizard_run", "error", partner=partner, error_code="run_creation_failed", team_id=team.id+ )+ return (+ error_response(+ "run_creation_failed", "Failed to start the wizard run", resource_id=str(team.id), status=500+ ),+ None,+ )++ run = created.latest_run+ _capture_provisioning_event("wizard_run", "success", partner=partner, team_id=team.id, task_id=str(created.task_id))+ return None, {+ "task_id": str(created.task_id),+ "run_id": str(run.id) if run else "",+ "status": str(run.status) if run else "queued",+ }+++@api_view(["POST"])+@authentication_classes([])+@permission_classes([])+@region_proxy(strategy="bearer_lookup")+def provisioning_wizard_runs(request: Request, resource_id: str) -> Response:+ auth_error, user, access_token = _authenticate_bearer(request)+ if auth_error:+ return auth_error++ if error := _verify_hmac_if_present(request):+ return error+ if error := verify_api_version(request):+ return error++ try:+ team_id = int(resource_id)+ except (ValueError, TypeError):+ return error_response("invalid_resource_id", "Invalid resource ID", resource_id=resource_id)++ if team_id not in (access_token.scoped_teams or []):+ return error_response(+ "forbidden", "Resource not accessible with this token", resource_id=resource_id, status=403+ )++ try:+ team = Team.objects.get(id=team_id)+ except Team.DoesNotExist:+ return error_response("not_found", "Resource not found", resource_id=resource_id, status=404)++ repository = request.data.get("repository")+ if not repository:+ return error_response("invalid_request", "repository is required", resource_id=resource_id)++ error, run_payload = _create_wizard_run(+ partner=access_token.application,+ user_id=user.id,+ team=team,+ repository=str(repository),+ branch=request.data.get("branch") or None,+ )+ if error:+ return error++ return Response({"status": "complete", "id": resource_id, "wizard_run": run_payload})++# ---------------------------------------------------------------------------# POST /provisioning/resources/:id/update_service# ---------------------------------------------------------------------------@@ -2203,17 +2427,17 @@ def provisioning_update_service(request: Request, resource_id: str) -> Response:try:team_id = int(resource_id)except (ValueError, TypeError):- return _error_response("invalid_resource_id", "Invalid resource ID", resource_id=resource_id)+ return error_response("invalid_resource_id", "Invalid resource ID", resource_id=resource_id)if team_id not in scoped_teams:- return _error_response(+ return error_response("forbidden", "Resource not accessible with this token", resource_id=resource_id, status=403)try:team = Team.objects.get(id=team_id)except Team.DoesNotExist:- return _error_response("not_found", "Resource not found", resource_id=resource_id, status=404)+ return error_response("not_found", "Resource not found", resource_id=resource_id, status=404)# A config with a non-null application belongs to the partner that provisioned# it; a null application is unclaimed (every team gets one by default) and is@@ -2222,15 +2446,15 @@ def provisioning_update_service(request: Request, resource_id: str) -> Response:TeamProvisioningConfig.objects.filter(team_id=team_id).values_list("application_id", flat=True).first())if owning_application_id is not None and owning_application_id != access_token.application_id:- return _error_response(+ return error_response("forbidden", "Resource owned by a different provisioning partner", resource_id=resource_id, status=403)service_id = request.data.get("service_id", "")if not service_id:- return _error_response("missing_service_id", "service_id is required", resource_id=resource_id)+ return error_response("missing_service_id", "service_id is required", resource_id=resource_id)if service_id not in VALID_SERVICE_IDS:- return _error_response("unknown_service", f"Unknown service_id: {service_id}", resource_id=resource_id)+ return error_response("unknown_service", f"Unknown service_id: {service_id}", resource_id=resource_id)billing_result = _try_activate_billing_with_spt(request, team, user)has_spt = billing_result is not None@@ -2243,7 +2467,7 @@ def provisioning_update_service(request: Request, resource_id: str) -> Response:team_id=team_id,has_spt=has_spt,)- return _error_response(+ return error_response("billing_activation_failed","Failed to activate billing with payment credentials",resource_id=resource_id,@@ -2253,7 +2477,7 @@ def provisioning_update_service(request: Request, resource_id: str) -> Response:_capture_provisioning_event("update_service", "error", error_code="requires_payment_credentials", service_id=service_id, team_id=team_id)- return _error_response(+ return error_response("requires_payment_credentials", "Payment credentials required for paid plan", resource_id=resource_id)@@ -2496,7 +2720,7 @@ def deep_links(request: Request) -> Response:# is not sufficient to mint a full web session via the deep-link primitive.if access_token.application.provisioning_auth_method == "hmac":if not request.META.get("HTTP_STRIPE_SIGNATURE"):- return _error_response(+ return error_response("hmac_signature_required","HMAC signature required for this partner",status=401,@@ -2511,7 +2735,7 @@ def deep_links(request: Request) -> Response:if not access_token.application.provisioning_can_issue_deep_links:_capture_provisioning_event("deep_link_created", "not_enabled", partner=access_token.application)- return _error_response(+ return error_response("deep_links_not_enabled","Deep links are not enabled for this partner",status=403,@@ -2525,7 +2749,7 @@ def deep_links(request: Request) -> Response:_capture_provisioning_event("deep_link_created", "invalid_path", partner=access_token.application, purpose=purpose)- return _error_response(+ return error_response("invalid_path","path must be a relative in-app path beginning with a single '/'",status=400,@@ -2625,15 +2849,8 @@ def _enforce_partner_rate_limit(partner: OAuthApplication, endpoint: str) -> Resevent_name = PARTNER_RATE_LIMIT_EVENT_NAMES.get(endpoint, endpoint)_capture_provisioning_event(event_name, "rate_limited", partner=partner, limit=limit, count=count)retry_after = PARTNER_RATE_LIMIT_WINDOW_SECONDS - (int(time.time()) % PARTNER_RATE_LIMIT_WINDOW_SECONDS)- response = Response(- {- "type": "error",- "error": {- "code": "rate_limited",- "message": f"Rate limit exceeded for this partner ({endpoint}). Try again later.",- },- },- status=429,+ response = typed_error_response(+ "rate_limited", f"Rate limit exceeded for this partner ({endpoint}). Try again later.", status=429)response["Retry-After"] = str(retry_after)return response@@ -2654,15 +2871,8 @@ def _enforce_cimd_registration_throttle(request: Request) -> Response | None:throttle = throttle_cls()if not throttle.allow_request(request, view=None): # type: ignore[arg-type]logger.warning("cimd_rate_limited", client_id=client_id, scope=throttle.scope, wait=throttle.wait())- return Response(- {- "type": "error",- "error": {- "code": "rate_limited",- "message": "Too many new client registrations. Try again later.",- },- },- status=429,+ return typed_error_response(+ "rate_limited", "Too many new client registrations. Try again later.", status=429)if error := _enforce_cimd_domain_rate_limit(cast(str, client_id)):@@ -2690,15 +2900,8 @@ def _enforce_cimd_domain_rate_limit(client_id: str) -> Response | None:if count > CIMD_DOMAIN_RATE_LIMIT_MAX:logger.warning("cimd_domain_rate_limited", client_id=client_id, domain=domain, count=count)_capture_provisioning_event("account_request", "cimd_domain_rate_limited", domain=domain, count=count)- return Response(- {- "type": "error",- "error": {- "code": "rate_limited",- "message": "Too many new client registrations from this domain. Try again later.",- },- },- status=429,+ return typed_error_response(+ "rate_limited", "Too many new client registrations from this domain. Try again later.", status=429)return None@@ -2714,11 +2917,6 @@ def _verify_hmac_if_present(request: Request) -> Response | None:return None-def _error_response(code: str, message: str, resource_id: str = "", status: int = 400) -> Response:- logger.warning("provisioning.error_response", code=code, message=message, resource_id=resource_id, status=status)- return Response({"status": "error", "id": resource_id, "error": {"code": code, "message": message}}, status=status)--def _authenticate_bearer(request: Request) -> tuple[Response | None, Any, Any]:"""Authenticate via Bearer token. Returns (error_response, user, access_token).@@ -2728,27 +2926,27 @@ def _authenticate_bearer(request: Request) -> tuple[Response | None, Any, Any]:auth_header = request.headers.get("authorization", "")if not auth_header.startswith("Bearer "):- return (_error_response("unauthorized", "Missing bearer token", status=401), None, None)+ return (error_response("unauthorized", "Missing bearer token", status=401), None, None)token_value = auth_header[len("Bearer ") :].strip()if not token_value:- return (_error_response("unauthorized", "Missing bearer token", status=401), None, None)+ return (error_response("unauthorized", "Missing bearer token", status=401), None, None)access_token = find_oauth_access_token(token_value)if access_token is None:- return (_error_response("unauthorized", "Invalid access token", status=401), None, None)+ return (error_response("unauthorized", "Invalid access token", status=401), None, None)if access_token.expires and access_token.expires < timezone.now():- return (_error_response("unauthorized", "Access token expired", status=401), None, None)+ return (error_response("unauthorized", "Access token expired", status=401), None, None)# Check if token belongs to any active provisioning partner's appapp = access_token.applicationif app and app.is_provisioning_partner:if not app.provisioning_active:- return (_error_response("unauthorized", "Partner is deactivated", status=401), None, None)+ return (error_response("unauthorized", "Partner is deactivated", status=401), None, None)if not app.provisioning_can_provision_resources:return (- _error_response("forbidden", "Resource provisioning not enabled for this partner", status=403),+ error_response("forbidden", "Resource provisioning not enabled for this partner", status=403),None,None,)@@ -2758,7 +2956,7 @@ def _authenticate_bearer(request: Request) -> tuple[Response | None, Any, Any]:if app and app.client_id == settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID:return None, access_token.user, access_token- return (_error_response("unauthorized", "Authentication failed", status=401), None, None)+ return (error_response("unauthorized", "Authentication failed", status=401), None, None)class LegacyStripeOAuthAppMissingError(Exception):diff --git a/ee/urls.py b/ee/urls.pyindex be73ca7bb258..1132ffa08dfc 100644--- a/ee/urls.py+++ b/ee/urls.py@@ -346,6 +346,16 @@ urlpatterns: list[Any] = [csrf_exempt(agentic_provisioning_views.provisioning_resource_remove),name="agentic_provisioning_resource_remove",),+ path(+ "api/agentic/provisioning/resources/<str:resource_id>/github_integration",+ csrf_exempt(agentic_provisioning_views.provisioning_github_integration),+ name="agentic_provisioning_github_integration",+ ),+ path(+ "api/agentic/provisioning/resources/<str:resource_id>/wizard_runs",+ csrf_exempt(agentic_provisioning_views.provisioning_wizard_runs),+ name="agentic_provisioning_wizard_runs",+ ),path("api/agentic/provisioning/resources/<str:resource_id>",csrf_exempt(agentic_provisioning_views.provisioning_resource_detail),@@ -412,6 +422,16 @@ urlpatterns: list[Any] = [csrf_exempt(agentic_provisioning_views.provisioning_resource_remove),name="provisioning_resource_remove",),+ path(+ "api/provisioning/resources/<str:resource_id>/github_integration",+ csrf_exempt(agentic_provisioning_views.provisioning_github_integration),+ name="provisioning_github_integration",+ ),+ path(+ "api/provisioning/resources/<str:resource_id>/wizard_runs",+ csrf_exempt(agentic_provisioning_views.provisioning_wizard_runs),+ name="provisioning_wizard_runs",+ ),path("api/provisioning/resources/<str:resource_id>",csrf_exempt(agentic_provisioning_views.provisioning_resource_detail),diff --git a/frontend/src/generated/core/api.schemas.ts b/frontend/src/generated/core/api.schemas.tsindex f9dfcce630ca..12060f880663 100644--- a/frontend/src/generated/core/api.schemas.ts+++ b/frontend/src/generated/core/api.schemas.ts@@ -3421,6 +3421,7 @@ export const ShortcutPositionEnumApi = {* * `delegated` - Delegated to teammate* * `later` - Skipped for later* * `other` - Other+ * * `provisioned` - Account provisioned by a partner*/export type OnboardingSkippedReasonEnumApi =(typeof OnboardingSkippedReasonEnumApi)[keyof typeof OnboardingSkippedReasonEnumApi]@@ -3429,6 +3430,7 @@ export const OnboardingSkippedReasonEnumApi = {Delegated: 'delegated',Later: 'later',Other: 'other',+ Provisioned: 'provisioned',} as const/**diff --git a/posthog/api/github_callback/team_services.py b/posthog/api/github_callback/team_services.pyindex aedddb8f13cc..8b269d4223bb 100644--- a/posthog/api/github_callback/team_services.py+++ b/posthog/api/github_callback/team_services.py@@ -30,6 +30,7 @@ from posthog.models.integration import (GitHubInstallationAccess,GitHubInstallationAccessFetchError,GitHubIntegration,+ GitHubUserAuthorization,Integration,invalidate_github_repository_caches_for_installation,)@@ -99,6 +100,24 @@ def create_team_github_integration_from_oauth_code(if authorization is None:raise ValidationError("Failed to exchange the OAuth code — ensure GITHUB_APP_CLIENT_SECRET is configured")+ return link_github_installation_for_user(+ user=user, team_id=team_id, installation_id=installation_id, authorization=authorization+ )+++def link_github_installation_for_user(+ *,+ user: User,+ team_id: int,+ installation_id: str,+ authorization: "GitHubUserAuthorization",+) -> Integration:+ """Verify the user controls the installation, then create both GitHub records:+ the team-scoped Integration and the personal UserIntegration.++ Request-free core of the team GitHub link, shared with flows that hold an+ already-exchanged authorization (e.g. agentic provisioning GitHub grants).+ """if not is_valid_github_installation_id(installation_id):raise ValidationError("Invalid installation_id")try:@@ -110,14 +129,14 @@ def create_team_github_integration_from_oauth_code(user_id=user.id,exc_info=True,)- raise ValidationError("Failed to verify installation access")+ raise ValidationError("Failed to verify installation access", code="installation_verify_failed")if not has_access:logger.warning("github_integration_create: user does not have access to installation",installation_id=installation_id,user_id=user.id,)- raise ValidationError("You do not have access to this GitHub installation")+ raise ValidationError("You do not have access to this GitHub installation", code="installation_access_denied")instance = GitHubIntegration.integration_from_installation_id(installation_id, team_id, user)diff --git a/posthog/migrations/1255_wizard_runs_rate_limit_and_provisioned_onboarding_reason.py b/posthog/migrations/1255_wizard_runs_rate_limit_and_provisioned_onboarding_reason.pynew file mode 100644index 000000000000..178db4f0de6c--- /dev/null+++ b/posthog/migrations/1255_wizard_runs_rate_limit_and_provisioned_onboarding_reason.py@@ -0,0 +1,40 @@+# Generated by Django 5.2.14 on 2026-07-09 23:08++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [("posthog", "1254_datadeletionrequest_last_dagster_run_id")]++ operations = [+ migrations.AddField(+ model_name="oauthapplication",+ name="provisioning_rate_limit_wizard_runs",+ field=models.IntegerField(+ blank=True,+ help_text="Override default rate limit for wizard cloud run creation (per hour)",+ null=True,+ ),+ ),+ # choices are Python-state only (sqlmigrate shows "-- (no-op)"), so this is+ # state-only by construction — no ALTER TABLE ever touches the hot posthog_user table.+ migrations.SeparateDatabaseAndState(+ state_operations=[+ migrations.AlterField(+ model_name="user",+ name="onboarding_skipped_reason",+ field=models.CharField(+ blank=True,+ choices=[+ ("delegated", "Delegated to teammate"),+ ("later", "Skipped for later"),+ ("other", "Other"),+ ("provisioned", "Account provisioned by a partner"),+ ],+ max_length=32,+ null=True,+ ),+ ),+ ],+ ),+ ]diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txtindex 281e094313eb..f388cda5ad1e 100644--- a/posthog/migrations/max_migration.txt+++ b/posthog/migrations/max_migration.txt@@ -1 +1 @@-1254_datadeletionrequest_last_dagster_run_id+1255_wizard_runs_rate_limit_and_provisioned_onboarding_reasondiff --git a/posthog/models/oauth.py b/posthog/models/oauth.pyindex b8841c145050..458ce4d15413 100644--- a/posthog/models/oauth.py+++ b/posthog/models/oauth.py@@ -237,6 +237,9 @@ class OAuthApplication(ModelActivityMixin, AbstractApplication): # type: ignoreprovisioning_rate_limit_github_grants: models.IntegerField = models.IntegerField(null=True, blank=True, help_text="Override default rate limit for GitHub grant creation (per hour)")+ provisioning_rate_limit_wizard_runs: models.IntegerField = models.IntegerField(+ null=True, blank=True, help_text="Override default rate limit for wizard cloud run creation (per hour)"+ )provisioning_disabled: models.BooleanField = models.BooleanField(default=False,help_text=(diff --git a/posthog/models/user.py b/posthog/models/user.pyindex 086838cb10df..168c7461f3cd 100644--- a/posthog/models/user.py+++ b/posthog/models/user.py@@ -202,6 +202,7 @@ class OnboardingSkippedReason(models.TextChoices):DELEGATED = "delegated", "Delegated to teammate"LATER = "later", "Skipped for later"OTHER = "other", "Other"+ PROVISIONED = "provisioned", "Account provisioned by a partner"class User(AbstractUser, UUIDTClassicModel, ModelActivityMixin): # type: ignore[django-manager-missing]diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex b0e6889e6a10..a2c0a45ac0f2 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -34157,6 +34157,7 @@ export namespace Schemas {* * `delegated` - Delegated to teammate* * `later` - Skipped for later* * `other` - Other+ * * `provisioned` - Account provisioned by a partner*/export type OnboardingSkippedReasonEnum = typeof OnboardingSkippedReasonEnum[keyof typeof OnboardingSkippedReasonEnum];@@ -34165,6 +34166,7 @@ export namespace Schemas {Delegated: 'delegated',Later: 'later',Other: 'other',+ Provisioned: 'provisioned',} as const;/**