11import re
2- import datetime
32import i18n # type: ignore
43from slack_sdk import WebClient
5- from slack_bolt import Ack
4+ from slack_bolt import Ack , App
65
76from integrations import opsgenie
87from integrations .slack import users as slack_users
9- from integrations .sentinel import log_to_sentinel
108from integrations .google_workspace import meet
9+ from integrations .sentinel import log_to_sentinel
1110
1211from modules .incident import (
12+ incident_conversation ,
1313 incident_folder ,
1414 incident_document ,
1515 db_operations ,
3131i18n .set ("fallback" , "en-US" )
3232
3333
34- def register (bot ):
34+ def register (bot : App ):
3535 bot .command (f"/{ PREFIX } incident" )(open_create_incident_modal )
3636 bot .view ("incident_view" )(submit )
3737 bot .action ("incident_change_locale" )(handle_change_locale_button )
3838
3939
40- def open_create_incident_modal (client , ack , command , body ):
40+ def open_create_incident_modal (client : WebClient , ack , command , body ):
4141 ack ()
42+ # private_metadata = json.dumps(
43+ # {
44+ # "channel_id": body["channel"]["id"],
45+ # "message_ts": body["message_ts"],
46+ # }
47+ # )
4248 logger .info (
4349 "incident_command_called" ,
4450 command = command ,
@@ -73,7 +79,7 @@ def open_create_incident_modal(client, ack, command, body):
7379 }
7480 for i in folders
7581 ]
76- loaded_view = generate_incident_modal_view (command , options , locale )
82+ loaded_view = generate_incident_modal_view (command , options , None , locale )
7783 client .views_update (view_id = view ["id" ], view = loaded_view )
7884
7985
@@ -96,7 +102,7 @@ def handle_change_locale_button(ack, client, body):
96102 command = {"text" : body ["view" ]["state" ]["values" ]["name" ]["name" ]["value" ]}
97103 if command ["text" ] is None :
98104 command ["text" ] = ""
99- view = generate_incident_modal_view (command , options , locale )
105+ view = generate_incident_modal_view (command , options , None , locale )
100106 client .views_update (view_id = body ["view" ]["id" ], view = view )
101107
102108
@@ -126,6 +132,53 @@ def submit(ack: Ack, view, say, body, client: WebClient): # noqa: C901
126132 ack (response_action = "errors" , errors = errors )
127133 return
128134
135+ channel_id = None
136+ channel_name = None
137+ slug = None
138+
139+ # private_metadata = json.loads(body["view"].get("private_metadata"))
140+ # source_channel_id = private_metadata.get("channel_id")
141+ # source_message_ts = private_metadata.get("message_ts")
142+ try :
143+ channel_created = incident_conversation .create_incident_conversation (
144+ client , name
145+ )
146+ channel_id = channel_created ["channel_id" ]
147+ channel_name = channel_created ["channel_name" ]
148+ slug = channel_created ["slug" ]
149+
150+ except Exception as e :
151+ logger .error (
152+ "incident_channel_creation_failed" ,
153+ error = str (e ),
154+ incident_name = name ,
155+ )
156+ say (
157+ text = ":warning: Channel creation failed. Please contact the SRE team." ,
158+ channel = body ["user" ]["id" ],
159+ )
160+ return
161+
162+ # if private_metadata:
163+ # # private_metadata = json.loads(private_metadata)
164+ # logger.info("private_metadata_found", private_metadata=private_metadata)
165+ # incident_alert.update_alert_with_channel_link(
166+ # client,
167+ # source_channel_id,
168+ # source_message_ts,
169+ # incident_details={"channel_id": channel_id, "channel_name": channel_name},
170+ # )
171+
172+ logger .info (
173+ "incident_channel_created" ,
174+ channel_id = channel_id ,
175+ channel_name = channel_name ,
176+ slug = slug ,
177+ )
178+
179+ view = generate_success_modal (body , channel_id , channel_name )
180+ client .views_open (trigger_id = body ["trigger_id" ], view = view )
181+
129182 logger .info (
130183 "incident_modal_submitted" ,
131184 name = name ,
@@ -150,42 +203,16 @@ def submit(ack: Ack, view, say, body, client: WebClient): # noqa: C901
150203 if r .get ("ok" ):
151204 oncall .append (r ["user" ])
152205
153- date = datetime .datetime .now ().strftime ("%Y-%m-%d" )
154- slug = f"{ date } { name } " .replace (" " , "-" ).lower ()
155-
156206 # Create channel
157- # if we are testing ie PREFIX is "dev" then create the channel with name incident-dev-{slug}. Otherwise create the channel with name incident-{slug}
158207 environment = "prod"
159- channel_to_create = f"incident-{ slug } "
160208 if PREFIX == "dev-" :
161209 environment = "dev"
162- channel_to_create = f"incident-dev-{ slug } "
163- try :
164- if len (channel_to_create ) > 80 :
165- channel_to_create = channel_to_create [:80 ]
166- response = client .conversations_create (name = channel_to_create )
167- except Exception as e :
168- logger .error (
169- "incident_channel_creation_failed" ,
170- error = str (e ),
171- channel_name = channel_to_create ,
172- )
173- say (
174- text = ":warning: Channel creation failed. Please contact the SRE team." ,
175- channel = body ["user" ]["id" ],
176- )
177- return
178- channel_id = response ["channel" ]["id" ]
179- channel_name = response ["channel" ]["name" ]
180210 logger .info (
181211 "incident_channel_created" ,
182212 channel_id = channel_id ,
183213 channel_name = channel_name ,
184214 )
185215
186- view = generate_success_modal (body , channel_id , channel_name )
187- client .views_open (trigger_id = body ["trigger_id" ], view = view )
188-
189216 channel_url = f"https://gcdigital.slack.com/archives/{ channel_id } "
190217
191218 # Set topic
@@ -318,13 +345,19 @@ def submit(ack: Ack, view, say, body, client: WebClient): # noqa: C901
318345 )
319346
320347
321- def generate_incident_modal_view (command , options = [], locale = "en-US" ):
348+ def generate_incident_modal_view (
349+ command , options = None , private_metadata = None , locale = "en-US"
350+ ):
351+ """Generate the incident creation modal view."""
352+ if options is None :
353+ options = []
322354 handbook_string = f"For more details on what constitutes a security incident, visit our <{ INCIDENT_HANDBOOK_URL } |Incident Management Handbook>"
323355 return {
324356 "type" : "modal" ,
325357 "callback_id" : "incident_view" ,
326358 "title" : {"type" : "plain_text" , "text" : i18n .t ("incident.modal.title" )},
327359 "submit" : {"type" : "plain_text" , "text" : i18n .t ("incident.submit" )},
360+ "private_metadata" : private_metadata ,
328361 "blocks" : [
329362 {
330363 "type" : "actions" ,
0 commit comments