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
80 changes: 71 additions & 9 deletions src/sentry/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,6 @@ def transfer_to(self, organization: Organization) -> None:
from sentry.deletions.models.scheduleddeletion import CellScheduledDeletion
from sentry.incidents.utils.types import DATA_SOURCE_SNUBA_QUERY_SUBSCRIPTION
from sentry.integrations.base import INTEGRATION_TYPE_TO_PROVIDER, IntegrationDomain
from sentry.integrations.models.external_issue import ExternalIssue
from sentry.integrations.models.repository_project_path_config import (
RepositoryProjectPathConfig,
)
Expand Down Expand Up @@ -797,26 +796,89 @@ def resolve_environment_id(environment_id: int | None) -> int | None:
owner_team_id__isnull=True, owner_user_id__isnull=True
).update(owner_team_id=None, owner_user_id=None)

# Manually move over external issues to the new org
linked_groups = GroupLink.objects.filter(project_id=self.id).values_list(
"linked_id", flat=True
)

# Delete issue ownership objects to prevent them from being stuck on the old org
ProjectCodeOwners.objects.filter(project_id=self.id).delete()
RepositoryProjectPathConfig.objects.filter(project_repository__project_id=self.id).delete()
ProjectRepository.objects.filter(project_id=self.id).delete()

if org_changed:
self._transfer_external_issues(old_org_id, organization.id)

def _transfer_external_issues(self, old_org_id: int, new_org_id: int) -> None:
"""
Give the moved project's groups an ExternalIssue owned by the new org. Inbound sync and
the Jira issue panel resolve groups through ExternalIssue.organization_id, so a row can
only be moved when no group left behind still links it; otherwise this project's links
are repointed to a copy (or an existing row) in the new org.
"""
from sentry.integrations.models.external_issue import ExternalIssue

project_issue_links = GroupLink.objects.filter(
project_id=self.id, linked_type=GroupLink.LinkedType.issue
)

for external_issues in chunked(
RangeQuerySetWrapper(
ExternalIssue.objects.filter(organization_id=old_org_id, id__in=linked_groups),
ExternalIssue.objects.filter(
organization_id=old_org_id,
id__in=project_issue_links.values_list("linked_id", flat=True),
),
step=1000,
),
1000,
):
ids = [ei.id for ei in external_issues]
shared_ids = set(
GroupLink.objects.filter(linked_type=GroupLink.LinkedType.issue, linked_id__in=ids)
.exclude(project_id=self.id)
.values_list("linked_id", flat=True)
)
existing_in_new_org = {
(ei.integration_id, ei.key): ei
for ei in ExternalIssue.objects.filter(
organization_id=new_org_id,
integration_id__in={ei.integration_id for ei in external_issues},
key__in={ei.key for ei in external_issues},
)
}

to_move: list[ExternalIssue] = []
to_copy: list[ExternalIssue] = []
# Rows nothing will link once this project's links point at the new org's row.
orphaned_ids: list[int] = []
# Old ExternalIssue id -> the new org's ExternalIssue this project's links should use.
repoint: dict[int, ExternalIssue] = {}
for ei in external_issues:
ei.organization_id = organization.id
ExternalIssue.objects.bulk_update(external_issues, ["organization_id"])
target = existing_in_new_org.get((ei.integration_id, ei.key))
if target is not None:
repoint[ei.id] = target
if ei.id not in shared_ids:
orphaned_ids.append(ei.id)
elif ei.id in shared_ids:
to_copy.append(ei)
else:
ei.organization_id = new_org_id
to_move.append(ei)

copies = [
ExternalIssue(
**{
field.attname: getattr(ei, field.attname)
for field in ExternalIssue._meta.concrete_fields
if not field.primary_key
}
| {"organization_id": new_org_id}
)
for ei in to_copy
]

