Skip to content

refactor: consolidate duplicate date-utility and repo-analytics-utili…#2731

Open
harshitanagpal05 wants to merge 4 commits into
Priyanshu-byte-coder:mainfrom
harshitanagpal05:feat/consolidate-utilities
Open

refactor: consolidate duplicate date-utility and repo-analytics-utili…#2731
harshitanagpal05 wants to merge 4 commits into
Priyanshu-byte-coder:mainfrom
harshitanagpal05:feat/consolidate-utilities

Conversation

@harshitanagpal05

Copy link
Copy Markdown
Contributor

Related Issue

Closes #2567


Type of Change

  • Refactor
  • Code Quality Improvement
  • Maintainability Improvement

Summary

This PR removes duplicated helper logic across multiple Next.js API endpoints by consolidating date range calculations, commit streak processing, and repository analytics into shared utility modules.

Previously, several routes maintained their own versions of the same fetch and aggregation logic, increasing maintenance overhead and the risk of inconsistent results.

This refactor creates single sources of truth while preserving existing behavior.


What Changed

1. Centralized Date & Streak Utilities

Created shared logic in:

src/lib/streak.ts

Added:

  • fetchActiveDates

Features:

  • Fetches user commit activity with pagination support
  • Supports up to 10 pages of results
  • Uses a 1-year lookback window
  • Handles timezone conversion consistently

Refactored:

  • src/app/api/metrics/streak/route.ts

    • Removed local fetchActiveDates
  • src/app/api/metrics/consistency-score/route.ts

    • Removed duplicate commit history fetching
  • src/app/api/metrics/weekly-summary/route.ts

    • Removed duplicate:

      • getCurrentWeekStartUtc
      • calculateCurrentStreak
      • fetchActiveDates
  • src/app/api/badge/streak-shield/route.ts

    • Replaced duplicate streak fetching logic
  • src/lib/public-profile-data.ts

    • Reused centralized streak utilities

2. Shared Week Calculation Helper

Updated:

src/lib/date-utils.ts

Changes:

  • Exported existing getUtcWeekStart
  • Reused common UTC week calculation logic

Refactored:

  • src/app/api/metrics/weekly-summary/route.ts
  • src/app/api/metrics/pr-review-time/route.ts

Removed duplicate week-start implementations.


3. Consolidated Repository Analytics

Created:

src/lib/repo-analytics-utils.ts

Added:

  • fetchTopRepos

Supports:

  • Custom lookback windows
  • Organization inclusion/exclusion filtering
  • Shared repository analytics calculations

Refactored:

  • src/lib/public-profile-data.ts
  • src/app/api/metrics/repo-health/route.ts
  • src/app/api/metrics/repos/route.ts

Code Quality Impact

  • Reduced duplicate business logic
  • Improved maintainability
  • Added reusable analytics helpers
  • Created consistent calculation behavior across endpoints

Changes:

  • 11 files modified
  • 171 additions
  • 441 deletions
  • Net reduction: ~270 lines

Testing

  • Existing behavior preserved
  • Shared helpers reused across previous duplicate paths
  • No intentional API response changes introduced

@github-actions github-actions Bot added type:bug GSSoC type bonus: bug fix type:feature GSSoC type bonus: new feature type:performance GSSoC type bonus: performance (+15 pts) gssoc26 GSSoC 2026 contribution type:refactor GSSoC type bonus: refactor (+10 pts) labels Jun 23, 2026
@github-actions

Copy link
Copy Markdown

GSSoC Label Checklist 🏷️

@Umbrella-io — please apply the appropriate labels before merging:

Difficulty (pick one):

  • level:beginner — 20 pts
  • level:intermediate — 35 pts
  • level:advanced — 55 pts
  • level:critical — 80 pts

Quality (optional):

  • quality:clean — ×1.2 multiplier
  • quality:exceptional — ×1.5 multiplier

Validation (required to score):

  • gssoc:approved — counts for points
  • gssoc:invalid / gssoc:spam / gssoc:ai-slop — does not score

Type labels (type:*) are auto-detected from files and title. Review and adjust if needed.
Points formula: (difficulty × quality_multiplier) + type_bonus

@Priyanshu-byte-coder

Copy link
Copy Markdown
Owner

Great refactoring work — consolidating duplicated code is exactly what we need right now. Net -270 lines is solid. A few issues to address before merge:

  1. Behavioral regression in repos/route.ts (important): The old code specifically checked for HTTP 401 and threw GitHubAuthError, which the GET handler catches to return a "reconnect your account" response. The shared fetchTopRepos throws a generic Error for all non-ok responses, so 401s will no longer trigger githubAuthErrorResponse() — they'll fall through to the generic 502 handler. Please either have fetchTopRepos throw a typed error on 401, or re-check the error type in the consumer.

  2. Broken .catch() pattern in repos/route.ts: The .catch() discards the original error's message and stack trace just to throw a different generic error. Either re-throw the original or remove the catch entirely.

  3. stale flag in streak-shield/route.ts: The old code only set stale: true on 403 (rate limit). The new catch-all sets it for every error type. Please preserve the original behavior of only marking stale on rate-limit errors.

Fix these and this is merge-ready. The consolidation itself is well done.

@harshitanagpal05

Copy link
Copy Markdown
Contributor Author

Hi, I’ve addressed all the requested review changes in PR #2731.

  • Restored GitHubAuthError handling for 401 responses
  • Removed the .catch() pattern that was losing original errors
  • Restored stale: true behavior only for rate-limit errors

The branch has been updated and the original behavior is preserved. Could you please review it again whenever you get a chance?

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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Good DRY refactoring — extracts shared fetchActiveDates to reduce duplication across streak/consistency-score routes.

@Priyanshu-byte-coder Priyanshu-byte-coder added gssoc:approved GSSoC: PR approved for scoring level2 GSSoC Level 2 - Medium complexity (25 points) labels Jul 1, 2026
@Priyanshu-byte-coder

Copy link
Copy Markdown
Owner

Approved — has merge conflicts. Please rebase on main.

@Priyanshu-byte-coder

Copy link
Copy Markdown
Owner

This has merge conflicts with main. Please rebase/resolve conflicts and make sure CI is green — then it'll get a fresh review.

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 level2 GSSoC Level 2 - Medium complexity (25 points) type:bug GSSoC type bonus: bug fix type:feature GSSoC type bonus: new feature type:performance GSSoC type bonus: performance (+15 pts) type:refactor GSSoC type bonus: refactor (+10 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT] Consolidate duplicate date-utility and repo-analytics-utility modules into single sources of truth

2 participants