import requests
import re


def get_repo_urls(group="orgs", name="OpenITI", path_pattern=r"\d{4}AH$"):
    """Get a list of all repos owned by organisation/user `name`
    that match the regex `path_pattern`

    Args:
        group (str): either "users" or "orgs". Defaults to "orgs"
        name (str): GitHub name of the user/organization. Defaults to "OpenITI"
        path_pattern(str): regex pattern that matches the desired repository names.
            If none is defined, all repos will be cloned. Defaults to r"\d{4}AH$".

    Returns:
        (list): a list of repo urls that matches the `path_pattern` regex.
    """
    repo_urls = []
    page = 1
    while True:
        url = 'https://api.github.com/{0}/{1}/repos?per_page=100&page={2}'
        r = requests.get(url.format(group, name, page))
        print(url.format(group, name, page))
        if r.status_code == 200:
            rdata = r.json()
            for repo in rdata:
                if path_pattern:
                    if re.compile(path_pattern).search(repo['full_name']):
                        repo_urls.append(repo['clone_url'])
                else:
                    repo_urls.append(repo['clone_url'])
            if (len(rdata) >= 100):
                page = page + 1
            else:
                print('Found {0} repos.'.format(len(repo_urls)))
                break
        else:
            print(r)
            return False
    return repo_urls