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