Browse Source

fix(hybrid-cloud): Fix commit task and update it for split db (#54660)

Alberto Leal 1 year ago
parent
commit
1e4c79eb96

+ 4 - 4
src/sentry/integrations/example/repository.py

@@ -1,5 +1,5 @@
-from sentry.models import Integration
 from sentry.plugins.providers import IntegrationRepositoryProvider
+from sentry.services.hybrid_cloud.integration import integration_service
 from sentry.shared_integrations.exceptions import IntegrationError
 
 
@@ -8,9 +8,9 @@ class ExampleRepositoryProvider(IntegrationRepositoryProvider):
     repo_provider = "example"
 
     def compare_commits(self, repo, start_sha, end_sha):
-        installation = Integration.objects.get(id=repo.integration_id).get_installation(
-            repo.organization_id
-        )
+        installation = integration_service.get_integration(
+            integration_id=repo.integration_id
+        ).get_installation(organization_id=repo.organization_id)
 
         try:
             raise IntegrationError("{'error': 'Repository not found'}")

+ 18 - 4
src/sentry/models/release.py

@@ -18,7 +18,7 @@ from sentry_relay.exceptions import RelayError
 from sentry_relay.processing import parse_release
 
 from sentry import features
-from sentry.constants import BAD_RELEASE_CHARS, COMMIT_RANGE_DELIMITER
+from sentry.constants import BAD_RELEASE_CHARS, COMMIT_RANGE_DELIMITER, ObjectStatus
 from sentry.db.models import (
     ArrayField,
     BaseQuerySet,
@@ -953,9 +953,23 @@ class Release(Model):
                 for idx, data in enumerate(commit_list):
                     repo_name = data.get("repository") or f"organization-{self.organization_id}"
                     if repo_name not in repos:
-                        repos[repo_name] = repo = Repository.objects.get_or_create(
-                            organization_id=self.organization_id, name=repo_name
-                        )[0]
+                        repo = (
+                            Repository.objects.filter(
+                                organization_id=self.organization_id,
+                                name=repo_name,
+                                status=ObjectStatus.ACTIVE,
+                            )
+                            .order_by("-pk")
+                            .first()
+                        )
+
+                        if repo is None:
+                            repo = Repository.objects.create(
+                                organization_id=self.organization_id,
+                                name=repo_name,
+                            )
+
+                        repos[repo_name] = repo
                     else:
                         repo = repos[repo_name]
 

+ 9 - 2
src/sentry/tasks/commits.py

@@ -6,6 +6,7 @@ import sentry_sdk
 from django.urls import reverse
 from sentry_sdk import set_tag
 
+from sentry.constants import ObjectStatus
 from sentry.exceptions import InvalidIdentity, PluginError
 from sentry.models import (
     Deploy,
@@ -94,8 +95,14 @@ def fetch_commits(release_id: int, user_id: int, refs, prev_release_id=None, **k
 
     for ref in refs:
         try:
-            repo = Repository.objects.get(
-                organization_id=release.organization_id, name=ref["repository"]
+            repo = (
+                Repository.objects.filter(
+                    organization_id=release.organization_id,
+                    name=ref["repository"],
+                    status=ObjectStatus.ACTIVE,
+                )
+                .order_by("-pk")
+                .first()
             )
         except Repository.DoesNotExist:
             logger.info(

+ 22 - 6
tests/sentry/tasks/test_commits.py

@@ -2,6 +2,7 @@ from unittest.mock import patch
 
 from django.core import mail
 
+from sentry.constants import ObjectStatus
 from sentry.exceptions import InvalidIdentity, PluginError
 from sentry.locks import locks
 from sentry.models import (
@@ -13,12 +14,14 @@ from sentry.models import (
     ReleaseHeadCommit,
     Repository,
 )
+from sentry.silo import SiloMode
 from sentry.tasks.commits import fetch_commits, handle_invalid_identity
 from sentry.testutils.cases import TestCase
-from sentry.testutils.silo import control_silo_test
+from sentry.testutils.silo import assume_test_silo_mode, control_silo_test, region_silo_test
 from social_auth.models import UserSocialAuth
 
 
+@region_silo_test(stable=True)
 class FetchCommitsTest(TestCase):
     def _test_simple_action(self, user, org):
         repo = Repository.objects.create(name="example", provider="dummy", organization_id=org.id)
@@ -75,6 +78,15 @@ class FetchCommitsTest(TestCase):
         org = self.create_organization(owner=self.user, name="baz")
         self._test_simple_action(user=self.user, org=org)
 
+    def test_duplicate_repositories(self):
+        self.login_as(user=self.user)
+        org = self.create_organization(owner=self.user, name="baz")
+        Repository.objects.create(
+            name="example", provider="dummy", organization_id=org.id, status=ObjectStatus.DISABLED
+        )
+        Repository.objects.create(name="example", provider="dummy", organization_id=org.id)
+        self._test_simple_action(user=self.user, org=org)
+
     def test_simple_owner_from_team(self):
         user = self.create_user()
         self.login_as(user=user)
@@ -130,7 +142,8 @@ class FetchCommitsTest(TestCase):
 
         release2 = Release.objects.create(organization_id=org.id, version="12345678")
 
-        usa = UserSocialAuth.objects.create(user=self.user, provider="dummy")
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            usa = UserSocialAuth.objects.create(user=self.user, provider="dummy")
 
         mock_compare_commits.side_effect = InvalidIdentity(identity=usa)
 
@@ -158,7 +171,8 @@ class FetchCommitsTest(TestCase):
 
         release2 = Release.objects.create(organization_id=org.id, version="12345678")
 
-        UserSocialAuth.objects.create(user=self.user, provider="dummy")
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            UserSocialAuth.objects.create(user=self.user, provider="dummy")
 
         mock_compare_commits.side_effect = Exception("secrets")
 
@@ -228,7 +242,8 @@ class FetchCommitsTest(TestCase):
 
         release2 = Release.objects.create(organization_id=org.id, version="12345678")
 
-        UserSocialAuth.objects.create(user=self.user, provider="dummy")
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            UserSocialAuth.objects.create(user=self.user, provider="dummy")
 
         mock_compare_commits.side_effect = PluginError("You can read me")
 
@@ -249,8 +264,9 @@ class FetchCommitsTest(TestCase):
         self.login_as(user=self.user)
         org = self.create_organization(owner=self.user, name="baz")
 
-        integration = Integration.objects.create(provider="example", name="Example")
-        integration.add_organization(org)
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            integration = Integration.objects.create(provider="example", name="Example")
+            integration.add_organization(org)
 
         repo = Repository.objects.create(
             name="example",