: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(gitlab): surface actionable error when token scope is rejected (#72123)
Tom Owersb40ac99661b3
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/gitlab/gitlab.py b/products/warehouse_sources/backend/temporal/data_imports/sources/gitlab/gitlab.pyindex cdf7d32a48e6..f66869cdf689 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/gitlab/gitlab.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/gitlab/gitlab.py@@ -233,11 +233,26 @@ def validate_credentials(if response.status_code == 404:return False, f"Project '{project}' not found or not accessible with this token"+ if response.status_code == 403:+ # GitLab returns a raw OAuth-style JSON body (e.g. {"error":"insufficient_scope",...}) here,+ # which has no "message" field and is meaningless to the user. Guide them to the scope needed.+ return False, (+ "Your GitLab personal access token doesn't have the required scope. Create a token with "+ "the 'read_api' scope (or 'api') and access to this project, then try again."+ )++ # Surface GitLab's own error text only when it's a plain string; otherwise a generic message, so+ # we never echo a raw JSON body (which can embed the request context) back to the user.try:- body = response.json()- return False, body.get("message", response.text)+ message = response.json().get("message")except Exception:- return False, response.text+ message = None+ if isinstance(message, str) and message.strip():+ return False, message+ return False, (+ f"GitLab rejected the connection (HTTP {response.status_code}). "+ "Please check your GitLab host, token, and project, then try again."+ )def _parse_retry_after(response: requests.Response) -> float | None:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/gitlab/tests/test_gitlab.py b/products/warehouse_sources/backend/temporal/data_imports/sources/gitlab/tests/test_gitlab.pyindex 1d7f74afe968..7338f8855a3a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/gitlab/tests/test_gitlab.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/gitlab/tests/test_gitlab.py@@ -251,6 +251,23 @@ class TestValidateCredentials:else:assert expected_msg_substr in (msg or "")+ def test_403_maps_to_scope_guidance_without_echoing_raw_body(self):+ # GitLab answers an under-scoped token with a raw OAuth JSON body that has no "message" field;+ # it must be replaced with actionable scope guidance, never echoed back verbatim.+ body = {"error": "insufficient_scope", "error_description": "requires higher privileges"}+ with self._patch_session(_response(status_code=403, json_data=body, text=str(body))):+ valid, msg = validate_credentials("https://gitlab.com", "tok", "group/project")+ assert valid is False+ assert "read_api" in (msg or "")+ assert "insufficient_scope" not in (msg or "")++ def test_unmapped_error_does_not_echo_raw_response_body(self):+ with self._patch_session(_response(status_code=500, json_data={}, text="<html>internal details</html>")):+ valid, msg = validate_credentials("https://gitlab.com", "tok", "group/project")+ assert valid is False+ assert "internal details" not in (msg or "")+ assert "HTTP 500" in (msg or "")+@pytest.mark.parametrize("host, token, project, expected_msg",[