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

Only run member join or remove for incident channels #4457

Merged
merged 2 commits into from
Feb 28, 2024
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
36 changes: 22 additions & 14 deletions src/dispatch/plugins/dispatch_slack/incident/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
shortcut_context_middleware,
)
from dispatch.plugins.dispatch_slack.modals.common import send_success_modal
from dispatch.plugins.dispatch_slack.models import MonitorMetadata, TaskMetadata
from dispatch.plugins.dispatch_slack.models import MonitorMetadata, TaskMetadata, IncidentSubjects, CaseSubjects
from dispatch.plugins.dispatch_slack.service import (
get_user_email,
get_user_profile_by_email,
Expand Down Expand Up @@ -365,7 +365,7 @@ def handle_list_incidents_command(

projects = []

if context["subject"].type == "incident":
if context["subject"].type == IncidentSubjects.incident:
# command was run in an incident conversation
incident = incident_service.get(db_session=db_session, incident_id=context["subject"].id)
projects.append(incident.project)
Expand Down Expand Up @@ -623,7 +623,7 @@ def draw_task_modal(
assignees = [f"<{a.individual.weblink}|{a.individual.name}>" for a in task.assignees]

button_metadata = TaskMetadata(
type="incident",
type=IncidentSubjects.incident,
action_type=action_type,
organization_slug=task.project.organization.slug,
id=task.incident.id,
Expand Down Expand Up @@ -695,7 +695,7 @@ def handle_timeline_added_event(
message_sender_id = response["messages"][0]["user"]

# TODO: (wshel) handle case reactions
if context["subject"].type == "incident":
if context["subject"].type == IncidentSubjects.incident:
# we fetch the incident
incident = incident_service.get(db_session=db_session, incident_id=context["subject"].id)

Expand Down Expand Up @@ -736,7 +736,7 @@ def handle_timeline_added_event(


@message_dispatcher.add(
subject="incident", exclude={"subtype": ["channel_join", "channel_leave"]}
subject=IncidentSubjects.incident, exclude={"subtype": ["channel_join", "channel_leave"]}
) # we ignore channel join and leave messages
def handle_participant_role_activity(
ack: Ack, db_session: Session, context: BoltContext, user: DispatchUser
Expand Down Expand Up @@ -783,7 +783,7 @@ def handle_participant_role_activity(


@message_dispatcher.add(
subject="incident", exclude={"subtype": ["channel_join", "group_join"]}
subject=IncidentSubjects.incident, exclude={"subtype": ["channel_join", "group_join"]}
) # we ignore user channel and group join messages
def handle_after_hours_message(
ack: Ack,
Expand Down Expand Up @@ -832,7 +832,7 @@ def handle_after_hours_message(
)


@message_dispatcher.add(subject="incident")
@message_dispatcher.add(subject=IncidentSubjects.incident)
def handle_thread_creation(
ack: Ack, client: WebClient, payload: dict, context: BoltContext, request: BoltRequest
) -> None:
Expand All @@ -852,7 +852,7 @@ def handle_thread_creation(
)


@message_dispatcher.add(subject="incident")
@message_dispatcher.add(subject=IncidentSubjects.incident)
def handle_message_monitor(
ack: Ack,
payload: dict,
Expand Down Expand Up @@ -889,7 +889,7 @@ def handle_message_monitor(
status_text += f"*{k.title()}*:\n{v.title()}\n"

button_metadata = MonitorMetadata(
type="incident",
type=IncidentSubjects.incident,
organization_slug=incident.project.organization.slug,
id=incident.id,
plugin_instance_id=p.id,
Expand Down Expand Up @@ -955,6 +955,10 @@ def handle_member_joined_channel(
"Unable to handle member_joined_channel Slack event. Dispatch user unknown."
)

if context["subject"].type != IncidentSubjects.incident:
# only run this workflow for incidents
return

participant = incident_flows.incident_add_or_reactivate_participant_flow(
user_email=user.email, incident_id=context["subject"].id, db_session=db_session
)
Expand Down Expand Up @@ -1001,6 +1005,10 @@ def handle_member_left_channel(
) -> None:
ack()

if context["subject"].type != IncidentSubjects.incident:
# only run this workflow for incidents
return

incident_flows.incident_remove_participant_flow(
user.email, context["subject"].id, db_session=db_session
)
Expand Down Expand Up @@ -1134,7 +1142,7 @@ def handle_update_participant_command(
"""Handles the update participant command."""
ack()

if context["subject"].type == "case":
if context["subject"].type == CaseSubjects.case:
raise CommandError("Command is not currently available for cases.")

incident = incident_service.get(
Expand Down Expand Up @@ -1220,7 +1228,7 @@ def handle_update_notifications_group_command(
ack()

# TODO handle cases
if context["subject"].type == "case":
if context["subject"].type == CaseSubjects.case:
raise CommandError("Command is not currently available for cases.")

incident = incident_service.get(db_session=db_session, incident_id=context["subject"].id)
Expand Down Expand Up @@ -1426,7 +1434,7 @@ def handle_engage_oncall_command(
ack()

# TODO: handle cases
if context["subject"].type == "case":
if context["subject"].type == CaseSubjects.case:
raise CommandError("Command is not currently available for cases.")

incident = incident_service.get(db_session=db_session, incident_id=context["subject"].id)
Expand Down Expand Up @@ -1551,7 +1559,7 @@ def handle_report_tactical_command(
"""Handles the report tactical command."""
ack()

if context["subject"].type == "case":
if context["subject"].type == CaseSubjects.case:
raise CommandError("Command is not available outside of incident channels.")

# we load the most recent tactical report
Expand Down Expand Up @@ -1670,7 +1678,7 @@ def handle_report_executive_command(
"""Handles executive report command."""
ack()

if context["subject"].type == "case":
if context["subject"].type == CaseSubjects.case:
raise CommandError("Command is not available outside of incident channels.")

executive_report = report_service.get_most_recent_by_incident_id_and_type(
Expand Down
6 changes: 3 additions & 3 deletions src/dispatch/plugins/dispatch_slack/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from dispatch.project import service as project_service

from .exceptions import ContextError, RoleError
from .models import EngagementMetadata, SubjectMetadata, FormMetadata
from .models import EngagementMetadata, SubjectMetadata, FormMetadata, IncidentSubjects, CaseSubjects

log = logging.getLogger(__file__)

Expand All @@ -43,14 +43,14 @@ def resolve_context_from_conversation(channel_id: str, thread_id: str = None) ->
if conversation:
if conversation.incident:
subject = SubjectMetadata(
type="incident",
type=IncidentSubjects.incident,
id=conversation.incident_id,
organization_slug=slug,
project_id=conversation.incident.project_id,
)
else:
subject = SubjectMetadata(
type="case",
type=CaseSubjects.case,
id=conversation.case_id,
organization_slug=slug,
project_id=conversation.case.project_id,
Expand Down
4 changes: 4 additions & 0 deletions src/dispatch/plugins/dispatch_slack/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class CaseSubjects(DispatchEnum):
case = "case"


class IncidentSubjects(DispatchEnum):
incident = "incident"


class SignalSubjects(DispatchEnum):
signal = "signal"
signal_instance = "signal_instance"
Loading