-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-main.py
More file actions
128 lines (96 loc) · 3.27 KB
/
Copy pathgithub-main.py
File metadata and controls
128 lines (96 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import sys
import os
import subprocess
import requests
from urllib.parse import urljoin
import json
from dotenv import load_dotenv
load_dotenv()
usernames = input("Enter GitHub usernames separated by space: ").split()
save_path = input("Enter dir path to save all repos (mass_clone_output): ") or "mass_clone_output"
depth = input("Enter depth (`None` for full history, 1 for just latest commit): ")
try:
depth = eval(depth)
except Exception:
sys.exit(1)
if not usernames:
sys.exit(1)
GITHUB_API_BASE_URL = "https://api.github.com"
headers = {
"Accept" : "application/vnd.github+json",
"X-GitHub-Api-Version" : "2022-11-28",
"Authorization" : os.environ.get("AUTHORIZATION_HEADER_VALUE")
}
session = requests.Session()
session.headers.update(headers)
def check_usernames_validity(usernames: list = usernames, session: requests.Session =session) -> dict:
"""
Returns a dictionary in the following format:
{
"result": bool,
"failing_users": list[str]
}
- `result` is True if all usernames are valid, otherwise False.
- `failing_users` contains usernames that failed validation. This list is empty when `result` is True.
"""
result = True
failing_users = []
for uname in usernames:
user_url = urljoin(GITHUB_API_BASE_URL, "users", uname)
response = session.get(user_url)
if not response.ok:
result = False
failing_users.append(uname)
return {
"result": result,
"failing_users": failing_users
}
def fetch_all_repos(username: str, session: requests.Session = session) -> list:
repos = []
page = 1
while True:
repos_url = f"{GITHUB_API_BASE_URL}/users/{username}/repos"
params = {"per_page": 100, "page": page}
response = session.get(repos_url, params=params)
if not response.ok:
print(f"[ERROR] Failed to fetch repos for {username}")
sys.exit(1)
data = response.json()
if not data:
break
repos.extend(data)
page += 1
return repos
def clone_repo(clone_url: str, target_path: str, depth):
if os.path.exists(target_path):
print(f"[SKIP] {target_path} already exists")
return
cmd = ["git", "clone"]
if depth is not None:
cmd += ["--depth", str(depth)]
cmd += [clone_url, target_path]
print(f"[CLONING] {clone_url} -> {target_path}")
process = subprocess.Popen(cmd)
process.communicate()
if process.returncode != 0:
print(f"[SKIP] Failed cloning {clone_url}")
return
uname_check_result = check_usernames_validity()
if uname_check_result["result"]:
print("All usernames valid")
else:
print("Failing users: " + " ".join(uname_check_result["failing_users"]))
os.makedirs(save_path, exist_ok=True)
for username in usernames:
print(f"\n[USER] {username}")
user_dir = os.path.join(save_path, username)
os.makedirs(user_dir, exist_ok=True)
repos = fetch_all_repos(username)
for repo in repos:
clone_url = repo.get("clone_url")
repo_name = repo.get("name")
if not clone_url or not repo_name:
continue
target_path = os.path.join(user_dir, repo_name)
clone_repo(clone_url, target_path, depth)
print("\nDone.")