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(data-imports): skip Sentry service hooks endpoint on 403 (#64181)

Tom Owers537440e71da8
diff --git a/posthog/temporal/data_imports/sources/sentry/sentry.py b/posthog/temporal/data_imports/sources/sentry/sentry.py
index 7474b66ff230..7addbec018d6 100644
--- a/posthog/temporal/data_imports/sources/sentry/sentry.py
+++ b/posthog/temporal/data_imports/sources/sentry/sentry.py
@@ -548,6 +548,33 @@ def get_resource(
# ---------------------------------------------------------------------------
+def _skip_endpoint_on_forbidden(resource: Iterable[Any], endpoint: str) -> Iterator[Any]:
+ """Yield pages from a fan-out resource, treating a 403 as "endpoint not
+ available for this organization" and stopping gracefully.
+
+ Sentry's project service hooks API is gated at the organization level, so it
+ returns 403 Forbidden even for tokens that already hold full project/admin
+ scopes. Letting that 403 propagate marks the whole schema as a non-retryable
+ failure (see ``SentrySource.get_non_retryable_errors``), permanently erroring
+ a source whose credentials are otherwise valid. Skipping the endpoint instead
+ lets the sync complete with an empty table — the same graceful-skip approach
+ used for persistent server errors in ``_iter_issue_tag_values_rows``. Genuine
+ scope problems still surface on the other (non-skipped) endpoints.
+ """
+ try:
+ yield from resource
+ except HTTPError as exc:
+ response = exc.response
+ if response is not None and response.status_code == 403:
+ logger.warning(
+ "sentry_source.endpoint_forbidden_skipped",
+ endpoint=endpoint,
+ status_code=response.status_code,
+ )
+ return
+ raise
+
+
def _make_source_response(endpoint_config: SentryEndpointConfig, items_fn) -> SourceResponse:
return SourceResponse(
name=endpoint_config.name,
@@ -620,6 +647,13 @@ def sentry_source(
incremental_config_factory=_sentry_incremental_window,
),
)
+ # Sentry gates the service hooks API at the org level, so it 403s even
+ # for fully-scoped tokens. Skip it gracefully rather than permanently
+ # erroring the schema (which the non-retryable 403 handling would do).
+ if endpoint == "project_service_hooks":
+ return _make_source_response(
+ endpoint_config, lambda: _skip_endpoint_on_forbidden(dependent_resource, endpoint)
+ )
return _make_source_response(endpoint_config, lambda: dependent_resource)
# --- Flat org-level endpoints (via rest_api_resources) ---
diff --git a/posthog/temporal/data_imports/sources/sentry/tests/test_sentry.py b/posthog/temporal/data_imports/sources/sentry/tests/test_sentry.py
index bba1650b617f..16fd4d4e8b2b 100644
--- a/posthog/temporal/data_imports/sources/sentry/tests/test_sentry.py
+++ b/posthog/temporal/data_imports/sources/sentry/tests/test_sentry.py
@@ -67,6 +67,18 @@ class _FakeDltResource:
return iter(self._rows)
+class _RaisingResource:
+ """Fan-out resource stand-in that raises an HTTPError when iterated."""
+
+ def __init__(self, status_code: int) -> None:
+ self._status_code = status_code
+
+ def __iter__(self):
+ response = Mock()
+ response.status_code = self._status_code
+ raise HTTPError(f"{self._status_code} Client Error", response=response)
+
+
class TestSentryTransport:
def test_normalize_api_base_url(self) -> None:
assert _normalize_api_base_url(None) == "https://sentry.io"
@@ -336,6 +348,60 @@ class TestSentrySourceValidation:
assert "_projects_id" not in row
assert "_projects_slug" not in row
+ # ----- Project service hooks: graceful skip on org-gated 403 -----
+
+ @patch("posthog.temporal.data_imports.sources.sentry.sentry.build_dependent_resource")
+ def test_project_service_hooks_skips_on_forbidden(self, mock_build) -> None:
+ # Sentry 403s the service hooks endpoint for orgs without the feature,
+ # even with full scopes. The schema should complete empty, not error.
+ mock_build.return_value = _RaisingResource(403)
+
+ resp = sentry_source(
+ auth_token="token",
+ organization_slug="acme",
+ api_base_url="https://sentry.io",
+ endpoint="project_service_hooks",
+ team_id=123,
+ job_id="job-id",
+ )
+
+ assert list(cast(Any, resp.items())) == []
+
+ @patch("posthog.temporal.data_imports.sources.sentry.sentry.build_dependent_resource")
+ def test_project_service_hooks_propagates_non_forbidden(self, mock_build) -> None:
+ # A server error is not the org-gate signal — it must still propagate.
+ mock_build.return_value = _RaisingResource(500)
+
+ resp = sentry_source(
+ auth_token="token",
+ organization_slug="acme",
+ api_base_url="https://sentry.io",
+ endpoint="project_service_hooks",
+ team_id=123,
+ job_id="job-id",
+ )
+
+ with pytest.raises(HTTPError):
+ list(cast(Any, resp.items()))
+
+ @patch("posthog.temporal.data_imports.sources.sentry.sentry.build_dependent_resource")
+ def test_other_fanout_endpoint_propagates_forbidden(self, mock_build) -> None:
+ # A 403 on a non-servicehooks fan-out endpoint is a genuine scope error
+ # and must still reach the non-retryable handler.
+ mock_build.return_value = _RaisingResource(403)
+
+ resp = sentry_source(
+ auth_token="token",
+ organization_slug="acme",
+ api_base_url="https://sentry.io",
+ endpoint="project_users",
+ team_id=123,
+ job_id="job-id",
+ )
+
+ with pytest.raises(HTTPError):
+ list(cast(Any, resp.items()))
+
# ----- Issue fan-out: dependent resources (issue_events, issue_hashes) -----
@parameterized.expand(