Skip to content

Commit

Permalink
fix: use issue attributes instead of events
Browse files Browse the repository at this point in the history
Signed-off-by: Zack Koppert <[email protected]>
  • Loading branch information
zkoppert committed Oct 23, 2024
1 parent c035f14 commit a116564
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
22 changes: 9 additions & 13 deletions test_time_in_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ 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)
),
]
self.issue.issue.created_at = datetime(2021, 1, 1, tzinfo=pytz.utc)
self.issue.issue.state = "open"

def test_time_in_draft_with_ready_for_review(self):
"""
Expand All @@ -38,7 +32,7 @@ def test_time_in_draft_with_ready_for_review(self):

def test_time_in_draft_without_ready_for_review(self):
"""
Test measure_time_in_draft when ready_for_review_at is not provided.
Test measure_time_in_draft when ready_for_review_at is not provided and issue is still open.
"""
now = datetime(2021, 1, 4, tzinfo=pytz.utc)
with unittest.mock.patch("time_in_draft.datetime") as mock_datetime:
Expand All @@ -47,13 +41,15 @@ def test_time_in_draft_without_ready_for_review(self):
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):
def test_time_in_draft_without_ready_for_review_and_closed(self):
"""
Test measure_time_in_draft when there are no events.
Test measure_time_in_draft when ready_for_review_at is not provided and issue is closed.
"""
self.issue.issue.events.return_value = []
self.issue.issue.state = "closed"
result = measure_time_in_draft(self.issue, None)
self.assertIsNone(result, "The result should be None when there are no events.")
self.assertIsNone(
result, "The result should be None when draft was never used."
)


class TestGetStatsTimeInDraft(unittest.TestCase):
Expand Down
22 changes: 4 additions & 18 deletions time_in_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,10 @@ def measure_time_in_draft(
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

if ready_for_review_at:
return ready_for_review_at - issue.issue.created_at
if issue.issue.state == "open":
return datetime.now(pytz.utc) - issue.issue.created_at
return None


Expand Down

0 comments on commit a116564

Please sign in to comment.