Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 3, 2025

The time_in_draft metric was returning None for all pull requests when DRAFT_PR_TRACKING was 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:

  1. Detecting initially-draft PRs: If a PR has "ready_for_review" events but no prior "converted_to_draft" events, it was initially created as draft
  2. Calculating initial draft time: Time from PR creation to first "ready_for_review" event
  3. Handling ongoing drafts: PRs initially created as draft that are still in draft state
  4. Maintaining compatibility: All existing "converted_to_draft" workflows continue to work unchanged

Example

Before this fix, a PR created as draft on 2025-09-01 and marked ready on 2025-09-02 would show:

Time in draft: None

After this fix, the same PR correctly shows:

Time in draft: 1 day, 0:00:00

Changes Made

  • Updated function signature: Added optional pull_request parameter for better context
  • Enhanced detection logic: Identifies initially-draft PRs through event analysis
  • Updated call site: Pass pull request object from issue_metrics.py
  • Added comprehensive tests: Cover all draft scenarios including edge cases
  • Maintained backward compatibility: No breaking changes to existing functionality

The fix correctly handles:

  • ✅ PRs initially created as draft then marked ready for review
  • ✅ PRs initially created as draft that are still in draft state
  • ✅ Traditional workflow where PRs are converted to draft after creation
  • ✅ Multiple draft/ready cycles
  • ✅ Error cases and fallback scenarios

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.

Co-authored-by: zkoppert <6935431+zkoppert@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Time in draft for pull requests metrics is always None Fix time_in_draft calculation for pull requests initially created as drafts Sep 3, 2025
@Copilot Copilot AI requested a review from zkoppert September 3, 2025 16:42
Copilot finished work on behalf of zkoppert September 3, 2025 16:42
@github-actions github-actions bot added the fix label Sep 4, 2025
Signed-off-by: Zack Koppert <zkoppert@github.com>
@zkoppert zkoppert changed the title Fix time_in_draft calculation for pull requests initially created as drafts fix: time_in_draft calculation for pull requests initially created as drafts Sep 4, 2025
@zkoppert zkoppert marked this pull request as ready for review September 4, 2025 03:00
@zkoppert zkoppert requested a review from a team as a code owner September 4, 2025 03:00
@Copilot Copilot AI review requested due to automatic review settings September 4, 2025 03:00
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py to 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
Copy link

Copilot AI Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Zack Koppert <zkoppert@github.com>
@jmeridth jmeridth merged commit 6fa9041 into main Sep 10, 2025
32 checks passed
@jmeridth jmeridth deleted the copilot/fix-586 branch September 10, 2025 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Time in draft for pull requests metrics is always None

4 participants