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.

chore(vercel): move webhook event handling out of the view (#103595)

Julian Bez420a3d729d22
diff --git a/ee/api/vercel/test/test_vercel_webhooks.py b/ee/api/vercel/test/test_vercel_webhooks.py
index 95bd759777ee..c01642e4b80f 100644
--- a/ee/api/vercel/test/test_vercel_webhooks.py
+++ b/ee/api/vercel/test/test_vercel_webhooks.py
@@ -109,7 +109,7 @@ class TestVercelWebhooks(VercelTestBase):
assert response.json()["status"] == "ignored"
@override_settings(VERCEL_CLIENT_INTEGRATION_SECRET="test_webhook_secret")
- @patch("ee.api.vercel.vercel_webhooks.VercelIntegration")
+ @patch("ee.api.vercel.webhook_events.VercelIntegration")
def test_deauthorization_native_calls_delete_installation(self, mock_vercel_integration):
assert OrganizationIntegration.objects.filter(integration_id=self.installation_id).exists()
@@ -203,7 +203,7 @@ class TestVercelWebhooks(VercelTestBase):
REGION_US_DOMAIN="us.posthog.com",
REGION_EU_DOMAIN="eu.posthog.com",
)
- @patch("ee.api.vercel.vercel_webhooks.outbound_requests.post")
+ @patch("ee.api.vercel.webhook_events.outbound_requests.post")
def test_deauthorization_unknown_config_on_us_proxies_to_eu(self, mock_post):
mock_eu_response = MagicMock()
mock_eu_response.status_code = 200
@@ -231,7 +231,7 @@ class TestVercelWebhooks(VercelTestBase):
REGION_US_DOMAIN="us.posthog.com",
REGION_EU_DOMAIN="eu.posthog.com",
)
- @patch("ee.api.vercel.vercel_webhooks.outbound_requests.post")
+ @patch("ee.api.vercel.webhook_events.outbound_requests.post")
def test_deauthorization_unknown_config_on_eu_does_not_proxy(self, mock_post):
payload = {
"type": "integration-configuration.removed",
@@ -250,7 +250,7 @@ class TestVercelWebhooks(VercelTestBase):
REGION_US_DOMAIN="us.posthog.com",
REGION_EU_DOMAIN="eu.posthog.com",
)
- @patch("ee.api.vercel.vercel_webhooks.outbound_requests.post")
+ @patch("ee.api.vercel.webhook_events.outbound_requests.post")
def test_deauthorization_unknown_config_on_dev_does_not_proxy(self, mock_post):
payload = {
"type": "integration-configuration.removed",
@@ -269,7 +269,7 @@ class TestVercelWebhooks(VercelTestBase):
REGION_US_DOMAIN="us.posthog.com",
REGION_EU_DOMAIN="eu.posthog.com",
)
- @patch("ee.api.vercel.vercel_webhooks.outbound_requests.post")
+ @patch("ee.api.vercel.webhook_events.outbound_requests.post")
def test_billing_event_unknown_config_does_not_proxy(self, mock_post):
payload = {
"type": "marketplace.invoice.paid",
@@ -283,8 +283,8 @@ class TestVercelWebhooks(VercelTestBase):
mock_post.assert_not_called()
@override_settings(VERCEL_CLIENT_INTEGRATION_SECRET="test_webhook_secret")
- @patch("ee.api.vercel.vercel_webhooks.BillingManager")
- @patch("ee.api.vercel.vercel_webhooks.License")
+ @patch("ee.api.vercel.webhook_events.BillingManager")
+ @patch("ee.api.vercel.webhook_events.License")
def test_billing_event_forwarded_to_billing_service(self, mock_license_model, mock_billing_manager_class):
mock_license = MagicMock()
mock_license_model.objects.first.return_value = mock_license
@@ -311,8 +311,8 @@ class TestVercelWebhooks(VercelTestBase):
)
@override_settings(VERCEL_CLIENT_INTEGRATION_SECRET="test_webhook_secret")
- @patch("ee.api.vercel.vercel_webhooks.BillingManager")
- @patch("ee.api.vercel.vercel_webhooks.License")
+ @patch("ee.api.vercel.webhook_events.BillingManager")
+ @patch("ee.api.vercel.webhook_events.License")
def test_billing_error_returns_500(self, mock_license_model, mock_billing_manager_class):
mock_license = MagicMock()
mock_license_model.objects.first.return_value = mock_license
@@ -333,7 +333,7 @@ class TestVercelWebhooks(VercelTestBase):
assert "Processing failed" in response.json()["error"]
@override_settings(VERCEL_CLIENT_INTEGRATION_SECRET="test_webhook_secret")
- @patch("ee.api.vercel.vercel_webhooks.License")
+ @patch("ee.api.vercel.webhook_events.License")
def test_no_license_returns_500(self, mock_license_model):
mock_license_model.objects.first.return_value = None
diff --git a/ee/api/vercel/vercel_webhooks.py b/ee/api/vercel/vercel_webhooks.py
index d1a28cafdb36..003800edbf1d 100644
--- a/ee/api/vercel/vercel_webhooks.py
+++ b/ee/api/vercel/vercel_webhooks.py
@@ -1,10 +1,8 @@
import hmac
import hashlib
-from typing import Any
from django.conf import settings
-import requests as outbound_requests
import structlog
from rest_framework import status
from rest_framework.decorators import api_view, authentication_classes, permission_classes
@@ -12,52 +10,11 @@ from rest_framework.request import Request
from rest_framework.response import Response
from posthog.exceptions_capture import capture_exception
-from posthog.models.organization_integration import OrganizationIntegration
-from ee.billing.billing_manager import BillingManager, BillingServiceOpenInvoicesError
-from ee.models import License
-from ee.vercel.integration import VercelIntegration
+from ee.api.vercel.webhook_events import VercelEventOutcome, VercelEventProcessingError, handle_vercel_event
logger = structlog.get_logger(__name__)
-BILLING_EVENT_PREFIX = "marketplace.invoice."
-DEAUTHORIZATION_EVENT = "integration-configuration.removed"
-
-CROSS_REGION_PROXY_TIMEOUT = 10
-DEFAULT_US_DOMAIN = "us.posthog.com"
-DEFAULT_EU_DOMAIN = "eu.posthog.com"
-
-
-def _is_us_region() -> bool:
- us_domain = getattr(settings, "REGION_US_DOMAIN", DEFAULT_US_DOMAIN)
- return settings.SITE_URL == f"https://{us_domain}"
-
-
-def _proxy_deauthorization_to_eu(raw_body: bytes, signature: str | None) -> int | None:
- """Forward a deauthorization webhook to EU. Returns the EU status code, or None on failure."""
- eu_domain = getattr(settings, "REGION_EU_DOMAIN", DEFAULT_EU_DOMAIN)
- target_url = f"https://{eu_domain}/webhooks/vercel"
-
- headers: dict[str, str] = {"Content-Type": "application/json"}
- if signature:
- headers["x-vercel-signature"] = signature
-
- try:
- response = outbound_requests.post(
- url=target_url,
- data=raw_body,
- headers=headers,
- timeout=CROSS_REGION_PROXY_TIMEOUT,
- )
- logger.info(
- "vercel_webhook_proxied_to_eu",
- status_code=response.status_code,
- )
- return response.status_code
- except outbound_requests.RequestException as e:
- logger.warning("vercel_webhook_proxy_to_eu_failed", error=str(e))
- return None
-
def _is_valid_signature(payload: bytes, signature: str | None) -> bool:
if not signature:
@@ -73,41 +30,13 @@ def _is_valid_signature(payload: bytes, signature: str | None) -> bool:
return hmac.compare_digest(expected, signature)
-def _extract_config_id(payload: dict[str, Any]) -> str | None:
- # Billing events use "installationId". Deauthorization events use "configuration.id".
- return payload.get("installationId") or payload.get("configuration", {}).get("id")
-
-
-def _is_billing_event(event_type: str | None) -> bool:
- return bool(event_type and event_type.startswith(BILLING_EVENT_PREFIX))
-
-
-def _is_deauthorization_event(event_type: str | None) -> bool:
- return event_type == DEAUTHORIZATION_EVENT
-
-
-def _get_integration(config_id: str) -> OrganizationIntegration | None:
- try:
- return OrganizationIntegration.objects.select_related("organization").get(
- kind=OrganizationIntegration.OrganizationIntegrationKind.VERCEL,
- integration_id=config_id,
- )
- except OrganizationIntegration.DoesNotExist:
- return None
-
-
-def _forward_to_billing_service(event_type: str, payload: dict[str, Any], integration: OrganizationIntegration) -> None:
- license = License.objects.first()
- if not license:
- raise ValueError("No license configured")
-
- billing_manager = BillingManager(license=license)
- billing_manager.handle_billing_provider_webhook(
- event_type=event_type,
- event_data=payload,
- organization=integration.organization,
- billing_provider="vercel",
- )
+# The outcomes the handling reports, and what each one answers Vercel.
+_OUTCOME_RESPONSES = {
+ VercelEventOutcome.ACCEPTED: ({"status": "ok"}, status.HTTP_200_OK),
+ VercelEventOutcome.IGNORED: ({"status": "ignored"}, status.HTTP_200_OK),
+ VercelEventOutcome.MISSING_CONFIG_ID: ({"error": "Missing configurationId"}, status.HTTP_400_BAD_REQUEST),
+ VercelEventOutcome.UNKNOWN_CONFIG: ({"error": "Unknown configuration"}, status.HTTP_404_NOT_FOUND),
+}
@api_view(["POST"])
@@ -123,83 +52,15 @@ def vercel_webhook(request: Request) -> Response:
logger.warning("vercel_webhook_invalid_signature")
return Response({"error": "Invalid signature"}, status=status.HTTP_401_UNAUTHORIZED)
- event_type = request.data.get("type")
- payload = request.data.get("payload", {})
- config_id = _extract_config_id(payload)
-
- logger.info("vercel_webhook_received", event_type=event_type, config_id=config_id)
-
- if _is_deauthorization_event(event_type):
- if not config_id:
- logger.error("vercel_webhook_deauthorize_missing_config_id")
- return Response({"error": "Missing configurationId"}, status=status.HTTP_400_BAD_REQUEST)
-
- integration = _get_integration(config_id)
- if integration:
- is_connectable = integration.config.get("type") == "connectable"
- logger.info(
- "vercel_webhook_deauthorize",
- config_id=config_id,
- org_id=str(integration.organization_id),
- is_connectable=is_connectable,
- )
- if is_connectable:
- integration.delete()
- else:
- try:
- VercelIntegration.delete_installation(config_id)
- except BillingServiceOpenInvoicesError as e:
- # Vercel ignores non-200 from webhooks and shows "Integration Deleted" regardless.
- # The marketplace DELETE API endpoint handles the actual blocking with 409.
- # Log the warning but return 200 to avoid misleading error tracking noise.
- logger.warning(
- "vercel_webhook_deauthorize_blocked_by_open_invoices",
- config_id=config_id,
- reason=e.message,
- )
- except Exception as e:
- logger.exception("vercel_webhook_deauthorize_delete_failed", config_id=config_id)
- capture_exception(e, {"config_id": config_id})
- return Response({"error": "Processing failed"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- elif _is_us_region():
- logger.info("vercel_webhook_deauthorize_proxying_to_eu", config_id=config_id)
- eu_status = _proxy_deauthorization_to_eu(request.body, signature)
- if eu_status is None or eu_status >= 300:
- logger.warning(
- "vercel_webhook_deauthorize_eu_proxy_non_ok",
- config_id=config_id,
- eu_status=eu_status,
- )
- else:
- logger.warning("vercel_webhook_deauthorize_unknown_config", config_id=config_id)
-
- return Response({"status": "ok"}, status=status.HTTP_200_OK)
-
- if not _is_billing_event(event_type):
- logger.info("vercel_webhook_non_billing_event", event_type=event_type)
- return Response({"status": "ignored"}, status=status.HTTP_200_OK)
-
- if not config_id:
- logger.error("vercel_webhook_missing_config_id", event_type=event_type)
- return Response({"error": "Missing configurationId"}, status=status.HTTP_400_BAD_REQUEST)
-
- assert event_type is not None # Guaranteed by _is_billing_event check above
-
- integration = _get_integration(config_id)
- if not integration:
- logger.error("vercel_webhook_unknown_config", config_id=config_id)
- capture_exception(
- OrganizationIntegration.DoesNotExist(),
- {"config_id": config_id, "event_type": event_type},
- )
- return Response({"error": "Unknown configuration"}, status=status.HTTP_404_NOT_FOUND)
-
try:
- _forward_to_billing_service(event_type, payload, integration)
- except Exception as e:
- logger.exception("vercel_webhook_billing_error", event_type=event_type)
- capture_exception(e, {"config_id": config_id, "event_type": event_type})
+ outcome = handle_vercel_event(
+ event_type=request.data.get("type"),
+ payload=request.data.get("payload", {}),
+ raw_body=request.body,
+ signature=signature,
+ )
+ except VercelEventProcessingError:
return Response({"error": "Processing failed"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- logger.info("vercel_webhook_processed", event_type=event_type, org_id=str(integration.organization_id))
- return Response({"status": "ok"}, status=status.HTTP_200_OK)
+ body, response_status = _OUTCOME_RESPONSES[outcome]
+ return Response(body, status=response_status)
diff --git a/ee/api/vercel/webhook_events.py b/ee/api/vercel/webhook_events.py
new file mode 100644
index 000000000000..2252b4e1ae52
--- /dev/null
+++ b/ee/api/vercel/webhook_events.py
@@ -0,0 +1,214 @@
+"""What PostHog does with one verified Vercel webhook event.
+
+Separate from the view, because the transport around this endpoint is being replaced and the
+handling is not. The caller decides what an outcome answers on the wire; nothing here does.
+"""
+
+from enum import StrEnum
+from typing import Any
+
+from django.conf import settings
+
+import requests as outbound_requests
+import structlog
+
+from posthog.exceptions_capture import capture_exception
+from posthog.models.organization_integration import OrganizationIntegration
+
+from ee.billing.billing_manager import BillingManager, BillingServiceOpenInvoicesError
+from ee.models import License
+from ee.vercel.integration import VercelIntegration
+
+logger = structlog.get_logger(__name__)
+
+BILLING_EVENT_PREFIX = "marketplace.invoice."
+DEAUTHORIZATION_EVENT = "integration-configuration.removed"
+
+CROSS_REGION_PROXY_TIMEOUT = 10
+DEFAULT_US_DOMAIN = "us.posthog.com"
+DEFAULT_EU_DOMAIN = "eu.posthog.com"
+
+
+class VercelEventOutcome(StrEnum):
+ """What handling one event concluded, for a caller that answers Vercel."""
+
+ ACCEPTED = "accepted"
+ # An event type this integration does not act on.
+ IGNORED = "ignored"
+ # The event named no installation, so there is nothing to look up.
+ MISSING_CONFIG_ID = "missing_config_id"
+ # A billing event for an installation this region does not hold.
+ UNKNOWN_CONFIG = "unknown_config"
+
+
+class VercelEventProcessingError(Exception):
+ """Handling failed on our side, so the event was not processed."""
+
+
+def _is_us_region() -> bool:
+ us_domain = getattr(settings, "REGION_US_DOMAIN", DEFAULT_US_DOMAIN)
+ return settings.SITE_URL == f"https://{us_domain}"
+
+
+def _proxy_deauthorization_to_eu(raw_body: bytes, signature: str | None) -> int | None:
+ """Forward a deauthorization webhook to EU. Returns the EU status code, or None on failure."""
+ eu_domain = getattr(settings, "REGION_EU_DOMAIN", DEFAULT_EU_DOMAIN)
+ target_url = f"https://{eu_domain}/webhooks/vercel"
+
+ headers: dict[str, str] = {"Content-Type": "application/json"}
+ if signature:
+ headers["x-vercel-signature"] = signature
+
+ try:
+ response = outbound_requests.post(
+ url=target_url,
+ data=raw_body,
+ headers=headers,
+ timeout=CROSS_REGION_PROXY_TIMEOUT,
+ )
+ logger.info(
+ "vercel_webhook_proxied_to_eu",
+ status_code=response.status_code,
+ )
+ return response.status_code
+ except outbound_requests.RequestException as e:
+ logger.warning("vercel_webhook_proxy_to_eu_failed", error=str(e))
+ return None
+
+
+def extract_config_id(payload: dict[str, Any]) -> str | None:
+ # Billing events use "installationId". Deauthorization events use "configuration.id".
+ return payload.get("installationId") or payload.get("configuration", {}).get("id")
+
+
+def is_billing_event(event_type: str | None) -> bool:
+ return bool(event_type and event_type.startswith(BILLING_EVENT_PREFIX))
+
+
+def is_deauthorization_event(event_type: str | None) -> bool:
+ return event_type == DEAUTHORIZATION_EVENT
+
+
+def _get_integration(config_id: str) -> OrganizationIntegration | None:
+ try:
+ return OrganizationIntegration.objects.select_related("organization").get(
+ kind=OrganizationIntegration.OrganizationIntegrationKind.VERCEL,
+ integration_id=config_id,
+ )
+ except OrganizationIntegration.DoesNotExist:
+ return None
+
+
+def _forward_to_billing_service(event_type: str, payload: dict[str, Any], integration: OrganizationIntegration) -> None:
+ license = License.objects.first()
+ if not license:
+ raise ValueError("No license configured")
+
+ billing_manager = BillingManager(license=license)
+ billing_manager.handle_billing_provider_webhook(
+ event_type=event_type,
+ event_data=payload,
+ organization=integration.organization,
+ billing_provider="vercel",
+ )
+
+
+def _handle_deauthorization(
+ config_id: str,
+ raw_body: bytes,
+ signature: str | None,
+) -> VercelEventOutcome:
+ integration = _get_integration(config_id)
+ if integration:
+ is_connectable = integration.config.get("type") == "connectable"
+ logger.info(
+ "vercel_webhook_deauthorize",
+ config_id=config_id,
+ org_id=str(integration.organization_id),
+ is_connectable=is_connectable,
+ )
+ if is_connectable:
+ integration.delete()
+ return VercelEventOutcome.ACCEPTED
+ try:
+ VercelIntegration.delete_installation(config_id)
+ except BillingServiceOpenInvoicesError as e:
+ # Vercel ignores non-200 from webhooks and shows "Integration Deleted" regardless.
+ # The marketplace DELETE API endpoint handles the actual blocking with 409.
+ # Log the warning but accept the event to avoid misleading error tracking noise.
+ logger.warning(
+ "vercel_webhook_deauthorize_blocked_by_open_invoices",
+ config_id=config_id,
+ reason=e.message,
+ )
+ except Exception as e:
+ logger.exception("vercel_webhook_deauthorize_delete_failed", config_id=config_id)
+ capture_exception(e, {"config_id": config_id})
+ raise VercelEventProcessingError(str(e)) from e
+ return VercelEventOutcome.ACCEPTED
+
+ if _is_us_region():
+ logger.info("vercel_webhook_deauthorize_proxying_to_eu", config_id=config_id)
+ eu_status = _proxy_deauthorization_to_eu(raw_body, signature)
+ if eu_status is None or eu_status >= 300:
+ logger.warning(
+ "vercel_webhook_deauthorize_eu_proxy_non_ok",
+ config_id=config_id,
+ eu_status=eu_status,
+ )
+ else:
+ logger.warning("vercel_webhook_deauthorize_unknown_config", config_id=config_id)
+
+ # A deauthorization for an installation this region never held is still done with.
+ return VercelEventOutcome.ACCEPTED
+
+
+def _handle_billing(event_type: str, payload: dict[str, Any], config_id: str) -> VercelEventOutcome:
+ integration = _get_integration(config_id)
+ if not integration:
+ logger.error("vercel_webhook_unknown_config", config_id=config_id)
+ capture_exception(
+ OrganizationIntegration.DoesNotExist(),
+ {"config_id": config_id, "event_type": event_type},
+ )
+ return VercelEventOutcome.UNKNOWN_CONFIG
+
+ try:
+ _forward_to_billing_service(event_type, payload, integration)
+ except Exception as e:
+ logger.exception("vercel_webhook_billing_error", event_type=event_type)
+ capture_exception(e, {"config_id": config_id, "event_type": event_type})
+ raise VercelEventProcessingError(str(e)) from e
+
+ logger.info("vercel_webhook_processed", event_type=event_type, org_id=str(integration.organization_id))
+ return VercelEventOutcome.ACCEPTED
+
+
+def handle_vercel_event(
+ *,
+ event_type: str | None,
+ payload: dict[str, Any],
+ raw_body: bytes,
+ signature: str | None,
+) -> VercelEventOutcome:
+ """Route one verified event. Raises `VercelEventProcessingError` when handling failed."""
+ config_id = extract_config_id(payload)
+ logger.info("vercel_webhook_received", event_type=event_type, config_id=config_id)
+
+ if is_deauthorization_event(event_type):
+ if not config_id:
+ logger.error("vercel_webhook_deauthorize_missing_config_id")
+ return VercelEventOutcome.MISSING_CONFIG_ID
+ return _handle_deauthorization(config_id, raw_body, signature)
+
+ if not is_billing_event(event_type):
+ logger.info("vercel_webhook_non_billing_event", event_type=event_type)
+ return VercelEventOutcome.IGNORED
+
+ if not config_id:
+ logger.error("vercel_webhook_missing_config_id", event_type=event_type)
+ return VercelEventOutcome.MISSING_CONFIG_ID
+
+ # Guaranteed by the is_billing_event check above.
+ assert event_type is not None
+ return _handle_billing(event_type, payload, config_id)