# -*- coding: utf-8 -*- # Script to clone Git repositories in a Gitlab group. # # Minimally tested. Seems to work. Use at your own risk. # # By James Eagan # https://james.eagan.fr import csv import gitlab import sys import subprocess import os.path GITLAB_URL='https://gitlab.telecom-paris.fr' PRIVATE_TOKEN=sys.argv[3] if len(sys.argv) > 3 else '' def getProjectsInGroupId(groupId): api = gitlab.Gitlab(GITLAB_URL, PRIVATE_TOKEN) group = api.groups.get(groupId) projects = group.projects.list(all=True) return (api.projects.get(project.id) for project in projects) def cloneProject(project, targetDir): name = project.attributes['name'] url = project.attributes['ssh_url_to_repo'] print("Cloning {} ({})").format(name, project.id) target = os.path.join(targetDir, name) subprocess.call(['git', 'clone', '--quiet', url, target]) if __name__ == '__main__': if len(sys.argv) != 4: print("Usage: python {} ".format(sys.argv[0])) print(" groupId: The GitLab id for the group") print(" dir: The directory into which to clone the projects") print(" token: A GitLab API token as created at ") print(" {}/-/profile/personal_access_tokens".format(GITLAB_URL)) print(" Be sure to enable API scope.") sys.exit(1) projects = getProjectsInGroupId(sys.argv[1]) for project in projects: cloneProject(project, sys.argv[2])