From 3e1f20f9b3541fa51e6daddf4c5cfe6d5e13ec73 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Fri, 23 Feb 2024 21:07:52 +0100 Subject: [PATCH] Add GitHub support --- config.py | 4 +++- main.py | 59 +++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/config.py b/config.py index b319ddf..14d6618 100644 --- a/config.py +++ b/config.py @@ -1,2 +1,4 @@ -# Need `repository` and `user` `Read` scopes with `All (public, private, and limited)`. +# Need for: +# Gitea: `repository` and `user` `Read` scopes with `All (public, private, and limited)`. +# GitHub: `public_report` TOKEN = 'YOUR_TOKEN' \ No newline at end of file diff --git a/main.py b/main.py index db13f43..9bdd235 100644 --- a/main.py +++ b/main.py @@ -1,29 +1,58 @@ import requests import json import config +from enum import Enum +from tqdm import tqdm -GITEA_INSTANCE = 'https://gitea.lemnoslife.com' +Git = Enum('Git', [ + 'TEA', + 'LAB', + 'HUB', +]) -headers = { - 'Authorization': f'token {config.TOKEN}' -} +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}' def getApi(url): - url = f'{GITEA_INSTANCE}/api/v1/{url}' - response = requests.get(url, headers = headers) + if GIT_INSTANCE_TYPE == Git.TEA: + url = f'api/v1/{url}' + response = requests.get(f'{GIT_INSTANCE_URL}/{url}', headers = headers) return response.json() -repositories = getApi(f'user/repos') +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'] -print(json.dumps(repositories, indent = 4)) - -for repository in repositories: +for repository in tqdm(repositories): repositoryFullName = repository['full_name'] collaborators = getApi(f'repos/{repositoryFullName}/collaborators') for collaborator in collaborators: - print(json.dumps(collaborators, indent = 4)) + # Does not seem necessary for GitHub. permission = getApi(f'repos/{repositoryFullName}/collaborators/{collaborator["login"]}/permission') - if permission['permission'] != 'read': - print(repositoryFullName) - print(json.dumps(permission, indent = 4)) - + 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) \ No newline at end of file