with transaction.atomic(router.db_for_write(ExternalIssue)):
ExternalIssue.objects.bulk_update(to_move, ["organization_id"])
for ei, copy in zip(to_copy, ExternalIssue.objects.bulk_create(copies)):
repoint[ei.id] = copy
for old_id, target in repoint.items():
project_issue_links.filter(linked_id=old_id).update(linked_id=target.id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy breaks Jira panel lookup

Medium Severity

Copying a shared ExternalIssue into the destination org leaves two rows with the same integration_id and key. The Jira issue panel loads that row with .get(integration_id, key), catches MultipleObjectsReturned, and treats the ticket as unlinked, so groups from both orgs disappear from the panel after transfer.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 64cfec4. Configure here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might still be an issue, though it looks like the DB constraint is on org_id + integration_id + key so if possible, getting a specific org to query would resolve this. IIRC though, there is no indicator on those jira webhooks as to which organization should be loaded, and i think we do something in the control silo to fanout (so maybe this isn't an issue)?

Worth confirming that no call sites will hit the multipleobjectsreturned issue tho

ExternalIssue.objects.filter(id__in=orphaned_ids).delete()

def add_team(self, team: Team) -> bool:
from sentry.models.projectteam import ProjectTeam
Expand Down
119 changes: 119 additions & 0 deletions tests/sentry/models/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from sentry.integrations.types import ExternalProviders
from sentry.models.custominboundfilter import CustomInboundFilter, LegacyFilter
from sentry.models.environment import Environment, EnvironmentProject
from sentry.models.group import Group
from sentry.models.grouplink import GroupLink
from sentry.models.organizationmember import OrganizationMember
from sentry.models.organizationmemberteam import OrganizationMemberTeam
Expand Down Expand Up @@ -534,6 +535,124 @@ def test_transfer_to_organization_external_issues(self) -> None:
assert other_ext_issue.organization_id == self.from_org.id
assert other_group_link.project_id == other_project.id

def test_transfer_to_organization_external_issue_shared_with_remaining_project(self) -> None:
group = self.create_group(project=self.project)
other_group = self.create_group(project=self.create_project(organization=self.from_org))
integration = self.create_integration(
organization=self.from_org, provider="jira", name="Jira", external_id="jira:1"
)
self.create_organization_integration(
organization_id=self.to_org.id, integration_id=integration.id
)
ext_issue = self.create_integration_external_issue(
group=group, integration=integration, key="APP-1"
)
self.create_group_link(
other_group, linked_id=ext_issue.id, linked_type=GroupLink.LinkedType.issue
)

self.project.transfer_to(organization=self.to_org)

ext_issue.refresh_from_db()
assert ext_issue.organization_id == self.from_org.id
assert list(
Group.objects.get_groups_by_external_issue(integration, [self.from_org], "APP-1")
) == [other_group]
assert list(
Group.objects.get_groups_by_external_issue(integration, [self.to_org], "APP-1")
) == [group]

def test_transfer_to_organization_external_issue_reuses_existing_in_new_org(self) -> None:
group = self.create_group(project=self.project)
other_group = self.create_group(project=self.create_project(organization=self.from_org))
to_org_group = self.create_group(project=self.create_project(organization=self.to_org))
integration = self.create_integration(
organization=self.from_org, provider="jira", name="Jira", external_id="jira:1"
)
self.create_organization_integration(
organization_id=self.to_org.id, integration_id=integration.id
)
ext_issue = self.create_integration_external_issue(
group=group, integration=integration, key="APP-1"
)
self.create_group_link(
other_group, linked_id=ext_issue.id, linked_type=GroupLink.LinkedType.issue
)
to_org_ext_issue = self.create_integration_external_issue(
group=to_org_group, integration=integration, key="APP-1"
)

self.project.transfer_to(organization=self.to_org)

assert GroupLink.objects.get(group_id=group.id).linked_id == to_org_ext_issue.id
assert list(
Group.objects.get_groups_by_external_issue(integration, [self.from_org], "APP-1")
) == [other_group]
assert set(
Group.objects.get_groups_by_external_issue(integration, [self.to_org], "APP-1")
) == {group, to_org_group}

def test_transfer_to_organization_external_issue_collides_in_new_org(self) -> None:
# Only the moved project links it, but the new org already tracks the same provider issue,
# so moving the row outright would violate (organization, integration_id, key) uniqueness.
group = self.create_group(project=self.project)
to_org_group = self.create_group(project=self.create_project(organization=self.to_org))
integration = self.create_integration(
organization=self.from_org, provider="jira", name="Jira", external_id="jira:1"
)
self.create_organization_integration(
organization_id=self.to_org.id, integration_id=integration.id
)
self.create_integration_external_issue(group=group, integration=integration, key="APP-1")
to_org_ext_issue = self.create_integration_external_issue(
group=to_org_group, integration=integration, key="APP-1"
)

self.project.transfer_to(organization=self.to_org)

assert GroupLink.objects.get(group_id=group.id).linked_id == to_org_ext_issue.id
assert set(
Group.objects.get_groups_by_external_issue(integration, [self.to_org], "APP-1")
) == {group, to_org_group}

def test_transfer_to_organization_ignores_commit_links_colliding_with_external_issue_ids(
self,
) -> None:
group = self.create_group(project=self.project)
other_group = self.create_group(project=self.create_project(organization=self.from_org))
integration = self.create_integration(
organization=self.from_org, provider="jira", name="Jira", external_id="jira:1"
)
other_ext_issue = self.create_integration_external_issue(
group=other_group, integration=integration, key="APP-1"
)
# GroupLink.linked_id holds Commit ids for commit links, so it can equal an unrelated
# ExternalIssue id.
self.create_group_link(
group, linked_id=other_ext_issue.id, linked_type=GroupLink.LinkedType.commit
)

self.project.transfer_to(organization=self.to_org)

other_ext_issue.refresh_from_db()
assert other_ext_issue.organization_id == self.from_org.id
assert ExternalIssue.objects.filter(organization_id=self.to_org.id).count() == 0

def test_transfer_to_same_organization_keeps_external_issues(self) -> None:
group = self.create_group(project=self.project)
integration = self.create_integration(
organization=self.from_org, provider="jira", name="Jira", external_id="jira:1"
)
ext_issue = self.create_integration_external_issue(
group=group, integration=integration, key="APP-1"
)

self.project.transfer_to(organization=self.from_org)

ext_issue.refresh_from_db()
assert ext_issue.organization_id == self.from_org.id
assert GroupLink.objects.get(group_id=group.id).linked_id == ext_issue.id

def test_transfer_to_organization_with_metric_issue_detector_and_workflow(self) -> None:
self.project.transfer_to(organization=self.to_org)

Expand Down
Loading