Skip to content

fix(metrics): correct merged PR count and merge rate calculation#340

Merged
Priyanshu-byte-coder merged 1 commit into
Priyanshu-byte-coder:mainfrom
anshul23102:fix/pr-metrics-merged-count-and-merge-rate
May 19, 2026
Merged

fix(metrics): correct merged PR count and merge rate calculation#340
Priyanshu-byte-coder merged 1 commit into
Priyanshu-byte-coder:mainfrom
anshul23102:fix/pr-metrics-merged-count-and-merge-rate

Conversation

@anshul23102
Copy link
Copy Markdown
Contributor

Closes #339

What was wrong

Two bugs in `fetchPRMetrics()` in `src/app/api/metrics/prs/route.ts` caused incorrect data for all users.

Bug 1 — Wrong merged count:
The GitHub Search API returns `state: "closed"` for both merged PRs and PRs closed without merging. The old code counted all closed PRs as merged. Fixed by checking `pull_request.merged_at`, which is non-null only for actually merged PRs.

Bug 2 — Wrong merge rate denominator:
`mergeRate` was computed as `merged / data.total_count` where `total_count` is the user's all-time PR count (potentially thousands) while `merged` was derived from `data.items`, capped at 100. This produced a near-zero rate for any active user. Fixed by using `data.items.length` (the fetched sample size) as the denominator.

Bug 3 — avgReviewHours used wrong end timestamp:
Average review time now uses `pull_request.merged_at` instead of `closed_at` and only counts actually merged PRs, giving a meaningful time-to-merge metric.

Files changed

  • `src/app/api/metrics/prs/route.ts` — `fetchPRMetrics()` function only

Type of change

  • Bug fix
  • Data integrity improvement

Two bugs in fetchPRMetrics() caused incorrect dashboard data for all users.

Bug 1 - Wrong merged count:
The GitHub Search API returns state "closed" for both merged PRs and PRs
that were closed without merging (rejected, abandoned, spam). The old code
counted all closed PRs as merged. Fixed by checking pull_request.merged_at,
which is non-null only for actually merged PRs.

Bug 2 - Wrong merge rate denominator:
mergeRate was computed as merged / data.total_count where total_count is
the user's all-time PR count (potentially thousands) while merged was
derived from data.items, capped at 100 results. This produced a near-zero
rate for any active user regardless of their actual merge history. Fixed
by using data.items.length (the fetched sample size) as the denominator.

Also tightened avgReviewHours to measure open-to-merge time using
merged_at instead of closed_at, which excluded unmerged PRs implicitly
but used the wrong timestamp for the end of the review window.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 19, 2026

@anshul23102 is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Priyanshu-byte-coder Priyanshu-byte-coder marked this pull request as ready for review May 19, 2026 05:14
@Priyanshu-byte-coder Priyanshu-byte-coder self-requested a review as a code owner May 19, 2026 05:14
Copy link
Copy Markdown
Owner

@Priyanshu-byte-coder Priyanshu-byte-coder left a comment

Choose a reason for hiding this comment

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

Correct. state: 'closed' in the GitHub Search API includes both merged and rejected PRs — checking pull_request.merged_at != null correctly distinguishes them. Using sample size instead of total_count for merge rate is the right fix — total_count is all-time and makes the rate near-zero for active users.

@Priyanshu-byte-coder Priyanshu-byte-coder merged commit 1d63eb1 into Priyanshu-byte-coder:main May 19, 2026
7 checks passed
@Priyanshu-byte-coder Priyanshu-byte-coder added gssoc:approved GSSoC: PR approved for scoring level:intermediate GSSoC: Intermediate difficulty (35 pts) gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix quality:clean GSSoC: Clean quality multiplier (×1.2) labels May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved GSSoC: PR approved for scoring gssoc26 GSSoC 2026 contribution level:intermediate GSSoC: Intermediate difficulty (35 pts) quality:clean GSSoC: Clean quality multiplier (×1.2) type:bug GSSoC type bonus: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] PR metrics show wrong merged count and near-zero merge rate for all users

2 participants