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.

feat(intercom): support Intercom API version 2.15 (#71657)

Tom Owersbe090d5c31fb
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/intercom.py b/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/intercom.py
index baf56424f38c..e062488dbe1b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/intercom.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/intercom.py
@@ -32,6 +32,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.intercom.s
)
INTERCOM_API_BASE = "https://api.intercom.io"
+# Version used for credential validation at source-create time, where no row pin exists yet.
+# Sync requests send the version resolved from the source pin (threaded into `intercom_source`).
INTERCOM_API_VERSION = "2.13"
logger = structlog.get_logger(__name__)
@@ -82,16 +84,16 @@ def _is_scroll_expired(exc: HTTPError) -> bool:
return resp is not None and resp.status_code == 404
-def _default_headers() -> dict[str, str]:
+def _default_headers(api_version: str) -> dict[str, str]:
return {
"Accept": "application/json",
"Content-Type": "application/json",
- "Intercom-Version": INTERCOM_API_VERSION,
+ "Intercom-Version": api_version,
}
-def _auth_headers(access_token: str) -> dict[str, str]:
- return {"Authorization": f"Bearer {access_token}", **_default_headers()}
+def _auth_headers(access_token: str, api_version: str) -> dict[str, str]:
+ return {"Authorization": f"Bearer {access_token}", **_default_headers(api_version)}
# The substream walk reaches `/conversations/search` via POST (the companies
@@ -111,14 +113,14 @@ _INTERCOM_RETRY = Retry(
)
-def _make_intercom_session(access_token: str) -> Session:
+def _make_intercom_session(access_token: str, api_version: str) -> Session:
"""Build a tracked session with Intercom auth + default headers baked in.
Reusing one session across the many requests a sync makes lets urllib3
keep the underlying TCP+TLS connection alive — the substream generators
(one GET per parent row) are the main beneficiary.
"""
- return make_tracked_session(headers=_auth_headers(access_token), retry=_INTERCOM_RETRY)
+ return make_tracked_session(headers=_auth_headers(access_token, api_version), retry=_INTERCOM_RETRY)
class IntercomSearchPaginator(BasePaginator):
@@ -531,7 +533,9 @@ def _substream_items(
raise ValueError(f"Unknown Intercom substream endpoint: {endpoint}")
-def validate_credentials(access_token: str, schema_name: str | None = None) -> tuple[bool, str | None]:
+def validate_credentials(
+ access_token: str, schema_name: str | None = None, api_version: str = INTERCOM_API_VERSION
+) -> tuple[bool, str | None]:
"""Validate an Intercom access token by hitting `/me`.
Works identically with OAuth-issued access tokens and Personal Access
@@ -547,7 +551,7 @@ def validate_credentials(access_token: str, schema_name: str | None = None) -> t
return False, "Missing Intercom access token"
try:
- response = _make_intercom_session(access_token).get(
+ response = _make_intercom_session(access_token, api_version).get(
f"{INTERCOM_API_BASE}/me",
timeout=10,
)
@@ -570,6 +574,7 @@ def intercom_source(
endpoint: str,
team_id: int,
job_id: str,
+ api_version: str,
should_use_incremental_field: bool = False,
incremental_field: str | None = None,
db_incremental_field_last_value: Optional[Any] = None,
@@ -581,13 +586,13 @@ def intercom_source(
# One session built here is reused across the parent walk and every
# per-row child fetch, so urllib3 keeps the connection alive instead
# of re-handshaking per request.
- session = _make_intercom_session(access_token)
+ session = _make_intercom_session(access_token, api_version)
items = lambda: _substream_items(session, endpoint, incremental_field, db_incremental_field_last_value)
elif cfg.paginator_kind == "scroll":
# The Scroll API doesn't fit the framework paginators (the cursor is a
# `scroll_param`, not a request mutation), so `companies` walks it with a
# custom iterator. One session is reused across the whole scroll walk.
- session = _make_intercom_session(access_token)
+ session = _make_intercom_session(access_token, api_version)
items = lambda: _iter_companies(session)
else:
config: RESTAPIConfig = {
@@ -597,7 +602,7 @@ def intercom_source(
"type": "bearer",
"token": access_token,
},
- "headers": _default_headers(),
+ "headers": _default_headers(api_version),
},
"resource_defaults": {},
"resources": [
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/source.py
index bd71f6f41618..30efd7b91eb3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/source.py
@@ -36,8 +36,8 @@ from products.warehouse_sources.backend.types import ExternalDataSourceType
@SourceRegistry.register
class IntercomSource(SimpleSource[IntercomSourceConfig], OAuthMixin):
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
- supported_versions = ("2.13",)
- default_version = "2.13"
+ supported_versions = ("2.13", "2.15")
+ default_version = "2.15"
api_docs_url = "https://developers.intercom.com/docs/references/rest-api"
@property
@@ -132,7 +132,11 @@ class IntercomSource(SimpleSource[IntercomSourceConfig], OAuthMixin):
if not integration.access_token:
return False, "Intercom integration has no access token. Please reconnect."
- return validate_intercom_credentials(integration.access_token, schema_name=schema_name)
+ # Probe under the source's resolved pin so a 2.13-pinned source validates against the
+ # version it syncs on; `None` (pre-creation) resolves to `default_version`.
+ return validate_intercom_credentials(
+ integration.access_token, schema_name=schema_name, api_version=self.resolve_api_version(api_version)
+ )
def source_for_pipeline(self, config: IntercomSourceConfig, inputs: SourceInputs) -> SourceResponse:
integration = self.get_oauth_integration(config.intercom_integration_id, inputs.team_id)
@@ -145,6 +149,7 @@ class IntercomSource(SimpleSource[IntercomSourceConfig], OAuthMixin):
endpoint=inputs.schema_name,
team_id=inputs.team_id,
job_id=inputs.job_id,
+ api_version=self.resolve_api_version(inputs.api_version),
should_use_incremental_field=inputs.should_use_incremental_field,
incremental_field=inputs.incremental_field if inputs.should_use_incremental_field else None,
db_incremental_field_last_value=inputs.db_incremental_field_last_value
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/tests/test_intercom.py b/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/tests/test_intercom.py
index 85b57334aca1..eb1879834ed4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/tests/test_intercom.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/tests/test_intercom.py
@@ -35,6 +35,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.intercom.i
validate_credentials,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.intercom.settings import INTERCOM_ENDPOINTS
+from products.warehouse_sources.backend.temporal.data_imports.sources.intercom.source import IntercomSource
def _make_response(json_body: Any, status_code: int = 200, text: str = "") -> Response:
@@ -652,7 +653,7 @@ class TestSubstreamSessionRetries:
# read timeout on those calls would propagate unretried (unlike the GETs in
# the same walk). These POSTs are read-only/idempotent, so the session must
# retry them on transient read timeouts and 429/5xx.
- session = _make_intercom_session("token")
+ session = _make_intercom_session("token", "2.13")
retry = cast(HTTPAdapter, session.get_adapter(INTERCOM_API_BASE)).max_retries
allowed_methods = cast("frozenset[str]", retry.allowed_methods)
@@ -676,6 +677,7 @@ class TestIntercomSource:
endpoint=endpoint,
team_id=1,
job_id="job-1",
+ api_version="2.15",
)
assert response.name == endpoint
@@ -696,10 +698,46 @@ class TestIntercomSource:
]
with mock.patch.object(intercom_module, "make_tracked_session", return_value=mock_session):
- response = intercom_source(access_token="token", endpoint="companies", team_id=1, job_id="job-1")
+ response = intercom_source(
+ access_token="token", endpoint="companies", team_id=1, job_id="job-1", api_version="2.15"
+ )
# `items()` is typed `Iterable | AsyncIterable`; the scroll path yields a sync iterator.
companies = list(cast(Iterable[dict[str, Any]], response.items()))
assert [c["id"] for c in companies] == ["co1"]
urls = [call.args[0] for call in mock_session.get.call_args_list]
assert urls and all(url.endswith("/companies/scroll") for url in urls)
+
+
+class TestVersionDispatch:
+ # The resolved pin must reach the wire as the `Intercom-Version` header for every
+ # supported version, on both request paths (session-based scroll/substream, and the
+ # framework REST path). Parameterizing over the source's declared versions keeps the
+ # coverage honest when a version is added.
+ @pytest.mark.parametrize("api_version", IntercomSource.supported_versions)
+ def test_session_path_sends_pinned_version_header(self, api_version: str):
+ captured: dict[str, Any] = {}
+
+ def fake_session(headers: dict[str, str], retry: Any) -> mock.MagicMock:
+ captured["headers"] = headers
+ session = mock.MagicMock()
+ session.get.return_value = _make_response({"data": [], "scroll_param": None})
+ return session
+
+ with mock.patch.object(intercom_module, "make_tracked_session", side_effect=fake_session):
+ response = intercom_source(
+ access_token="token", endpoint="companies", team_id=1, job_id="job-1", api_version=api_version
+ )
+ list(cast(Iterable[dict[str, Any]], response.items()))
+
+ assert captured["headers"]["Intercom-Version"] == api_version
+
+ @pytest.mark.parametrize("api_version", IntercomSource.supported_versions)
+ def test_rest_path_sends_pinned_version_header(self, api_version: str):
+ with mock.patch.object(intercom_module, "rest_api_resource", return_value=object()) as mock_rest:
+ intercom_source(
+ access_token="token", endpoint="contacts", team_id=1, job_id="job-1", api_version=api_version
+ )
+
+ config = mock_rest.call_args.args[0]
+ assert config["client"]["headers"]["Intercom-Version"] == api_version
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/tests/test_intercom_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/tests/test_intercom_source.py
index 0a77732fde68..8cce77e8b1e1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/tests/test_intercom_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/intercom/tests/test_intercom_source.py
@@ -22,6 +22,31 @@ class TestIntercomSource:
def test_source_type(self):
assert self.source.source_type == ExternalDataSourceType.INTERCOM
+ def test_default_version_is_latest(self):
+ # New sources are stamped with the default; keep it on the newest supported version.
+ assert self.source.default_version == "2.15"
+ assert self.source.default_version in self.source.supported_versions
+
+ @pytest.mark.parametrize("pinned,expected", [(None, "2.15"), ("", "2.15"), ("2.13", "2.13"), ("2.15", "2.15")])
+ @mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.intercom.source.intercom_source")
+ @mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.intercom.source.IntercomSource.get_oauth_integration"
+ )
+ def test_source_for_pipeline_resolves_version(self, mock_get_integration, mock_intercom_source, pinned, expected):
+ mock_get_integration.return_value = mock.MagicMock(access_token="token")
+
+ inputs = mock.MagicMock()
+ inputs.team_id = self.team_id
+ inputs.job_id = "job-1"
+ inputs.schema_name = "contacts"
+ inputs.api_version = pinned
+ inputs.should_use_incremental_field = False
+
+ self.source.source_for_pipeline(self.config, inputs)
+
+ _, kwargs = mock_intercom_source.call_args
+ assert kwargs["api_version"] == expected
+
def test_get_source_config(self):
config = self.source.get_source_config
@@ -110,7 +135,25 @@ class TestIntercomSource:
assert is_valid is True
assert error is None
mock_get_integration.assert_called_once_with(self.config.intercom_integration_id, self.team_id)
- mock_validate.assert_called_once_with("token", schema_name=None)
+ # No pin passed (pre-creation), so the probe runs on default_version.
+ mock_validate.assert_called_once_with("token", schema_name=None, api_version="2.15")
+
+ @pytest.mark.parametrize("pin,expected", [("2.13", "2.13"), ("2.15", "2.15"), (None, "2.15")])
+ @mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.intercom.source.validate_intercom_credentials"
+ )
+ @mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.intercom.source.IntercomSource.get_oauth_integration"
+ )
+ def test_validate_credentials_probes_under_resolved_pin(self, mock_get_integration, mock_validate, pin, expected):
+ # The probe must run on the source's pin, not a hardcoded version — otherwise a
+ # 2.13-pinned source validates against 2.15 and can report a scope it doesn't have.
+ mock_get_integration.return_value = mock.MagicMock(access_token="token")
+ mock_validate.return_value = (True, None)
+
+ self.source.validate_credentials(self.config, self.team_id, api_version=pin)
+
+ assert mock_validate.call_args.kwargs["api_version"] == expected
@mock.patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.intercom.source.IntercomSource.get_oauth_integration"
@@ -147,6 +190,7 @@ class TestIntercomSource:
inputs.team_id = self.team_id
inputs.job_id = "job-1"
inputs.schema_name = "contacts"
+ inputs.api_version = "2.13"
inputs.should_use_incremental_field = True
inputs.incremental_field = "updated_at"
inputs.db_incremental_field_last_value = "1700000000"
@@ -159,6 +203,7 @@ class TestIntercomSource:
endpoint="contacts",
team_id=self.team_id,
job_id="job-1",
+ api_version="2.13",
should_use_incremental_field=True,
incremental_field="updated_at",
db_incremental_field_last_value="1700000000",