-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(aci): Update API to allow filter monitors by assignee #95501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
7580ceb
9d41b27
f3dd95e
e349f18
faf3be8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from collections.abc import Iterable, Sequence | ||
from functools import partial | ||
|
||
from django.db import router, transaction | ||
|
@@ -17,6 +18,7 @@ | |
from sentry.api.event_search import SearchConfig, SearchFilter, SearchKey, default_config | ||
from sentry.api.event_search import parse_search_query as base_parse_search_query | ||
from sentry.api.exceptions import ResourceDoesNotExist | ||
from sentry.api.issue_search import convert_actor_or_none_value | ||
from sentry.api.paginator import OffsetPaginator | ||
from sentry.api.serializers import serialize | ||
from sentry.apidocs.constants import ( | ||
|
@@ -30,6 +32,9 @@ | |
from sentry.issues import grouptype | ||
from sentry.models.organization import Organization | ||
from sentry.models.project import Project | ||
from sentry.models.team import Team | ||
from sentry.users.models.user import User | ||
from sentry.users.services.user import RpcUser | ||
from sentry.workflow_engine.endpoints.serializers import DetectorSerializer | ||
from sentry.workflow_engine.endpoints.utils.filters import apply_filter | ||
from sentry.workflow_engine.endpoints.utils.sortby import SortByParam | ||
|
@@ -43,12 +48,34 @@ | |
detector_search_config = SearchConfig.create_from( | ||
default_config, | ||
text_operator_keys={"name", "type"}, | ||
allowed_keys={"name", "type"}, | ||
allowed_keys={"name", "type", "assignee"}, | ||
allow_boolean=False, | ||
free_text_key="query", | ||
) | ||
parse_detector_query = partial(base_parse_search_query, config=detector_search_config) | ||
|
||
|
||
def convert_assignee_values(value: Iterable[str], projects: Sequence[Project], user: User) -> Q: | ||
""" | ||
Convert an assignee search value to a Django Q object for filtering detectors. | ||
""" | ||
actors_or_none: list[RpcUser | Team | None] = convert_actor_or_none_value( | ||
value, projects, user, None | ||
) | ||
assignee_query = Q() | ||
for actor in actors_or_none: | ||
if isinstance(actor, (User, RpcUser)): | ||
assignee_query |= Q(owner_user_id=actor.id) | ||
elif isinstance(actor, Team): | ||
assignee_query |= Q(owner_team_id=actor.id) | ||
elif actor is None: | ||
assignee_query |= Q(owner_team_id__isnull=True, owner_user_id__isnull=True) | ||
else: | ||
# Unknown actor type, return a query that matches nothing | ||
assignee_query |= Q(pk__isnull=True) | ||
return assignee_query | ||
|
||
|
||
# Maps API field name to database field name, with synthetic aggregate fields keeping | ||
# to our field naming scheme for consistency. | ||
SORT_ATTRS = { | ||
|
@@ -138,6 +165,19 @@ def get(self, request: Request, organization: Organization) -> Response: | |
queryset = apply_filter(queryset, filter, "name") | ||
case SearchFilter(key=SearchKey("type"), operator=("=" | "IN" | "!=")): | ||
queryset = apply_filter(queryset, filter, "type") | ||
case SearchFilter(key=SearchKey("assignee"), operator=("=" | "IN" | "!=")): | ||
# Filter values can be emails, team slugs, "me", "my_teams", "none" | ||
values = ( | ||
filter.value.value | ||
if isinstance(filter.value.value, list) | ||
else [filter.value.value] | ||
) | ||
assignee_q = convert_assignee_values(values, projects, request.user) | ||
|
||
if filter.operator == "!=": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit: is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately not. It's a repeated pattern in the codebase seen in other parts of the product too. |
||
queryset = queryset.exclude(assignee_q) | ||
else: | ||
queryset = queryset.filter(assignee_q) | ||
case SearchFilter(key=SearchKey("query"), operator="="): | ||
# 'query' is our free text key; all free text gets returned here | ||
# as '=', and we search any relevant fields for it. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
from unittest import mock | ||
|
||
from django.db.models import Q | ||
from rest_framework.exceptions import ErrorDetail | ||
|
||
from sentry.api.serializers import serialize | ||
from sentry.grouping.grouptype import ErrorGroupType | ||
from sentry.incidents.grouptype import MetricIssue | ||
from sentry.incidents.models.alert_rule import AlertRuleDetectionType | ||
from sentry.models.environment import Environment | ||
from sentry.search.utils import _HACKY_INVALID_USER | ||
from sentry.snuba.dataset import Dataset | ||
from sentry.snuba.models import ( | ||
QuerySubscription, | ||
|
@@ -19,6 +21,7 @@ | |
from sentry.testutils.silo import region_silo_test | ||
from sentry.uptime.grouptype import UptimeDomainCheckFailure | ||
from sentry.uptime.types import DATA_SOURCE_UPTIME_SUBSCRIPTION | ||
from sentry.workflow_engine.endpoints.organization_detector_index import convert_assignee_values | ||
from sentry.workflow_engine.models import DataCondition, DataConditionGroup, DataSource, Detector | ||
from sentry.workflow_engine.models.data_condition import Condition | ||
from sentry.workflow_engine.models.detector_workflow import DetectorWorkflow | ||
|
@@ -592,21 +595,68 @@ def test_invalid_owner(self): | |
) | ||
assert "owner" in response.data | ||
|
||
def test_owner_not_in_organization(self): | ||
# Create a user in another organization | ||
other_org = self.create_organization() | ||
other_user = self.create_user() | ||
self.create_member(organization=other_org, user=other_user) | ||
|
||
# Test with owner not in current organization | ||
data_with_invalid_owner = { | ||
**self.valid_data, | ||
"owner": other_user.get_actor_identifier(), | ||
} | ||
@region_silo_test | ||
class ConvertAssigneeValuesTest(APITestCase): | ||
"""Test the convert_assignee_values function""" | ||
|
||
response = self.get_error_response( | ||
self.organization.slug, | ||
**data_with_invalid_owner, | ||
status_code=400, | ||
) | ||
assert "owner" in response.data | ||
def setUp(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible for us to have a test that actually filters by assignee? |
||
super().setUp() | ||
self.user = self.create_user() | ||
self.team = self.create_team(organization=self.organization) | ||
self.other_user = self.create_user() | ||
self.create_member(organization=self.organization, user=self.other_user) | ||
self.projects = [self.project] | ||
|
||
def test_convert_assignee_values_user_email(self): | ||
result = convert_assignee_values([self.user.email], self.projects, self.user) | ||
expected = Q(owner_user_id=self.user.id) | ||
self.assertEqual(str(result), str(expected)) | ||
|
||
def test_convert_assignee_values_user_username(self): | ||
result = convert_assignee_values([self.user.username], self.projects, self.user) | ||
expected = Q(owner_user_id=self.user.id) | ||
self.assertEqual(str(result), str(expected)) | ||
|
||
def test_convert_assignee_values_team_slug(self): | ||
result = convert_assignee_values([f"#{self.team.slug}"], self.projects, self.user) | ||
expected = Q(owner_team_id=self.team.id) | ||
self.assertEqual(str(result), str(expected)) | ||
|
||
def test_convert_assignee_values_me(self): | ||
result = convert_assignee_values(["me"], self.projects, self.user) | ||
expected = Q(owner_user_id=self.user.id) | ||
self.assertEqual(str(result), str(expected)) | ||
|
||
def test_convert_assignee_values_none(self): | ||
result = convert_assignee_values(["none"], self.projects, self.user) | ||
expected = Q(owner_team_id__isnull=True, owner_user_id__isnull=True) | ||
self.assertEqual(str(result), str(expected)) | ||
|
||
def test_convert_assignee_values_multiple(self): | ||
result = convert_assignee_values( | ||
[str(self.user.email), f"#{self.team.slug}"], self.projects, self.user | ||
) | ||
expected = Q(owner_user_id=self.user.id) | Q(owner_team_id=self.team.id) | ||
self.assertEqual(str(result), str(expected)) | ||
|
||
def test_convert_assignee_values_mixed(self): | ||
result = convert_assignee_values( | ||
["me", "none", f"#{self.team.slug}"], self.projects, self.user | ||
) | ||
expected = ( | ||
Q(owner_user_id=self.user.id) | ||
| Q(owner_team_id__isnull=True, owner_user_id__isnull=True) | ||
| Q(owner_team_id=self.team.id) | ||
) | ||
self.assertEqual(str(result), str(expected)) | ||
|
||
def test_convert_assignee_values_invalid(self): | ||
result = convert_assignee_values(["999999"], self.projects, self.user) | ||
expected = Q(owner_user_id=_HACKY_INVALID_USER.id) | ||
self.assertEqual(str(result), str(expected)) | ||
|
||
def test_convert_assignee_values_empty(self): | ||
result = convert_assignee_values([], self.projects, self.user) | ||
expected = Q() | ||
self.assertEqual(str(result), str(expected)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to raise here or otherwise report an error if there are types coming back from
convert_actor_or_none_value
that we don't support.I think officially the right thing is
assert_never
.