|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +from sentry.integrations.models.external_issue import ExternalIssue |
| 4 | +from sentry.integrations.models.integration import Integration |
| 5 | +from sentry.integrations.types import IntegrationProviderSlug |
| 6 | +from sentry.interfaces.user import User |
| 7 | +from sentry.issues.services.issue.model import RpcLinkedIssueSummary |
| 8 | +from sentry.issues.services.issue.service import issue_service |
| 9 | +from sentry.models.group import Group |
| 10 | +from sentry.models.organization import Organization |
| 11 | +from sentry.models.project import Project |
| 12 | +from sentry.testutils.cases import TestCase |
| 13 | +from sentry.testutils.silo import all_silo_test, create_test_regions |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class HcExternalIssueContext: |
| 18 | + org_owner: User |
| 19 | + region_name: str |
| 20 | + organization: Organization |
| 21 | + project: Project |
| 22 | + |
| 23 | + |
| 24 | +@all_silo_test(regions=create_test_regions("de", "us")) |
| 25 | +class TestIssueService(TestCase): |
| 26 | + def setUp(self): |
| 27 | + super().setUp() |
| 28 | + self.default_jira_integration = self.create_integration( |
| 29 | + provider=IntegrationProviderSlug.JIRA.value, |
| 30 | + organization=self.organization, |
| 31 | + external_id="jira-123", |
| 32 | + name="Jira", |
| 33 | + ) |
| 34 | + self.us_region_context = self.create_region_context( |
| 35 | + "us", |
| 36 | + org_owner=self.create_user(email="us_test@example.com"), |
| 37 | + integration=self.default_jira_integration, |
| 38 | + ) |
| 39 | + self.de_region_context = self.create_region_context( |
| 40 | + "de", |
| 41 | + org_owner=self.create_user(email="de_test@example.com"), |
| 42 | + integration=self.default_jira_integration, |
| 43 | + ) |
| 44 | + |
| 45 | + def create_region_context( |
| 46 | + self, |
| 47 | + region_name: str, |
| 48 | + org_owner: User | None = None, |
| 49 | + integration: Integration | None = None, |
| 50 | + ) -> HcExternalIssueContext: |
| 51 | + organization = self.create_organization( |
| 52 | + name="test_org", |
| 53 | + slug="test", |
| 54 | + owner=org_owner, |
| 55 | + region=region_name, |
| 56 | + ) |
| 57 | + |
| 58 | + project = self.create_project(organization=organization) |
| 59 | + |
| 60 | + return HcExternalIssueContext( |
| 61 | + region_name=region_name, |
| 62 | + organization=organization, |
| 63 | + project=project, |
| 64 | + org_owner=org_owner, |
| 65 | + ) |
| 66 | + |
| 67 | + def create_linked_issue( |
| 68 | + self, |
| 69 | + key: str, |
| 70 | + region_context: HcExternalIssueContext, |
| 71 | + group: Group, |
| 72 | + integration: Integration, |
| 73 | + title: str | None = None, |
| 74 | + ) -> ExternalIssue: |
| 75 | + |
| 76 | + external_issue = self.create_integration_external_issue( |
| 77 | + organization=region_context.organization, |
| 78 | + group=group, |
| 79 | + integration=integration, |
| 80 | + key=key, |
| 81 | + title=title, |
| 82 | + ) |
| 83 | + |
| 84 | + return external_issue |
| 85 | + |
| 86 | + def test_get_linked_issues(self): |
| 87 | + group = self.create_group(project=self.us_region_context.project) |
| 88 | + self.create_linked_issue( |
| 89 | + key="TEST-123", |
| 90 | + region_context=self.us_region_context, |
| 91 | + group=group, |
| 92 | + integration=self.default_jira_integration, |
| 93 | + title="US Group Link", |
| 94 | + ) |
| 95 | + |
| 96 | + response = issue_service.get_linked_issues( |
| 97 | + region_name=self.us_region_context.region_name, |
| 98 | + integration_id=self.default_jira_integration.id, |
| 99 | + organization_ids=[self.us_region_context.organization.id], |
| 100 | + external_issue_key="TEST-123", |
| 101 | + ) |
| 102 | + |
| 103 | + assert response == [ |
| 104 | + RpcLinkedIssueSummary( |
| 105 | + issue_link=group.get_absolute_url(), |
| 106 | + title=group.title, |
| 107 | + ) |
| 108 | + ] |
| 109 | + |
| 110 | + def test_get_linked_issues_with_multiple_organizations_in_multiple_regions(self): |
| 111 | + us_group = self.create_group(project=self.us_region_context.project) |
| 112 | + us_linked_issue = self.create_linked_issue( |
| 113 | + key="TEST-123", |
| 114 | + region_context=self.us_region_context, |
| 115 | + group=us_group, |
| 116 | + integration=self.default_jira_integration, |
| 117 | + title="US Group Link", |
| 118 | + ) |
| 119 | + |
| 120 | + de_group = self.create_group(project=self.de_region_context.project) |
| 121 | + de_linked_issue = self.create_linked_issue( |
| 122 | + key="TEST-123", |
| 123 | + region_context=self.de_region_context, |
| 124 | + group=de_group, |
| 125 | + integration=self.default_jira_integration, |
| 126 | + title="DE Group Link", |
| 127 | + ) |
| 128 | + |
| 129 | + response = issue_service.get_linked_issues( |
| 130 | + region_name=self.us_region_context.region_name, |
| 131 | + integration_id=self.default_jira_integration.id, |
| 132 | + organization_ids=[ |
| 133 | + self.us_region_context.organization.id, |
| 134 | + self.de_region_context.organization.id, |
| 135 | + ], |
| 136 | + external_issue_key="TEST-123", |
| 137 | + ) |
| 138 | + |
| 139 | + assert response == [ |
| 140 | + RpcLinkedIssueSummary( |
| 141 | + issue_link=us_group.get_absolute_url(), |
| 142 | + title=us_linked_issue.title, |
| 143 | + ), |
| 144 | + RpcLinkedIssueSummary( |
| 145 | + issue_link=de_group.get_absolute_url(), |
| 146 | + title=de_linked_issue.title, |
| 147 | + ), |
| 148 | + ] |
| 149 | + |
| 150 | + def test_get_empty_response_when_no_linked_issues(self): |
| 151 | + response = issue_service.get_linked_issues( |
| 152 | + region_name=self.us_region_context.region_name, |
| 153 | + integration_id=self.default_jira_integration.id, |
| 154 | + organization_ids=[self.us_region_context.organization.id], |
| 155 | + external_issue_key="TEST-123", |
| 156 | + ) |
| 157 | + |
| 158 | + assert response == [] |
| 159 | + |
| 160 | + def test_get_single_linked_issue_when_multiple_organizations_share_integration(self): |
| 161 | + us_group = self.create_group(project=self.us_region_context.project) |
| 162 | + us_linked_issue = self.create_linked_issue( |
| 163 | + key="TEST-123", |
| 164 | + region_context=self.us_region_context, |
| 165 | + group=us_group, |
| 166 | + integration=self.default_jira_integration, |
| 167 | + title="US Group Link", |
| 168 | + ) |
| 169 | + |
| 170 | + response = issue_service.get_linked_issues( |
| 171 | + region_name=self.us_region_context.region_name, |
| 172 | + integration_id=self.default_jira_integration.id, |
| 173 | + organization_ids=[self.us_region_context.organization.id], |
| 174 | + external_issue_key="TEST-123", |
| 175 | + ) |
| 176 | + |
| 177 | + assert response == [ |
| 178 | + RpcLinkedIssueSummary( |
| 179 | + issue_link=us_group.get_absolute_url(), |
| 180 | + title=us_linked_issue.title, |
| 181 | + ) |
| 182 | + ] |
| 183 | + |
| 184 | + def test_filters_out_issues_from_other_organizations(self): |
| 185 | + us_group = self.create_group(project=self.us_region_context.project) |
| 186 | + us_linked_issue = self.create_linked_issue( |
| 187 | + key="TEST-123", |
| 188 | + region_context=self.us_region_context, |
| 189 | + group=us_group, |
| 190 | + integration=self.default_jira_integration, |
| 191 | + title="US Group Link", |
| 192 | + ) |
| 193 | + |
| 194 | + other_integration = self.create_integration( |
| 195 | + provider=IntegrationProviderSlug.JIRA.value, |
| 196 | + organization=self.organization, |
| 197 | + external_id="jira-456", |
| 198 | + name="Other Jira", |
| 199 | + ) |
| 200 | + |
| 201 | + unrelated_us_group = self.create_group(project=self.us_region_context.project) |
| 202 | + # Create another linked issue with the same key but different integration. |
| 203 | + self.create_linked_issue( |
| 204 | + key="TEST-123", |
| 205 | + region_context=self.us_region_context, |
| 206 | + group=unrelated_us_group, |
| 207 | + integration=other_integration, |
| 208 | + title="Unrelated US Group Link", |
| 209 | + ) |
| 210 | + |
| 211 | + response = issue_service.get_linked_issues( |
| 212 | + region_name=self.us_region_context.region_name, |
| 213 | + integration_id=self.default_jira_integration.id, |
| 214 | + organization_ids=[self.us_region_context.organization.id], |
| 215 | + external_issue_key="TEST-123", |
| 216 | + ) |
| 217 | + |
| 218 | + assert response == [ |
| 219 | + RpcLinkedIssueSummary( |
| 220 | + issue_link=us_group.get_absolute_url(), |
| 221 | + title=us_linked_issue.title, |
| 222 | + ) |
| 223 | + ] |
0 commit comments