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(bing-webmaster-tools): suggest the verified site for a bare host (#83491)

Tom Owers4c26af41053f
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/bing_webmaster_tools.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/bing_webmaster_tools.py
index 6dac46cf0a6a..04e9cf63251a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/bing_webmaster_tools.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/bing_webmaster_tools.py
@@ -2,6 +2,7 @@ import re
import datetime as dt
from collections.abc import Iterator
from typing import Any
+from urllib.parse import urlparse
import requests
from structlog.types import FilteringBoundLogger
@@ -127,6 +128,25 @@ def _site_key(url: str) -> str:
return url.strip().rstrip("/").lower()
+def suggest_verified_site(filter_url: str, verified: list[str]) -> str | None:
+ """The verified site a bare-hostname filter entry most likely meant, else None.
+
+ Bing lists every site with a scheme (e.g. ``https://example.com/``), so an entry that omits the
+ scheme (``example.com``) matches nothing on the scheme-sensitive site key even when that host is
+ verified. When the entry is a bare hostname, point at the verified site sharing that host so the
+ "not verified" error can name the exact value to paste instead of dead-ending."""
+ stripped = filter_url.strip()
+ if not stripped or "://" in stripped:
+ return None
+ host = stripped.strip("/").lower()
+ if not host:
+ return None
+ for url in verified:
+ if urlparse(url).netloc.lower() == host:
+ return url
+ return None
+
+
def parse_site_urls(raw: str | None) -> list[str]:
"""Parse the optional site filter field: one URL per line, blanks skipped, duplicates dropped."""
if not raw:
@@ -166,8 +186,16 @@ def select_site_urls(sites: list[dict[str, Any]], site_url_filters: list[str]) -
else:
selected.append(matched)
if missing:
+ suggestions = {
+ entry: match for entry in missing if (match := suggest_verified_site(entry, verified)) is not None
+ }
+ hint = ""
+ if suggestions:
+ pairs = "; ".join(f"'{entered}' is verified as '{match}'" for entered, match in suggestions.items())
+ hint = f"Bing lists sites with their full URL, so {pairs}. "
raise ValueError(
f"These site URLs are not verified sites on the connected account: {', '.join(missing)}. "
+ f"{hint}"
"Enter each site exactly as Bing Webmaster Tools lists it, or clear the field to sync "
"every verified site."
)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/tests/test_bing_webmaster_tools.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/tests/test_bing_webmaster_tools.py
index d53df6b021b0..5f8541ecea2c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/tests/test_bing_webmaster_tools.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/tests/test_bing_webmaster_tools.py
@@ -17,6 +17,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.bing_webma
parse_site_urls,
parse_wcf_date,
select_site_urls,
+ suggest_verified_site,
validate_credentials,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.bing_webmaster_tools.settings import (
@@ -168,6 +169,27 @@ class TestSiteSelection:
with pytest.raises(ValueError, match="not verified sites on the connected account"):
select_site_urls(_SITES, ["http://unverified.example.org"])
+ @pytest.mark.parametrize(
+ "filter_url,expected",
+ [
+ # A bare hostname can never match Bing's scheme-prefixed site key, so it resolves to the
+ # verified site sharing that host, with or without a trailing slash and regardless of case.
+ ("example.com", "https://example.com/"),
+ ("example.com/", "https://example.com/"),
+ ("EXAMPLE.COM", "https://example.com/"),
+ # An entry that already carries a scheme is matched exactly, so no suggestion is offered.
+ ("https://example.com/", None),
+ # A host that isn't verified has nothing to suggest.
+ ("unknown.example.net", None),
+ ],
+ )
+ def test_suggest_verified_site(self, filter_url, expected):
+ assert suggest_verified_site(filter_url, ["https://example.com/"]) == expected
+
+ def test_bare_hostname_error_names_the_verified_form(self):
+ with pytest.raises(ValueError, match=r"'example.com' is verified as 'https://example.com/'"):
+ select_site_urls(_SITES, ["example.com"])
+
class TestRequest:
def test_returns_unwrapped_list(self):