# What this PR does Adds a button to reinstall the Slack app to migrate to Unified Slack App. Also adds backend support for this. <img width="1204" alt="Screenshot 2024-07-18 at 18 33 08" src="https://github.com/user-attachments/assets/a326b4a2-fc65-4b88-98c0-2955e3717e3a"> ## Which issue(s) this PR closes Related to https://github.com/grafana/oncall-gateway/issues/252 <!-- *Note*: if you have more than one GitHub issue that this PR closes, be sure to preface each issue link with a [closing keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue). This ensures that the issue(s) are auto-closed once the PR has been merged. --> ## 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.
72 lines
3 KiB
Python
72 lines
3 KiB
Python
import logging
|
|
|
|
from django.conf import settings
|
|
|
|
from apps.chatops_proxy.utils import unlink_slack_team
|
|
from apps.slack.tasks import (
|
|
clean_slack_integration_leftovers,
|
|
populate_slack_channels_for_team,
|
|
populate_slack_usergroups_for_team,
|
|
unpopulate_slack_user_identities,
|
|
)
|
|
from common.insight_log import ChatOpsEvent, ChatOpsTypePlug, write_chatops_insight_log
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class SlackInstallationExc(Exception):
|
|
"""
|
|
SlackInstallationExc represents some exception happened while managing Slack integration.
|
|
"""
|
|
|
|
def __init__(self, error_message=None):
|
|
# error message is a user-visible error message
|
|
self.error_message = error_message
|
|
|
|
|
|
def install_slack_integration(organization, user, oauth_response):
|
|
"""
|
|
Installs Slack integration for the organization.
|
|
Raises:
|
|
SlackInstallationExc if organization already has Slack integration.
|
|
"""
|
|
from apps.slack.models import SlackTeamIdentity
|
|
|
|
if organization.slack_team_identity and not organization.slack_team_identity.needs_reinstall:
|
|
raise SlackInstallationExc("Organization already has Slack integration")
|
|
|
|
slack_team_id = oauth_response["team"]["id"]
|
|
slack_team_identity, _ = SlackTeamIdentity.objects.get_or_create(slack_id=slack_team_id)
|
|
# update slack oauth fields by data from response
|
|
slack_team_identity.update_oauth_fields(user, organization, oauth_response)
|
|
write_chatops_insight_log(
|
|
author=user, event_name=ChatOpsEvent.WORKSPACE_CONNECTED, chatops_type=ChatOpsTypePlug.SLACK.value
|
|
)
|
|
populate_slack_channels_for_team.apply_async((slack_team_identity.pk,))
|
|
user.slack_user_identity.update_profile_info()
|
|
# todo slack: do we need update info for all existing slack users in slack team?
|
|
# 24.03.2024 - this todo here for a while. populate_slack_user_identities automatically links users to slack.
|
|
# Should be useful if we want to unify with Incident.
|
|
# populate_slack_user_identities.apply_async((organization.pk,))
|
|
populate_slack_usergroups_for_team.apply_async((slack_team_identity.pk,), countdown=10)
|
|
|
|
|
|
def uninstall_slack_integration(organization, user):
|
|
"""
|
|
Uninstalls Slack integration for the organization.
|
|
Raises:
|
|
SlackInstallationExc if organization has no Slack integration.
|
|
"""
|
|
slack_team_identity = organization.slack_team_identity
|
|
if slack_team_identity is not None:
|
|
clean_slack_integration_leftovers.apply_async((organization.pk,))
|
|
if settings.FEATURE_MULTIREGION_ENABLED and not settings.UNIFIED_SLACK_APP_ENABLED:
|
|
unlink_slack_team(str(organization.uuid), slack_team_identity.slack_id)
|
|
write_chatops_insight_log(
|
|
author=user,
|
|
event_name=ChatOpsEvent.WORKSPACE_DISCONNECTED,
|
|
chatops_type=ChatOpsTypePlug.SLACK.value,
|
|
)
|
|
unpopulate_slack_user_identities(organization.pk, True)
|
|
else:
|
|
raise SlackInstallationExc("Organization has no Slack integration.")
|