Created
April 25, 2019 06:14
-
-
Save xiangwangfeng/f5ce7a5254ae6ae8fea0c55ee9e63371 to your computer and use it in GitHub Desktop.
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
import requests | |
import sys | |
import os | |
import json | |
MAX_PAGE = 10 | |
def get_projects(gitlab_site,private_token): | |
projects = [] | |
for i in range(0, MAX_PAGE): | |
url = '{}/api/v3/projects?private_token={}&per_page=99&page={}' \ | |
.format(gitlab_site, private_token, i) | |
r = requests.get(url) | |
items = r.json() | |
if len(items) > 0: | |
projects += items | |
return projects | |
def clone_projects(projects): | |
try: | |
count = len(projects) | |
for i in range(count): | |
project = projects[i] | |
project_name = project["path_with_namespace"] | |
project_ssh_url = project["ssh_url_to_repo"] | |
clone_description = "clone project {} | progress {}/{}".format(project_name,i+1,count) | |
clone_command = "git clone --mirror {} ./{}.git".format(project_ssh_url,project_name) | |
print(clone_description) | |
print(clone_command) | |
os.system(clone_command) | |
except KeyboardInterrupt: | |
exit(0) | |
def dump_projects_metadata(projects): | |
with open("metadata.json", "w") as meta_file: | |
meta_file.write(json.dumps(projects, indent=2)) | |
if __name__ == "__main__": | |
# <your_gitlab_site_address>/profile/<personal_access_tokens> | |
if len(sys.argv) != 3: | |
print("Usage {} <gitlab_site> <personal_access_tokens>".format(sys.argv[0])) | |
exit(-1) | |
gitlab_site = sys.argv[1] | |
private_token = sys.argv[2] | |
projects = get_projects(gitlab_site,private_token) | |
dump_projects_metadata(projects) | |
clone_projects(projects) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment