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(warehouse_sources): support Mailgun API version v4 (#86332)

Tom Owersf120460f5282
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/source.py
index 043aa5e092c8..753e5f4c34e7 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/source.py
@@ -62,8 +62,14 @@ class MailgunSource(
):
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
- supported_versions = ("v3",)
- default_version = "v3"
+ # Mailgun versions each resource in its own URL path (no account or header version), so the
+ # per-endpoint paths in settings.py already target the newest route Mailgun serves for each
+ # resource — /v4 for the domains listing, /v3 everywhere v4 doesn't exist. The source-level
+ # label is therefore a pin recorded on the source, not a request-layer branch: every supported
+ # version resolves to the same requests. Defaulting to v4 only stamps new sources; pinned v3
+ # rows are byte-for-byte unaffected.
+ supported_versions = ("v3", "v4")
+ default_version = "v4"
api_docs_url = "https://documentation.mailgun.com/"
@property
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun_source.py
index ceccfc590fd4..61085ba2915f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun_source.py
@@ -1,3 +1,7 @@
+from collections.abc import Iterable
+from typing import Any, cast
+from urllib.parse import urlsplit
+
import pytest
from unittest import mock
@@ -26,6 +30,15 @@ from products.warehouse_sources.backend.types import ExternalDataSourceType
WEBHOOK_URL = "https://us.posthog.com/public/webhooks/abc"
+def _response(payload: dict[str, Any]) -> mock.MagicMock:
+ response = mock.MagicMock()
+ response.json.return_value = payload
+ response.status_code = 200
+ response.ok = True
+ response.headers = {}
+ return response
+
+
class TestMailgunSource:
def setup_method(self):
self.source = MailgunSource()
@@ -135,6 +148,53 @@ class TestMailgunSource:
assert error_message == expected_message
mock_validate.assert_called_once_with(self.config.api_key, self.config.region)
+ def test_version_declaration_defaults_to_v4_with_v3_supported(self):
+ # Mailgun versions each resource by URL path; both labels resolve to the same requests
+ # (see test_request_paths_are_version_independent), so the default tracks the newest label.
+ assert self.source.supported_versions == ("v3", "v4")
+ assert self.source.default_version == "v4"
+ assert self.source.deprecated_versions == ()
+
+ @pytest.mark.parametrize("pinned_version", [None, "v3", "v4"])
+ @pytest.mark.parametrize(
+ "endpoint, expected_paths",
+ [
+ # domains lists at /v4 under every pin; the resources with no v4 route stay /v3.
+ ("domains", ["/v4/domains"]),
+ ("events", ["/v4/domains", "/v3/a.com/events"]),
+ ("bounces", ["/v4/domains", "/v3/a.com/bounces"]),
+ ("mailing_lists", ["/v3/lists/pages"]),
+ ],
+ )
+ @mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mailgun.mailgun.make_tracked_session")
+ def test_request_paths_are_version_independent(self, mock_session, endpoint, expected_paths, pinned_version):
+ # The pin is declaration-only: a v3 and a v4 source must hit the exact same URLs, so a
+ # future accidental per-version URL branch (or a repin silently switching a table's route)
+ # would fail here.
+ requests_made: list[str] = []
+
+ def fake_get(url, **kwargs):
+ requests_made.append(url)
+ if "/v4/domains" in url:
+ return _response({"items": [{"name": "a.com"}]})
+ return _response({"items": [], "paging": {}})
+
+ mock_session.return_value.get.side_effect = fake_get
+
+ inputs = mock.MagicMock()
+ inputs.schema_name = endpoint
+ inputs.api_version = pinned_version
+ inputs.should_use_incremental_field = False
+ inputs.db_incremental_field_last_value = None
+ inputs.incremental_field = None
+ manager = mock.MagicMock()
+ manager.can_resume.return_value = False
+
+ response = self.source.source_for_pipeline(self.config, manager, inputs)
+ list(cast(Iterable[Any], response.items()))
+
+ assert [urlsplit(url).path for url in requests_made] == expected_paths
+
def test_get_resumable_source_manager_binds_resume_config(self):
inputs = mock.MagicMock()
manager = self.source.get_resumable_source_manager(inputs)