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

chore(typing): Add typing to SimpleEventSerializer #78133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
47 changes: 39 additions & 8 deletions src/sentry/api/serializers/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
from collections import defaultdict
from collections.abc import Sequence
from datetime import datetime, timezone
from typing import Any
from typing import Any, NotRequired, TypedDict

import sentry_sdk
import sqlparse
from sentry_relay.processing import meta_with_chunks

from sentry.api.serializers import Serializer, register, serialize
from sentry.api.serializers.models.release import GroupEventReleaseSerializer
from sentry.eventstore.models import Event, GroupEvent
from sentry.eventstore.models import BaseEvent, Event, GroupEvent
from sentry.interfaces.user import EventUserApiContext, User
from sentry.models.eventattachment import EventAttachment
from sentry.models.eventerror import EventError
from sentry.models.release import Release
from sentry.models.userreport import UserReport
from sentry.sdk_updates import SdkSetupState, get_suggested_updates
from sentry.search.utils import convert_user_tag_to_query, map_device_class_level
from sentry.stacktraces.processing import find_stacktraces_in_data
from sentry.users.models.user import User
from sentry.utils.json import prune_empty_keys
from sentry.utils.safe import get_path

Expand Down Expand Up @@ -483,6 +483,33 @@ def serialize(self, obj, attrs, user, **kwargs):
return result


class SimpleEventTag(TypedDict):
key: str
value: str
query: NotRequired[str]


SimpleEventSerializerResponse = TypedDict(
"SimpleEventSerializerResponse",
{
"id": str,
"event.type": str,
"groupID": str | None,
"eventID": str,
"projectID": str,
"message": str,
"title": str,
"location": str,
"culprit": str,
"user": EventUserApiContext | None,
"tags": list[SimpleEventTag],
"platform": str,
"dateCreated": datetime,
"crashFile": str | None,
},
)


class SimpleEventSerializer(EventSerializer):
"""
Simple event serializer that renders a basic outline of an event without
Expand All @@ -505,17 +532,19 @@ def get_attrs(self, item_list, user, **kwargs):
}
return {event: {"crash_file": serialized_files.get(event.event_id)} for event in item_list}

def serialize(self, obj, attrs, user, **kwargs):
tags = [{"key": key.split("sentry:", 1)[-1], "value": value} for key, value in obj.tags]
def serialize(self, obj: BaseEvent, attrs, user, **kwargs) -> SimpleEventSerializerResponse:
tags: list[SimpleEventTag] = [
{"key": key.split("sentry:", 1)[-1], "value": value} for key, value in obj.tags
]
for tag in tags:
query = convert_user_tag_to_query(tag["key"], tag["value"])
if query:
tag["query"] = query
map_device_class_tags(tags)

user = obj.get_minimal_user()
event_user = obj.get_minimal_user()

return {
response: SimpleEventSerializerResponse = {
"id": str(obj.event_id),
"event.type": str(obj.get_event_type()),
"groupID": str(obj.group_id) if obj.group_id else None,
Expand All @@ -527,14 +556,16 @@ def serialize(self, obj, attrs, user, **kwargs):
"title": obj.title,
"location": obj.location,
"culprit": obj.culprit,
"user": user and user.get_api_context(),
"user": event_user and event_user.get_api_context(),
"tags": tags,
"platform": obj.platform,
"dateCreated": obj.datetime,
# Needed to generate minidump links in UI
"crashFile": attrs["crash_file"],
}

return response


class ExternalEventSerializer(EventSerializer):
"""
Expand Down
13 changes: 12 additions & 1 deletion src/sentry/interfaces/user.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
__all__ = ("User",)


from typing import Any, NotRequired, TypedDict

from sentry.interfaces.base import Interface
from sentry.interfaces.geo import Geo
from sentry.utils.json import prune_empty_keys
from sentry.web.helpers import render_to_string


class EventUserApiContext(TypedDict):
id: NotRequired[str]
email: NotRequired[str]
username: NotRequired[str]
ip_address: NotRequired[str]
name: NotRequired[str]
data: dict[str, Any] | None


class User(Interface):
"""
An interface which describes the authenticated User for a request.
Expand Down Expand Up @@ -51,7 +62,7 @@ def to_json(self):
}
)

def get_api_context(self, is_public=False, platform=None):
def get_api_context(self, is_public=False, platform=None) -> EventUserApiContext:
return {
"id": self.id,
"email": self.email,
Expand Down
Loading