Last active
April 18, 2019 08:56
-
-
Save wooramel/fbe19def4bfe99b3c803cecf96eaf329 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
if __name__ == "__main__": | |
'''https://<your_gitlab_site_address>/profile/personal_access_tokens''' | |
if len(sys.argv) != 3: | |
print("Usage {} <gitlab_site> <token>".format(sys.argv[0])) | |
exit(-1) | |
gitlab_site = sys.argv[1] | |
private_token = sys.argv[2] | |
try: | |
for i in range(0, MAX_PAGE): | |
url = '{}/api/v3/projects?private_token={}&per_page=200&page={}' \ | |
.format(gitlab_site, private_token, i) | |
r = requests.get(url) | |
items = r.json() | |
if len(items) > 0: | |
for item in items: | |
namespace = item['namespace']['full_path'] if len(item['namespace']['full_path']) > 0 else "" | |
if len(namespace) > 0: | |
os.makedirs(namespace, exist_ok=True) | |
save_path = item['name'] if len(namespace) <= 0 else namespace + "/" + item['name'] | |
with open(save_path + ".json", "w") as meta_file: | |
meta_file.write(json.dumps(item, indent=2)) | |
os.system("git clone --mirror {} {}".format(item['ssh_url_to_repo'], save_path + ".git")) | |
except KeyboardInterrupt: | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment