def get_branch_name(ref: str, ref_prefix='refs/heads/', clean=True, max_len=15):
    """
    Get and preprocess the branch name from the reference within a CodeCommit event.

    :param ref: As obtained from the CodeCommit event
    :param ref_prefix:
    :param clean:
    :param max_len: Maximal length of the resulting branch_name. If None, the returned string will not be shortened 
    :return : Branch name. If clean is set to True replaces "/" by "-" in the branch name
    """
    if ref.startswith(ref_prefix):
        branch_name = ref[len(ref_prefix):]
    else:
        raise Exception(f"Expected reference {ref} to start with ${ref_prefix}")
    if clean:
        branch_name = branch_name.replace('/', '-').lower()
    if max_len is not None:
        branch_name = branch_name[:max_len]
    return branch_name