def create(*, db_session, service_in: ServiceCreate) -> Service: """Creates a new service.""" project = project_service.get_by_name(db_session=db_session, name=service_in.project.name) terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in service_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, project_id=project.id, name=n.name) for n in service_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, project_id=project.id, name=n.name) for n in service_in.incident_types ] service = Service( **service_in.dict(exclude={ "terms", "incident_priorities", "incident_types", "project" }), incident_priorities=incident_priorities, incident_types=incident_types, terms=terms, project=project, ) db_session.add(service) db_session.commit() return service
def create(*, db_session, document_in: DocumentCreate) -> Document: """Creates a new document.""" terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in document_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in document_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in document_in.incident_types ] project = project_service.get_by_name(db_session=db_session, name=document_in.project.name) # set the last reminder to now if document_in.evergreen: document_in.evergreen_last_reminder_at = datetime.utcnow() document = Document( **document_in.dict(exclude={ "terms", "incident_priorities", "incident_types", "project" }), incident_priorities=incident_priorities, incident_types=incident_types, terms=terms, project=project, ) db_session.add(document) db_session.commit() return document
def report_incident( db_session: SessionLocal, channel_id: str, project_name: str = None, title: str = None, description: str = None, ): """Builds all blocks required for the reporting incident modal.""" project = project_service.get_by_name(db_session=db_session, name=project_name) modal_template = { "type": "modal", "title": { "type": "plain_text", "text": "Incident Report" }, "blocks": [ { "type": "context", "elements": [{ "type": "mrkdwn", "text": "If you suspect an incident and require help, " "please fill out the following to the best of your abilities.", }], }, title_input_block(initial_value=title), description_input_block(initial_value=description), project_select_block(db_session=db_session, initial_option=project), ], "close": { "type": "plain_text", "text": "Cancel" }, "submit": { "type": "plain_text", "text": "Submit" }, "callback_id": ReportIncidentCallbackId.update_view, "private_metadata": json.dumps({"channel_id": str(channel_id)}), } # switch from update to submit when we have a project if project: modal_template["callback_id"] = ReportIncidentCallbackId.submit_form modal_template["blocks"] += [ incident_type_select_block(db_session=db_session, project_id=project.id), incident_priority_select_block(db_session=db_session, project_id=project.id), tag_multi_select_block(), ] return modal_template
def create(*, db_session, team_contact_in: TeamContactCreate) -> TeamContact: terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in team_contact_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in team_contact_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in team_contact_in.incident_types ] project = project_service.get_by_name(db_session=db_session, name=team_contact_in.project.name) team = TeamContact( **team_contact_in.dict(exclude={ "terms", "incident_priorities", "incident_types", "project" }), project=project, terms=terms, incident_types=incident_types, incident_priorities=incident_priorities, ) db_session.add(team) db_session.commit() return team
def create(*, db_session, document_in: DocumentCreate) -> Document: """Creates a new document.""" project = None if document_in.project: project = project_service.get_by_name(db_session=db_session, name=document_in.project.name) filters = [ search_filter_service.get(db_session=db_session, search_filter_id=f.id) for f in document_in.filters ] # set the last reminder to now if document_in.evergreen: document_in.evergreen_last_reminder_at = datetime.utcnow() document = Document( **document_in.dict(exclude={"project", "filters"}), filters=filters, project=project, ) db_session.add(document) db_session.commit() return document
def create(*, db_session, incident_type_in: IncidentTypeCreate) -> IncidentType: """Creates an incident type.""" project = project_service.get_by_name(db_session=db_session, name=incident_type_in.project.name) incident_type = IncidentType( **incident_type_in.dict( exclude={"commander_service", "template_document", "project"}), project=project, ) if incident_type_in.template_document: template_document = document_service.get( db_session=db_session, document_id=incident_type_in.template_document.id) incident_type.template_document = template_document if incident_type_in.commander_service: commander_service = service_service.get( db_session=db_session, service_id=incident_type_in.commander_service.id) incident_type.commander_service = commander_service if incident_type_in.liaison_service: liaison_service = service_service.get( db_session=db_session, service_id=incident_type_in.liaison_service.id) incident_type.liaison_service = liaison_service db_session.add(incident_type) db_session.commit() return incident_type
def create_or_update_project_default(*, db_session, user: DispatchUser, user_project_in: UserProject): """Creates a new user project or updates an existing one.""" if user_project_in.project.id: project_id = user_project_in.project.id else: project = project_service.get_by_name( db_session=db_session, name=user_project_in.project.name) project_id = project.id user_project = (db_session.query(DispatchUserProject).filter( DispatchUserProject.dispatch_user_id == user.id, ).filter( DispatchUserProject.project_id == project_id).one_or_none()) if not user_project: user_project = DispatchUserProject( dispatch_user_id=user.id, project_id=project_id, default=True, ) db_session.add(user_project) return user_project user_project.default = user_project_in.default return user_project
def create( *, db_session, individual_contact_in: IndividualContactCreate) -> IndividualContact: """Creates an individual.""" project = project_service.get_by_name( db_session=db_session, name=individual_contact_in.project.name) terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in individual_contact_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in individual_contact_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in individual_contact_in.incident_types ] contact = IndividualContact( **individual_contact_in.dict(exclude={ "terms", "incident_priorities", "incident_types", "project" }), terms=terms, incident_types=incident_types, incident_priorities=incident_priorities, project=project, ) db_session.add(contact) db_session.commit() return contact
def get_by_external_id_and_project_name( *, db_session, external_id: str, project_name: str) -> Optional[Service]: """Gets a service by external id (e.g. PagerDuty service id) and project name.""" project = project_service.get_by_name(db_session=db_session, name=project_name) service = get_by_external_id_and_project_id(db_session=db_session, external_id=external_id, project_id=project.id) return service
def create(*, db_session, search_filter_in: SearchFilterCreate) -> SearchFilter: """Creates a new search filter.""" project = project_service.get_by_name(db_session=db_session, name=search_filter_in.project.name) search_filter = SearchFilter(**search_filter_in.dict(exclude={"project"}), project=project) db_session.add(search_filter) db_session.commit() return search_filter
def create(*, db_session, incident_priority_in: IncidentPriorityCreate) -> IncidentPriority: """Creates an incident priority.""" project = project_service.get_by_name( db_session=db_session, name=incident_priority_in.project.name) incident_priority = IncidentPriority(**incident_priority_in.dict( exclude={"project"}), project=project) db_session.add(incident_priority) db_session.commit() return incident_priority
def create(*, db_session, tag_in: TagCreate) -> Tag: tag_type = tag_type_service.get_by_name(db_session=db_session, name=tag_in.tag_type.name) project = project_service.get_by_name(db_session=db_session, name=tag_in.project.name) tag = Tag(**tag_in.dict(exclude={"tag_type", "project"}), project=project, tag_type=tag_type) db_session.add(tag) db_session.commit() return tag
def create(*, db_session, tag_type_in: TagTypeCreate) -> TagType: """ Creates a new tag type. """ project = project_service.get_by_name(db_session=db_session, name=tag_type_in.project.name) tag_type = TagType(**tag_type_in.dict(exclude={"project"}), project=project) db_session.add(tag_type) db_session.commit() return tag_type
def create(*, db_session, term_in: TermCreate) -> Term: project = project_service.get_by_name(db_session=db_session, name=term_in.project.name) definitions = [ definition_service.upsert(db_session=db_session, definition_in=d) for d in term_in.definitions ] term = Term( **term_in.dict(exclude={"definitions", "project"}), project=project, definitions=definitions ) db_session.add(term) db_session.commit() return term
def create(*, db_session, workflow_in: WorkflowCreate) -> Workflow: """Creates a new workflow.""" plugin = plugin_service.get(db_session=db_session, plugin_id=workflow_in.plugin.id) project = project_service.get_by_name(db_session=db_session, name=workflow_in.project.name) workflow = Workflow( **workflow_in.dict(exclude={"plugin", "project"}), plugin=plugin, project=project, ) db_session.add(workflow) db_session.commit() return workflow
def create(*, db_session, incident_cost_type_in: IncidentCostTypeCreate) -> IncidentCostType: """ Creates a new incident cost type. """ project = project_service.get_by_name( db_session=db_session, name=incident_cost_type_in.project.name) incident_cost_type = IncidentCostType(**incident_cost_type_in.dict( exclude={"project"}), project=project) db_session.add(incident_cost_type) db_session.commit() return incident_cost_type
def create(*, db_session, definition_in: DefinitionCreate) -> Definition: terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in definition_in.terms ] project = project_service.get_by_name(db_session=db_session, name=definition_in.project.name) definition = Definition(**definition_in.dict(exclude={"terms", "project"}), project=project, terms=terms) db_session.add(definition) db_session.commit() return definition
def create_instance( *, db_session, plugin_instance_in: PluginInstanceCreate) -> PluginInstance: """Creates a new plugin instance.""" project = project_service.get_by_name(db_session=db_session, name=plugin_instance_in.project.name) plugin = get(db_session=db_session, plugin_id=plugin_instance_in.plugin.id) plugin_instance = PluginInstance(**plugin_instance_in.dict( exclude={"project", "plugin"}), project=project, plugin=plugin) db_session.add(plugin_instance) db_session.commit() return plugin_instance
def create(*, db_session, team_contact_in: TeamContactCreate) -> TeamContact: project = project_service.get_by_name(db_session=db_session, name=team_contact_in.project.name) filters = [ search_filter_service.get(db_session=db_session, search_filter_id=f.id) for f in team_contact_in.filters ] team = TeamContact( **team_contact_in.dict(exclude={"project", "filters"}), filters=filters, project=project, ) db_session.add(team) db_session.commit() return team
def create(*, db_session, service_in: ServiceCreate) -> Service: """Creates a new service.""" project = project_service.get_by_name(db_session=db_session, name=service_in.project.name) filters = [ search_filter_service.get(db_session=db_session, search_filter_id=f.id) for f in service_in.filters ] service = Service( **service_in.dict(exclude={"filters", "project"}), filters=filters, project=project, ) db_session.add(service) db_session.commit() return service
def update( *, db_session, individual_contact: IndividualContact, individual_contact_in: IndividualContactUpdate, ) -> IndividualContact: individual_contact_data = jsonable_encoder(individual_contact_in) project = project_service.get_by_name( db_session=db_session, name=individual_contact_in.project.name) terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in individual_contact_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, project_id=project.id, name=n.name) for n in individual_contact_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, project_id=project.id, name=n.name) for n in individual_contact_in.incident_types ] update_data = individual_contact_in.dict( skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types", "project"}) for field in individual_contact_data: if field in update_data: setattr(individual_contact, field, update_data[field]) individual_contact.terms = terms individual_contact.incident_types = incident_types individual_contact.incident_priorities = incident_priorities individual_contact.project = project db_session.add(individual_contact) db_session.commit() return individual_contact
def create( *, db_session, individual_contact_in: IndividualContactCreate) -> IndividualContact: """Creates an individual.""" project = project_service.get_by_name( db_session=db_session, name=individual_contact_in.project.name) filters = [ search_filter_service.get(db_session=db_session, search_filter_id=f.id) for f in individual_contact_in.filters ] contact = IndividualContact( **individual_contact_in.dict(exclude={"project", "filters"}), filters=filters, project=project, ) db_session.add(contact) db_session.commit() return contact
def create(*, db_session, incident_cost_in: IncidentCostCreate) -> IncidentCost: """ Creates a new incident cost. """ project = project_service.get_by_name(db_session=db_session, name=incident_cost_in.project.name) incident_cost_type = incident_cost_type_service.get_by_name( db_session=db_session, project_id=project.id, incident_cost_type_name=incident_cost_in.incident_cost_type.name, ) incident_cost = IncidentCost( **incident_cost_in.dict(exclude={"incident_cost_type", "project"}), incident_cost_type=incident_cost_type, project=project, ) db_session.add(incident_cost) db_session.commit() return incident_cost
def create(*, db_session, notification_in: NotificationCreate) -> Notification: """Creates a new notification.""" filters = [] if notification_in.filters: filters = [ search_service.get(db_session=db_session, search_filter_id=f.id) for f in notification_in.filters ] project = project_service.get_by_name(db_session=db_session, name=notification_in.project.name) notification = Notification(**notification_in.dict( exclude={"filters", "project"}), filters=filters, project=project) db_session.add(notification) db_session.commit() return notification
def create_or_update_project_role(*, db_session, user: DispatchUser, role_in: UserProject): """Creates a new project role or updates an existing role.""" if not role_in.project.id: project = project_service.get_by_name(db_session=db_session, name=role_in.project.name) project_id = project.id else: project_id = role_in.project.id project_role = (db_session.query(DispatchUserProject).filter( DispatchUserProject.dispatch_user_id == user.id, ).filter( DispatchUserProject.project_id == project_id).one_or_none()) if not project_role: return DispatchUserProject( project_id=project_id, role=role_in.role, ) project_role.role = role_in.role return project_role
def create( *, db_session, project: str, incident_priority: str, incident_type: str, reporter_email: str, title: str, status: str, description: str, tags: List[dict], visibility: str = None, ) -> Incident: """Creates a new incident.""" # We get the incident type by name if not project: project = project_service.get_default(db_session=db_session) if not project: raise Exception("No project specificed and no default has been defined.") else: project = project_service.get_by_name(db_session=db_session, name=project["name"]) if not incident_type: incident_type = incident_type_service.get_default(db_session=db_session) if not incident_type: raise Exception("No incident type specified and no default has been defined.") else: incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_type["name"] ) # We get the incident priority by name if not incident_priority: incident_priority = incident_priority_service.get_default(db_session=db_session) if not incident_priority: raise Exception("No incident priority specified and no default has been defined.") else: incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_priority["name"] ) if not visibility: visibility = incident_type.visibility tag_objs = [] for t in tags: tag_objs.append(tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) # We create the incident incident = Incident( title=title, description=description, status=status, incident_type=incident_type, incident_priority=incident_priority, visibility=visibility, tags=tag_objs, project=project, ) db_session.add(incident) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description="Incident created", incident_id=incident.id, ) # Add other incident roles (e.g. commander and liaison) assign_incident_role(db_session, incident, reporter_email, ParticipantRoleType.reporter) assign_incident_role( db_session, incident, reporter_email, ParticipantRoleType.incident_commander ) assign_incident_role(db_session, incident, reporter_email, ParticipantRoleType.liaison) return incident
def list_incidents( user_id: str, user_email: str, channel_id: str, incident_id: int, config: SlackConversationConfiguration = None, command: dict = None, db_session=None, slack_client=None, ): """Returns the list of current active and stable incidents, and closed incidents in the last 24 hours.""" projects = [] incidents = [] args = command["text"].split(" ") # scopes reply to the current incident's project incident = incident_service.get(db_session=db_session, incident_id=incident_id) if incident: # command was run in an incident conversation projects.append(incident.project) else: # command was run in a non-incident conversation if len(args) == 2: project = project_service.get_by_name(db_session=db_session, name=args[1]) if project: projects.append() else: raise ValidationError( [ ErrorWrapper( NotFoundError( msg=f"Project name '{args[1]}' in organization '{args[0]}' not found. Check your spelling." ), loc="project", ) ], model=BaseModel, ) else: projects = project_service.get_all(db_session=db_session) for project in projects: # we fetch active incidents incidents.extend( incident_service.get_all_by_status( db_session=db_session, project_id=project.id, status=IncidentStatus.active ) ) # We fetch stable incidents incidents.extend( incident_service.get_all_by_status( db_session=db_session, project_id=project.id, status=IncidentStatus.stable, ) ) # We fetch closed incidents in the last 24 hours incidents.extend( incident_service.get_all_last_x_hours_by_status( db_session=db_session, project_id=project.id, status=IncidentStatus.closed, hours=24, ) ) blocks = [] blocks.append({"type": "header", "text": {"type": "plain_text", "text": "List of Incidents"}}) if incidents: for incident in incidents: if incident.visibility == Visibility.open: ticket_weblink = resolve_attr(incident, "ticket.weblink") try: blocks.append( { "type": "section", "text": { "type": "mrkdwn", "text": ( f"*<{ticket_weblink}|{incident.name}>*\n" f"*Title*: {incident.title}\n" f"*Type*: {incident.incident_type.name}\n" f"*Priority*: {incident.incident_priority.name}\n" f"*Status*: {incident.status}\n" f"*Incident Commander*: <{incident.commander.individual.weblink}|{incident.commander.individual.name}>\n" f"*Project*: {incident.project.name}" ), }, } ) except Exception as e: log.exception(e) dispatch_slack_service.send_ephemeral_message( slack_client, channel_id, user_id, "Incident List", blocks=blocks, )