From 0d70d9dcfe2df14cd337f628f3051b2e0b8de3be Mon Sep 17 00:00:00 2001 From: thehighestmath Date: Mon, 23 Sep 2024 17:32:16 +0300 Subject: [PATCH] added flag -c contributors logging --- README.md | 5 ++- contributors_parser.py | 81 ++++++++++++++++++++++++++++++++++++++++++ main.py | 5 +++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 contributors_parser.py diff --git a/README.md b/README.md index 54cf421..4acd3a6 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,10 @@ python3 main.py --invites [-t, --token] token (github токен вместо to ```commandline python3 main.py [-w, --wikis] [-t, --token] token (github токен вместо token) [-l, --list] list (list - строка пути к txt файлу со списком репозиториев) --dowland_repos path_drepo (path_drepo - строка пути к директории, где сохраняются вики-репозитории) [-o, --out] out (out - название csv файла, в который будут помещены все логи) ``` - +6. Логирование contributors +```commandline +python3 main.py [--contributors] [-t, --token] token (github токен вместо token) [-l, --list] list (list - строка пути к txt файлу со списком репозиториев) [-o, --out] out (out - название csv файла, в который будут помещены все логи) +``` ## Получение токена для работы с Google таблицей: Сначала нужно создать проект на сайте [Google Cloud](https://console.cloud.google.com/). Выбираем название проекта, жмем на кнопку "Create". diff --git a/contributors_parser.py b/contributors_parser.py new file mode 100644 index 0000000..26834fd --- /dev/null +++ b/contributors_parser.py @@ -0,0 +1,81 @@ +import csv +from time import sleep +from github import Github, Repository +import requests + +EMPTY_FIELD = 'Empty field' +TIMEDELTA = 0.05 +TIMEZONE = 'Europe/Moscow' +FIELDNAMES = ('repository name', 'login', 'name', 'email', 'url', 'permissions', 'total_commits', 'id', 'node_id', 'type', 'bio', 'site_admin') + + +def log_contributors_to_csv(info:dict, csv_name:str): + with open(csv_name, 'a', newline='') as file: + writer = csv.DictWriter(file, fieldnames=FIELDNAMES) + writer.writerow(info) + + +def log_repository_contributors(repository: Repository, csv_name:str, token:str): + + contributors = repository.get_contributors() + total_commits_dict = get_contributor_commits(repository.owner, repository.name, token) + nvl = lambda val: val or EMPTY_FIELD + for contributor in contributors: + contributor_permissons = repository.get_collaborator_permission(contributor) + contributor_total_commits = total_commits_dict.get(contributor.login, EMPTY_FIELD) + + info_tmp = { + 'repository name': repository.full_name, + 'login': contributor.login, + 'name': nvl(contributor.name), + 'email': nvl(contributor.email), + 'url': contributor.html_url, + 'permissions': nvl(contributor_permissons), + 'total_commits': contributor_total_commits, + 'id': contributor.id, + 'node_id': contributor.node_id, + 'type': contributor.type, + 'bio': nvl(contributor.bio), + 'site_admin': contributor.site_admin, + } + log_contributors_to_csv(info_tmp, csv_name) + print(info_tmp) + sleep(TIMEDELTA) + + +def log_contributors(client: Github, token:str, working_repos:list, csv_name:str, fork_flag:str): + with open(csv_name, 'w', newline='') as file: + writer = csv.writer(file) + writer.writerow(FIELDNAMES) + + for repo in working_repos: + try: + print('=' * 20, repo.full_name, '=' * 20) + log_repository_contributors(repo, csv_name,token) + if fork_flag: + for forked_repo in repo.get_forks(): + print('=' * 20, "FORKED:", forked_repo.full_name, '=' * 20) + log_repository_contributors(forked_repo, csv_name) + except Exception as e: + print(e) + +def get_contributor_commits(repo_owner, repo_name, token): + headers = {"Authorization": f"Bearer {token}"} + request_name = f"https://api.github.com/repos/{repo_owner.login}/{repo_name}/stats/contributors" + request = requests.get(request_name, headers=headers) + + while request.status_code == 202: + print("Waiting for response...") + sleep(10) + request = requests.get(request_name, headers=headers) + + if request.status_code != 200: + return {} + + response_data = request.json() + total_commits_dict = {} + for contributor in response_data: + contributor_name = contributor["author"]["login"] + total_commits = contributor["total"] + total_commits_dict[contributor_name] = total_commits + return total_commits_dict \ No newline at end of file diff --git a/main.py b/main.py index e4c435a..2110d31 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ import issues_parser import invites_parser import wikipars +import contributors_parser def parse_args(): parser = argparse.ArgumentParser() @@ -17,6 +18,7 @@ def parse_args(): parser.add_argument("-p", "--pull_requests", help="log pull requests", action="store_true") parser.add_argument("-i", "--issues", help="log issues", action="store_true") parser.add_argument("-w", "--wikis", help="log wikis", action="store_true") + parser.add_argument("--contributors", help="log contributors", action="store_true") parser.add_argument("--forks_include", help="logging data from forks", action="store_true") parser.add_argument("-e", "--export_google_sheets", help="export table to google sheets", action="store_true") parser.add_argument('-t', '--token', type=str, required=True, help='token github account') @@ -81,9 +83,12 @@ def main(): invites_parser.log_invitations(client, working_repos, csv_name) if args.wikis: wikipars.wikiparser(client, repositories, path_drepo, csv_name) + if args.contributors: + contributors_parser.log_contributors(client, token, working_repos, csv_name, fork_flag) if args.export_google_sheets: export_sheets.write_data_to_table(csv_name, args.google_token, args.table_id, args.sheet_id) if __name__ == '__main__': main() +