2024-02-23 17:03:51 +01:00
import requests
import json
import config
2024-02-23 21:07:52 +01:00
from enum import Enum
from tqdm import tqdm
2024-02-23 17:03:51 +01:00
2024-02-23 21:07:52 +01:00
Git = Enum ( ' Git ' , [
' TEA ' ,
' LAB ' ,
' HUB ' ,
] )
2024-02-23 17:03:51 +01:00
2024-02-23 21:07:52 +01:00
GIT_INSTANCE_URL = ' https://api.github.com '
GIT_INSTANCE_TYPE = Git . HUB
if GIT_INSTANCE_TYPE == Git . HUB :
USERNAME = getApi ( ' user ' ) [ ' login ' ]
headers = { }
match GIT_INSTANCE_TYPE :
case Git . TEA :
headers [ ' Authorization ' ] = f ' token { config . TOKEN } '
case Git . HUB :
headers [ ' Authorization ' ] = f ' Bearer { config . TOKEN } '
2024-02-23 17:03:51 +01:00
2024-02-23 17:35:22 +01:00
def getApi ( url ) :
2024-02-23 21:07:52 +01:00
if GIT_INSTANCE_TYPE == Git . TEA :
url = f ' api/v1/ { url } '
response = requests . get ( f ' { GIT_INSTANCE_URL } / { url } ' , headers = headers )
2024-02-23 17:35:22 +01:00
return response . json ( )
2024-02-23 17:03:51 +01:00
2024-02-23 21:07:52 +01:00
match GIT_INSTANCE_TYPE :
case Git . TEA :
repositories = getApi ( f ' user/repos ' )
case Git . HUB :
repositories = getApi ( f ' search/repositories?q=user: { USERNAME } &per_page=1000 ' ) [ ' items ' ]
2024-02-23 17:35:22 +01:00
2024-02-23 21:07:52 +01:00
for repository in tqdm ( repositories ) :
2024-02-23 17:35:22 +01:00
repositoryFullName = repository [ ' full_name ' ]
collaborators = getApi ( f ' repos/ { repositoryFullName } /collaborators ' )
for collaborator in collaborators :
2024-02-23 21:07:52 +01:00
# Does not seem necessary for GitHub.
2024-02-23 17:35:22 +01:00
permission = getApi ( f ' repos/ { repositoryFullName } /collaborators/ { collaborator [ " login " ] } /permission ' )
2024-02-23 21:07:52 +01:00
login = permission [ ' user ' ] [ ' login ' ]
toPrint = [
repositoryFullName ,
login ,
]
match GIT_INSTANCE_TYPE :
case Git . TEA :
if permission . get ( ' message ' ) == ' Only admins can query all permissions, repo admins can query all repo permissions, collaborators can query only their own ' :
continue
if permission [ ' permission ' ] != ' read ' :
print ( * toPrint )
case Git . HUB :
if login != USERNAME :
print ( * toPrint )