|
37 | 37 | from sentry.roles.manager import Role
|
38 | 38 | from sentry.services.hybrid_cloud.notifications import notifications_service
|
39 | 39 | from sentry.services.hybrid_cloud.organization_mapping import organization_mapping_service
|
40 |
| -from sentry.services.hybrid_cloud.user import RpcUser |
| 40 | +from sentry.services.hybrid_cloud.user import RpcUser, RpcUserProfile |
41 | 41 | from sentry.services.hybrid_cloud.user.service import user_service
|
42 | 42 | from sentry.types.organization import OrganizationAbsoluteUrlMixin
|
43 | 43 | from sentry.utils.http import is_using_customer_domain
|
@@ -329,6 +329,61 @@ def default_owner_id(self):
|
329 | 329 | self._default_owner_id = owners[0].id
|
330 | 330 | return self._default_owner_id
|
331 | 331 |
|
| 332 | + @classmethod |
| 333 | + def _get_bulk_owner_ids(cls, organizations: Collection[Organization]) -> dict[int, int]: |
| 334 | + """Find user IDs of the default owners of multiple organization. |
| 335 | +
|
| 336 | + The returned table maps organization ID to user ID. |
| 337 | + """ |
| 338 | + from sentry.models.organizationmember import OrganizationMember |
| 339 | + |
| 340 | + owner_id_table: dict[int, int] = {} |
| 341 | + org_ids_to_query: list[int] = [] |
| 342 | + for org in organizations: |
| 343 | + default_owner = getattr(org, "_default_owner", None) |
| 344 | + if default_owner: |
| 345 | + owner_id_table[org.id] = default_owner.id |
| 346 | + else: |
| 347 | + org_ids_to_query.append(org.id) |
| 348 | + |
| 349 | + if org_ids_to_query: |
| 350 | + queried_owner_ids = OrganizationMember.objects.filter( |
| 351 | + organization_id__in=org_ids_to_query, role=roles.get_top_dog().id |
| 352 | + ).values_list("organization_id", "user_id") |
| 353 | + |
| 354 | + for (org_id, user_id) in queried_owner_ids: |
| 355 | + # An org may have multiple owners. Here we mimic the behavior of |
| 356 | + # `get_default_owner`, which is to use the first one in the query |
| 357 | + # result's iteration order. |
| 358 | + if org_id not in owner_id_table: |
| 359 | + owner_id_table[org_id] = user_id |
| 360 | + |
| 361 | + return owner_id_table |
| 362 | + |
| 363 | + @classmethod |
| 364 | + def get_bulk_owner_profiles( |
| 365 | + cls, organizations: Collection[Organization] |
| 366 | + ) -> dict[int, RpcUserProfile]: |
| 367 | + """Query for profile data of owners of multiple organizations. |
| 368 | +
|
| 369 | + The returned table is keyed by organization ID and shows the default owner. |
| 370 | + An organization may have multiple owners, in which case only the default |
| 371 | + owner is shown. Organization IDs may be absent from the returned table if no |
| 372 | + owner was found. |
| 373 | + """ |
| 374 | + |
| 375 | + owner_id_table = cls._get_bulk_owner_ids(organizations) |
| 376 | + owner_ids = list(owner_id_table.values()) |
| 377 | + |
| 378 | + profiles = user_service.get_many_profiles(filter=dict(user_ids=owner_ids)) |
| 379 | + profile_table = {c.id: c for c in profiles} |
| 380 | + |
| 381 | + return { |
| 382 | + org_id: profile_table[user_id] |
| 383 | + for (org_id, user_id) in owner_id_table.items() |
| 384 | + if user_id in profile_table |
| 385 | + } |
| 386 | + |
332 | 387 | def has_single_owner(self):
|
333 | 388 | owners = list(
|
334 | 389 | self.get_members_with_org_roles([roles.get_top_dog().id])[:2].values_list(
|
|
0 commit comments