# What this PR does Related to https://github.com/grafana/oncall-private/issues/2947 Right now `general_log_channel_id` is just a string value representing the Slack Channel ID (ex. `C043HQ70QMB`). This PR migrates this instead to be a foreign key relationship on the `slack_slackchannel` table and updates all references to `general_log_channel_id`. Tested migrations locally: ```bash Operations to perform: Apply all migrations: [redacted secret grafana-admin-creds:admin-user], alerts, auth, auth_token, base, contenttypes, email, exotel, fcm_django, google, heartbeat, labels, mobile_app, oss_installation, phone_notifications, schedules, sessions, slack, social_django, telegram, twilioapp, user_management, webhooks, zvonok Running migrations: Applying user_management.0024_organization_general_log_slack_channel... OK source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Starting migration to populate general_log_slack_channel field. source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Total organizations to process: 1 source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Organization 1 updated with SlackChannel 2 (slack_id: C043LL6RTS7). source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Finished migration. Total organizations processed: 1. Organizations updated: 1. Missing SlackChannels: 0. Applying user_management.0025_auto_20241017_1919... OK ``` ## Future incoming PRs - Drop `Organization.general_log_channel_id` column - Migrate `ChannelFilter.slack_channel_id` and `ResolutionNoteSlackMessage.slack_channel_id` to use foreign key relationships ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes.
132 lines
4.7 KiB
Python
132 lines
4.7 KiB
Python
from contextlib import suppress
|
|
|
|
from rest_framework import status
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from apps.api.permissions import RBACPermission
|
|
from apps.api.serializers.organization import CurrentOrganizationConfigChecksSerializer, CurrentOrganizationSerializer
|
|
from apps.auth_token.auth import PluginAuthentication
|
|
from apps.base.messaging import get_messaging_backend_from_id
|
|
from apps.mobile_app.auth import MobileAppAuthTokenAuthentication
|
|
from apps.telegram.client import TelegramClient
|
|
from common.insight_log import EntityEvent, write_resource_insight_log
|
|
|
|
|
|
class CurrentOrganizationView(APIView):
|
|
authentication_classes = (
|
|
MobileAppAuthTokenAuthentication,
|
|
PluginAuthentication,
|
|
)
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
rbac_permissions = {
|
|
"get": [RBACPermission.Permissions.OTHER_SETTINGS_READ],
|
|
"put": [RBACPermission.Permissions.OTHER_SETTINGS_WRITE],
|
|
}
|
|
|
|
def get(self, request):
|
|
organization = request.auth.organization
|
|
serializer = CurrentOrganizationSerializer(organization, context={"request": request})
|
|
return Response(serializer.data)
|
|
|
|
def put(self, request):
|
|
organization = self.request.auth.organization
|
|
prev_state = organization.insight_logs_serialized
|
|
serializer = CurrentOrganizationSerializer(
|
|
instance=organization, data=request.data, context={"request": request}
|
|
)
|
|
serializer.is_valid(raise_exception=True)
|
|
serializer.save()
|
|
new_state = serializer.instance.insight_logs_serialized
|
|
write_resource_insight_log(
|
|
instance=serializer.instance,
|
|
author=self.request.user,
|
|
event=EntityEvent.UPDATED,
|
|
prev_state=prev_state,
|
|
new_state=new_state,
|
|
)
|
|
return Response(serializer.data)
|
|
|
|
|
|
class OrganizationConfigChecksView(APIView):
|
|
authentication_classes = (PluginAuthentication,)
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
rbac_permissions = {
|
|
"get": [RBACPermission.Permissions.OTHER_SETTINGS_READ],
|
|
}
|
|
|
|
def get(self, request):
|
|
organization = request.auth.organization
|
|
serializer = CurrentOrganizationConfigChecksSerializer(organization)
|
|
return Response(serializer.data)
|
|
|
|
|
|
class GetTelegramVerificationCode(APIView):
|
|
authentication_classes = (PluginAuthentication,)
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
rbac_permissions = {
|
|
"get": [RBACPermission.Permissions.INTEGRATIONS_WRITE],
|
|
}
|
|
|
|
def get(self, request):
|
|
organization = request.auth.organization
|
|
user = request.user
|
|
from apps.telegram.models import TelegramChannelVerificationCode
|
|
|
|
with suppress(TelegramChannelVerificationCode.DoesNotExist):
|
|
existing_verification_code = organization.telegram_verification_code
|
|
existing_verification_code.delete()
|
|
new_code = TelegramChannelVerificationCode.objects.create(organization=organization, author=user)
|
|
telegram_client = TelegramClient()
|
|
bot_username = telegram_client.api_client.username
|
|
bot_link = f"https://t.me/{bot_username}"
|
|
return Response(
|
|
{"telegram_code": str(new_code.uuid_with_org_uuid), "bot_link": bot_link}, status=status.HTTP_200_OK
|
|
)
|
|
|
|
|
|
class GetChannelVerificationCode(APIView):
|
|
authentication_classes = (PluginAuthentication,)
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
rbac_permissions = {
|
|
"get": [RBACPermission.Permissions.INTEGRATIONS_WRITE],
|
|
}
|
|
|
|
def get(self, request):
|
|
organization = request.auth.organization
|
|
backend_id = request.query_params.get("backend")
|
|
backend = get_messaging_backend_from_id(backend_id)
|
|
if backend is None:
|
|
return Response(status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
code = backend.generate_channel_verification_code(organization)
|
|
return Response(code)
|
|
|
|
|
|
class SetDefaultSlackChannel(APIView):
|
|
authentication_classes = (PluginAuthentication,)
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
rbac_permissions = {
|
|
"post": [RBACPermission.Permissions.CHATOPS_UPDATE_SETTINGS],
|
|
}
|
|
|
|
def post(self, request):
|
|
from apps.slack.models import SlackChannel
|
|
|
|
organization = request.auth.organization
|
|
slack_team_identity = organization.slack_team_identity
|
|
slack_channel_id = request.data["id"]
|
|
|
|
slack_channel = SlackChannel.objects.get(
|
|
public_primary_key=slack_channel_id, slack_team_identity=slack_team_identity
|
|
)
|
|
|
|
organization.set_default_slack_channel(slack_channel, request.user)
|
|
|
|
return Response(status=200)
|