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(zendesk): drop since from the activities pagination cursor (#109919)

Tom Owersf0d75c0aa3d2
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/zendesk/tests/test_zendesk.py b/products/warehouse_sources/backend/temporal/data_imports/sources/zendesk/tests/test_zendesk.py
index 8a02ec116c49..9eaa013f9dc3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/zendesk/tests/test_zendesk.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/zendesk/tests/test_zendesk.py
@@ -38,6 +38,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.zendesk.ze
ZendeskCursorIncrementalPaginator,
ZendeskIncrementalEndpointPaginator,
ZendeskResumeConfig,
+ ZendeskSinceCursorPaginator,
get_declarative_resource,
get_resource,
normalize_subdomain,
@@ -488,6 +489,48 @@ class TestZendeskAfterUrlPaginator:
assert p.has_next_page is False
+class TestZendeskSinceCursorPaginator:
+ def test_drops_since_from_the_next_page_url(self) -> None:
+ # Zendesk echoes `since` back into `links.next` in its own format, which it then
+ # rejects on the next request (400). The cursor alone is enough to continue.
+ p = ZendeskSinceCursorPaginator(next_url_path="links.next")
+ next_url = (
+ f"{BASE}/api/v2/activities.json?page%5Bafter%5D=abc123&page%5Bsize%5D=100&since=1970-01-01+00%3A00%3A00+UTC"
+ )
+
+ p.update_state(_make_response({"activities": [{"id": 1}], "links": {"next": next_url}}))
+
+ req = Request(method="GET", url=f"{BASE}/api/v2/activities.json")
+ req.params = {"page[size]": 100, "since": "1970-01-01T00:00:00Z"}
+ p.update_request(req)
+
+ assert "since" not in req.url
+ assert "page%5Bafter%5D=abc123" in req.url
+ assert req.params == {}
+
+ def test_endpoint_wiring_uses_the_since_cursor_paginator(self) -> None:
+ endpoint_config = _endpoint(get_resource("activities", should_use_incremental_field=True))
+ assert isinstance(endpoint_config["paginator"], ZendeskSinceCursorPaginator)
+
+ def test_drops_since_from_a_restored_checkpoint(self) -> None:
+ # A run that already failed on this URL checkpointed it with `since` still attached —
+ # that's the exact failure this paginator exists to fix. A retry must not resume
+ # straight back into the same URL, or it hits the same 400 forever.
+ p = ZendeskSinceCursorPaginator(next_url_path="links.next")
+ dirty_next_url = (
+ f"{BASE}/api/v2/activities.json?page%5Bafter%5D=abc123&page%5Bsize%5D=100&since=1970-01-01+00%3A00%3A00+UTC"
+ )
+
+ p.set_resume_state({"next_url": dirty_next_url})
+
+ req = Request(method="GET", url=f"{BASE}/api/v2/activities.json")
+ req.params = {"page[size]": 100, "since": "1970-01-01T00:00:00Z"}
+ p.init_request(req)
+
+ assert "since" not in req.url
+ assert "page%5Bafter%5D=abc123" in req.url
+
+
class TestZendeskDeclarativeIncremental:
def test_activities_uses_the_server_side_since_filter(self) -> None:
endpoint_config = _endpoint(get_resource("activities", should_use_incremental_field=True))
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/zendesk/zendesk.py b/products/warehouse_sources/backend/temporal/data_imports/sources/zendesk/zendesk.py
index 227eb9226bac..1f1d082bbf67 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/zendesk/zendesk.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/zendesk/zendesk.py
@@ -4,6 +4,7 @@ import dataclasses
from collections.abc import Callable
from datetime import UTC, datetime
from typing import Any, Optional, cast
+from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
from requests import Request, Response
from requests.exceptions import HTTPError
@@ -137,6 +138,8 @@ def paginator_for(config: ZendeskEndpointConfig) -> BasePaginator:
return SinglePagePaginator()
if config.next_url_path == "after_url":
return ZendeskAfterUrlPaginator()
+ if config.incremental_start_param is not None:
+ return ZendeskSinceCursorPaginator(next_url_path=config.next_url_path)
return JSONLinkPaginator(next_url_path=config.next_url_path)
@@ -532,6 +535,37 @@ class ZendeskIncrementalEndpointPaginator(BasePaginator):
self._has_next_page = True
+def _without_query_param(url: str, name: str) -> str:
+ parts = urlsplit(url)
+ kept = [(key, value) for key, value in parse_qsl(parts.query, keep_blank_values=True) if key != name]
+ return urlunsplit(parts._replace(query=urlencode(kept)))
+
+
+class ZendeskSinceCursorPaginator(JSONLinkPaginator):
+ """Cursor pagination for a declarative endpoint that also takes a server-side `since` filter
+ (currently only `activities`).
+
+ Zendesk echoes the request's `since` back into `links.next`, but re-serialized in its own
+ `YYYY-MM-DD HH:MM:SS UTC` format rather than the ISO 8601 this source sends — and then
+ rejects that format with a 400 on the next request. The cursor alone already encodes the
+ stream position, so continuing pages don't need `since` at all; drop it from the next-page
+ URL before following it.
+ """
+
+ def update_state(self, response: Response, data: Optional[list[Any]] = None) -> None:
+ super().update_state(response, data)
+ if self._next_url:
+ self._next_url = _without_query_param(self._next_url, "since")
+
+ def set_resume_state(self, state: dict[str, Any]) -> None:
+ # A run that already failed on this URL checkpointed it with `since` still attached
+ # (that's the exact failure this paginator exists to fix) — clean it here too, or a
+ # retry just resumes straight back into the same 400.
+ super().set_resume_state(state)
+ if self._next_url:
+ self._next_url = _without_query_param(self._next_url, "since")
+
+
class ZendeskAfterUrlPaginator(JSONLinkPaginator):
"""Cursor pagination for `/api/v2/ticket_audits`, which returns its next-page link as
`after_url` rather than the `links.next` the newer list endpoints use.