Skip to content

Commit

Permalink
fix(github-growth): catch and raise RepoExistsError (#53946)
Browse files Browse the repository at this point in the history
  • Loading branch information
cathteng authored Aug 1, 2023
1 parent e2710b8 commit e658f63
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/sentry/plugins/providers/integration_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

from dateutil.parser import parse as parse_date
from django.db import IntegrityError, router, transaction
from rest_framework.exceptions import APIException
from rest_framework.request import Request
from rest_framework.response import Response

from sentry import analytics
from sentry.api.exceptions import SentryAPIException, status
from sentry.api.serializers import serialize
from sentry.constants import ObjectStatus
from sentry.integrations import IntegrationInstallation
Expand All @@ -21,9 +21,10 @@
from sentry.utils import metrics


class RepoExistsError(APIException):
status_code = 400
detail = {"errors": {"__all__": "A repository with that name already exists"}}
class RepoExistsError(SentryAPIException):
status_code = status.HTTP_400_BAD_REQUEST
code = "repo_exists"
message = "A repository with that configuration already exists"


def get_integration_repository_provider(integration):
Expand Down Expand Up @@ -164,7 +165,7 @@ def dispatch(self, request: Request, organization, **kwargs):
result, repo = self.create_repository(repo_config=config, organization=organization)
except RepoExistsError as e:
metrics.incr("sentry.integration_repo_provider.repo_exists")
return self.handle_api_error(e)
raise (e)
except Exception as e:
return self.handle_api_error(e)

Expand Down
30 changes: 30 additions & 0 deletions tests/sentry/api/endpoints/test_organization_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,33 @@ def test_floating_repo(self, mock_build_repository_config):
assert repo.url == "https://github.com/getsentry/sentry"
assert repo.config == {"name": "getsentry/sentry"}
assert repo.status == 0

@patch.object(
ExampleRepositoryProvider, "get_repository_data", return_value={"my_config_key": "some_var"}
)
def test_existing_repo(self, mock_build_repository_config):
Repository.objects.create(
organization_id=self.org.id,
name="getsentry/sentry",
status=0,
external_id="my_external_id",
integration_id="2",
provider="integrations:example",
url="https://github.com/getsentry/sentry",
)

with patch.object(
ExampleRepositoryProvider, "build_repository_config", return_value=self.repo_config_data
) as mock_get_repository_data:
response = self.client.post(
self.url, data={"provider": "integrations:example", "name": "getsentry/sentry"}
)
mock_get_repository_data.assert_called_once_with(
organization=self.org, data={"my_config_key": "some_var"}
)

assert response.status_code == 400
assert (
response.content
== b'{"detail":{"code":"repo_exists","message":"A repository with that configuration already exists","extra":{}}}'
)

0 comments on commit e658f63

Please sign in to comment.