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

ref: fix type for serializers.models.rule #83444

Merged
merged 1 commit into from
Jan 15, 2025
Merged
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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ module = [
"sentry.api.serializers.models.plugin",
"sentry.api.serializers.models.project",
"sentry.api.serializers.models.role",
"sentry.api.serializers.models.rule",
"sentry.auth.helper",
"sentry.auth.provider",
"sentry.auth.system",
Expand Down
41 changes: 26 additions & 15 deletions src/sentry/api/serializers/models/rule.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from collections.abc import Mapping
from typing import TypedDict
from typing import Any, TypedDict

from django.db.models import Max, Q, prefetch_related_objects
from rest_framework import serializers
Expand Down Expand Up @@ -30,7 +32,7 @@ def _is_filter(data):
from sentry.rules import rules

rule_cls = rules.get(data["id"])
return rule_cls.rule_type == "filter/event"
return rule_cls is not None and rule_cls.rule_type == "filter/event"


class RuleCreatedBy(TypedDict):
Expand All @@ -39,21 +41,28 @@ class RuleCreatedBy(TypedDict):
email: str


class _ErrorDict(TypedDict):
detail: str


class RuleSerializerResponseOptional(TypedDict, total=False):
owner: str | None
createdBy: RuleCreatedBy | None
environment: str | None
lastTriggered: str | None
snoozeCreatedBy: str | None
snoozeForEveryone: bool | None
disableReason: str
disableDate: str
errors: list[_ErrorDict]


class RuleSerializerResponse(RuleSerializerResponseOptional):
"""
This represents a Sentry Rule.
"""

id: str
id: str | None
conditions: list[dict]
filters: list[dict]
actions: list[dict]
Expand Down Expand Up @@ -89,6 +98,7 @@ def get_attrs(self, item_list, user, **kwargs):
[_f for _f in [i.environment_id for i in item_list] if _f]
)

result: dict[Rule, dict[str, Any]]
result = {i: {"environment": environments.get(i.environment_id)} for i in item_list}
ras = list(
RuleActivity.objects.filter(
Expand All @@ -104,15 +114,18 @@ def get_attrs(self, item_list, user, **kwargs):
}

for rule_activity in ras:
u = users.get(rule_activity.user_id)
if u:
creator = {
"id": u.id,
"name": u.get_display_name(),
"email": u.email,
}
else:
if rule_activity.user_id is None:
creator = None
else:
u = users.get(rule_activity.user_id)
if u:
creator = {
"id": u.id,
"name": u.get_display_name(),
"email": u.email,
}
else:
creator = None

result[rule_activity.rule].update({"created_by": creator})

Expand Down Expand Up @@ -226,7 +239,7 @@ def serialize(self, obj, attrs, user, **kwargs) -> RuleSerializerResponse:
# Integrations can be deleted and we don't want to fail to load the rule
pass

d = {
d: RuleSerializerResponse = {
# XXX(dcramer): we currently serialize unsaved rule objects
# as part of the rule editor
"id": str(obj.id) if obj.id else None,
Expand All @@ -245,6 +258,7 @@ def serialize(self, obj, attrs, user, **kwargs) -> RuleSerializerResponse:
"environment": environment.name if environment is not None else None,
"projects": [obj.project.slug],
"status": "active" if obj.status == ObjectStatus.ACTIVE else "disabled",
"snooze": "snooze" in attrs,
}
if "last_triggered" in attrs:
d["lastTriggered"] = attrs["last_triggered"]
Expand All @@ -254,7 +268,6 @@ def serialize(self, obj, attrs, user, **kwargs) -> RuleSerializerResponse:

if "snooze" in attrs:
snooze = attrs["snooze"]
d["snooze"] = True
created_by = None
if user.id == snooze.get("owner_id"):
created_by = "You"
Expand All @@ -266,8 +279,6 @@ def serialize(self, obj, attrs, user, **kwargs) -> RuleSerializerResponse:
if created_by is not None:
d["snoozeCreatedBy"] = created_by
d["snoozeForEveryone"] = snooze.get("user_id") is None
else:
d["snooze"] = False

if "disable_date" in attrs:
d["disableReason"] = "noisy"
Expand Down
Loading