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(intercom): follow companies/list pagination with POST (#62131)

Tom Owers8d607cd0c548
diff --git a/posthog/temporal/data_imports/sources/intercom/intercom.py b/posthog/temporal/data_imports/sources/intercom/intercom.py
index 107e08e4768a..3cebd82f052e 100644
--- a/posthog/temporal/data_imports/sources/intercom/intercom.py
+++ b/posthog/temporal/data_imports/sources/intercom/intercom.py
@@ -189,15 +189,19 @@ def get_resource(
}
+def _resolve_intercom_url(path_or_url: str) -> str:
+ """Accept either an API path or a full URL (e.g. a `pages.next` link)."""
+ return path_or_url if path_or_url.startswith("http") else f"{INTERCOM_API_BASE}{path_or_url}"
+
+
def _intercom_get(session: Session, path_or_url: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
- url = path_or_url if path_or_url.startswith("http") else f"{INTERCOM_API_BASE}{path_or_url}"
- response = session.get(url, params=params, timeout=30)
+ response = session.get(_resolve_intercom_url(path_or_url), params=params, timeout=30)
response.raise_for_status()
return response.json()
-def _intercom_post(session: Session, path: str, body: dict[str, Any]) -> dict[str, Any]:
- response = session.post(f"{INTERCOM_API_BASE}{path}", json=body, timeout=30)
+def _intercom_post(session: Session, path_or_url: str, body: dict[str, Any]) -> dict[str, Any]:
+ response = session.post(_resolve_intercom_url(path_or_url), json=body, timeout=30)
response.raise_for_status()
return response.json()
@@ -247,11 +251,12 @@ def _iter_companies(session: Session) -> Iterator[dict[str, Any]]:
if next_url is None:
payload = _intercom_post(session, "/companies/list", body)
else:
- # `pages.next` is a full URL; the framework follows it as GET, but
- # /companies/list is POST. Empirically Intercom honors GET on the
- # `pages.next` URL too — same response shape — so we use GET to
- # avoid having to re-build the body with the embedded cursor.
- payload = _intercom_get(session, next_url)
+ # `pages.next` is a full URL carrying the page cursor in its query
+ # string. `/companies/list` is POST-only — a GET against it 404s —
+ # so we re-POST the same body to the next-page URL, mirroring how
+ # the REST next-URL paginator advances the other list endpoints
+ # (it preserves the POST method + body and only swaps the URL).
+ payload = _intercom_post(session, next_url, body)
yield from (payload.get("data") or [])
next_url = (payload.get("pages") or {}).get("next")
if not next_url:
diff --git a/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py b/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py
index 939b079c1832..a7d3d83c2a80 100644
--- a/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py
+++ b/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py
@@ -285,20 +285,28 @@ class TestSubstreamGenerators:
assert segments[0]["company_id"] == "co1"
assert segments[1]["company_id"] == "co2"
- def test_iter_companies_follows_next_url(self):
- next_url = f"{INTERCOM_API_BASE}/companies/list?cursor=2"
+ def test_iter_companies_follows_next_url_with_post(self):
+ # `/companies/list` is POST-only; its `pages.next` URL carries the page
+ # cursor in the query string. Following it with GET 404s in production,
+ # so every page — including subsequent ones — must be a POST carrying
+ # the same body.
+ next_url = f"{INTERCOM_API_BASE}/companies/list?per_page=60&page=2"
mock_session = mock.MagicMock()
mock_session.post.side_effect = [
_make_response({"data": [{"id": "co1"}], "pages": {"next": next_url}}),
- ]
- mock_session.get.side_effect = [
_make_response({"data": [{"id": "co2"}], "pages": {}}),
]
companies = list(_iter_companies(mock_session))
assert [c["id"] for c in companies] == ["co1", "co2"]
- assert mock_session.get.call_args_list[0].args[0] == next_url
+ # Second page is POSTed to the next-page URL with the same body — not
+ # GET (the production 404 this guards against).
+ assert mock_session.post.call_args_list[1].args[0] == next_url
+ assert mock_session.post.call_args_list[1].kwargs["json"] == {
+ "per_page": INTERCOM_ENDPOINTS["companies"].page_size
+ }
+ assert mock_session.get.call_count == 0
class TestSubstreamSessionRetries: