-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassess_copilot_repos.py
More file actions
412 lines (336 loc) · 14.1 KB
/
assess_copilot_repos.py
File metadata and controls
412 lines (336 loc) · 14.1 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
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
"""
GitHub Copilot Directory Assessment Tool
Scans GitHub repositories for Copilot workspace directories and exports results to CSV.
Requirements:
- GitHub CLI (gh) installed and authenticated
- Python 3.7+
Usage:
python assess_copilot_repos.py
Configuration:
Edit the CONFIG section below to customize behavior
"""
import subprocess
import json
import sys
import csv
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from time import time, sleep
from datetime import datetime
from pathlib import Path
import threading
# ============================================================================
# CONFIGURATION
# ============================================================================
CONFIG = {
# GitHub CLI command (change if gh is not in PATH)
'gh_command': 'gh',
# Directories to check in .github folder
'copilot_dirs': ['prompts', 'instructions', 'agents', 'collections', 'scripts', 'skills'],
# Performance settings
'max_workers_fetch': 10, # Parallel workers for fetching repos
'max_workers_check': 15, # Parallel workers for checking directories
# Rate limiting (for large enterprise scenarios with 1000s of repos)
'enable_rate_limit_check': True, # Check GitHub rate limits
'rate_limit_threshold': 100, # Pause if remaining requests < this
'rate_limit_wait_time': 60, # Seconds to wait when rate limited
'request_delay': 0.05, # Delay between requests (seconds)
# Output settings
'output_dir': '.', # Where to save CSV files (. = current directory)
'csv_prefix': 'github_copilot_assessment',
'include_timestamp': True,
# Personal account identifier (used to detect personal vs org repos)
# Leave empty to auto-detect from GitHub CLI authenticated user
'personal_account': '',
# Verbose output
'verbose': True,
}
# ============================================================================
# CORE FUNCTIONS
# ============================================================================
# Rate limit tracking
rate_limit_lock = threading.Lock()
rate_limit_info = {'remaining': None, 'reset_time': None, 'checked': False}
def log(message, verbose_only=False):
"""Print message if verbose or not verbose_only"""
if not verbose_only or CONFIG['verbose']:
print(message)
def check_gh_installed():
"""Check if GitHub CLI is installed and accessible"""
try:
result = subprocess.run(
[CONFIG['gh_command'], '--version'],
capture_output=True,
check=True,
timeout=5
)
return True
except (subprocess.CalledProcessError, FileNotFoundError, subprocess.TimeoutExpired):
return False
def check_rate_limit():
"""Check GitHub API rate limit status"""
try:
result = subprocess.run(
f"{CONFIG['gh_command']} api rate_limit",
shell=True,
capture_output=True,
text=True,
check=True,
timeout=10
)
data = json.loads(result.stdout)
core_rate = data.get('resources', {}).get('core', {})
return {
'remaining': core_rate.get('remaining', 5000),
'limit': core_rate.get('limit', 5000),
'reset_time': core_rate.get('reset', 0)
}
except:
return None
def wait_for_rate_limit():
"""Wait if rate limit is approaching threshold"""
if not CONFIG['enable_rate_limit_check']:
return
with rate_limit_lock:
# Check rate limit periodically
if not rate_limit_info['checked'] or rate_limit_info['remaining'] is None:
limit_data = check_rate_limit()
if limit_data:
rate_limit_info['remaining'] = limit_data['remaining']
rate_limit_info['reset_time'] = limit_data['reset_time']
rate_limit_info['checked'] = True
log(f"📊 Rate Limit: {limit_data['remaining']}/{limit_data['limit']} requests remaining", verbose_only=True)
# If approaching threshold, wait
if limit_data['remaining'] < CONFIG['rate_limit_threshold']:
wait_time = CONFIG['rate_limit_wait_time']
log(f"⚠️ Rate limit threshold reached ({limit_data['remaining']} remaining)")
log(f" Waiting {wait_time} seconds before continuing...")
sleep(wait_time)
rate_limit_info['checked'] = False # Recheck after waiting
# Small delay between requests
if CONFIG['request_delay'] > 0:
sleep(CONFIG['request_delay'])
def run_gh_command(command):
"""Run GitHub CLI command and return JSON output"""
wait_for_rate_limit()
try:
result = subprocess.run(
command,
shell=True,
capture_output=True,
text=True,
check=True,
timeout=30
)
return json.loads(result.stdout)
except (subprocess.CalledProcessError, json.JSONDecodeError, subprocess.TimeoutExpired):
return None
def fetch_repositories():
"""Fetch all accessible repositories in parallel"""
log("Fetching repositories in parallel...")
# Auto-detect personal account if not set
if not CONFIG['personal_account']:
user_info = run_gh_command(f"{CONFIG['gh_command']} api user --jq '.login'")
if user_info:
CONFIG['personal_account'] = user_info.strip('"')
log(f"Detected personal account: {CONFIG['personal_account']}", verbose_only=True)
# Check initial rate limit
if CONFIG['enable_rate_limit_check']:
limit_data = check_rate_limit()
if limit_data:
log(f"📊 Initial Rate Limit: {limit_data['remaining']}/{limit_data['limit']} requests remaining")
all_repos = []
with ThreadPoolExecutor(max_workers=CONFIG['max_workers_fetch']) as executor:
futures = []
# Fetch user repos
futures.append(executor.submit(
run_gh_command,
f"{CONFIG['gh_command']} repo list --json nameWithOwner,name,owner --limit 1000"
))
# Get and fetch organization repos
orgs_future = executor.submit(
run_gh_command,
f"{CONFIG['gh_command']} api user/orgs --paginate"
)
orgs_data = orgs_future.result()
if orgs_data:
for org in orgs_data:
futures.append(executor.submit(
run_gh_command,
f"{CONFIG['gh_command']} repo list {org['login']} --json nameWithOwner,name,owner --limit 1000"
))
for future in as_completed(futures):
result = future.result()
if result:
all_repos.extend(result)
# Remove duplicates
seen = set()
unique_repos = []
for repo in all_repos:
if repo['nameWithOwner'] not in seen:
seen.add(repo['nameWithOwner'])
unique_repos.append(repo)
return unique_repos
def check_repo_copilot(repo):
"""Check a single repository for Copilot directories"""
full_name = repo['nameWithOwner']
result = {
'name': full_name,
'has_github_dir': False,
'folders': {folder: False for folder in CONFIG['copilot_dirs']},
'error': None
}
github_contents = run_gh_command(
f"{CONFIG['gh_command']} api repos/{full_name}/contents/.github"
)
if github_contents is None:
return result
elif isinstance(github_contents, dict) and 'message' in github_contents:
result['error'] = github_contents.get('message', 'Unknown error')
return result
else:
result['has_github_dir'] = True
folder_names = [item['name'] for item in github_contents if item['type'] == 'dir']
for folder in result['folders'].keys():
result['folders'][folder] = folder in folder_names
return result
def check_all_repositories(repos):
"""Check all repositories for Copilot directories in parallel"""
log("\nChecking Copilot directories (parallel execution)...")
results = []
total = len(repos)
with ThreadPoolExecutor(max_workers=CONFIG['max_workers_check']) as executor:
future_to_repo = {executor.submit(check_repo_copilot, repo): repo for repo in repos}
completed = 0
for future in as_completed(future_to_repo):
result = future.result()
results.append(result)
completed += 1
# Progress indicator
log(f"⚡ Progress: {completed}/{total} repositories checked ({(completed/total*100):.0f}%)", verbose_only=True)
# Sort by name for consistent output
results.sort(key=lambda x: x['name'])
return results
def export_to_csv(results, repos):
"""Export results to CSV file"""
log("\n" + "=" * 80)
log("EXPORTING TO CSV")
log("=" * 80)
export_data = []
for result in results:
repo_name = result['name']
owner = repo_name.split('/')[0]
is_org = owner != CONFIG['personal_account']
row = {
'Repository': repo_name,
'Owner': owner,
'Type': 'Organization' if is_org else 'Personal',
'Organization': owner if is_org else 'N/A',
'Has .github': 'Yes' if result['has_github_dir'] else 'No',
}
# Add columns for each copilot directory
for folder in CONFIG['copilot_dirs']:
row[f'Has {folder}/'] = 'Yes' if result['folders'][folder] else 'No'
row['Has Copilot Directories'] = 'Yes' if any(result['folders'].values()) else 'No'
row['Error'] = result['error'] if result['error'] else 'None'
export_data.append(row)
# Generate filename - always include date and time
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{CONFIG['csv_prefix']}_{timestamp}.csv"
filepath = Path(CONFIG['output_dir']) / filename
# Ensure output directory exists
filepath.parent.mkdir(parents=True, exist_ok=True)
# Write CSV
if export_data:
fieldnames = list(export_data[0].keys())
with open(filepath, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(export_data)
log(f"✅ CSV file created: {filepath}")
log(f" Total rows: {len(export_data)}")
log(f" Columns: {len(fieldnames)}")
return str(filepath)
else:
log("❌ No data to export")
return None
def print_summary(results, repos, fetch_time, check_time, total_time):
"""Print summary statistics"""
repos_checked = sum(1 for r in results if not r['error'])
repos_with_copilot = sum(1 for r in results if any(r['folders'].values()))
repos_with_errors = sum(1 for r in results if r['error'])
log("\n" + "=" * 80)
log("SUMMARY")
log("=" * 80)
log(f"Total repositories: {len(repos)}")
log(f"Repositories checked: {repos_checked}")
log(f"Repositories with Copilot directories: {repos_with_copilot}")
log(f"Repositories with errors: {repos_with_errors}")
# Show final rate limit status for enterprise monitoring
if CONFIG['enable_rate_limit_check']:
final_limit = check_rate_limit()
if final_limit:
log(f"\n📊 Final Rate Limit: {final_limit['remaining']}/{final_limit['limit']} requests remaining")
log(f"\n⚡ PERFORMANCE METRICS:")
log(f" Repository fetch: {fetch_time:.2f}s")
log(f" Directory check: {check_time:.2f}s")
log(f" Total execution: {total_time:.2f}s")
log(f" Average per repo: {(check_time/len(repos)):.3f}s")
log(f" Speedup: ~{(len(repos) * 0.5 / check_time):.1f}x faster than sequential")
# Show repositories with Copilot directories
if repos_with_copilot > 0:
log(f"\n✅ REPOSITORIES WITH COPILOT DIRECTORIES:")
for result in results:
if any(result['folders'].values()):
found_dirs = [f for f, exists in result['folders'].items() if exists]
log(f" • {result['name']}: {', '.join(found_dirs)}")
# ============================================================================
# MAIN FUNCTION
# ============================================================================
def main():
"""Main execution function"""
start_time = time()
log("=" * 80)
log("GITHUB COPILOT DIRECTORY ASSESSMENT TOOL")
log("=" * 80)
# Check prerequisites
if not check_gh_installed():
log("\n❌ GitHub CLI (gh) is not installed or not in PATH!")
log("\nTo install:")
log(" Windows: winget install --id GitHub.cli")
log(" macOS: brew install gh")
log(" Linux: See https://cli.github.com/")
log("\nAfter installation, authenticate with: gh auth login")
return 1
# Fetch repositories
fetch_start = time()
repos = fetch_repositories()
fetch_time = time() - fetch_start
if not repos:
log("\n❌ Could not fetch repositories. Make sure you're authenticated:")
log(" gh auth login")
return 1
log(f"✓ Found {len(repos)} repositories in {fetch_time:.2f}s")
# Check repositories
check_start = time()
results = check_all_repositories(repos)
check_time = time() - check_start
# Export results
csv_file = export_to_csv(results, repos)
# Print summary
total_time = time() - start_time
print_summary(results, repos, fetch_time, check_time, total_time)
log("\n" + "=" * 80)
log("✅ Assessment complete!")
log("=" * 80)
return 0
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
log("\n\n⚠️ Operation cancelled by user")
sys.exit(130)
except Exception as e:
log(f"\n❌ Unexpected error: {e}")
sys.exit(1)