fix: time_in_draft calculation for pull requests initially created as drafts#587
Merged
fix: time_in_draft calculation for pull requests initially created as drafts#587
Conversation
Co-authored-by: zkoppert <6935431+zkoppert@users.noreply.github.com>
Copilot
AI
changed the title
[WIP]
Fix time_in_draft calculation for pull requests initially created as drafts
Sep 3, 2025
Time in draft for pull requests metrics is always NoneSigned-off-by: Zack Koppert <zkoppert@github.com>
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug in the time_in_draft calculation where pull requests initially created as drafts were returning None instead of the actual time spent in draft state. The fix enhances the detection logic to handle PRs created as drafts from the beginning, not just those converted to draft status later.
Key changes:
- Enhanced the
measure_time_in_draft()function to detect and calculate draft time for PRs initially created as drafts - Added comprehensive test coverage for the new draft detection scenarios
- Updated the function call in
issue_metrics.pyto pass the pull request object for better context
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| time_in_draft.py | Enhanced draft time calculation logic to handle PRs initially created as drafts |
| test_time_in_draft.py | Added comprehensive test cases for initially-draft PR scenarios |
| issue_metrics.py | Updated function call to pass pull request object parameter |
Comment on lines
+172
to
+173
| # Keep the real datetime class but only mock the now() method | ||
| mock_datetime.fromisoformat = datetime.fromisoformat |
There was a problem hiding this comment.
This approach of selectively mocking datetime methods is fragile and could break if the implementation changes. Consider using freezegun or a similar library for more robust datetime mocking in tests.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Zack Koppert <zkoppert@github.com>
paul-uz
approved these changes
Sep 4, 2025
jmeridth
approved these changes
Sep 10, 2025
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
time_in_draftmetric was returningNonefor all pull requests whenDRAFT_PR_TRACKINGwas enabled, even for PRs that were actually in draft status or had previously been drafts.Root Cause
The
measure_time_in_draft()function only tracked "converted_to_draft" events, which occur when a ready PR is converted back to draft. However, it missed the common case where pull requests are initially created as drafts. When a PR is created as a draft, there is no "converted_to_draft" event - it simply starts in that state.The Fix
Enhanced the function to detect and handle PRs initially created as drafts by:
Example
Before this fix, a PR created as draft on 2025-09-01 and marked ready on 2025-09-02 would show:
After this fix, the same PR correctly shows:
Changes Made
pull_requestparameter for better contextissue_metrics.pyThe fix correctly handles:
Fixes #586.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.