Last active
November 16, 2018 01:52
-
-
Save naderghanbari/d542ef7404235d25811ae6cc484e1edd to your computer and use it in GitHub Desktop.
Gitlab Python script to checkout all Projects belonging to a Group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Usage: ./gitlab_group_checkout_all.py -g group_name -d target_dir | |
# Assumes there is a default configuration and python-gitlab is installed | |
# https://python-gitlab.readthedocs.io/en/stable/cli.html#cli-configuration | |
# Work with GitLab API V4 (gitlab-python version 1.6.x) | |
import getopt, shlex, subprocess, sys, gitlab | |
def clone_all_repos(group_name, directory): | |
gl = gitlab.Gitlab.from_config() | |
groups = gl.groups | |
group_id = next(x.id for x in groups.list(as_list=False) if x.name == group_name) | |
group = groups.get(group_id) | |
projects = group.projects.list(as_list=False) | |
clone_urls = (project.ssh_url_to_repo for project in projects) | |
for clone_url in clone_urls: | |
clone_repo(clone_url, directory) | |
def clone_repo(clone_url, directory): | |
try: | |
command = shlex.split('git clone %s' % clone_url) | |
return subprocess.Popen(command, cwd=directory) | |
except Exception as e: | |
print("Error on %s: %s" % (clone_url, e)) | |
return 1 | |
def main(argv): | |
group_name = '' | |
directory = '' | |
try: | |
opts, args = getopt.getopt(argv, "g:d:", ["group=", "dir="]) | |
except getopt.GetoptError: | |
print('tlm.py -g group -d dir') | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt in ("-g", "--group"): | |
group_name = arg | |
elif opt in ("-d", "--dir"): | |
directory = arg | |
clone_all_repos(group_name, directory) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment