Commit 1c4ea74f authored by Hermann Krumrey's avatar Hermann Krumrey
Browse files

Fix limited repository fetching

parent b44377a5
Loading
Loading
Loading
Loading
+22 −11
Original line number Original line Diff line number Diff line
@@ -33,6 +33,7 @@ def parse_args():


    parser = argparse.ArgumentParser()
    parser = argparse.ArgumentParser()
    parser.add_argument("url", help="The Gitlab URL")
    parser.add_argument("url", help="The Gitlab URL")
    parser.add_argument("user", help="The Gitlab user")
    parser.add_argument("token", help="The Gitlab API Token")
    parser.add_argument("token", help="The Gitlab API Token")
    parser.add_argument("-a", "--archived", default=False, action="store_true",
    parser.add_argument("-a", "--archived", default=False, action="store_true",
                        help="Includes archived repositories")
                        help="Includes archived repositories")
@@ -85,7 +86,7 @@ def clone_repos(projects, destination):
    os.chdir(cwd)  # Return to original working directory
    os.chdir(cwd)  # Return to original working directory




def get_repositories(gitlab_url, api_key, archived):
def get_repositories(gitlab_url, user, api_key, archived):
    """
    """
    Fetches the repository information from the gitlab API.
    Fetches the repository information from the gitlab API.
    Requires a valid Gitlab URL, the user's access token.
    Requires a valid Gitlab URL, the user's access token.
@@ -96,18 +97,28 @@ def get_repositories(gitlab_url, api_key, archived):
    if not gitlab_url.endswith("/"):
    if not gitlab_url.endswith("/"):
        gitlab_url += "/"
        gitlab_url += "/"


    query = gitlab_url + "api/v4/projects?private_token=" + api_key
    query = gitlab_url + "api/v4/users/" + user + "/projects"
    query += "?private_token=" + api_key

    if archived:
        query += "&archived=true"

    result = requests.get(query)
    result = requests.get(query)
    projects = []

    count = 1
    more_pages = True
    while more_pages:

        more_pages = result.headers["X-Next-Page"] != ""


        if result.status_code != 200:  # If unauthorized or other error, stop.
        if result.status_code != 200:  # If unauthorized or other error, stop.
            print(result.text)
            print(result.text)
            sys.exit(1)
            sys.exit(1)

        else:
        else:

            projects += json.loads(result.text)
        projects = json.loads(result.text)
            count += 1
        if archived:
            result = requests.get(query + "&page=" + str(count))
            projects += json.loads(requests.get(query + "&archived=true").text)


    return projects
    return projects


@@ -115,5 +126,5 @@ def get_repositories(gitlab_url, api_key, archived):
if __name__ == "__main__":
if __name__ == "__main__":


    args = parse_args()
    args = parse_args()
    projects = get_repositories(args.url, args.token, args.archived)
    projects = get_repositories(args.url, args.user, args.token, args.archived)
    clone_repos(projects, args.destination)
    clone_repos(projects, args.destination)