diff --git a/GitHubRepoAPI.py b/GitHubRepoAPI.py index a74a5efa..576021bb 100644 --- a/GitHubRepoAPI.py +++ b/GitHubRepoAPI.py @@ -1,3 +1,5 @@ +from github import Github, GithubException + from interface_wrapper import ( Branch, Comment, @@ -13,8 +15,6 @@ logging, ) -from github import Github, GithubException - class GitHubRepoAPI(IRepositoryAPI): def __init__(self, client: Github): diff --git a/commits_parser.py b/commits_parser.py index e4e248f3..333a5af5 100644 --- a/commits_parser.py +++ b/commits_parser.py @@ -1,7 +1,7 @@ from dataclasses import asdict, dataclass +from datetime import datetime from time import sleep from typing import Generator -from datetime import datetime import pytz @@ -88,3 +88,4 @@ def log_commits( sleep(TIMEDELTA) except Exception as e: print(e) + exit(1) diff --git a/interface_wrapper.py b/interface_wrapper.py index c3df4342..aa28736d 100644 --- a/interface_wrapper.py +++ b/interface_wrapper.py @@ -3,7 +3,7 @@ from dataclasses import dataclass from datetime import datetime -from github import Github, Auth +from github import Auth, Github from pyforgejo import PyforgejoApi # Настройка логирования diff --git a/invites_parser.py b/invites_parser.py index 33848d8f..1198b4da 100644 --- a/invites_parser.py +++ b/invites_parser.py @@ -45,3 +45,4 @@ def log_invitations( log_repository_invitations(client, repo, csv_name) except Exception as e: print(e) + exit(1) diff --git a/issues_parser.py b/issues_parser.py index c10cea59..aefede77 100644 --- a/issues_parser.py +++ b/issues_parser.py @@ -1,8 +1,8 @@ import json from dataclasses import asdict, dataclass +from datetime import datetime from time import sleep from typing import Generator -from datetime import datetime import pytz import requests @@ -36,11 +36,11 @@ class IssueData: @dataclass(kw_only=True, frozen=True) class IssueDataWithComment(IssueData): - body: str = '' - created_at: str = '' - author_name: str = '' - author_login: str = '' - author_email: str = '' + comment_body: str = '' + comment_created_at: str = '' + comment_author_name: str = '' + comment_author_login: str = '' + comment_author_email: str = '' def get_connected_pulls(issue_number, repo_owner, repo_name, token): @@ -171,12 +171,12 @@ def log_issue_and_comments(csv_name, issue_data: IssueData, comments): if comments: for comment in comments: comment_data = IssueDataWithComment( - **issue_data, - body=comment.body, - created_at=str(comment.created_at), - author_name=comment.author.username, - author_login=comment.author.login, - author_email=comment.author.email, + **asdict(issue_data), + comment_body=comment.body, + comment_created_at=str(comment.created_at), + comment_author_name=comment.author.username, + comment_author_login=comment.author.login, + comment_author_email=comment.author.email, ) comment_data = asdict(comment_data) @@ -213,3 +213,4 @@ def log_issues( sleep(TIMEDELTA) except Exception as e: print("log_issues exception:", e) + exit(1) diff --git a/main.py b/main.py index dc72a0bf..c2e72ac5 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,14 @@ import argparse import traceback -import git_logger -import export_sheets import commits_parser import contributors_parser -import pull_requests_parser +import export_sheets +import git_logger import invites_parser import issues_parser +import pull_requests_parser import wikipars - from utils import parse_time diff --git a/pull_requests_parser.py b/pull_requests_parser.py index fb9f59c6..2b23df03 100644 --- a/pull_requests_parser.py +++ b/pull_requests_parser.py @@ -1,8 +1,8 @@ import json from dataclasses import asdict, dataclass +from datetime import datetime from time import sleep from typing import Generator -from datetime import datetime import pytz import requests @@ -39,11 +39,11 @@ class PullRequestData: @dataclass(kw_only=True, frozen=True) class PullRequestDataWithComment(PullRequestData): - body: str = '' - created_at: str = '' - author_name: str = '' - author_login: str = '' - author_email: str = '' + comment_body: str = '' + comment_created_at: str = '' + comment_author_name: str = '' + comment_author_login: str = '' + comment_author_email: str = '' def get_related_issues(pull_request_number, repo_owner, repo_name, token): @@ -165,12 +165,12 @@ def get_info(obj, attr): if comments: for comment in comments: comment_data = PullRequestDataWithComment( - **pr_data, - body=comment.body, - created_at=str(comment.created_at), - author_name=comment.author.name, - author_login=comment.author.login, - author_email=nvl(comment.author.email), + **asdict(pr_data), + comment_body=comment.body, + comment_created_at=str(comment.created_at), + comment_author_name=comment.author.name, + comment_author_login=comment.author.login, + comment_author_email=nvl(comment.author.email), ) comment_data = asdict(comment_data) @@ -222,3 +222,4 @@ def log_pull_requests( sleep(TIMEDELTA) except Exception as e: print(e) + exit(1) diff --git a/requirements.txt b/requirements.txt index 44ebea63..75446911 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ pandas==2.2.3 pytz==2024.2 requests==2.32.3 pyforgejo==2.0.0 -isodate==0.7.2 \ No newline at end of file +isodate==0.7.2 +unittest-parametrize==1.6.0 diff --git a/test_token_usage.py b/test_token_usage.py index ac6d516f..81148248 100644 --- a/test_token_usage.py +++ b/test_token_usage.py @@ -1,10 +1,11 @@ -import unittest import argparse import sys +import unittest -from main import run +from unittest_parametrize import ParametrizedTestCase, param, parametrize import git_logger +from main import run def parse_args(args): @@ -25,7 +26,7 @@ def parse_args(args): return parser.parse_args(args) -class TestTokenUsage(unittest.TestCase): +class TestTokenUsage(ParametrizedTestCase): def setUp(self): test_args = parse_args(sys.argv[1:]) self.tokens = [test_args.tt1, test_args.tt2] @@ -79,60 +80,21 @@ def _get_usage(self, binded_repos, clients): return limit_start, limit_finish - def test_commits_parser(self): - self.args.commits = True - for i in range(2): - clients = git_logger.Clients( - "github", self._change_tokens_order(self.tokens, i) - ) - binded_repos = git_logger.get_next_binded_repo(clients, [self.repo]) - - limit_start, limit_finish = self._get_usage(binded_repos, clients) - - self.assertTrue(self._is_only_one_token_used(limit_start, limit_finish)) - self.assertTrue(self._is_max_token_used(limit_start, limit_finish)) - - def test_contributors_parser(self): - self.args.contributors = True - for i in range(2): - clients = git_logger.Clients( - "github", self._change_tokens_order(self.tokens, i) - ) - binded_repos = git_logger.get_next_binded_repo(clients, [self.repo]) - - limit_start, limit_finish = self._get_usage(binded_repos, clients) - - self.assertTrue(self._is_only_one_token_used(limit_start, limit_finish)) - self.assertTrue(self._is_max_token_used(limit_start, limit_finish)) - - def test_issues_parser(self): - self.args.issues = True - for i in range(2): - clients = git_logger.Clients( - "github", self._change_tokens_order(self.tokens, i) - ) - binded_repos = git_logger.get_next_binded_repo(clients, [self.repo]) - - limit_start, limit_finish = self._get_usage(binded_repos, clients) - - self.assertTrue(self._is_only_one_token_used(limit_start, limit_finish)) - self.assertTrue(self._is_max_token_used(limit_start, limit_finish)) - - def test_invites_parser(self): - self.args.invites = True - for i in range(2): - clients = git_logger.Clients( - "github", self._change_tokens_order(self.tokens, i) - ) - binded_repos = git_logger.get_next_binded_repo(clients, [self.repo]) - - limit_start, limit_finish = self._get_usage(binded_repos, clients) - - self.assertTrue(self._is_only_one_token_used(limit_start, limit_finish)) - self.assertTrue(self._is_max_token_used(limit_start, limit_finish)) + @parametrize( + 'args', + [ + param({'commits': True}, id='commits'), + param({'contributors': True}, id='contributors'), + param({'issues': True}, id='issues'), + param({'invites': True}, id='invites'), + param({'pull_requests': True}, id='pull_requests'), + ], + ) + def test_commits_parser(self, args: dict[str, bool]): + # patch args + for k, v in args.items(): + setattr(self.args, k, v) - def test_pull_requests_parser(self): - self.args.pull_requests = True for i in range(2): clients = git_logger.Clients( "github", self._change_tokens_order(self.tokens, i) diff --git a/utils.py b/utils.py index 9088b574..62ba46a8 100644 --- a/utils.py +++ b/utils.py @@ -1,8 +1,9 @@ import csv from datetime import datetime + import pytz -from constants import MIN_SIDE_PADDING, SIDE_WHITE_SPACES, TITLE_LEN, TIMEZONE +from constants import MIN_SIDE_PADDING, SIDE_WHITE_SPACES, TIMEZONE, TITLE_LEN class logger: diff --git a/wikipars.py b/wikipars.py index fbb86425..67318c25 100644 --- a/wikipars.py +++ b/wikipars.py @@ -4,7 +4,6 @@ from git import Repo, exc from constants import WIKI_FIELDNAMES - from utils import logger