import re


def get_organisation_name_from_remote(remote_name: str) -> str:
    """
    Takes the GitHub repo path and obtains the source organisation
    based on its remote URL e.g.:
    >>> get_repo_name_from_remote(\
        'git@github.com:populationgenomics/cpg-utils.git'\
    )
    'populationgenomics'
    >>> get_repo_name_from_remote(\
        'https://github.com/populationgenomics/cpg-utils.git'\
    )
    'populationgenomics'

    Parameters
    ----------
    remote_name

    Returns
    -------
    the organisation name
    """

    organisation = None

    try:
        if remote_name.startswith('http'):
            match = re.match(
                r'http[s]?:\/\/[A-z0-9\.]+?\/(?P<org>.+?)\/.+$', remote_name
            )
            if match:
                organisation = match.group('org')

        elif remote_name.startswith('git@'):
            match = re.match(r'git@[A-z0-9\.]+?:(?P<org>.+?)\/.+$', remote_name)
            if match:
                organisation = match.group('org')
    except AttributeError as ae:
        raise Exception(f'Unsupported remote format: "{remote_name}"') from ae

    if organisation is None:
        raise Exception(f'Unsupported remote format: "{remote_name}"')

    return organisation