def add_status_update(incident: Incident, user_id: str, message: str): name = get_user_profile(user_id)["name"] action_reporter, _ = ExternalUser.objects.get_or_create_slack( external_id=user_id, display_name=name) StatusUpdate(incident=incident, text=message, user=action_reporter).save() msg = Message() msg.add_block( Section( block_id="update", text=Text(f":warning: *Update:*\n{message} "), )) comms_channel = CommsChannel.objects.get(incident=incident) ts = HeadlinePost.objects.get(incident=incident).message_ts msg.send(comms_channel.channel_id, None) settings.SLACK_CLIENT.send_message(settings.INCIDENT_CHANNEL_ID, ":warning: *Update*: " + message, thread_ts=ts) # comms_channel.post_in_channel(msg) return True, None
def update_in_slack(self): "Creates/updates the slack headline post with the latest incident info" logging.info( f"Updating headline post in Slack for incident {self.incident.pk}") msg = Message() # Set the fallback text so notifications look nice msg.set_fallback_text( f"{self.incident.report} reported by {user_reference(self.incident.reporter)}" ) # Add report/people msg.add_block( Section(block_id="report", text=Text(f"*{self.incident.report}*"))) msg.add_block( Section( block_id="reporter", text=Text( f"🙋🏻♂️ Reporter: {user_reference(self.incident.reporter.external_id)}" ), )) incident_lead_text = (user_reference(self.incident.lead.external_id) if self.incident.lead else "-") msg.add_block( Section(block_id="lead", text=Text(f"👩🚒 Incident Lead: {incident_lead_text}"))) msg.add_block(Divider()) # Add additional info msg.add_block( Section( block_id="status", text=Text( f"{self.incident.status_emoji()} Status: {self.incident.status_text().capitalize()}" ), )) severity_text = (self.incident.severity_text().capitalize() if self.incident.severity_text() else "-") msg.add_block( Section( block_id="severity", text=Text( f"{self.incident.severity_emoji()} Severity: {severity_text}" ), )) doc_url = urljoin( settings.SITE_URL, reverse("incident_doc", kwargs={"incident_id": self.incident.pk}), ) msg.add_block( Section( block_id="incident_doc", text=Text( f"📄 Details: <{doc_url}|Incident {self.incident.pk}>"), )) if not self.incident.report_only: channel_ref = (channel_reference(self.comms_channel.channel_id) if self.comms_channel else None) if channel_ref: msg.add_block( Section( block_id="comms_channel", text=Text( f":slack: Incident Room: {channel_ref or '-'}"), )) if not self.incident.report_only: zoom_ref = self.zoom_meeting.weblink if self.zoom_meeting else None if zoom_ref: msg.add_block( Section( block_id="zoom_meeting", text=Text( f":telephone_receiver: Zoom Meeting: {zoom_ref or '-'}" ), )) # Add buttons (if the incident is open) if not self.incident.is_closed(): msg.add_block(Section(text=Text("Need something else?"))) actions = Actions(block_id="actions") # Add all actions mapped by @headline_post_action decorators for key in sorted(SLACK_HEADLINE_POST_ACTION_MAPPINGS.keys()): funclist = SLACK_HEADLINE_POST_ACTION_MAPPINGS[key] for f in funclist: action = f(self) if action: actions.add_element(action) msg.add_block(actions) # Post / update the slack message if self.incident.report_only and hasattr(settings, "INCIDENT_REPORT_CHANNEL_ID"): channel_id = settings.INCIDENT_REPORT_CHANNEL_ID else: channel_id = settings.INCIDENT_CHANNEL_ID try: response = msg.send(channel_id, self.message_ts) logger.info( f"Got response back from Slack after updating headline post: {response}" ) # Save the message ts identifier if not already set if not self.message_ts: self.message_ts = response["ts"] self.save() except SlackError as e: logger.error( f"Failed to update headline post in {channel_id} with ts {self.message_ts}. Error: {e}" )
def update_in_slack(self): "Creates/updates the slack headline post with the latest incident info" logging.info( f"Updating headline post in Slack for incident {self.incident.pk}") msg = Message() # Set the fallback text so notifications look nice msg.set_fallback_text( f"{self.incident.report} reported by {user_reference(self.incident.reporter)}" ) # Add report/people msg.add_block( Section(block_id="report", text=Text(f"*{self.incident.report}*"))) msg.add_block( Section( block_id="reporter", text=Text( f"🙋🏻♂️ Reporter: {user_reference(self.incident.reporter.external_id)}" ), )) # incident_lead_text = ( # user_reference(self.incident.lead.external_id) # if self.incident.lead # else "-" # ) # msg.add_block( # Section( # block_id="lead", text=Text(f"👩🚒 Incident Lead: {incident_lead_text}") # ) # ) pd_url = urljoin( settings.PAGER_DUTY_BASE_URL, "incidents/" + self.incident.pdschedule if self.incident.pdschedule else "", ) msg.add_block( Section( block_id="pager", text=Text( f"{self.incident.pd_emoji()} Pagerduty schedule: <{pd_url}|{self.incident.pdschedule}>" ), )) msg.add_block(Divider()) # Add additional info msg.add_block( Section( block_id="status", text=Text( f"{self.incident.status_emoji()} Status: {self.incident.status_text().capitalize()}" ), )) severity_text = (self.incident.severity_text().capitalize() if self.incident.severity_text() else "-") msg.add_block( Section( block_id="severity", text=Text( f"{self.incident.severity_emoji()} Severity: {severity_text}" ), )) doc_url = urljoin( settings.SITE_URL, reverse("incident_doc", kwargs={"incident_id": self.incident.pk}), ) msg.add_block( Section( block_id="incident_doc", text=Text( f"📄 Document: <{doc_url}|Incident {self.incident.pk}>"), )) if not self.incident.report_only: channel_ref = (channel_reference(self.comms_channel.channel_id) if self.comms_channel else None) if channel_ref: msg.add_block( Section( block_id="comms_channel", text=Text(f"🗣 Comms Channel: {channel_ref or '-'}"), )) # Add buttons (if the incident is open) if not self.incident.is_closed(): msg.add_block(Section(text=Text("Need something else?"))) actions = Actions(block_id="actions") # Add all actions mapped by @headline_post_action decorators for key in sorted(SLACK_HEADLINE_POST_ACTION_MAPPINGS.keys()): funclist = SLACK_HEADLINE_POST_ACTION_MAPPINGS[key] for f in funclist: action = f(self) if action: actions.add_element(action) msg.add_block(actions) # Post / update the slack message if self.incident.report_only and hasattr(settings, "INCIDENT_REPORT_CHANNEL_ID"): channel_id = settings.INCIDENT_REPORT_CHANNEL_ID else: channel_id = settings.INCIDENT_CHANNEL_ID response = msg.send(channel_id, self.message_ts) logging.info( f"Got response back from Slack after updating headline post: {response}" ) # Save the message ts identifier if not already set if not self.message_ts: self.message_ts = response["ts"] self.save()