-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_team_membership_check.py
More file actions
504 lines (406 loc) · 20 KB
/
github_team_membership_check.py
File metadata and controls
504 lines (406 loc) · 20 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env python3
"""
Script to check GitHub organization team membership for users in Zephyr Project.
Usage:
# Check a specific user's team membership
python github_team_membership_check.py --org zephyrproject-rtos --user username
# Check all users in MAINTAINERS.yml
python github_team_membership_check.py --org zephyrproject-rtos --maintainers-file MAINTAINERS.yml
# Check a specific user from MAINTAINERS.yml
python github_team_membership_check.py --org zephyrproject-rtos --maintainers-file MAINTAINERS.yml --user username
"""
import argparse
import requests
import sys
import os
import yaml
import time
def get_org_teams(org, token=None):
"""
Get all teams in a GitHub organization.
Args:
org (str): GitHub organization name
token (str, optional): GitHub personal access token for authentication
Returns:
list: List of team objects
"""
# Construct the API URL
url = f"https://api.github.com/orgs/{org}/teams?per_page=100"
# Set up headers
headers = {
"Accept": "application/vnd.github.v3+json"
}
# Add token if provided
if token:
headers["Authorization"] = f"token {token}"
teams = []
try:
# GitHub uses pagination for large result sets
while url:
# Make the request
response = requests.get(url, headers=headers)
# Check if request was successful
if response.status_code == 200:
# Add teams to our list
page_teams = response.json()
teams.extend(page_teams)
# Check if there are more pages
if "next" in response.links:
url = response.links["next"]["url"]
else:
url = None
else:
if response.status_code == 401:
print("Error: Authentication failed. Check your GitHub token.", file=sys.stderr)
elif response.status_code == 403:
print("Error: API rate limit exceeded or insufficient permissions. Try using a GitHub token with 'read:org' scope.", file=sys.stderr)
# Get rate limit info
rate_limit_url = "https://api.github.com/rate_limit"
rate_response = requests.get(rate_limit_url, headers=headers)
if rate_response.status_code == 200:
rate_info = rate_response.json()
reset_time = rate_info['resources']['core']['reset']
import datetime
reset_datetime = datetime.datetime.fromtimestamp(reset_time)
print(f"Rate limit will reset at: {reset_datetime}", file=sys.stderr)
else:
print(f"Error: Unexpected status code {response.status_code}", file=sys.stderr)
print(f"Response: {response.text}", file=sys.stderr)
sys.exit(1)
return teams
except requests.exceptions.RequestException as e:
print(f"Error making request: {e}", file=sys.stderr)
sys.exit(1)
def check_team_membership(org, team_slug, username, token=None):
"""
Check if a user is a member of a GitHub team.
Args:
org (str): GitHub organization name
team_slug (str): Team slug
username (str): GitHub username to check
token (str, optional): GitHub personal access token for authentication
Returns:
str or None: Membership state ('active', 'pending') or None if not a member
"""
# Construct the API URL
url = f"https://api.github.com/orgs/{org}/teams/{team_slug}/memberships/{username}"
# Set up headers
headers = {
"Accept": "application/vnd.github.v3+json"
}
# Add token if provided
if token:
headers["Authorization"] = f"token {token}"
try:
# Make the request
response = requests.get(url, headers=headers)
# Status code 200 means the user is a member
if response.status_code == 200:
membership = response.json()
return membership.get('state')
# Status code 404 means the user is not a member
elif response.status_code == 404:
return None
# Handle other status codes
else:
if response.status_code == 401:
print("Error: Authentication failed. Check your GitHub token.", file=sys.stderr)
elif response.status_code == 403:
print("Error: API rate limit exceeded or insufficient permissions. Try using a GitHub token with 'read:org' scope.", file=sys.stderr)
else:
print(f"Error: Unexpected status code {response.status_code}", file=sys.stderr)
print(f"Response: {response.text}", file=sys.stderr)
sys.exit(1)
except requests.exceptions.RequestException as e:
print(f"Error making request: {e}", file=sys.stderr)
sys.exit(1)
def get_user_team_memberships(org, username, teams, token=None):
"""
Get all teams a user is a member of in a GitHub organization.
Args:
org (str): GitHub organization name
username (str): GitHub username to check
teams (list): List of team objects
token (str, optional): GitHub personal access token for authentication
Returns:
list: List of team names the user is a member of
"""
user_teams = []
for team in teams:
team_slug = team['slug']
membership = check_team_membership(org, team_slug, username, token)
if membership == 'active':
user_teams.append(team['name'])
# Add a small delay to avoid rate limiting
time.sleep(0.1)
return user_teams
def get_all_team_members(org, token=None):
"""
Get all members of all teams in a GitHub organization.
Args:
org (str): GitHub organization name
token (str, optional): GitHub personal access token for authentication
Returns:
dict: Dictionary mapping usernames to lists of team names
"""
# Get all teams in the organization
teams = get_org_teams(org, token)
print(f"Found {len(teams)} teams in the {org} organization")
# Dictionary to store team memberships
all_members = {}
# For each team, get its members
for team in teams:
team_name = team['name']
team_slug = team['slug']
# Construct the API URL for team members
url = f"https://api.github.com/orgs/{org}/teams/{team_slug}/members?per_page=100"
# Set up headers
headers = {
"Accept": "application/vnd.github.v3+json"
}
# Add token if provided
if token:
headers["Authorization"] = f"token {token}"
try:
# GitHub uses pagination for large result sets
while url:
# Make the request
response = requests.get(url, headers=headers)
# Check if request was successful
if response.status_code == 200:
# Process members
members = response.json()
for member in members:
username = member['login']
if username not in all_members:
all_members[username] = []
all_members[username].append(team_name)
# Check if there are more pages
if "next" in response.links:
url = response.links["next"]["url"]
else:
url = None
else:
if response.status_code == 401:
print(f"Error: Authentication failed when fetching members for team {team_name}.", file=sys.stderr)
elif response.status_code == 403:
print(f"Error: API rate limit exceeded or insufficient permissions for team {team_name}.", file=sys.stderr)
else:
print(f"Error: Unexpected status code {response.status_code} for team {team_name}", file=sys.stderr)
# Skip this team and continue with others
break
# Add a small delay to avoid rate limiting
time.sleep(0.1)
except requests.exceptions.RequestException as e:
print(f"Error making request for team {team_name}: {e}", file=sys.stderr)
# Skip this team and continue with others
continue
return all_members
def get_maintainers_users(file_path):
"""
Load the MAINTAINERS.yml file and extract all unique usernames.
Args:
file_path (str): Path to the MAINTAINERS.yml file
Returns:
dict: Dictionary with sets of maintainers and collaborators
"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
maintainers_data = yaml.safe_load(f)
# Sets to store unique usernames
all_maintainers = set()
all_collaborators = set()
# Track which areas each person is involved with
maintainer_areas = {}
collaborator_areas = {}
# Process each area in the MAINTAINERS.yml file
for area_id, area_data in maintainers_data.items():
# Skip non-dictionary entries (like description)
if not isinstance(area_data, dict):
continue
area_name = area_data.get('name', area_id)
# Extract maintainers for this area
maintainers = area_data.get('maintainers', [])
for maintainer in maintainers:
all_maintainers.add(maintainer)
if maintainer not in maintainer_areas:
maintainer_areas[maintainer] = []
maintainer_areas[maintainer].append(area_name)
# Extract collaborators for this area
collaborators = area_data.get('collaborators', [])
for collaborator in collaborators:
all_collaborators.add(collaborator)
if collaborator not in collaborator_areas:
collaborator_areas[collaborator] = []
collaborator_areas[collaborator].append(area_name)
return {
'maintainers': all_maintainers,
'collaborators': all_collaborators,
'all_users': all_maintainers.union(all_collaborators),
'maintainer_areas': maintainer_areas,
'collaborator_areas': collaborator_areas
}
except Exception as e:
print(f"Error loading MAINTAINERS.yml: {e}", file=sys.stderr)
sys.exit(1)
def main():
parser = argparse.ArgumentParser(
description="Check GitHub organization team membership for users in Zephyr Project."
)
parser.add_argument("--org", "-o", required=True, help="GitHub organization name (e.g., zephyrproject-rtos)")
parser.add_argument("--user", "-u", help="GitHub username to check")
parser.add_argument("--maintainers-file", "-m", help="Path to MAINTAINERS.yml file")
parser.add_argument("--token", "-t", help="GitHub personal access token for authentication")
parser.add_argument("--output", "-f", help="Output results to a text file")
parser.add_argument("--list-teams", "-l", action="store_true", help="List all teams in the organization")
args = parser.parse_args()
# Get token from environment variable if not provided
token = args.token or os.environ.get("GITHUB_TOKEN")
if not token:
print("Warning: No GitHub token provided. API rate limits may apply and organization data may be inaccessible.", file=sys.stderr)
print("For best results, provide a token with 'read:org' scope using --token or GITHUB_TOKEN environment variable.", file=sys.stderr)
# List all teams in the organization if requested
if args.list_teams:
teams = get_org_teams(args.org, token)
if teams:
print(f"Teams in the {args.org} organization:")
for team in sorted(teams, key=lambda t: t['name']):
print(f"- {team['name']} (slug: {team['slug']})")
else:
print(f"No teams found in the {args.org} organization or you don't have permission to view them.")
return
# Check a specific user (without MAINTAINERS.yml)
if args.user and not args.maintainers_file:
teams = get_org_teams(args.org, token)
user_teams = get_user_team_memberships(args.org, args.user, teams, token)
if user_teams:
print(f"{args.user} is a member of the following teams in {args.org}:")
for team in sorted(user_teams):
print(f"- {team}")
else:
print(f"{args.user} is not a member of any team in {args.org}")
return
# If we have a maintainers file, process it
if args.maintainers_file:
print(f"Loading maintainers from {args.maintainers_file}...")
maintainers_data = get_maintainers_users(args.maintainers_file)
all_maintainers = maintainers_data['maintainers']
all_collaborators = maintainers_data['collaborators']
all_users = maintainers_data['all_users']
maintainer_areas = maintainers_data['maintainer_areas']
collaborator_areas = maintainers_data['collaborator_areas']
print(f"Found {len(all_maintainers)} maintainers and {len(all_collaborators)} collaborators "
f"({len(all_users)} unique users) in MAINTAINERS.yml")
# Check a specific user from MAINTAINERS.yml
if args.user:
if args.user not in all_users:
print(f"{args.user} is not found in MAINTAINERS.yml")
return
# Determine role in MAINTAINERS.yml
roles = []
if args.user in all_maintainers:
roles.append("Maintainer")
if args.user in all_collaborators:
roles.append("Collaborator")
maintainer_yml_role = " & ".join(roles)
# Get areas
areas = []
if args.user in maintainer_areas:
areas.extend(maintainer_areas[args.user])
if args.user in collaborator_areas:
areas.extend(collaborator_areas[args.user])
# Remove duplicates
areas = sorted(set(areas))
# Check GitHub team membership
teams = get_org_teams(args.org, token)
user_teams = get_user_team_memberships(args.org, args.user, teams, token)
print(f"\nUser: {args.user}")
print(f"Role in MAINTAINERS.yml: {maintainer_yml_role}")
if user_teams:
print(f"GitHub team membership: Yes, member of {len(user_teams)} teams")
print("\nTeams:")
for team in sorted(user_teams):
print(f"- {team}")
else:
print("GitHub team membership: No")
if areas:
print("\nAreas in MAINTAINERS.yml:")
for area in areas:
print(f"- {area}")
return
# Check all users from MAINTAINERS.yml
print(f"Checking team membership for all {len(all_users)} users in MAINTAINERS.yml...")
print("This may take some time due to GitHub API rate limits...")
# Get all team members in one go (more efficient than checking each user individually)
all_team_members = get_all_team_members(args.org, token)
# Find users in MAINTAINERS.yml who are not team members
not_team_members = set()
are_team_members = set()
for user in all_users:
if user in all_team_members:
are_team_members.add(user)
else:
not_team_members.add(user)
# Separate maintainers and collaborators for reporting
maintainers_not_members = all_maintainers.intersection(not_team_members)
collaborators_not_members = all_collaborators.intersection(not_team_members)
maintainers_are_members = all_maintainers.intersection(are_team_members)
collaborators_are_members = all_collaborators.intersection(are_team_members)
# Prepare output
output_lines = []
# Users who are NOT team members
output_lines.append(f"{len(not_team_members)} users in MAINTAINERS.yml are NOT members of any team in {args.org}:")
if maintainers_not_members:
output_lines.append(f"\nMaintainers who are NOT team members ({len(maintainers_not_members)} users):")
for username in sorted(maintainers_not_members):
output_lines.append(f"- {username}")
if collaborators_not_members:
output_lines.append(f"\nCollaborators who are NOT team members ({len(collaborators_not_members)} users):")
for username in sorted(collaborators_not_members):
output_lines.append(f"- {username}")
# Users who ARE team members
output_lines.append(f"\n{len(are_team_members)} users in MAINTAINERS.yml ARE members of teams in {args.org}:")
if maintainers_are_members:
output_lines.append(f"\nMaintainers who ARE team members ({len(maintainers_are_members)} users):")
for username in sorted(maintainers_are_members):
teams_str = ", ".join(all_team_members.get(username, []))
output_lines.append(f"- {username} (Teams: {teams_str})")
if collaborators_are_members:
output_lines.append(f"\nCollaborators who ARE team members ({len(collaborators_are_members)} users):")
for username in sorted(collaborators_are_members):
teams_str = ", ".join(all_team_members.get(username, []))
output_lines.append(f"- {username} (Teams: {teams_str})")
# Add verification counts
output_lines.append("\nVerification Counts:")
output_lines.append(f"- Total users in MAINTAINERS.yml: {len(all_users)}")
output_lines.append(f" - Maintainers: {len(all_maintainers)}")
output_lines.append(f" - Collaborators: {len(all_collaborators)}")
output_lines.append(f"- Users who are team members: {len(are_team_members)}")
output_lines.append(f" - Maintainers: {len(maintainers_are_members)}")
output_lines.append(f" - Collaborators: {len(collaborators_are_members)}")
output_lines.append(f"- Users who are not team members: {len(not_team_members)}")
output_lines.append(f" - Maintainers: {len(maintainers_not_members)}")
output_lines.append(f" - Collaborators: {len(collaborators_not_members)}")
output_lines.append(f"- Sum of both lists: {len(are_team_members) + len(not_team_members)}")
if len(all_users) == len(are_team_members) + len(not_team_members):
output_lines.append("[SUCCESS] All users accounted for!")
else:
output_lines.append("[ERROR] User count mismatch!")
# Print output
for line in output_lines:
print(line)
# Write to file if requested
if args.output:
try:
with open(args.output, 'w', encoding='utf-8') as f:
f.write(f"Users in MAINTAINERS.yml and their GitHub team membership status in {args.org}:\n\n")
for line in output_lines:
f.write(f"{line}\n")
print(f"\nResults written to {args.output}")
except Exception as e:
print(f"Error writing to file: {e}", file=sys.stderr)
# If we get here without any action, show usage
else:
parser.print_help()
if __name__ == "__main__":
main()