Example #1
0
def test_create_instance(session, incident, workflow, participant, project):
    from dispatch.workflow.service import create_instance
    from dispatch.workflow.models import WorkflowInstanceCreate
    from dispatch.document.models import DocumentCreate

    parameters = [{}]
    run_reason = "reason"
    status = "submitted"

    artifacts = [
        DocumentCreate(
            name="name",
            resource_id="resource_id",
            resource_type="resource_type",
            project=project,
            weblink="https://www.example.com/doc",
        )
    ]

    workflow_in = WorkflowInstanceCreate(
        parameters=parameters,
        run_reason=run_reason,
        status=status,
        incident=incident,
        workflow=workflow,
        creator=participant,
        artifacts=artifacts,
    )
    workflow_instance = create_instance(db_session=session,
                                        workflow_in=workflow_in)
    assert workflow_instance
Example #2
0
def run_workflow_submitted_form(
    user_id: str,
    user_email: str,
    channel_id: str,
    incident_id: int,
    action: dict,
    db_session=None,
    slack_client=None,
):
    """Runs an external flow."""
    submitted_form = action.get("view")
    parsed_form_data = parse_submitted_form(submitted_form)

    params = {}
    named_params = []
    for i in parsed_form_data.keys():
        if i.startswith(RunWorkflowBlockId.param):
            key = i.split("-")[1]
            value = parsed_form_data[i]
            params.update({key: value})
            named_params.append({"key": key, "value": value})

    workflow_id = parsed_form_data.get(
        RunWorkflowBlockId.workflow_select)["value"]
    run_reason = parsed_form_data.get(RunWorkflowBlockId.run_reason)
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)
    workflow = workflow_service.get(db_session=db_session,
                                    workflow_id=workflow_id)

    creator_email = get_user_email(slack_client, action["user"]["id"])

    instance = workflow_service.create_instance(
        db_session=db_session,
        instance_in=WorkflowInstanceCreate(
            workflow={"id": workflow.id},
            incident={"id": incident.id},
            creator={"email": creator_email},
            run_reason=run_reason,
            parameters=named_params,
        ),
    )
    params.update({
        "incident_id": incident.id,
        "incident_name": incident.name,
        "instance_id": instance.id
    })

    workflow.plugin.instance.run(workflow.resource_id, params)

    send_workflow_notification(
        incident.conversation.channel_id,
        INCIDENT_WORKFLOW_CREATED_NOTIFICATION,
        db_session,
        instance_creator_name=instance.creator.individual.name,
        workflow_name=instance.workflow.name,
        workflow_description=instance.workflow.description,
    )