# What this PR does This PR adds support for routing alerts based on labels. https://www.loom.com/share/4401de6e3c4945d5b8961fe43ee373c9 Additionally: - improve the typing around the `get_object` method that is inherited by [`PublicPrimaryKeyMixin.get_object`](https://github.com/grafana/oncall/blob/dev/engine/common/api_helpers/mixins.py#L153) in most of our models. `PublicPrimaryKeyMixin` is generic, so it can be more strongly typed when it is being subclassed, which results in better typing of the `get_object` method in child classes - I decided to do this because I started looking into this task via the [`AlertReceiveChannelView.send_demo_alert` method/endpoint](https://github.com/grafana/oncall/blob/dev/engine/apps/api/views/alert_receive_channel.py#L242). Within that method, `instance` is not typed because the inherited `get_object` method is not typed.. I digress 😄 - improve typing around `Alert.create` and `apps.integrations.tasks.create_alert` functions - make `Alert.render_group_data` more DRY by extracting some logic out into `Alert._apply_jinja_template_to_alert_payload_and_labels` - deduplicate the logic of `value.strip().lower() in ["1", "true", "ok"]` into a shared function, `common.jinja_templater.apply_jinja_template.templated_value_is_truthy` Closes https://github.com/grafana/oncall-private/issues/2490 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required) - [x] Documentation added (or `pr:no public docs` PR label added if not required) (will be done in #3762)
52 lines
2 KiB
Python
52 lines
2 KiB
Python
from rest_framework import mixins, status, viewsets
|
|
from rest_framework.decorators import action
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
|
|
from apps.api.permissions import RBACPermission
|
|
from apps.api.serializers.telegram import TelegramToOrganizationConnectorSerializer
|
|
from apps.auth_token.auth import PluginAuthentication
|
|
from apps.telegram.models import TelegramToOrganizationConnector
|
|
from common.api_helpers.mixins import PublicPrimaryKeyMixin
|
|
from common.insight_log.chatops_insight_logs import ChatOpsEvent, ChatOpsTypePlug, write_chatops_insight_log
|
|
|
|
|
|
class TelegramChannelViewSet(
|
|
PublicPrimaryKeyMixin[TelegramToOrganizationConnector],
|
|
mixins.RetrieveModelMixin,
|
|
mixins.DestroyModelMixin,
|
|
mixins.ListModelMixin,
|
|
viewsets.GenericViewSet,
|
|
):
|
|
authentication_classes = (PluginAuthentication,)
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
rbac_permissions = {
|
|
"metadata": [RBACPermission.Permissions.CHATOPS_READ],
|
|
"list": [RBACPermission.Permissions.CHATOPS_READ],
|
|
"retrieve": [RBACPermission.Permissions.CHATOPS_READ],
|
|
"destroy": [RBACPermission.Permissions.CHATOPS_UPDATE_SETTINGS],
|
|
"set_default": [RBACPermission.Permissions.CHATOPS_UPDATE_SETTINGS],
|
|
}
|
|
|
|
serializer_class = TelegramToOrganizationConnectorSerializer
|
|
|
|
def get_queryset(self):
|
|
return TelegramToOrganizationConnector.objects.filter(organization=self.request.user.organization)
|
|
|
|
@action(detail=True, methods=["post"])
|
|
def set_default(self, request, pk):
|
|
telegram_channel = self.get_object()
|
|
telegram_channel.make_channel_default(request.user)
|
|
|
|
return Response(status=status.HTTP_200_OK)
|
|
|
|
def perform_destroy(self, instance):
|
|
user = self.request.user
|
|
write_chatops_insight_log(
|
|
author=user,
|
|
event_name=ChatOpsEvent.CHANNEL_DISCONNECTED,
|
|
chatops_type=ChatOpsTypePlug.TELEGRAM.value,
|
|
channel_name=instance.channel_name,
|
|
)
|
|
instance.delete()
|