def get_pack_ref_from_metadata(metadata, pack_directory_name=None):
    """
    Utility function which retrieves pack "ref" attribute from the pack metadata file.

    If this attribute is not provided, an attempt is made to infer "ref" from the "name" attribute.

    :rtype: ``str``
    """
    pack_ref = None

    # The rules for the pack ref are as follows:
    # 1. If ref attribute is available, we used that
    # 2. If pack_directory_name is available we use that (this only applies to packs
    # which are in sub-directories)
    # 2. If attribute is not available, but pack name is and pack name meets the valid name
    # criteria, we use that
    if metadata.get("ref", None):
        pack_ref = metadata["ref"]
    elif pack_directory_name and re.match(
        PACK_REF_WHITELIST_REGEX, pack_directory_name
    ):
        pack_ref = pack_directory_name
    else:
        if re.match(PACK_REF_WHITELIST_REGEX, metadata["name"]):
            pack_ref = metadata["name"]
        else:
            msg = (
                'Pack name "%s" contains invalid characters and "ref" attribute is not '
                'available. You either need to add "ref" attribute which contains only word '
                "characters to the pack metadata file or update name attribute to contain only"
                "word characters."
            )
            raise ValueError(msg % (metadata["name"]))

    if pack_ref in RESERVED_PACK_LIST:
        raise ValueError(
            f"{pack_ref} is a reserved name, and cannot be used as a pack name"
        )
    return pack_ref

    Utility function which retrieves pack "ref" attribute from the pack metadata file.

    If this attribute is not provided, an attempt is made to infer "ref" from the "name" attribute.

    :rtype: ``str``
    
st2common/st2common/util/pack.py