Skip to content

Commit 66e9f5b

Browse files
authored
Merge pull request #121 from eichisanden/main
2 parents 5d12b79 + 867f855 commit 66e9f5b

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ The metrics that are measured are:
1313
| Time to answer | (Discussions only) The time between when a discussion is created and when it is answered. |
1414
| Time in label | The time between when a label has a specific label applied to an issue/pull request/discussion and when it is removed. This requires the LABELS_TO_MEASURE env variable to be set. |
1515

16-
*For pull requests, these metrics exclude the time the PR was in draft mode.
16+
*For pull requests, these metrics exclude the time the PR was in draft mode.
17+
*For Issue and pull requests, issue/pull request author's own comments and comments by bots are excluded.
1718

1819
This action was developed by the GitHub OSPO for our own use and developed in a way that we could open source it that it might be useful to you as well! If you want to know more about how we use it, reach out in an issue in this repository.
1920

test_time_to_first_response.py

+30
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,36 @@ def test_measure_time_to_first_response_ignore_issue_owners_comment(self):
280280
# Check the results
281281
self.assertEqual(result, expected_result)
282282

283+
def test_measure_time_to_first_response_ignore_bot(self):
284+
"""Test that measure_time_to_first_response ignore bot's comment."""
285+
# Set up the mock GitHub issues
286+
mock_issue1 = MagicMock()
287+
mock_issue1.comments = 2
288+
mock_issue1.created_at = "2023-01-01T00:00:00Z"
289+
290+
# Set up the mock GitHub issue comments
291+
mock_comment1 = MagicMock()
292+
mock_comment1.user.type = "Bot"
293+
mock_comment1.created_at = datetime.fromisoformat("2023-01-02T00:00:00Z")
294+
mock_issue1.issue.comments.return_value = [mock_comment1]
295+
296+
# Set up the mock GitHub pull request comments
297+
mock_pr_comment1 = MagicMock()
298+
mock_pr_comment1.user.type = "Bot"
299+
mock_pr_comment1.submitted_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
300+
mock_pr_comment2 = MagicMock()
301+
mock_pr_comment2.user.type = "User"
302+
mock_pr_comment2.submitted_at = datetime.fromisoformat("2023-01-04T00:00:00Z") # first response
303+
mock_pull_request = MagicMock()
304+
mock_pull_request.reviews.return_value = [mock_pr_comment1, mock_pr_comment2]
305+
306+
# Call the function
307+
result = measure_time_to_first_response(mock_issue1, None, mock_pull_request, None)
308+
expected_result = timedelta(days=3)
309+
310+
# Check the results
311+
self.assertEqual(result, expected_result)
312+
283313

284314
class TestGetAverageTimeToFirstResponse(unittest.TestCase):
285315
"""Test the get_average_time_to_first_response function."""

time_to_first_response.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ def measure_time_to_first_response(
5757
number=20, sort="created", direction="asc"
5858
) # type: ignore
5959
for comment in comments:
60-
if comment.user.login in ignore_users:
61-
continue
62-
if comment.user.login == issue.issue.user.login:
63-
continue
64-
if ready_for_review_at and comment.created_at < ready_for_review_at:
60+
if ignore_comment(issue.issue.user, comment.user, ignore_users, comment.created_at, ready_for_review_at):
6561
continue
6662
first_comment_time = comment.created_at
6763
break
@@ -71,11 +67,8 @@ def measure_time_to_first_response(
7167
if pull_request:
7268
review_comments = pull_request.reviews(number=50) # type: ignore
7369
for review_comment in review_comments:
74-
if review_comment.user.login in ignore_users:
75-
continue
76-
if review_comment.user.login == issue.issue.user.login:
77-
continue
78-
if ready_for_review_at and review_comment.submitted_at < ready_for_review_at:
70+
if ignore_comment(issue.issue.user, review_comment.user, ignore_users,
71+
review_comment.submitted_at, ready_for_review_at):
7972
continue
8073
first_review_comment_time = review_comment.submitted_at
8174
break
@@ -109,6 +102,25 @@ def measure_time_to_first_response(
109102
return None
110103

111104

105+
def ignore_comment(
106+
issue_user: github3.users.User,
107+
comment_user: github3.users.User,
108+
ignore_users: List[str],
109+
comment_created_at: datetime,
110+
ready_for_review_at: Union[datetime, None],
111+
) -> bool:
112+
"""Check if a comment should be ignored."""
113+
return (
114+
# ignore comments by IGNORE_USERS
115+
comment_user.login in ignore_users
116+
# ignore comments by bots
117+
or comment_user.type == "Bot"
118+
# ignore comments by the issue creator
119+
or comment_user.login == issue_user.login
120+
# ignore comments created before the issue was ready for review
121+
or (ready_for_review_at and comment_created_at < ready_for_review_at))
122+
123+
112124
def get_average_time_to_first_response(
113125
issues: List[IssueWithMetrics],
114126
) -> Union[timedelta, None]:

0 commit comments

Comments
 (0)