Skip to content

Commit a970490

Browse files
committed
add check user is admin
1 parent 9e2406e commit a970490

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tools/last_user_activity.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import os
8+
import sys
89
import asyncio
910
import aiohttp
1011
from rich import print
@@ -225,12 +226,33 @@ def clear_cache() -> None:
225226
print(f"[red]Error clearing cache: {str(e)}[/red]")
226227

227228

229+
async def check_user_admin(session: aiohttp.ClientSession, org: str, username: str) -> bool:
230+
url = f"https://api.github.com/orgs/{org}/memberships/{username}"
231+
async with session.get(url, headers=headers) as response:
232+
if response.status == 404:
233+
return False
234+
elif response.status != 200:
235+
print(f"[red]Error fetching membership for {username} in {org}: {response.status}[/red]")
236+
return False
237+
return (await response.json())['role'] == 'admin'
238+
239+
228240
async def main(orgs, debug: bool, timelimit_days: int):
229241
"""Main execution function."""
230242
# Show cache status
231243
print(f"[blue]Cache directory: {CACHE_DIR} (size: {get_cache_size()})[/blue]")
232244
print(f"[blue]Cache contains {len(cache)} items[/blue]")
233245

246+
# check who the current user is
247+
async with aiohttp.ClientSession() as session:
248+
async with session.get("https://api.github.com/user", headers=headers) as response:
249+
if response.status == 200:
250+
user_data = await response.json()
251+
current_user = user_data["login"]
252+
print(f"[blue]Current user: {current_user}[/blue]")
253+
else:
254+
sys.exit(f"[red]Error fetching user data: {response.status}[/red]")
255+
234256
async with aiohttp.ClientSession() as session:
235257
# Check rate limit
236258
async with session.get(
@@ -284,6 +306,12 @@ async def main(orgs, debug: bool, timelimit_days: int):
284306
)
285307
for org in orgs:
286308
print(f"[bold]{org}[/bold]")
309+
is_admin = await check_user_admin(session, org, current_user)
310+
if is_admin:
311+
if debug:
312+
print(f" [green]{current_user} is an admin in {org}[/green]")
313+
else:
314+
print(f" [yellow]{current_user} is not an admin in {org}, list of users will be incomplete (limited to public membership)[/yellow]")
287315
n_active = 0
288316
n_inactive = 0
289317
for username, last_activity, user_orgs in sorted(

0 commit comments

Comments
 (0)