Skip to content

Commit 4e71a22

Browse files
fix(eco): Addresses ApiForbidden errors, IntegrationConfigurationError noise (#109711)
1 parent cc1632b commit 4e71a22

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/sentry/integrations/gitlab/integration.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
from sentry.pipeline.views.nested import NestedPipelineView
4040
from sentry.shared_integrations.exceptions import (
4141
ApiError,
42+
ApiForbiddenError,
43+
ApiUnauthorized,
4244
IntegrationConfigurationError,
4345
IntegrationProviderError,
4446
)
@@ -159,10 +161,15 @@ def has_repo_access(self, repo: RpcRepository) -> bool:
159161
def get_repositories(
160162
self, query: str | None = None, page_number_limit: int | None = None
161163
) -> list[dict[str, Any]]:
162-
# Note: gitlab projects are the same things as repos everywhere else
163-
group = self.get_group_id()
164-
resp = self.get_client().search_projects(group, query)
165-
return [{"identifier": repo["id"], "name": repo["name_with_namespace"]} for repo in resp]
164+
try:
165+
# Note: gitlab projects are the same things as repos everywhere else
166+
group = self.get_group_id()
167+
resp = self.get_client().search_projects(group, query)
168+
return [
169+
{"identifier": repo["id"], "name": repo["name_with_namespace"]} for repo in resp
170+
]
171+
except (ApiForbiddenError, ApiUnauthorized) as e:
172+
raise IntegrationConfigurationError(self.message_from_error(e)) from e
166173

167174
def source_url_matches(self, url: str) -> bool:
168175
return url.startswith("https://{}".format(self.model.metadata["domain_name"]))

src/sentry/integrations/source_code_management/repository.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ def check_file(self, repo: Repository, filepath: str, branch: str | None = None)
133133
filepath = filepath.lstrip("/")
134134
try:
135135
client = self.get_client()
136+
except IntegrationConfigurationError:
137+
# This is likely due to access being revoked by the user, or
138+
# some other misconfiguration on the integration's side.
139+
return None
136140
except (Identity.DoesNotExist, IntegrationError):
137141
sentry_sdk.capture_exception()
138142
return None

tests/sentry/integrations/gitlab/test_integration.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from urllib.parse import parse_qs, quote, urlencode, urlparse
55

66
import orjson
7+
import pytest
78
import responses
89
from django.core.cache import cache
910
from django.test import override_settings
@@ -21,6 +22,7 @@
2122
)
2223
from sentry.integrations.types import ExternalProviders
2324
from sentry.models.repository import Repository
25+
from sentry.shared_integrations.exceptions import ApiForbiddenError, IntegrationConfigurationError
2426
from sentry.silo.base import SiloMode
2527
from sentry.silo.util import PROXY_BASE_PATH, PROXY_OI_HEADER, PROXY_SIGNATURE_HEADER
2628
from sentry.testutils.cases import IntegrationTestCase
@@ -29,6 +31,7 @@
2931
from sentry.testutils.silo import assume_test_silo_mode, control_silo_test
3032
from sentry.users.models.identity import Identity, IdentityProvider, IdentityStatus
3133
from sentry.users.services.user.serial import serialize_rpc_user
34+
from sentry.utils import json
3235
from tests.sentry.integrations.test_helpers import add_control_silo_proxy_response
3336

3437

@@ -1126,3 +1129,16 @@ def test_create_comment_attribution(self) -> None:
11261129
result = installation.create_comment_attribution(self.user.id, comment_text)
11271130

11281131
assert result == "**Test User** wrote:\n\n> This is a comment\n> With multiple lines"
1132+
1133+
def test_get_repositories_unauthorized_raises_integration_configuration_error(self) -> None:
1134+
installation = self.installation
1135+
1136+
with patch.object(installation, "get_client") as mock_get_client:
1137+
mock_get_client.return_value.search_projects.side_effect = ApiForbiddenError(
1138+
text=json.dumps({"message": "unauthorized"})
1139+
)
1140+
with pytest.raises(IntegrationConfigurationError) as exception_info:
1141+
installation.get_repositories()
1142+
assert (
1143+
str(exception_info.value) == "Error Communicating with GitLab (HTTP 403): unauthorized"
1144+
)

0 commit comments

Comments
 (0)