-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add measure_time_in_draft() and tests
Signed-off-by: Zack Koppert <[email protected]>
- Loading branch information
Showing
4 changed files
with
117 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""A test suite for the measure_time_in_draft function.""" | ||
|
||
import unittest | ||
from datetime import datetime, timedelta | ||
from unittest.mock import MagicMock | ||
|
||
import pytz | ||
from time_in_draft import measure_time_in_draft | ||
|
||
|
||
class TestMeasureTimeInDraft(unittest.TestCase): | ||
""" | ||
Unit tests for the measure_time_in_draft function. | ||
""" | ||
|
||
def setUp(self): | ||
""" | ||
Setup common test data and mocks. | ||
""" | ||
self.issue = MagicMock() | ||
self.issue.issue.events.return_value = [ | ||
MagicMock( | ||
event="created_at", created_at=datetime(2021, 1, 1, tzinfo=pytz.utc) | ||
), | ||
MagicMock( | ||
event="other_event", created_at=datetime(2021, 1, 2, tzinfo=pytz.utc) | ||
), | ||
] | ||
|
||
def test_time_in_draft_with_ready_for_review(self): | ||
""" | ||
Test measure_time_in_draft when ready_for_review_at is provided. | ||
""" | ||
ready_for_review_at = datetime(2021, 1, 3, tzinfo=pytz.utc) | ||
result = measure_time_in_draft(self.issue, ready_for_review_at) | ||
expected = timedelta(days=2) | ||
self.assertEqual(result, expected, "The time in draft should be 2 days.") | ||
|
||
def test_time_in_draft_without_ready_for_review(self): | ||
""" | ||
Test measure_time_in_draft when ready_for_review_at is not provided. | ||
""" | ||
now = datetime(2021, 1, 4, tzinfo=pytz.utc) | ||
with unittest.mock.patch("time_in_draft.datetime") as mock_datetime: | ||
mock_datetime.now.return_value = now | ||
result = measure_time_in_draft(self.issue, None) | ||
expected = timedelta(days=3) | ||
self.assertEqual(result, expected, "The time in draft should be 3 days.") | ||
|
||
def test_time_in_draft_with_no_events(self): | ||
""" | ||
Test measure_time_in_draft when there are no events. | ||
""" | ||
self.issue.issue.events.return_value = [] | ||
result = measure_time_in_draft(self.issue, None) | ||
self.assertIsNone(result, "The result should be None when there are no events.") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
This module contains a function that measures the time a pull request has been in draft state. | ||
""" | ||
|
||
from datetime import datetime | ||
from typing import Union | ||
|
||
import github3 | ||
import pytz | ||
|
||
|
||
def measure_time_in_draft( | ||
issue: github3.issues.Issue, | ||
ready_for_review_at: Union[datetime, None], | ||
) -> Union[datetime, None]: | ||
"""If a pull request has had time in the draft state, return the amount of time it was in draft. | ||
args: | ||
issue (github3.issues.Issue): A GitHub issue which has been pre-qualified as a pull request. | ||
ready_for_review_at (datetime): The time the pull request was marked as ready for review. | ||
returns: | ||
Union[datetime, None]: The time the pull request was in draft state. | ||
""" | ||
events = issue.issue.events(number=50) | ||
try: | ||
pr_opened_at = None | ||
for event in events: | ||
if event.event == "created_at": | ||
pr_opened_at = event.created_at | ||
if pr_opened_at and ready_for_review_at: | ||
return ready_for_review_at - pr_opened_at | ||
if pr_opened_at and not ready_for_review_at: | ||
return datetime.now(pytz.utc) - pr_opened_at | ||
|
||
except TypeError as e: | ||
print( | ||
f"An error occurred processing review events for {issue.issue.html_url}. \ | ||
Perhaps issue contains a ghost user. {e}" | ||
) | ||
return None | ||
|
||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters