Skip to content

[TW-4734] refactor(cli): reduce code duplication across commands and adapters#42

Merged
qasim-nylas merged 2 commits intomainfrom
feature/TW-4734-reduce-code-duplication
Mar 28, 2026
Merged

[TW-4734] refactor(cli): reduce code duplication across commands and adapters#42
qasim-nylas merged 2 commits intomainfrom
feature/TW-4734-reduce-code-duplication

Conversation

@qasim-nylas
Copy link
Copy Markdown
Collaborator

@qasim-nylas qasim-nylas commented Mar 28, 2026

Summary

  • Centralize repeated status-to-color mapping into common.StatusColor() / StatusIcon() / ColorSprint(), replacing duplicate functions in scheduler, webhook, and notetaker packages
  • Add common.SetupPagination() with explicit PaginationMode (SinglePage / WithCap / All) to eliminate duplicate limit/maxItems resolution logic across list commands
  • Add common.NormalizePageSize() and FetchCursorPages() to reduce cursor-based pagination boilerplate
  • Extract dashboard.setDPoPProof() from duplicate blocks in doPostRaw() and doGetRaw()
  • Add testutil.WriteJSONResponse() — goroutine-safe, buffer-first encoding with proper 500 on failure — for the JSON response pattern repeated 186 times across 38 test files
  • Consolidate email list command into shared fetchListMessages() / applyListFolderFilter() / resolveListPagination()
  • Clean up CLAUDE.md: remove git push/commit rules, add shared helper learnings, reduce redundancy

Changed Files

New files (7)

File Purpose
common/status.go StatusColor, StatusIcon, ColorSprint — 17 status values
common/status_test.go 29 test cases
dashboard/http_test.go 13 test cases (DPoP, error parsing, envelope, integration)
calendar/events_list_test.go New test coverage for calendar list
contacts/list_test.go New test coverage for contacts list
email/list_test.go New test coverage for email list
webhook/render_test.go New test coverage for webhook rendering

Modified files (16)

File Change
common/pagination.go Added SetupPagination, PaginationMode, NormalizePageSize, FetchCursorPages
common/pagination_test.go 8 new test cases for pagination helpers
dashboard/http.go Extracted setDPoPProof() from 2 duplicate blocks
testutil/helpers.go Added WriteJSONResponse() with buffer-first encoding
testutil/helpers_test.go 5 test cases including encode-failure path
scheduler/bookings.go Removed getStatusColor() + inline switch → common.ColorSprint()
webhook/list.go Removed getStatusIcon()common.StatusIcon()
webhook/show.go Refactored to use displayWebhookDetails() + common.StatusIcon()
webhook/update.go getStatusIcon()common.StatusIcon()
webhook/webhook_advanced_test.go Updated to use common.StatusIcon()
notetaker/list.go Simplified formatState() from 22 lines to 7 via common.StatusColor()
contacts/list.go Manual pagination → common.SetupPagination()
email/list.go Consolidated into resolveListPagination, fetchListMessages, applyListFolderFilter
email/search.go Uses NormalizePageSize() and FetchCursorPages()
calendar/events_list.go Uses SetupPagination() and FetchCursorPages()
CLAUDE.md Removed git push/commit rules, added shared helper learnings, trimmed sections

Stats

  • 22 files changed — 7 new, 15 modified
  • ~1,558 lines added, ~326 removed
  • Net reduction in duplicate code; new lines are mostly tests

Test plan

  • make ci passes (fmt, vet, lint, unit tests, race detection, security, vuln, build)
  • All new helpers have dedicated unit tests (55+ new test cases)
  • Existing tests unbroken — verified TestFetchMessages, TestFormatState, StatusIcon_* etc.
  • nylas email list --all pagination behavior preserved (3-mode PaginationMode)
  • testutil.WriteJSONResponse encode-failure returns HTTP 500 (not original status)
  • Race detection clean on all 8 affected packages

JIRA: TW-4734

…adapters

Centralize repeated patterns into shared helpers to improve maintainability:

- Add common.StatusColor/StatusIcon/ColorSprint for status-to-color mapping,
  replacing duplicate functions in scheduler, webhook, and notetaker packages
- Add common.SetupPagination with explicit PaginationMode (SinglePage/WithCap/All)
  to eliminate duplicate limit/maxItems logic in list commands
- Add common.NormalizePageSize and FetchCursorPages to reduce pagination boilerplate
- Extract dashboard setDPoPProof() from duplicate blocks in doPostRaw/doGetRaw
- Add testutil.WriteJSONResponse() with buffer-first encoding (goroutine-safe,
  returns proper 500 on encode failure)
- Consolidate email list command into shared fetchListMessages/applyListFolderFilter

Includes unit tests for all new helpers and updated consumer tests.
…arnings

- Remove NEVER git commit/push rules (handled by global CLAUDE.md)
- Add learnings for new shared helpers: StatusColor, SetupPagination,
  NormalizePageSize, FetchCursorPages, WriteJSONResponse
- Consolidate Testing/Hooks/Context sections to reduce redundancy
@qasim-nylas qasim-nylas requested a review from AaronDDM March 28, 2026 13:52
Copy link
Copy Markdown
Collaborator

@AaronDDM AaronDDM left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@qasim-nylas qasim-nylas merged commit dcb533e into main Mar 28, 2026
6 checks passed
@qasim-nylas qasim-nylas deleted the feature/TW-4734-reduce-code-duplication branch March 28, 2026 14:29
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