Repository navigation
Expand file tree
/
Copy pathfetch_github_repos.py
More file actions
executable file
Β·401 lines (326 loc) Β· 13.2 KB
/
Copy pathfetch_github_repos.py
File metadata and controls
executable file
Β·401 lines (326 loc) Β· 13.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
import json
import os
import subprocess
import sys
# ANSI color codes
class Colors:
HEADER = "\033[95m"
BLUE = "\033[94m"
CYAN = "\033[96m"
GREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
def print_header(text):
"""Print a formatted header."""
print(f"\n{Colors.HEADER}{Colors.BOLD}{'=' * 50}{Colors.ENDC}")
print(f"{Colors.HEADER}{Colors.BOLD} {text}{Colors.ENDC}")
print(f"{Colors.HEADER}{Colors.BOLD}{'=' * 50}{Colors.ENDC}\n")
def print_section(text):
"""Print a formatted section header."""
print(f"\n{Colors.BLUE}{Colors.BOLD}βΆ {text}{Colors.ENDC}")
def print_success(text):
"""Print a success message."""
print(f"{Colors.GREEN}β {text}{Colors.ENDC}")
def print_warning(text):
"""Print a warning message."""
print(f"{Colors.WARNING}β {text}{Colors.ENDC}")
def print_error(text):
"""Print an error message."""
print(f"{Colors.FAIL}β {text}{Colors.ENDC}")
def print_info(text):
"""Print an info message."""
print(f"{Colors.CYAN}βΉ {text}{Colors.ENDC}")
def get_all_repos(org):
"""Fetch all repositories for a given GitHub organization using GitHub CLI."""
try:
# Check if GitHub CLI is installed
subprocess.run(
["gh", "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except (subprocess.SubprocessError, FileNotFoundError):
print_error("GitHub CLI (gh) is not installed or not in PATH.")
print_info("Please install it from: https://cli.github.com/")
sys.exit(1)
# Check if user is authenticated
try:
subprocess.run(
["gh", "auth", "status"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except subprocess.CalledProcessError:
print_error("Not authenticated with GitHub CLI.")
print_info("Please run 'gh auth login' to authenticate.")
sys.exit(1)
print_info(f"Fetching repositories for organization: {org}")
# Fetch all repos for the organization
try:
result = subprocess.run(
[
"gh",
"repo",
"list",
org,
"--limit",
"1000",
"--json",
"name,url,sshUrl,isPrivate",
],
check=True,
capture_output=True,
text=True,
)
repos = json.loads(result.stdout)
if not repos:
print_warning(f"No repositories found for organization: {org}")
return []
return repos
except subprocess.CalledProcessError as e:
print_error(f"Error fetching repositories: {e}")
print_error(f"Error details: {e.stderr}")
sys.exit(1)
def filter_repos_by_visibility(repos, visibility):
"""Filter repositories by visibility."""
# if visibility.lower() == "all":
# return repos
# return [repo for repo in repos if repo["visibility"].lower() == visibility.lower()]
if visibility.lower() == "all":
return repos
is_private = visibility.lower() == "private"
return [repo for repo in repos if repo.get("isPrivate") == is_private]
def is_git_repository(path):
"""Check if the specified path is a git repository."""
git_dir = os.path.join(path, ".git")
return os.path.exists(git_dir) and os.path.isdir(git_dir)
def get_repo_remote_url(repo_path):
"""Get the remote URL of a repository."""
try:
result = subprocess.run(
["git", "-C", repo_path, "remote", "get-url", "origin"],
check=True,
capture_output=True,
text=True,
)
return result.stdout.strip()
except subprocess.CalledProcessError:
return None
def update_repository(repo_path):
"""Update an existing repository by fetching the latest changes without merging."""
print_info(f"Updating repository: {os.path.basename(repo_path)}")
try:
# Fetch the latest changes without merging
subprocess.run(
["git", "-C", repo_path, "fetch", "--all"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Get current branch
result = subprocess.run(
["git", "-C", repo_path, "rev-parse", "--abbrev-ref", "HEAD"],
check=True,
capture_output=True,
text=True,
)
current_branch = result.stdout.strip()
# Show commit difference information
result = subprocess.run(
[
"git",
"-C",
repo_path,
"rev-list",
"--count",
f"{current_branch}..origin/{current_branch}",
],
check=True,
capture_output=True,
text=True,
)
commit_diff = result.stdout.strip()
if commit_diff and int(commit_diff) > 0:
print_success(
f"Fetched {os.path.basename(repo_path)} ({commit_diff} new commits available)"
)
else:
print_success(f"Fetched {os.path.basename(repo_path)} (already up to date)")
return True
except subprocess.CalledProcessError as e:
# Handle the error message correctly based on whether it's bytes or string
error_msg = e.stderr if isinstance(e.stderr, str) else e.stderr.decode().strip()
print_error(f"Failed to update {os.path.basename(repo_path)}: {error_msg}")
return False
def clone_or_update_repos(repos, output_dir=None, use_ssh=True):
"""Clone or update repositories."""
if output_dir:
os.makedirs(output_dir, exist_ok=True)
os.chdir(output_dir)
working_dir = os.getcwd()
new_repos = 0
updated_repos = 0
failed_repos = 0
skipped_repos = 0
print_header("Starting Repository Operations")
for repo in repos:
name = repo["name"]
# Choose between SSH and HTTPS URL
url = repo["sshUrl"] if use_ssh else repo["url"]
repo_path = os.path.join(working_dir, name)
if os.path.exists(repo_path):
if is_git_repository(repo_path):
remote_url = get_repo_remote_url(repo_path)
# Check if the remote URL matches (ignoring .git suffix variations)
remote_base = remote_url.rstrip(".git") if remote_url else ""
url_base = url.rstrip(".git")
if remote_base == url_base:
# Repository exists and has the correct remote, update it
if update_repository(repo_path):
updated_repos += 1
else:
failed_repos += 1
else:
print_warning(
f"Skipping {name}: Directory exists but has different remote URL"
)
skipped_repos += 1
else:
print_warning(
f"Skipping {name}: Directory exists but is not a git repository"
)
skipped_repos += 1
else:
# Directory doesn't exist, clone the repository
# print_info(f"Cloning: {name} ({repo['visibility']})")
visibility = "private" if repo.get("isPrivate") else "public"
print_info(f"Cloning: {name} ({visibility})")
try:
# Simple clone with all branches
subprocess.run(
["git", "clone", url, name],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
print_success(f"Cloned {name}")
new_repos += 1
except subprocess.CalledProcessError as e:
error_msg = (
e.stderr if isinstance(e.stderr, str) else e.stderr.decode().strip()
)
print_error(f"Failed to clone {name}: {error_msg}")
failed_repos += 1
print_header("Operation Summary")
print_success(f"New repositories cloned: {new_repos}")
print_success(f"Existing repositories updated: {updated_repos}")
print_error(f"Failed operations: {failed_repos}")
print_warning(f"Skipped repositories: {skipped_repos}")
def is_in_master_repository():
"""Check if we're currently in what appears to be a master repository.
This checks if we're in a directory that contains multiple git repositories.
"""
current_dir = os.getcwd()
subdirs = [f for f in os.listdir(current_dir) if os.path.isdir(f)]
git_repos = [d for d in subdirs if is_git_repository(os.path.join(current_dir, d))]
# If there are multiple git repositories in the current directory,
# it's likely we're in a master repository
return len(git_repos) > 1
def get_input_with_default(prompt, default):
"""Get user input with a default value if the user just presses Enter."""
formatted_prompt = f"{Colors.BLUE}{prompt}{Colors.ENDC}"
response = input(formatted_prompt).strip()
return response if response else default
def main():
if len(sys.argv) < 2:
print_header("GitHub Repository Manager")
print_error("Usage: fetch_github_repos.py <organization> [output_directory]")
print_info("Example: fetch_github_repos.py microsoft ./microsoft-repos")
sys.exit(1)
print_header("GitHub Repository Manager")
# Enhanced tip message with spacers and green coloring
print(f"\n{Colors.GREEN}{Colors.BOLD}{'*' * 70}{Colors.ENDC}")
print(
f"{Colors.GREEN}{Colors.BOLD}{'*' * 5}{'':^5}TIP: PRESS ENTER TO SELECT DEFAULT OPTIONS (IN BRACKETS){'':^5}{'*' * 5}{Colors.ENDC}"
)
print(f"{Colors.GREEN}{Colors.BOLD}{'*' * 70}{Colors.ENDC}\n")
org = sys.argv[1]
output_dir = sys.argv[2] if len(sys.argv) > 2 else None
# Check if we're already in what appears to be a master repository
if output_dir is None and is_in_master_repository():
print_info("Detected multiple git repositories in current directory.")
choice = get_input_with_default(
"Use current directory as master repository? [Y/n]: ", "y"
).lower()
if choice != "y":
output_dir = get_input_with_default(
"Enter output directory (or leave empty to use current directory): ", ""
)
if not output_dir:
output_dir = None
repos = get_all_repos(org)
print_section(f"Repository Analysis for {org}")
print_info(f"Found {len(repos)} repositories")
# visibility_count = {}
# for repo in repos:
# repo_visibility = repo["visibility"]
# visibility_count[repo_visibility] = visibility_count.get(repo_visibility, 0) + 1
visibility_count = {"public": 0, "private": 0}
for repo in repos:
visibility = "private" if repo.get("isPrivate") else "public"
visibility_count[visibility] += 1
for repo_visibility, count in visibility_count.items():
if repo_visibility == "public":
print_info(f"π Public: {count}")
else:
print_info(f"π Private: {count}")
# Let user choose which visibility to clone
print_section("Repository Visibility Selection")
print_info("Which repositories would you like to clone or update?")
print(f" 1. π Public (default)")
print(f" 2. π Private")
print(f" 3. π All")
visibility_choice = get_input_with_default("Enter your choice (1-3) [1]: ", "1")
# Map input choice to visibility type
visibility_map = {"1": "public", "2": "private", "3": "all"}
visibility_emoji = {"public": "π", "private": "π", "all": "π"}
visibility = visibility_map.get(visibility_choice, "public")
# Filter repositories by visibility
if visibility != "all":
filtered_repos = filter_repos_by_visibility(repos, visibility)
print_info(
f"Filtered to {len(filtered_repos)} {visibility_emoji[visibility]} {visibility} repositories"
)
else:
filtered_repos = repos
# Let user choose between SSH and HTTPS
print_section("Connection Protocol Selection")
print_info("How would you like to clone the repositories?")
print(f" 1. π SSH (default)")
print(f" 2. π HTTPS")
protocol_choice = get_input_with_default("Enter your choice (1-2) [1]: ", "1")
# Determine if SSH should be used
use_ssh = protocol_choice != "2"
if filtered_repos:
# Default to "y" if user just presses Enter
print_section("Confirmation")
choice = get_input_with_default(
f"Do you want to clone/update these {visibility_emoji[visibility]} {visibility} repositories? [Y/n]: ",
"y",
).lower()
if choice == "y":
clone_or_update_repos(filtered_repos, output_dir, use_ssh)
else:
# Just print the repo list if not cloning
print_section(f"{visibility.capitalize()} Repository List")
for repo in filtered_repos:
url_to_show = repo["sshUrl"] if use_ssh else repo["url"]
print_info(f"{repo['name']}: {url_to_show}")
if __name__ == "__main__":
main()