def parse_adviser_output() -> None:
    """Investigate on unresolved packages in adviser output."""
    adviser_run_path = Path(os.environ["FILE_PATH"])

    file_found = True
    unresolved_found = True

    unresolved_packages = []
    packages_to_solve = {}

    if not adviser_run_path.exists():
        _LOGGER.warning(
            f"Cannot find the file on this path: {adviser_run_path}")
        file_found = False

    if file_found:

        with open(adviser_run_path, "r") as f:
            content = json.load(f)

        report = content["result"]["report"]

        if report:
            errors_details = report.get("_ERROR_DETAILS")
            if errors_details:
                unresolved_packages = errors_details["unresolved"]

        if not unresolved_packages:
            _LOGGER.warning(
                "No packages to be solved with priority identified.")
            unresolved_found = False

        if unresolved_found:
            _LOGGER.info(
                f"Identified the following unresolved packages: {unresolved_packages}"
            )

            parameters = content["result"]["parameters"]
            runtime_environment = parameters["project"].get(
                "runtime_environment")

            solver = OpenShift.obtain_solver_from_runtime_environment(
                runtime_environment=runtime_environment)

            requirements = parameters["project"].get("requirements")

            pipfile = Pipfile.from_dict(requirements)
            packages = pipfile.packages.packages
            dev_packages = pipfile.dev_packages.packages

            for package_name in unresolved_packages:

                if package_name in packages:
                    packages_to_solve[package_name] = packages[package_name]

                if package_name in dev_packages:
                    packages_to_solve[package_name] = dev_packages[
                        package_name]

            _LOGGER.info(
                f"Unresolved packages identified.. {packages_to_solve}")

    output_messages = []

    for package, package_info in packages_to_solve.items():

        message_input = {
            "component_name": {
                "type": "str",
                "value": __COMPONENT_NAME__
            },
            "service_version": {
                "type": "str",
                "value": __service_version__
            },
            "package_name": {
                "type": "Dict",
                "value": package_info.name
            },
            "package_version": {
                "type": "str",
                "value": package_info.version
            },
            "index_url": {
                "type": "str",
                "value": package_info.index
            },
            "solver": {
                "type": "int",
                "value": solver
            },
        }

        # We store the message to put in the output file here.
        output_messages.append({
            "topic_name": "thoth.investigator.unresolved-package",
            "message_contents": message_input
        })

    # Store message to file that need to be sent.
    store_messages(output_messages)
def parse_adviser_output(file_test_path: Optional[Path] = None) -> None:
    """Investigate on unresolved packages."""
    if file_test_path:
        _LOGGER.debug("Dry run..")
        adviser_run_path = file_test_path
    else:
        adviser_run_path = Path(os.environ["FILE_PATH"])

    if not adviser_run_path.exists():
        raise FileNotFoundError(
            f"Cannot find the file on this path: {adviser_run_path}")

    with open(adviser_run_path, "r") as f:
        content = json.load(f)

    unresolved_packages = []
    report = content["result"]["report"]
    if report:
        errors_details = report.get("_ERROR_DETAILS")
        if errors_details:
            unresolved_packages = errors_details["unresolved"]

    if not unresolved_packages:
        _LOGGER.warning("No packages to be solved with priority identified.")
        sys.exit(2)

    parameters = content["result"]["parameters"]
    runtime_environment = parameters["project"].get("runtime_environment")

    solver = OpenShift.obtain_solver_from_runtime_environment(
        runtime_environment=runtime_environment)

    requirements = parameters["project"].get("requirements")

    pipfile = Pipfile.from_dict(requirements)
    packages = pipfile.packages.packages
    dev_packages = pipfile.dev_packages.packages

    packages_to_solve = {}
    for package_name in unresolved_packages:

        if package_name in packages:
            packages_to_solve[package_name] = packages[package_name]

        if package_name in dev_packages:
            packages_to_solve[package_name] = dev_packages[package_name]

    _LOGGER.info(f"Unresolved packages identified.. {packages_to_solve}")

    output_messages = []

    for package, package_info in packages_to_solve.items():

        message_input = {
            "component_name": {
                "type": "str",
                "value": __COMPONENT_NAME__
            },
            "service_version": {
                "type": "str",
                "value": __service_version__
            },
            "package_name": {
                "type": "Dict",
                "value": package_info.name
            },
            "package_version": {
                "type": "str",
                "value": package_info.version
            },
            "index_url": {
                "type": "str",
                "value": package_info.index
            },
            "solver": {
                "type": "int",
                "value": solver
            },
        }

        # We store the message to put in the output file here.
        output_messages.append({
            "topic_name": "thoth.investigator.unresolved-package",
            "message_contents": message_input
        })

    # Store message to file that need to be sent.
    with open(f"/mnt/workdir/messages_to_be_sent.json", "w") as json_file:
        json.dump(output_messages, json_file)

    if output_messages:
        _LOGGER.info(
            f"Successfully stored file with messages to be sent!: {output_messages}"
        )