title: GitLab Python Clone Everything with Structure
body: <p>I want to clone all of the projects in my GitLab environment. There are a number of groups and subgroups with their own projects. I'd like to clone the projects while maintaining the structure of the groups locally. For example:</p>
  <pre><code>Group1
    Subgroup1
       Project1
  Group2
    Project1
  Group3
    Subgroup1
       Project1
    Subgroup2
       Project1
       Project2
  </code></pre>
  <p>So if I run <code>cd Group3/Subgroup2/Project2</code> locally I would be in that respective projects code.</p>
  <p>My code does some of the work but it breaks on subgroups:</p>
  <pre><code>import os
  from git import Repo
  
  GITLAB_TOKEN = os.getenv('GITLAB_TOKEN')
  
  gl = gitlab.Gitlab(url='https://gitlab.example.priv', private_token=GITLAB_TOKEN)
  
  all_groups = gl.groups.list(all=True,top_level_only=True)
  count=0
  for group in all_groups:
      os.chdir('/Users/ken/repos')
      print(&quot;---------------- {} ----------------&quot;.format(group.full_path))
      if not os.path.exists(group.full_path):
          print(&quot;\tCreating directory for group &quot; + group.full_path)
          os.makedirs(group.full_path)
      os.chdir(group.full_path)
      print(&quot;Cloning projects for group &quot; + group.full_path)
      for project in group.projects.list(all=True):
          if not os.path.exists(project.path):
              Repo.clone_from(project.ssh_url_to_repo, project.path)
      print(&quot;Creating directories for subgroups&quot;)
      subgroups = group.subgroups.list()
      for subgroup in subgroups:
          if not os.path.exists(subgroup.full_path):
              os.makedirs(subgroup.full_path)
          os.chdir(subgroup.full_path)
          print(&quot;Cloning projects for subgroup &quot; + subgroup.full_path)
          group = gl.groups.get(subgroup.id, lazy=True)
          for project in group.projects.list(all=True):
              Repo.clone_from(project.ssh_url_to_repo, project.path)
          os.chdir('..')
  </code></pre>
  <p>How can I iterate over groups and subgroups to clone all of the projects while maintaining the folder structure locally?</p>