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(github): treat a 422 on the topics endpoint as a benign zero-row skip (#84170)

Tom Owers452046b867f8
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py
index 4b9e1bae1ca6..41dbf4665b99 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py
@@ -5,7 +5,7 @@ import dataclasses
from collections.abc import AsyncIterator, Callable, Iterator
from datetime import UTC, date, datetime, timedelta
from typing import Any, Literal, Optional
-from urllib.parse import urlencode
+from urllib.parse import urlencode, urlsplit
import pyarrow as pa
import requests
@@ -301,6 +301,12 @@ def _is_repository_too_large_for_code_frequency(response: requests.Response) ->
return isinstance(message, str) and "fewer than 10000 commits" in message.lower()
+def _is_topics_endpoint(page_url: str) -> bool:
+ """The repository topics endpoint (/repos/{owner}/{repo}/topics). Matched on the URL path so a
+ repository literally named `topics`, or a query string, can't be mistaken for it."""
+ return urlsplit(page_url).path.endswith("/topics")
+
+
def _as_utc(dt: datetime) -> datetime:
"""Treat naive datetimes as UTC so tz-aware values (GitHub returns ISO 8601
with `Z`) can be safely compared against naive cutoffs from the DB."""
@@ -870,6 +876,15 @@ def _fetch_page(
if _is_repository_too_large_for_code_frequency(response):
raise GithubRepositoryTooLargeError()
+ # GitHub answers 422 on /repos/{owner}/{repo}/topics for some repositories rather than an empty
+ # {"names": []} body. Topics are optional repository metadata, so one repository's 422 there
+ # must not fail its whole schema the way a generic 422 does — sync zero rows, the same benign
+ # skip as a switched-off feature. A genuinely broken connection still fails loudly on the
+ # repository's other tables (401/403/404), so skipping topics here cannot hide it.
+ if response.status_code == 422 and _is_topics_endpoint(page_url):
+ logger.info(f"Github: topics not available for this repository, syncing zero rows: url={page_url}")
+ raise GithubResourceUnavailableError(_github_error_message(response))
+
# An org-scoped endpoint 404s when the repo owner is a user (no org) or the token lacks org
# access. Signal it so the caller syncs zero rows rather than failing the schema — a benign skip
# like an empty repository, not a real "repository not found".
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_fetch_page.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_fetch_page.py
index 94831e75dc6e..ab44b22ea54b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_fetch_page.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_fetch_page.py
@@ -131,6 +131,17 @@ def test_fetch_page_reraises_other_422_errors():
github._fetch_page("https://api.github.com/repos/o/r/stats/code_frequency", {}, mock.Mock())
+def test_fetch_page_treats_topics_422_as_resource_unavailable():
+ # GitHub 422s the topics endpoint for some repositories; it's optional metadata, so the caller
+ # must sync zero rows rather than crash and fail the schema over a raw 422.
+ session = mock.Mock()
+ session.request.return_value = _unprocessable_response("Validation failed")
+
+ with mock.patch.object(github, "make_tracked_session", return_value=session):
+ with pytest.raises(github.GithubResourceUnavailableError):
+ github._fetch_page("https://api.github.com/repos/o/r/topics?per_page=100", {}, mock.Mock())
+
+
def test_fetch_page_retries_chunked_encoding_error():
session = mock.Mock()
session.request.side_effect = [requests.exceptions.ChunkedEncodingError("Connection broken"), _ok_response()]