Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/sentry/integrations/gitlab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@

logger = logging.getLogger("sentry.integrations.gitlab")

# Repo sync reconciles hooks for a whole batch of repositories inside one task
# deadline, so an unreachable self-hosted instance has to fail fast on connect.
# The read timeout matches the IntegrationProxyClient default.
PROJECT_WEBHOOK_TIMEOUT = (3, 10)


class GitLabApiRequestType(StrEnum):
CREATE_PROJECT_WEBHOOK = "create_project_webhook"
Expand Down Expand Up @@ -554,6 +559,7 @@ def create_project_webhook(self, project_id):
path,
data=self._project_webhook_data(),
api_request_type=GitLabApiRequestType.CREATE_PROJECT_WEBHOOK,
timeout=PROJECT_WEBHOOK_TIMEOUT,
)

return resp["id"]
Expand All @@ -578,6 +584,7 @@ def update_project_webhook(self, project_id, hook_id):
path,
data=self._project_webhook_data(),
api_request_type=GitLabApiRequestType.UPDATE_PROJECT_WEBHOOK,
timeout=PROJECT_WEBHOOK_TIMEOUT,
)

def ensure_project_webhook(self, project_id: int | str, hook_id: int | str | None) -> int | str:
Expand Down Expand Up @@ -624,7 +631,11 @@ def delete_project_webhook(self, project_id, hook_id):
path = GitLabApiClientPath.project_hook.format(
project=safe_quote(project_id), hook_id=hook_id
)
return self.delete(path, api_request_type=GitLabApiRequestType.DELETE_PROJECT_WEBHOOK)
return self.delete(
path,
api_request_type=GitLabApiRequestType.DELETE_PROJECT_WEBHOOK,
timeout=PROJECT_WEBHOOK_TIMEOUT,
)

def create_branch(self, project_id: str, branch: str, ref: str):
"""https://docs.gitlab.com/api/branches/#create-repository-branch"""
Expand Down
17 changes: 16 additions & 1 deletion tests/sentry/integrations/gitlab/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sentry.auth.exceptions import IdentityNotValid
from sentry.exceptions import RestrictedIPAddress
from sentry.integrations.gitlab.blame import GitLabCommitResponse, GitLabFileBlameResponseItem
from sentry.integrations.gitlab.client import GitLabApiRequestType
from sentry.integrations.gitlab.client import PROJECT_WEBHOOK_TIMEOUT, GitLabApiRequestType
from sentry.integrations.gitlab.utils import get_rate_limit_info_from_response
from sentry.integrations.source_code_management.commit_context import (
CommitInfo,
Expand Down Expand Up @@ -829,3 +829,18 @@ def test_hook_calls_are_tagged_by_request_type(self, mock_incr: mock.MagicMock)
sample_rate=1.0,
tags={"integration": "gitlab", "status": status, "api_request_type": request_type},
)

@responses.activate
def test_hook_calls_use_short_connect_timeout(self) -> None:
hooks_url = f"https://example.gitlab.com/api/v4/projects/{self.gitlab_id}/hooks"
responses.add(responses.POST, hooks_url, status=201, json={"id": 7})
responses.add(responses.PUT, f"{hooks_url}/7", status=200, json={"id": 7})
responses.add(responses.DELETE, f"{hooks_url}/7", status=204)

self.gitlab_client.create_project_webhook(self.gitlab_id)
self.gitlab_client.update_project_webhook(self.gitlab_id, 7)
self.gitlab_client.delete_project_webhook(self.gitlab_id, 7)

assert [call.request.req_kwargs["timeout"] for call in responses.calls] == [
PROJECT_WEBHOOK_TIMEOUT
] * 3
Loading