Skip to content

Instantly share code, notes, and snippets.

@naderghanbari
Last active November 16, 2018 01:52
Show Gist options
  • Select an option

  • Save naderghanbari/d542ef7404235d25811ae6cc484e1edd to your computer and use it in GitHub Desktop.

Select an option

Save naderghanbari/d542ef7404235d25811ae6cc484e1edd to your computer and use it in GitHub Desktop.

Revisions

  1. naderghanbari revised this gist Nov 16, 2018. No changes.
  2. naderghanbari renamed this gist Nov 16, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion checkout_all.py → gitlab_group_checkout_all.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env python3

    # Usage: ./checkout_all -g group_name -d target_dir
    # 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
  3. naderghanbari created this gist Nov 16, 2018.
    46 changes: 46 additions & 0 deletions checkout_all.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #!/usr/bin/env python3

    # Usage: ./checkout_all -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:])