: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
feat(integrations): route gitlab calls through egress and add merge request methods (#123102)
Dylan Martin0c955096b3f9
diff --git a/posthog/api/integration.py b/posthog/api/integration.pyindex 3199d1155d26..9722ae246c84 100644--- a/posthog/api/integration.py+++ b/posthog/api/integration.py@@ -115,6 +115,7 @@ from posthog.models.integration import (GitHubIntegration,GitHubIntegrationError,GitLabIntegration,+ GitLabIntegrationError,GoogleAdsIntegration,GoogleCloudIntegration,GoogleCloudServiceAccountIntegration,@@ -1077,9 +1078,12 @@ class IntegrationSerializer(serializers.ModelSerializer, UserAccessControlSerialproject_id = config.get("project_id")project_access_token = config.get("project_access_token")- instance = GitLabIntegration.create_integration(- hostname, project_id, project_access_token, team_id, request.user- )+ try:+ instance = GitLabIntegration.create_integration(+ hostname, project_id, project_access_token, team_id, request.user+ )+ except GitLabIntegrationError as e:+ raise ValidationError(str(e))return instanceelif validated_data["kind"] == "anthropic":diff --git a/posthog/api/test/test_integration.py b/posthog/api/test/test_integration.pyindex 90168fcb3821..0648db0871ba 100644--- a/posthog/api/test/test_integration.py+++ b/posthog/api/test/test_integration.py@@ -974,6 +974,43 @@ class TestDatabricksIntegration:assert not Integration.objects.filter(team=self.team, kind="databricks").exists()+class TestGitLabIntegration:+ @pytest.fixture(autouse=True)+ def setup_integration(self, db):+ self.organization = Organization.objects.create(name="Test Org")+ self.team = Team.objects.create(organization=self.organization, name="Test Team")+ self.user = User.objects.create_and_join(+ self.organization, "[email protected]", "test", level=OrganizationMembership.Level.ADMIN+ )++ @patch("requests.request")+ @patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))+ def test_create_returns_400_when_the_host_does_not_return_json(+ self, _mock_is_url_allowed, mock_request, client: HttpClient+ ):+ mock_request.return_value = MagicMock(+ status_code=200, **{"json.side_effect": requests.JSONDecodeError("Expecting value", "", 0)}+ )+ client.force_login(self.user)++ response = client.post(+ f"/api/environments/{self.team.pk}/integrations",+ {+ "kind": "gitlab",+ "config": {+ "hostname": "https://gitlab.example.com",+ "project_id": "1",+ "project_access_token": "token123",+ },+ },+ content_type="application/json",+ )++ assert response.status_code == status.HTTP_400_BAD_REQUEST+ assert "isn't JSON" in response.json()["detail"]+ assert not Integration.objects.filter(team=self.team, kind="gitlab").exists()++class TestGoogleCloudServiceAccountIntegration:@pytest.fixture(autouse=True)def setup_integration(self, db):diff --git a/posthog/egress/gitlab/README.md b/posthog/egress/gitlab/README.mdnew file mode 100644index 000000000000..7f5c73c192a5--- /dev/null+++ b/posthog/egress/gitlab/README.md@@ -0,0 +1,58 @@+# GitLab egress++Calls to the GitLab REST API (`/api/v4`) on gitlab.com and on self-managed instances.++## Identity++A hash of the instance host and the access token, built by `token_scope` in `transport.py`.+GitLab meters authenticated API traffic per user, and each token belongs to one user (a bot user for project and group access tokens).+The token is a secret and the scope is a metric label, so both parts go through `scope_fingerprint`.+A user with several tokens shows as several scopes. GitLab does not tell us the user id without an extra call.++Integration setup is the exception and passes `scoped=False`.+It probes GitLab with a token the caller just typed in, which PostHog does not store unless the probe succeeds.+A scope retains its counter and gauge children for the life of the process, so scoping that probe would let repeated setup attempts with rejected tokens grow the registry without a bound.+The call still records the counter under an empty scope; the gauges are skipped.++## Budget++None: calls are recorded, never gated (`RecordedEgressClient`).+The customer owns the token on their own GitLab account or instance.+The admin of each self-managed instance sets its limits, and gitlab.com limits depend on the plan, so PostHog has no single limit to stay under.+The transport returns `429` responses without retries or delays.+The integration does not use `Retry-After`.+Add a gate if one token starts to carry enough PostHog traffic to reach the gitlab.com limit.++## Lanes and callers++No lanes, because nothing is gated.+`posthog/models/integration/gitlab.py` is the caller, with `source="integration"`.+It creates, closes and searches issues, lists project members, lists projects and creates and reads merge requests.+Error tracking code search in `products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py` still calls GitLab directly and has not moved here yet.++## Rate-limit headers++GitLab sends these on every response, throttled or not:++- `RateLimit-Limit` feeds `gitlab_api_rate_limit_limit`.+- `RateLimit-Remaining` feeds `gitlab_api_rate_limit_remaining`.+- `RateLimit-Reset`, a Unix timestamp, feeds `gitlab_api_rate_limit_reset_timestamp_seconds`.+- `RateLimit-Name`, the throttle that applies, is the `resource` label. A value outside GitLab's documented throttle names records as `unknown`, because a self-managed instance controls the header.++The headers show only the most restrictive Rack::Attack throttle.+Application limits, such as issue creation, can answer `429` while the headers still show quota.+Throttled responses from the Projects, Groups and Users APIs carry no informational headers.+The counter is `gitlab_api_requests_total`, labeled `scope, method, endpoint, status_code, source`.+Callers pass an endpoint template, because a project path in a URL is customer data.++## Auth++The customer's access token in the `PRIVATE-TOKEN` header.+No PostHog-owned credential.+The caller validates the host before the call and never follows a redirect, because the token header would go to the redirect target.++## Sources++- [GitLab.com rate limits](https://docs.gitlab.com/user/gitlab_com/rate_limits/): authenticated API traffic for a user is limited per minute, plan-based limits are proposed, and all responses carry `RateLimit-Limit` and `RateLimit-Remaining` except throttled responses from the Projects, Groups and Users APIs.+- [User and IP rate limits, response headers](https://docs.gitlab.com/administration/settings/user_and_ip_rate_limits/#response-headers): `RateLimit-Limit`, `RateLimit-Name`, `RateLimit-Observed`, `RateLimit-Remaining`, `RateLimit-Reset` as Unix time, and `RateLimit-ResetTime` and `Retry-After` on a `429`. Only the most restrictive Rack::Attack limit appears.+- [Rate limits](https://docs.gitlab.com/rate_limits/): self-managed admins set most limits in the Admin area.diff --git a/posthog/egress/gitlab/__init__.py b/posthog/egress/gitlab/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/posthog/egress/gitlab/observability.py b/posthog/egress/gitlab/observability.pynew file mode 100644index 000000000000..eadd646fe954--- /dev/null+++ b/posthog/egress/gitlab/observability.py@@ -0,0 +1,82 @@+"""GitLab egress telemetry.++GitLab documents ``RateLimit-Limit``, ``RateLimit-Remaining``, ``RateLimit-Reset`` (Unix time) and+``RateLimit-Name`` on every response, throttled or not, so the parser reads only those. The headers+show only the most restrictive Rack::Attack throttle; application limits can still answer 429.+"""++from collections.abc import Mapping++from prometheus_client import Counter, Gauge++from posthog.egress.observability.observability import (+ EgressMetrics,+ EgressObservability,+ RateLimitSnapshot,+ float_header,+)++# GitLab's documented Rack::Attack throttles. A self-managed instance controls the header, so an+# unlisted value records as "unknown" rather than minting a gauge child per value the server sends.+_THROTTLE_NAMES = frozenset(+ {+ "throttle_authenticated_api",+ "throttle_authenticated_deprecated_api",+ "throttle_authenticated_files_api",+ "throttle_authenticated_git_http",+ "throttle_authenticated_git_lfs",+ "throttle_authenticated_mcp",+ "throttle_authenticated_packages_api",+ "throttle_authenticated_protected_paths_api",+ "throttle_authenticated_protected_paths_web",+ "throttle_authenticated_web",+ "throttle_unauthenticated",+ "throttle_unauthenticated_api",+ "throttle_unauthenticated_deprecated_api",+ "throttle_unauthenticated_files_api",+ "throttle_unauthenticated_git_http",+ "throttle_unauthenticated_packages_api",+ "throttle_unauthenticated_protected_paths",+ "throttle_unauthenticated_web",+ }+)++_metrics = EgressMetrics(+ request_counter=Counter(+ "gitlab_api_requests",+ "Number of GitLab API requests made through the GitLab egress client.",+ labelnames=["scope", "method", "endpoint", "status_code", "source"],+ ),+ remaining_gauge=Gauge(+ "gitlab_api_rate_limit_remaining",+ "Most recently observed GitLab rate limit remaining count.",+ labelnames=["scope", "resource"],+ ),+ limit_gauge=Gauge(+ "gitlab_api_rate_limit_limit",+ "Most recently observed GitLab rate limit.",+ labelnames=["scope", "resource"],+ ),+ reset_gauge=Gauge(+ "gitlab_api_rate_limit_reset_timestamp_seconds",+ "Most recently observed GitLab rate limit reset time as a Unix timestamp.",+ labelnames=["scope", "resource"],+ ),+)+++def _throttle_name(headers: Mapping[str, str] | None) -> str:+ name = headers.get("RateLimit-Name") if headers is not None else None+ return name if name in _THROTTLE_NAMES else "unknown"+++def _parse_gitlab_rate_limit(headers: Mapping[str, str] | None, _url: str | None) -> RateLimitSnapshot:+ return RateLimitSnapshot(+ resource=_throttle_name(headers),+ remaining=float_header(headers, "RateLimit-Remaining"),+ limit=float_header(headers, "RateLimit-Limit"),+ reset_at=float_header(headers, "RateLimit-Reset"),+ )+++gitlab_egress = EgressObservability(_metrics, _parse_gitlab_rate_limit)diff --git a/posthog/egress/gitlab/transport.py b/posthog/egress/gitlab/transport.pynew file mode 100644index 000000000000..74dbebd603ba--- /dev/null+++ b/posthog/egress/gitlab/transport.py@@ -0,0 +1,60 @@+"""Recorded transport for GitLab REST API calls, on gitlab.com and on self-managed instances."""++from typing import Any+from urllib.parse import urlsplit++import requests++from posthog.egress.gitlab.observability import gitlab_egress+from posthog.egress.observability.observability import scope_fingerprint+from posthog.egress.transport.transport import RecordedEgressClient+++def token_scope(url: str, token: str) -> str:+ """GitLab meters each authenticated user on each instance, and a token belongs to one user.+ Host and token are hashed together because the token is a secret and the scope is a metric label."""+ return scope_fingerprint(urlsplit(url).hostname or "", token)+++class GitLabClient(RecordedEgressClient):+ """Recorded, never gated. The customer owns the token, and each instance admin sets its own+ limits, so PostHog has no budget it can model. The caller handles error responses."""++ observability = gitlab_egress+ egress_domain = "gitlab"++ def _standard_headers(self) -> dict[str, str]:+ return {"Accept": "application/json"}+++_gitlab_client = GitLabClient()+++def gitlab_request(+ method: str,+ url: str,+ *,+ token: str,+ source: str,+ endpoint: str,+ scoped: bool = True,+ timeout: float | tuple[float, float] | None = None,+ **kwargs: Any,+) -> requests.Response:+ """``endpoint`` is a template such as ``/projects/{id}/issues``. Project paths in a URL are+ customer data, so the raw URL never becomes the label.++ Pass ``scoped=False`` when the token is a candidate PostHog has not stored yet. Each scope+ retains a counter child and three gauge children for the lifetime of the process, so a caller+ that probes with rejected tokens would otherwise grow the metric registry without a bound.+ """+ return _gitlab_client.request(+ method,+ url,+ source=source,+ headers={"PRIVATE-TOKEN": token},+ scope=token_scope(url, token) if scoped else None,+ endpoint=endpoint,+ timeout=timeout,+ **kwargs,+ )diff --git a/posthog/egress/test/test_gitlab_transport.py b/posthog/egress/test/test_gitlab_transport.pynew file mode 100644index 000000000000..93a2785c7797--- /dev/null+++ b/posthog/egress/test/test_gitlab_transport.py@@ -0,0 +1,71 @@+from unittest.mock import patch++from django.test import SimpleTestCase++import requests+from parameterized import parameterized+from prometheus_client import REGISTRY++from posthog.egress.gitlab.transport import gitlab_request, token_scope+from posthog.egress.observability.observability import scope_fingerprint++_TOKEN = "glpat-not-a-real-token"+_URL = "https://gitlab.example.com/api/v4/projects/7/merge_requests"+++def _response(headers: dict[str, str]) -> requests.Response:+ response = requests.Response()+ response.status_code = 201+ response.headers.update(headers)+ return response+++def _gitlab_samples() -> list:+ return [+ sample for metric in REGISTRY.collect() if metric.name.startswith("gitlab_api") for sample in metric.samples+ ]+++class TestGitLabTransport(SimpleTestCase):+ def test_the_token_is_sent_as_a_header_and_never_reaches_a_label(self) -> None:+ labels = {+ "scope": scope_fingerprint("gitlab.example.com", _TOKEN),+ "method": "POST",+ "endpoint": "/api/v4/projects/{id}/merge_requests",+ "status_code": "201",+ "source": "integration",+ }+ before = REGISTRY.get_sample_value("gitlab_api_requests_total", labels) or 0+ with patch("requests.request", return_value=_response({})) as request:+ gitlab_request(+ "POST", _URL, token=_TOKEN, source="integration", endpoint="/api/v4/projects/{id}/merge_requests"+ )++ assert request.call_args.kwargs["headers"]["PRIVATE-TOKEN"] == _TOKEN+ assert REGISTRY.get_sample_value("gitlab_api_requests_total", labels) == before + 1+ assert not any(_TOKEN in value for sample in _gitlab_samples() for value in sample.labels.values())++ @parameterized.expand(+ [+ ("documented_name", "throttle_authenticated_api", "throttle_authenticated_api"),+ # A self-managed instance controls this header, so a value GitLab does not document must+ # not become a label value, whether or not it is shaped like a throttle name.+ ("undocumented_name", "throttle_made_up_by_the_server", "unknown"),+ ("unexpected_name", "Some Proxy/1.0", "unknown"),+ ]+ )+ def test_rate_limit_headers_feed_the_gauges(self, _name: str, throttle: str, resource: str) -> None:+ headers = {+ "RateLimit-Name": throttle,+ "RateLimit-Limit": "2000",+ "RateLimit-Remaining": "1990",+ "RateLimit-Reset": "1609844400",+ }+ with patch("requests.request", return_value=_response(headers)):+ gitlab_request(+ "POST", _URL, token=_TOKEN, source="integration", endpoint="/api/v4/projects/{id}/merge_requests"+ )++ labels = {"scope": token_scope(_URL, _TOKEN), "resource": resource}+ assert REGISTRY.get_sample_value("gitlab_api_rate_limit_remaining", labels) == 1990+ assert REGISTRY.get_sample_value("gitlab_api_rate_limit_reset_timestamp_seconds", labels) == 1609844400diff --git a/posthog/models/integration/gitlab.py b/posthog/models/integration/gitlab.pyindex c72f894c7392..35ca7dec6444 100644--- a/posthog/models/integration/gitlab.py+++ b/posthog/models/integration/gitlab.py@@ -1,11 +1,13 @@"""GitLab integration."""import re-from typing import Any+from typing import Any, Literalfrom urllib.parse import urlencode, urlparseimport requests+from posthog.dataclasses import frozen+from posthog.egress.gitlab.transport import gitlab_requestfrom posthog.integrations.contracts import IntegrationRecordfrom posthog.security.url_validation import is_url_allowed@@ -17,6 +19,26 @@ class GitLabIntegrationError(Exception):pass+# Caps one listing at 1,000 projects so that a token with access to a whole instance stays cheap.+MAX_PROJECT_PAGES = 10+++@frozen+class GitLabProject:+ id: int+ path_with_namespace: str+ web_url: str+ default_branch: str | None+++@frozen+class GitLabMergeRequest:+ iid: int+ web_url: str+ state: Literal["opened", "closed", "locked", "merged"]+ draft: bool++class GitLabIntegration:integration: model.Integration@@ -24,44 +46,76 @@ class GitLabIntegration:def _validate_api_url(url: str) -> None:if urlparse(url).scheme != "https":raise GitLabIntegrationError("Invalid GitLab hostname: HTTPS is required")- allowed, error = is_url_allowed(url)+ allowed, _ = is_url_allowed(url)if not allowed:- raise GitLabIntegrationError(f"Invalid GitLab hostname: {error}")+ # The validator's reason names the IP the hostname resolved to, and setup surfaces this+ # error to the API caller, so the detail stays in the url_validation.blocked log line.+ raise GitLabIntegrationError("Invalid GitLab hostname: it must be a publicly reachable GitLab instance")@staticmethod- def get(hostname: str, endpoint: str, project_access_token: str) -> dict:- url = f"{hostname}/api/v4/{endpoint}"+ def _request(+ method: str, hostname: str, path: str, access_token: str, *, endpoint: str, scoped: bool = True, **kwargs: Any+ ) -> requests.Response:+ """``endpoint`` is the template for the metric label. A project id can be an encoded path+ such as ``group%2Frepo``, so the path itself is customer data and never becomes a label."""+ url = f"{hostname}/api/v4/{path}"GitLabIntegration._validate_api_url(url)-- response = requests.get(+ return gitlab_request(+ method,url,- headers={"PRIVATE-TOKEN": project_access_token},+ token=access_token,+ source="integration",+ endpoint=endpoint,+ scoped=scoped,# disallow redirects to prevent SSRF on redirected hostallow_redirects=False,timeout=10,+ **kwargs,)- return response.json()-@staticmethod- def post(hostname: str, endpoint: str, project_access_token: str, json: dict) -> dict:- url = f"{hostname}/api/v4/{endpoint}"- GitLabIntegration._validate_api_url(url)+ def _json(response: requests.Response) -> Any:+ try:+ return response.json()+ except ValueError as error:+ raise GitLabIntegrationError(+ f"GitLab returned a response that isn't JSON (HTTP {response.status_code}). "+ "Check that the hostname points to a GitLab instance."+ ) from error- response = requests.post(- url,- json=json,- headers={"PRIVATE-TOKEN": project_access_token},- # disallow redirects to prevent SSRF on redirected host- allow_redirects=False,- timeout=10,+ @staticmethod+ def get(hostname: str, path: str, project_access_token: str, *, endpoint: str) -> dict:+ return GitLabIntegration._json(+ GitLabIntegration._request("GET", hostname, path, project_access_token, endpoint=endpoint))- return response.json()+ @staticmethod+ def post(hostname: str, path: str, project_access_token: str, json: dict, *, endpoint: str) -> dict:+ return GitLabIntegration._json(+ GitLabIntegration._request("POST", hostname, path, project_access_token, endpoint=endpoint, json=json)+ )@classmethoddef create_integration(cls, hostname, project_id, project_access_token, team_id, user) -> model.Integration:- project = cls.get(hostname, f"projects/{project_id}", project_access_token)+ try:+ response = cls._request(+ "GET",+ hostname,+ f"projects/{project_id}",+ project_access_token,+ endpoint="/api/v4/projects/{id}",+ # Setup runs on a token the caller supplied and PostHog may never store, so this+ # probe must not mint a metric scope per rejected token.+ scoped=False,+ )+ except requests.RequestException as error:+ raise GitLabIntegrationError("Couldn't connect to GitLab. Check the hostname and try again.") from error+ if response.status_code != 200:+ raise GitLabIntegrationError(+ f"GitLab returned HTTP {response.status_code} for this project. "+ "Check the hostname, project ID, and access token."+ )+ project = cls._json(response)integration = model.Integration.objects.create(team_id=team_id,@@ -102,7 +156,12 @@ class GitLabIntegration:params["query"] = search.strip()query = urlencode(params)# This endpoint answers with a JSON array, unlike the objects `get` is typed for.- members: object = GitLabIntegration.get(hostname, f"projects/{project_id}/members/all?{query}", access_token)+ members: object = GitLabIntegration.get(+ hostname,+ f"projects/{project_id}/members/all?{query}",+ access_token,+ endpoint="/api/v4/projects/{id}/members/all",+ )if not isinstance(members, list):# GitLab reports failures as an object such as {"message": "403 Forbidden"}.error = members.get("message") or members.get("error") if isinstance(members, dict) else None@@ -136,6 +195,7 @@ class GitLabIntegration:f"projects/{project_id}/issues",access_token,payload,+ endpoint="/api/v4/projects/{id}/issues",)return {"issue_id": issue["iid"]}@@ -146,15 +206,13 @@ class GitLabIntegration:project_id = self.integration.config.get("project_id")access_token = self.integration.sensitive_config.get("access_token")- url = f"{hostname}/api/v4/projects/{project_id}/issues/{issue_id}"- self._validate_api_url(url)-- response = requests.put(- url,+ response = self._request(+ "PUT",+ hostname,+ f"projects/{project_id}/issues/{issue_id}",+ access_token,+ endpoint="/api/v4/projects/{id}/issues/{iid}",json={"state_event": "close"},- headers={"PRIVATE-TOKEN": access_token},- allow_redirects=False,- timeout=10,)if response.status_code != 200:raise GitLabIntegrationError(f"GitLabIntegration: failed to close issue: {response.text[:300]}")@@ -163,14 +221,12 @@ class GitLabIntegration:hostname = self.integration.config.get("hostname")project_id = self.integration.config.get("project_id")access_token = self.integration.sensitive_config.get("access_token")- url = f"{hostname}/api/v4/projects/{project_id}/issues/{issue_iid}"- self._validate_api_url(url)-- response = requests.get(- url,- headers={"PRIVATE-TOKEN": access_token},- allow_redirects=False,- timeout=10,+ response = self._request(+ "GET",+ hostname,+ f"projects/{project_id}/issues/{issue_iid}",+ access_token,+ endpoint="/api/v4/projects/{id}/issues/{iid}",)if response.status_code == 404:return None@@ -210,20 +266,18 @@ class GitLabIntegration:hostname = self.integration.config.get("hostname")project_id = self.integration.config.get("project_id")access_token = self.integration.sensitive_config.get("access_token")- url = f"{hostname}/api/v4/projects/{project_id}/issues"- self._validate_api_url(url)-# A blank query lists the project's recent issues instead of filtering.params: dict[str, str | int] = {"per_page": limit, "order_by": "updated_at"}if query.strip():params["search"] = queryparams["in"] = "title"- response = requests.get(- url,- headers={"PRIVATE-TOKEN": access_token},+ response = self._request(+ "GET",+ hostname,+ f"projects/{project_id}/issues",+ access_token,+ endpoint="/api/v4/projects/{id}/issues",params=params,- allow_redirects=False,- timeout=10,)if response.status_code != 200:raise GitLabIntegrationError(@@ -234,3 +288,90 @@ class GitLabIntegration:return []return self._issue_results(issues)++ def list_projects(self, search: str = "") -> list[GitLabProject]:+ """Projects the token can push to, most recently active first."""+ hostname = self.integration.config.get("hostname")+ access_token = self.integration.sensitive_config.get("access_token")++ # Developer (30) is the lowest role that can push a branch.+ params: dict[str, str | int] = {+ "membership": "true",+ "min_access_level": 30,+ "archived": "false",+ "simple": "true",+ "order_by": "last_activity_at",+ "per_page": 100,+ }+ if search.strip():+ params["search"] = search.strip()++ projects: list[GitLabProject] = []+ page = "1"+ for _ in range(MAX_PROJECT_PAGES):+ response = self._request(+ "GET", hostname, "projects", access_token, endpoint="/api/v4/projects", params={**params, "page": page}+ )+ if response.status_code != 200:+ raise GitLabIntegrationError(f"GitLabIntegration: failed to list projects: {response.text[:300]}")+ projects.extend(+ GitLabProject(+ id=project["id"],+ path_with_namespace=project["path_with_namespace"],+ web_url=project["web_url"],+ default_branch=project.get("default_branch"),+ )+ for project in self._json(response)+ )+ page = response.headers.get("X-Next-Page", "")+ if not page:+ break+ return projects++ @staticmethod+ def _merge_request(payload: dict[str, Any]) -> GitLabMergeRequest:+ return GitLabMergeRequest(+ iid=payload["iid"], web_url=payload["web_url"], state=payload["state"], draft=bool(payload.get("draft"))+ )++ def create_merge_request(+ self, project_id: int, *, source_branch: str, target_branch: str, title: str, description: str+ ) -> GitLabMergeRequest:+ """Open a draft merge request. Raises on failure."""+ hostname = self.integration.config.get("hostname")+ access_token = self.integration.sensitive_config.get("access_token")++ response = self._request(+ "POST",+ hostname,+ f"projects/{project_id}/merge_requests",+ access_token,+ endpoint="/api/v4/projects/{id}/merge_requests",+ # GitLab marks a merge request as a draft from the title prefix.+ json={+ "source_branch": source_branch,+ "target_branch": target_branch,+ "title": f"Draft: {title}",+ "description": description,+ },+ )+ if response.status_code != 201:+ raise GitLabIntegrationError(f"GitLabIntegration: failed to create merge request: {response.text[:300]}")+ return self._merge_request(self._json(response))++ def get_merge_request(self, project_id: int, merge_request_iid: int) -> GitLabMergeRequest | None:+ hostname = self.integration.config.get("hostname")+ access_token = self.integration.sensitive_config.get("access_token")++ response = self._request(+ "GET",+ hostname,+ f"projects/{project_id}/merge_requests/{merge_request_iid}",+ access_token,+ endpoint="/api/v4/projects/{id}/merge_requests/{iid}",+ )+ if response.status_code == 404:+ return None+ if response.status_code != 200:+ raise GitLabIntegrationError(f"GitLabIntegration: failed to retrieve merge request: {response.text[:300]}")+ return self._merge_request(self._json(response))diff --git a/posthog/models/test/integration/test_gitlab.py b/posthog/models/test/integration/test_gitlab.pyindex a76d1426a2c1..d7e6f59c7582 100644--- a/posthog/models/test/integration/test_gitlab.py+++ b/posthog/models/test/integration/test_gitlab.py@@ -3,11 +3,13 @@import pytestfrom unittest.mock import MagicMock, patch+import requests+class TestGitLabIntegrationSSRFProtection:"""Test SSRF protections in GitLabIntegration."""- @patch("posthog.models.integration.gitlab.requests.get")+ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed")def test_get_uses_allow_redirects_false(self, mock_is_url_allowed, mock_get):"""GET requests must use allow_redirects=False to prevent redirect-based SSRF bypass."""@@ -16,13 +18,13 @@ class TestGitLabIntegrationSSRFProtection:mock_is_url_allowed.return_value = (True, None)mock_get.return_value.json.return_value = {"data": "test"}- GitLabIntegration.get("https://gitlab.com", "projects/1", "token123")+ GitLabIntegration.get("https://gitlab.com", "projects/1", "token123", endpoint="/api/v4/projects/{id}")mock_get.assert_called_once()call_kwargs = mock_get.call_args.kwargsassert call_kwargs.get("allow_redirects") is False, "GET must use allow_redirects=False for SSRF protection"- @patch("posthog.models.integration.gitlab.requests.post")+ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed")def test_post_uses_allow_redirects_false(self, mock_is_url_allowed, mock_post):"""POST requests must use allow_redirects=False to prevent redirect-based SSRF bypass."""@@ -31,36 +33,48 @@ class TestGitLabIntegrationSSRFProtection:mock_is_url_allowed.return_value = (True, None)mock_post.return_value.json.return_value = {"data": "test"}- GitLabIntegration.post("https://gitlab.com", "projects/1/issues", "token123", {"title": "test"})+ GitLabIntegration.post(+ "https://gitlab.com",+ "projects/1/issues",+ "token123",+ {"title": "test"},+ endpoint="/api/v4/projects/{id}/issues",+ )mock_post.assert_called_once()call_kwargs = mock_post.call_args.kwargsassert call_kwargs.get("allow_redirects") is False, "POST must use allow_redirects=False for SSRF protection"- @patch("posthog.models.integration.gitlab.requests.get")+ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed")def test_get_validates_url_before_request(self, mock_is_url_allowed, mock_get):"""URL validation must happen before the request is made."""from posthog.models.integration import GitLabIntegration, GitLabIntegrationError- mock_is_url_allowed.return_value = (False, "Private IP address not allowed")+ mock_is_url_allowed.return_value = (False, "Disallowed target IP: 10.1.2.3")- with pytest.raises(GitLabIntegrationError, match="Invalid GitLab hostname"):- GitLabIntegration.get("http://192.168.1.1", "projects/1", "token123")+ with pytest.raises(GitLabIntegrationError, match="Invalid GitLab hostname") as rejection:+ GitLabIntegration.get(+ "https://gitlab.internal.example.com", "projects/1", "token123", endpoint="/api/v4/projects/{id}"+ )+ # Setup turns this error into a 400 body, so the resolved address must not travel with it.+ assert "10.1.2.3" not in str(rejection.value)mock_get.assert_not_called()- @patch("posthog.models.integration.gitlab.requests.get")+ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))def test_get_rejects_http_before_sending_credentials(self, _mock_is_url_allowed, mock_get):from posthog.models.integration import GitLabIntegration, GitLabIntegrationErrorwith pytest.raises(GitLabIntegrationError, match="HTTPS is required"):- GitLabIntegration.get("http://gitlab.example.com", "projects/1", "token123")+ GitLabIntegration.get(+ "http://gitlab.example.com", "projects/1", "token123", endpoint="/api/v4/projects/{id}"+ )mock_get.assert_not_called()- @patch("posthog.models.integration.gitlab.requests.post")+ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed")def test_post_validates_url_before_request(self, mock_is_url_allowed, mock_post):"""URL validation must happen before the request is made."""@@ -69,13 +83,58 @@ class TestGitLabIntegrationSSRFProtection:mock_is_url_allowed.return_value = (False, "Private IP address not allowed")with pytest.raises(GitLabIntegrationError, match="Invalid GitLab hostname"):- GitLabIntegration.post("http://192.168.1.1", "projects/1/issues", "token123", {"title": "test"})+ GitLabIntegration.post(+ "http://192.168.1.1",+ "projects/1/issues",+ "token123",+ {"title": "test"},+ endpoint="/api/v4/projects/{id}/issues",+ )mock_post.assert_not_called()class TestGitLabIntegrationModel:- @patch("posthog.models.integration.gitlab.requests.get")+ @pytest.mark.parametrize(+ "response, expected_error",+ [+ (MagicMock(status_code=301), "HTTP 301"),+ (MagicMock(status_code=401), "HTTP 401"),+ (+ MagicMock(status_code=200, **{"json.side_effect": requests.JSONDecodeError("Expecting value", "", 0)}),+ "isn't JSON",+ ),+ (requests.ConnectionError("Name or service not known"), "Couldn't connect"),+ ],+ ids=["redirect", "unauthorized", "html_body", "unreachable_host"],+ )+ @patch("requests.request")+ @patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))+ def test_create_integration_rejects_a_host_that_does_not_answer_like_gitlab(+ self, _mock_is_url_allowed, mock_request, response, expected_error+ ):+ from prometheus_client import REGISTRY++ from posthog.egress.gitlab.transport import token_scope+ from posthog.models.integration import GitLabIntegration, GitLabIntegrationError++ if isinstance(response, Exception):+ mock_request.side_effect = response+ else:+ mock_request.return_value = response++ with pytest.raises(GitLabIntegrationError, match=expected_error):+ GitLabIntegration.create_integration("https://gitlab.example.com", "1", "token123", 1, None)++ # A rejected token is never stored, so scoping the setup probe would let repeated attempts+ # retain one counter child and three gauge children per token for the life of the process.+ rejected_scope = token_scope("https://gitlab.example.com", "token123")+ samples = (+ sample for metric in REGISTRY.collect() if metric.name.startswith("gitlab_api") for sample in metric.samples+ )+ assert not any(sample.labels.get("scope") == rejected_scope for sample in samples)++ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))def test_search_issues_filters_by_issue_id(self, _mock_is_url_allowed, mock_get):from posthog.models.integration import GitLabIntegration@@ -95,10 +154,10 @@ class TestGitLabIntegrationModel:results = GitLabIntegration(integration).search_issues("#42")assert results[0]["id"] == "42"- assert mock_get.call_args.args[0] == "https://gitlab.com/api/v4/projects/1/issues/42"+ assert mock_get.call_args.args[1] == "https://gitlab.com/api/v4/projects/1/issues/42"assert "params" not in mock_get.call_args.kwargs- @patch("posthog.models.integration.gitlab.requests.get")+ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))def test_search_issues_falls_back_to_title_search_when_issue_id_is_not_found(self, _mock_is_url_allowed, mock_get):from posthog.models.integration import GitLabIntegration@@ -122,7 +181,7 @@ class TestGitLabIntegrationModel:assert mock_get.call_args.kwargs["params"]["search"] == "#42"assert "iids[]" not in mock_get.call_args.kwargs["params"]- @patch("posthog.models.integration.gitlab.requests.post")+ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))def test_create_issue_sends_assignee_as_numeric_id(self, _mock_is_url_allowed, mock_post):from posthog.models.integration import GitLabIntegration@@ -138,7 +197,7 @@ class TestGitLabIntegrationModel:assert mock_post.call_args.kwargs["json"]["assignee_ids"] == [42]- @patch("posthog.models.integration.gitlab.requests.get")+ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))def test_list_assignees_searches_and_skips_inactive_members(self, _mock_is_url_allowed, mock_get):from posthog.models.integration import GitLabIntegration@@ -156,11 +215,11 @@ class TestGitLabIntegrationModel:from posthog.models.integration import Assigneeassert GitLabIntegration(integration).list_assignees("ad") == [Assignee(id="1", name="Ada")]- assert mock_get.call_args.args[0] == (+ assert mock_get.call_args.args[1] == ("https://gitlab.com/api/v4/projects/1/members/all?state=active&per_page=100&query=ad")- @patch("posthog.models.integration.gitlab.requests.get")+ @patch("requests.request")@patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))def test_list_assignees_reports_the_gitlab_error(self, _mock_is_url_allowed, mock_get):from posthog.models.integration import AssigneeLookupFailed, GitLabIntegration@@ -174,3 +233,125 @@ class TestGitLabIntegrationModel:with pytest.raises(AssigneeLookupFailed, match="403 Forbidden"):GitLabIntegration(integration).list_assignees()++ @patch("requests.request")+ @patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))+ def test_list_projects_follows_next_page_header(self, _mock_is_url_allowed, mock_request):+ from posthog.models.integration import GitLabIntegration++ integration = MagicMock(+ kind="gitlab",+ config={"hostname": "https://gitlab.com"},+ sensitive_config={"access_token": "token123"},+ )+ first_page = MagicMock(status_code=200, headers={"X-Next-Page": "2"})+ first_page.json.return_value = [+ {"id": 1, "path_with_namespace": "acme/app", "web_url": "https://gitlab.com/acme/app"}+ ]+ last_page = MagicMock(status_code=200, headers={"X-Next-Page": ""})+ last_page.json.return_value = [+ {+ "id": 2,+ "path_with_namespace": "acme/platform/api",+ "web_url": "https://gitlab.com/acme/platform/api",+ "default_branch": "main",+ }+ ]+ mock_request.side_effect = [first_page, last_page]++ projects = GitLabIntegration(integration).list_projects()++ assert [project.path_with_namespace for project in projects] == ["acme/app", "acme/platform/api"]+ assert [call.kwargs["params"]["page"] for call in mock_request.call_args_list] == ["1", "2"]++ @patch("requests.request")+ @patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))+ def test_create_merge_request_opens_a_draft(self, _mock_is_url_allowed, mock_request):+ from posthog.models.integration import GitLabIntegration++ integration = MagicMock(+ kind="gitlab",+ config={"hostname": "https://gitlab.com"},+ sensitive_config={"access_token": "token123"},+ )+ mock_request.return_value = MagicMock(status_code=201)+ mock_request.return_value.json.return_value = {+ "iid": 5,+ "web_url": "https://gitlab.com/acme/app/-/merge_requests/5",+ "state": "opened",+ "draft": True,+ }++ merge_request = GitLabIntegration(integration).create_merge_request(+ 7, source_branch="posthog/fix-checkout", target_branch="main", title="Fix checkout", description="Details"+ )++ assert merge_request.iid == 5+ assert mock_request.call_args.args[1] == "https://gitlab.com/api/v4/projects/7/merge_requests"+ assert mock_request.call_args.kwargs["json"]["title"] == "Draft: Fix checkout"+ assert mock_request.call_args.kwargs["allow_redirects"] is False++ @pytest.mark.parametrize(+ "method, kwargs, status_code",+ [+ ("list_projects", {}, 200),+ (+ "create_merge_request",+ {+ "project_id": 7,+ "source_branch": "posthog/fix-checkout",+ "target_branch": "main",+ "title": "Fix checkout",+ "description": "Details",+ },+ 201,+ ),+ ("get_merge_request", {"project_id": 7, "merge_request_iid": 5}, 200),+ ],+ )+ @patch("requests.request")+ @patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))+ def test_project_and_merge_request_methods_reject_non_json_responses(+ self,+ _mock_is_url_allowed: MagicMock,+ mock_request: MagicMock,+ method: str,+ kwargs: dict[str, str | int],+ status_code: int,+ ) -> None:+ from posthog.models.integration import GitLabIntegration, GitLabIntegrationError++ integration = MagicMock(+ kind="gitlab",+ config={"hostname": "https://gitlab.example.com"},+ sensitive_config={"access_token": "glpat-not-a-real-token"},+ )+ mock_request.return_value = MagicMock(+ status_code=status_code,+ **{"json.side_effect": requests.JSONDecodeError("Expecting value", "", 0)},+ )++ with pytest.raises(GitLabIntegrationError, match="isn't JSON"):+ getattr(GitLabIntegration(integration), method)(**kwargs)++ @patch("requests.request")+ @patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None))+ def test_an_encoded_project_path_never_reaches_the_endpoint_label(self, _mock_is_url_allowed, mock_request):+ from prometheus_client import REGISTRY++ from posthog.models.integration import GitLabIntegration++ mock_request.return_value = MagicMock(status_code=200, headers={})++ GitLabIntegration.get(+ "https://gitlab.com", "projects/acme%2Fsecret-app", "token123", endpoint="/api/v4/projects/{id}"+ )++ endpoints = {+ sample.labels["endpoint"]+ for metric in REGISTRY.collect()+ if metric.name == "gitlab_api_requests"+ for sample in metric.samples+ }+ assert "/api/v4/projects/{id}" in endpoints+ assert not any("secret-app" in endpoint for endpoint in endpoints)