|
| 1 | +"""A module for measuring the number of comments on pull requests. |
| 2 | +
|
| 3 | +This module provides functions for counting comments on GitHub pull requests, |
| 4 | +excluding bot comments, and calculating statistics about comment counts. |
| 5 | +
|
| 6 | +Functions: |
| 7 | + count_pr_comments( |
| 8 | + issue: Union[github3.issues.Issue, None], |
| 9 | + pull_request: Union[github3.pulls.PullRequest, None], |
| 10 | + ignore_users: Union[List[str], None] = None, |
| 11 | + ) -> Union[int, None]: |
| 12 | + Count the number of comments on a pull request, excluding bot comments. |
| 13 | + get_stats_pr_comments( |
| 14 | + issues_with_metrics: List[IssueWithMetrics], |
| 15 | + ) -> Union[dict[str, float], None]: |
| 16 | + Calculate stats describing the comment count for a list of pull requests. |
| 17 | +""" |
| 18 | + |
| 19 | +from typing import List, Union |
| 20 | + |
| 21 | +import github3 |
| 22 | +import numpy |
| 23 | +from classes import IssueWithMetrics |
| 24 | + |
| 25 | + |
| 26 | +def count_pr_comments( |
| 27 | + issue: Union[github3.issues.Issue, None], # type: ignore |
| 28 | + pull_request: Union[github3.pulls.PullRequest, None] = None, |
| 29 | + ignore_users: Union[List[str], None] = None, |
| 30 | +) -> Union[int, None]: |
| 31 | + """Count the number of comments on a pull request, excluding bot comments. |
| 32 | +
|
| 33 | + Args: |
| 34 | + issue (Union[github3.issues.Issue, None]): A GitHub issue. |
| 35 | + pull_request (Union[github3.pulls.PullRequest, None]): A GitHub pull request. |
| 36 | + ignore_users (Union[List[str], None]): A list of GitHub usernames to ignore. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + Union[int, None]: The number of comments on the pull request, excluding bots. |
| 40 | + Returns None if not a pull request. |
| 41 | + """ |
| 42 | + if not pull_request or not issue: |
| 43 | + return None |
| 44 | + |
| 45 | + if ignore_users is None: |
| 46 | + ignore_users = [] |
| 47 | + |
| 48 | + comment_count = 0 |
| 49 | + |
| 50 | + # Count issue comments |
| 51 | + try: |
| 52 | + comments = issue.issue.comments() # type: ignore |
| 53 | + for comment in comments: |
| 54 | + # Skip bot comments and ignored users |
| 55 | + if ( |
| 56 | + str(comment.user.type.lower()) != "bot" |
| 57 | + and comment.user.login not in ignore_users |
| 58 | + ): |
| 59 | + comment_count += 1 |
| 60 | + except (AttributeError, TypeError): |
| 61 | + # If we can't get comments, just continue |
| 62 | + pass |
| 63 | + |
| 64 | + # Count pull request review comments |
| 65 | + try: |
| 66 | + review_comments = pull_request.review_comments() |
| 67 | + for comment in review_comments: |
| 68 | + # Skip bot comments and ignored users |
| 69 | + if ( |
| 70 | + str(comment.user.type.lower()) != "bot" |
| 71 | + and comment.user.login not in ignore_users |
| 72 | + ): |
| 73 | + comment_count += 1 |
| 74 | + except (AttributeError, TypeError): |
| 75 | + # If we can't get review comments, just continue |
| 76 | + pass |
| 77 | + |
| 78 | + return comment_count |
| 79 | + |
| 80 | + |
| 81 | +def get_stats_pr_comments( |
| 82 | + issues_with_metrics: List[IssueWithMetrics], |
| 83 | +) -> Union[dict[str, float], None]: |
| 84 | + """Calculate stats describing the comment count for a list of pull requests. |
| 85 | +
|
| 86 | + Args: |
| 87 | + issues_with_metrics (List[IssueWithMetrics]): A list of GitHub issues with metrics attached. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + Union[Dict[str, float], None]: The stats describing comment counts for PRs. |
| 91 | + """ |
| 92 | + # Filter out issues that are not pull requests or have no comment count |
| 93 | + prs_with_comment_counts = [ |
| 94 | + issue.pr_comment_count |
| 95 | + for issue in issues_with_metrics |
| 96 | + if issue.pr_comment_count is not None |
| 97 | + ] |
| 98 | + |
| 99 | + if not prs_with_comment_counts: |
| 100 | + return None |
| 101 | + |
| 102 | + # Calculate statistics |
| 103 | + average_comment_count = numpy.round(numpy.average(prs_with_comment_counts), 1) |
| 104 | + median_comment_count = numpy.round(numpy.median(prs_with_comment_counts), 1) |
| 105 | + ninety_percentile_comment_count = numpy.round( |
| 106 | + numpy.percentile(prs_with_comment_counts, 90), 1 |
| 107 | + ) |
| 108 | + |
| 109 | + stats = { |
| 110 | + "avg": average_comment_count, |
| 111 | + "med": median_comment_count, |
| 112 | + "90p": ninety_percentile_comment_count, |
| 113 | + } |
| 114 | + |
| 115 | + # Print the statistics |
| 116 | + print(f"Average number of comments per PR: {average_comment_count}") |
| 117 | + print(f"Median number of comments per PR: {median_comment_count}") |
| 118 | + print(f"90th percentile of comments per PR: {ninety_percentile_comment_count}") |
| 119 | + |
| 120 | + return stats |
0 commit comments