Skip to content

Commit

Permalink
feat(hybridcloud): Created get_org_by_id method (#67980)
Browse files Browse the repository at this point in the history
Some of our `convert_args` functions for our endpoints use
`get_org_by_slug(slug=organization_slug)`.

We are working on supporting slugs and parameters for our apis, so we
need a 1:1 function that we can use for these convert_args when id is
passed instead of slug.

Created the function & wrote a test. It will also have more upstream
tests.
  • Loading branch information
iamrajjoshi authored Apr 1, 2024
1 parent bccf5d5 commit 6ddc371
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/sentry/services/hybrid_cloud/organization/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ def get_org_by_slug(
except Organization.DoesNotExist:
return None

def get_org_by_id(
self,
*,
id: int,
user_id: int | None = None,
) -> RpcOrganizationSummary | None:
query = Organization.objects.filter(id=id)
if user_id is not None:
query = query.filter(
status=OrganizationStatus.ACTIVE,
member_set__user_id=user_id,
)
try:
return serialize_organization_summary(query.get())
except Organization.DoesNotExist:
return None

def get_organizations_by_user_and_scope(
self, *, region_name: str, user: RpcUser, scope: str | None = None
) -> list[RpcOrganization]:
Expand Down
14 changes: 14 additions & 0 deletions src/sentry/services/hybrid_cloud/organization/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ def get_org_by_slug(
RpcOrganizationSummary instead of org contexts
"""

@regional_rpc_method(resolve=ByOrganizationId("id"), return_none_if_mapping_not_found=True)
@abstractmethod
def get_org_by_id(
self,
*,
id: int,
user_id: int | None = None,
) -> RpcOrganizationSummary | None:
"""
Fetches the organization, by an organization id. If user_id is passed, it will enforce visibility
rules. This method is differentiated from get_organization_by_id by not being cached and returning
RpcOrganizationSummary instead of org contexts
"""

@regional_rpc_method(resolve=ByRegionName())
@abstractmethod
def get_organizations_by_user_and_scope(
Expand Down
26 changes: 26 additions & 0 deletions tests/sentry/hybridcloud/test_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,32 @@ def test_get_organization_id(org_factory: Callable[[], tuple[Organization, list[
assert_get_organization_by_id_works(user_context, orm_org)


@assume_test_silo_mode(SiloMode.REGION)
def assert_get_org_by_id_works(user_context: User | None, orm_org: Organization):
assert (
organization_service.get_org_by_id(id=-2, user_id=user_context.id if user_context else None)
is None
)
org_context = organization_service.get_org_by_id(
id=orm_org.id, user_id=user_context.id if user_context else None
)
assert org_context is not None

assert orm_org.id == org_context.id
assert orm_org.name == org_context.name
assert orm_org.slug == org_context.slug


@django_db_all(transaction=True)
@all_silo_test
@parameterize_with_orgs
def test_get_org_id(org_factory: Callable[[], tuple[Organization, list[User]]]):
orm_org, orm_users = org_factory()

for user_context in itertools.chain([None], orm_users):
assert_get_org_by_id_works(user_context, orm_org)


@django_db_all(transaction=True)
@all_silo_test
@parameterize_with_orgs
Expand Down

0 comments on commit 6ddc371

Please sign in to comment.