: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(google-sheets): give an actionable error for a non-Sheets URL (#78733)
Tom Owers5cde71a01c7a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.pyindex a6f8300043e0..af3b88bcc6e1 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py@@ -143,6 +143,15 @@ class GoogleSheetsSource(SimpleSource[GoogleSheetsSourceConfig]):try:client.open_by_url(config.spreadsheet_url)return True, None+ except gspread.exceptions.NoValidUrlKeyFound:+ # gspread couldn't find a spreadsheet key in the value — it isn't a Sheets URL. Its+ # str() is empty, so the generic fallback below would surface a bare "Invalid+ # credentials" for what's really a URL-format problem. Guide the user instead.+ return (+ False,+ "That doesn't look like a Google Sheets URL. Paste the full URL of your sheet, "+ "for example https://docs.google.com/spreadsheets/d/<id>/edit.",+ )except gspread.SpreadsheetNotFound:return False, "Spreadsheet not found at URL provided"except PermissionError:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.pyindex cd11bc0515f5..216c0737541a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py@@ -676,6 +676,20 @@ def test_validate_credentials_maps_api_error_to_friendly_message(api_message, exassert "APIError" not in (error_message or "")+def test_validate_credentials_rejects_non_sheets_url_with_actionable_message():+ with mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.source.google_sheets_client"+ ) as mock_client:+ # gspread raises this (with an empty str()) when the value has no spreadsheet key.+ mock_client.return_value.open_by_url.side_effect = gspread.exceptions.NoValidUrlKeyFound()+ config = GoogleSheetsSourceConfig(spreadsheet_url="https://example.com/not-a-sheet")+ is_valid, error_message = GoogleSheetsSource().validate_credentials(config, team_id=1)++ assert is_valid is False+ # The empty str() would otherwise surface as a bare "Invalid credentials"; point at the URL.+ assert "Google Sheets URL" in (error_message or "")++def test_validate_credentials_permission_denied_names_service_account(settings):settings.GOOGLE_SHEETS_SERVICE_ACCOUNT_CLIENT_EMAIL = "[email protected]"with mock.patch(