Skip to content

Commit eee57a3

Browse files
Copilotzkoppert
andcommitted
Fix time_in_draft iterator exhaustion bug
Co-authored-by: zkoppert <[email protected]>
1 parent 78e9bc3 commit eee57a3

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

test_time_in_draft.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,40 @@ def test_time_in_draft_with_attribute_error_scenario(self):
204204
expected = timedelta(days=3)
205205
self.assertEqual(result, expected, "The time in draft should be 3 days.")
206206

207+
def test_time_in_draft_with_iterator_events(self):
208+
"""
209+
Test measure_time_in_draft with events() returning an iterator instead of a list.
210+
This test ensures the function works correctly when events() returns an iterator
211+
(as it does in the real GitHub API), which can only be consumed once.
212+
"""
213+
# Set up issue created_at time
214+
self.issue.issue.created_at = "2021-01-01T00:00:00Z"
215+
216+
# Create an iterator of events (simulating real GitHub API behavior)
217+
def events_iterator():
218+
return iter(
219+
[
220+
MagicMock(
221+
event="converted_to_draft",
222+
created_at=datetime(2021, 1, 1, tzinfo=pytz.utc),
223+
),
224+
MagicMock(
225+
event="ready_for_review",
226+
created_at=datetime(2021, 1, 3, tzinfo=pytz.utc),
227+
),
228+
]
229+
)
230+
231+
self.issue.issue.events = events_iterator
232+
233+
result = measure_time_in_draft(self.issue)
234+
expected = timedelta(days=2)
235+
self.assertEqual(
236+
result,
237+
expected,
238+
"The time in draft should be 2 days when events() returns an iterator.",
239+
)
240+
207241

208242
class TestGetStatsTimeInDraft(unittest.TestCase):
209243
"""

time_in_draft.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def measure_time_in_draft(
2424
returns:
2525
Union[timedelta, None]: Total time the pull request has spent in draft state.
2626
"""
27-
events = issue.issue.events()
27+
events = list(issue.issue.events())
2828
draft_start = None
2929
total_draft_time = timedelta(0)
3030

0 commit comments

Comments
 (0)