From 49e8e29e7a39a32e8f0a7e392fe63b480abb2adf Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Fri, 14 Mar 2025 09:48:38 +0100 Subject: [PATCH] updated the type annotations for the utils functions, much better now --- utils.py | 59 ++++++++++++++++---------------------------------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/utils.py b/utils.py index 3ac81fe..6a9d140 100644 --- a/utils.py +++ b/utils.py @@ -1,35 +1,22 @@ from datetime import datetime +from github.Commit import Commit +from github.PaginatedList import PaginatedList +from github.PullRequestComment import PullRequestComment from tqdm import tqdm import logging def parse_date(date: str) -> datetime: return datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ") -def has_only_1_round_of_comments(commits, comments): - if not comments or not commits: +def has_only_1_round_of_comments(commits: PaginatedList[Commit], comments: PaginatedList[PullRequestComment]): + if ( + comments is None or commits is None + or comments.totalCount == 0 or commits.totalCount == 0 + ): return False - commit_dates = [] - for commit in commits: - if isinstance(commit.commit.author.date, str): - commit_dates.append(parse_date(commit.commit.author.date)) - elif isinstance(commit.commit.author.date, datetime): - commit_dates.append(commit.commit.author.date) - else: - logging.warning(f"The commit {commit.sha} has an unexpected date format: {commit.commit.author.date}") - logging.warning(f"Tied to PR: {comments[0]['pull_request_url']}") - return False - - comment_dates = [] - for comment in comments: - if isinstance(comment.created_at, str): - comment_dates.append(parse_date(comment.created_at)) - elif isinstance(comment.created_at, datetime): - comment_dates.append(comment.created_at) - else: - logging.warning(f"The comment {comment['id']} has an unexpected date format: {comment['created_at']}") - logging.warning(f"Tied to PR: {comment['pull_request_url']}") - return False + commit_dates = [commit.commit.author.date for commit in tqdm(commits, total=commits.totalCount, desc="Extracting date from commits", leave=False)] + comment_dates = [comment.created_at for comment in tqdm(comments, total=comments.totalCount, desc="Extracting date from comments", leave=False)] commit_dates.sort() comment_dates.sort() @@ -51,29 +38,17 @@ def has_only_1_round_of_comments(commits, comments): return n_before >= 1 and n_after >= 1 -def has_only_1_comment(commits, comments): - if not comments or not commits: +def has_only_1_comment(commits: PaginatedList[Commit], comments: PaginatedList[PullRequestComment]): + if ( + comments is None or commits is None + or comments.totalCount == 0 or commits.totalCount == 0 + ): return False - commit_dates = [] - for commit in commits: - if isinstance(commit.commit.author.date, str): - commit_dates.append(parse_date(commit.commit.author.date)) - elif isinstance(commit.commit.author.date, datetime): - commit_dates.append(commit.commit.author.date) - else: - logging.warning(f"The commit {commit.sha} has an unexpected date format: {commit.commit.author.date}") - logging.warning(f"Tied to PR: {comments[0]['pull_request_url']}") - return False + commit_dates = [commit.commit.author.date for commit in tqdm(commits, total=commits.totalCount, desc="Extracting date from commits", leave=False)] commit_dates.sort() - if isinstance(comments[0].created_at, datetime): - comment_date = comments[0].created_at - elif isinstance(comments[0].created_at, str): - comment_date = parse_date(comments[0].created_at) - else: - logging.warning(f"The comment {comments[0]['id']} has an unexpected date format: {comments[0]['created_at']}") - return False + comment_date = comments[0].created_at n_before = n_after = 0 for commit_date in tqdm(commit_dates, desc="Checking for 1 comment", leave=False):