Skip to content
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

feat(issues): Add ability to query issues by hash #76701

Merged
merged 8 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions api-docs/paths/events/project-issues.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
"type": "string"
}
},
{
"name": "hash",
"in": "query",
"description": "A list of hashes of groups to return. Is not compatible with 'query' parameter as it will only return the request groups with corresponding hashes. The maximum number of hashes that can be sent is 100. If more are sent, only the first 100 will be used.",
roggenkemper marked this conversation as resolved.
Show resolved Hide resolved
"schema": {
"type": "string"
}
},
{
"$ref": "../../components/parameters/pagination-cursor.json#/PaginationCursor"
}
Expand Down
23 changes: 23 additions & 0 deletions src/sentry/api/endpoints/project_group_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
from sentry.api.serializers.models.group_stream import StreamGroupSerializer
from sentry.models.environment import Environment
from sentry.models.group import QUERY_STATUS_LOOKUP, Group, GroupStatus
from sentry.models.grouphash import GroupHash
from sentry.search.events.constants import EQUALITY_OPERATORS
from sentry.signals import advanced_search
from sentry.types.ratelimit import RateLimit, RateLimitCategory
from sentry.utils.validators import normalize_event_id

ERR_INVALID_STATS_PERIOD = "Invalid stats_period. Valid choices are '', '24h', and '14d'"
ERR_HASHES_AND_OTHER_QUERY = "Cannot use 'hash' with 'query'"


@region_silo_endpoint
Expand Down Expand Up @@ -77,6 +79,7 @@ def get(self, request: Request, project) -> Response:
``"is:unresolved"`` is assumed.)
:qparam string environment: this restricts the issues to ones containing
events from this environment
:qparam list hash: hashes of groups to return, overrides 'query' parameter, only returning list of groups found from hashes. The maximum number of hashes that can be sent is 100. If more are sent, only the first 100 will be used.
:pparam string organization_id_or_slug: the id or slug of the organization the
issues belong to.
:pparam string project_id_or_slug: the id or slug of the project the issues
Expand All @@ -99,7 +102,27 @@ def get(self, request: Request, project) -> Response:
stats_period=stats_period,
)

hashes = request.GET.getlist("hash", [])
query = request.GET.get("query", "").strip()

if hashes:
roggenkemper marked this conversation as resolved.
Show resolved Hide resolved
if query:
return Response({"detail": ERR_HASHES_AND_OTHER_QUERY}, status=400)

# limit to 100 hashes
hashes = hashes[:100]
groups_from_hashes = GroupHash.objects.filter(hash__in=hashes).values_list(
"group_id", flat=True
)
groups = list(Group.objects.filter(id__in=groups_from_hashes, project_id=project.id))
roggenkemper marked this conversation as resolved.
Show resolved Hide resolved

serialized_groups = serialize(
groups,
request.user,
serializer(),
)
return Response(serialized_groups)

if query:
matching_group = None
matching_event = None
Expand Down
30 changes: 30 additions & 0 deletions tests/snuba/api/endpoints/test_project_group_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,36 @@ def test_filter_not_unresolved(self):
assert response.status_code == 200
assert [int(r["id"]) for r in response.data] == [event.group.id]

def test_group_by_hash(self):
roggenkemper marked this conversation as resolved.
Show resolved Hide resolved
event = self.store_event(
data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
project_id=self.project.id,
)

self.login_as(user=self.user)

response = self.client.get(f"{self.path}?hash={event.get_primary_hash()}")
assert response.status_code == 200
assert len(response.data) == 1
roggenkemper marked this conversation as resolved.
Show resolved Hide resolved

def test_groups_by_hashes(self):
event = self.store_event(
data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
project_id=self.project.id,
)

event2 = self.store_event(
data={"timestamp": iso_format(before_now(seconds=400)), "fingerprint": ["group-2"]},
project_id=self.project.id,
)
self.login_as(user=self.user)

response = self.client.get(
f"{self.path}?hash={event.get_primary_hash()}&hash={event2.get_primary_hash()}"
)
assert response.status_code == 200
assert len(response.data) == 2


class GroupUpdateTest(APITestCase, SnubaTestCase):
def setUp(self):
Expand Down
Loading