Skip to content

refactor(db): extract shared query builder in prWorkItemsRepository#982

Merged
aaight merged 1 commit intodevfrom
refactor/pr-work-items-repository
Mar 22, 2026
Merged

refactor(db): extract shared query builder in prWorkItemsRepository#982
aaight merged 1 commit intodevfrom
refactor/pr-work-items-repository

Conversation

@aaight
Copy link
Copy Markdown
Collaborator

@aaight aaight commented Mar 22, 2026

Summary

Refactors src/db/repositories/prWorkItemsRepository.ts to eliminate ~120 lines of repeated Drizzle query code across 3 near-identical PR listing functions.

  • Extract getPRSummarySelect() — lazy getter for the shared 7-column select map used by all PR summary queries
  • Extract getPRSummaryGroupBy() — lazy getter for the matching groupBy columns, also spread into listUnifiedWorkForProject's extended groupBy
  • Create queryPRSummaries(whereCondition) — internal query builder that encapsulates the full select/from/leftJoin/where/groupBy/orderBy pattern; listPRsForProject, listPRsForOrg, and listPRsForWorkItem are now single-line call-throughs
  • Extract resolveOrgProjectIds(orgId) — DRY helper for fetching project IDs by org, removing duplication between listPRsForOrg and listWorkItems

The helpers use lazy getter functions (not module-level constants) to avoid breaking tests that mock schema/index.js without including prWorkItems.

Test plan

  • All 43 existing unit tests for prWorkItemsRepository pass without modification
  • All 32 PR router unit tests (tests/unit/api/routers/prs.test.ts) pass
  • All previously failing tests (router.test.ts, routers/projects.test.ts) now pass
  • Full unit test suite: 332 files / 6290 tests all pass
  • npm run typecheck — zero errors
  • npm run lint — zero errors

Related

https://trello.com/c/cZaMf79A/507-find-top-candidate-for-refactoring-and-plan-clean-refactoring-of-it-look-for-god-classes-modules-functions-files-code-duplicatio

🤖 Generated with Claude Code

🕵️ claude-code · claude-sonnet-4-6 · run details

Copy link
Copy Markdown
Collaborator

@nhopeatall nhopeatall left a comment

Choose a reason for hiding this comment

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

LGTM — Clean DRY refactoring that eliminates ~120 lines of repeated Drizzle query code without changing any observable behavior. The extraction of getPRSummarySelect(), getPRSummaryGroupBy(), queryPRSummaries(), and resolveOrgProjectIds() is well-motivated, correctly preserves all column selections and groupings, and the lazy-evaluation approach is a smart choice for test compatibility. All CI checks pass.

One minor note: the as SQL cast in listPRsForWorkItem (line 317) is a pragmatic trade-off introduced by the refactoring. It's safe at runtime since and() with two concrete SQL arguments always returns SQL, but it's worth knowing this is a type assertion rather than a type-safe narrowing. Not blocking — just documenting the trade-off. An alternative would be to widen queryPRSummaries to accept SQL | undefined, but that would weaken the contract and allow accidentally unfiltered queries, so the current approach is the better trade-off.

🕵️ claude-code · claude-opus-4-6 · run details

@aaight aaight merged commit a577548 into dev Mar 22, 2026
8 checks passed
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants