Conversation
- Created generic `aggregateSummaryItems` helper that handles common aggregation pattern - Refactored `buildMissingToolsSummary` to use generic helper - Refactored `buildMCPFailuresSummary` to use generic helper - Added comprehensive test `TestAggregateSummaryItems` for the new helper - Reduces code duplication by ~47 lines (from 74 to 27 per function) - All existing tests pass, maintaining 100% backward compatibility - Single source of truth for aggregation logic reduces maintenance burden Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR successfully refactors duplicate aggregation logic in the logs report functionality by introducing a generic helper function. The refactoring eliminates ~94 lines of duplicate code (47 lines in each of two functions) while maintaining all original behavior.
Key Changes
- Added
aggregateSummaryItemsgeneric helper function - Provides a reusable pattern for aggregating items by key, counting occurrences, deduplicating workflows, and collecting run IDs using function parameters for type-specific behavior - Refactored
buildMissingToolsSummaryandbuildMCPFailuresSummary- Both functions now use the generic helper, reducing each from ~74 lines to ~40 lines (46% reduction per function) - Added comprehensive test coverage - New
TestAggregateSummaryItemsvalidates the generic helper's aggregation, deduplication, and finalization behavior
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pkg/cli/logs_report.go | Introduced generic aggregateSummaryItems helper function and refactored two existing summary builder functions to use it, eliminating duplicate aggregation logic |
| pkg/cli/logs_report_test.go | Added TestAggregateSummaryItems to validate the new generic helper function's behavior with actual data types |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| // Use the generic aggregation helper directly | ||
| result := aggregateSummaryItems( |
There was a problem hiding this comment.
[nitpick] The test calls aggregateSummaryItems directly, but since this is a generic helper function that's not exported (starts with lowercase a), it won't be accessible outside the package. This test will only work because it's in the same package (cli).
While this works for now, consider whether aggregateSummaryItems should be:
- Exported (renamed to
AggregateSummaryItems) if it's intended to be reusable across packages - Kept private if it's only for internal use within this package (current approach)
The current approach is fine for internal testing, but the naming and visibility should be intentional.
There was a problem hiding this comment.
The function is intentionally kept private (unexported) as it's an internal helper specifically designed for use within the cli package. The current approach ensures encapsulation - the generic aggregation pattern is not exposed as a public API since it's implementation-specific to the logs report functionality. Exporting it would create an unnecessary public interface that we'd need to maintain for backward compatibility.
|
@copilot merge main |
…ions Resolved conflicts in pkg/cli/logs_report_test.go by keeping both test suites: - TestAggregateSummaryItems (from this branch) - TestAggregateDomainStats, TestConvertDomainsToSortedSlices, and related tests (from main) Both helper functions (aggregateSummaryItems and aggregateDomainStats) are now present and serve complementary purposes.
buildMissingToolsSummaryandbuildMCPFailuresSummaryinpkg/cli/logs_report.gocontained ~47 lines of identical aggregation logic per function, differing only in struct types and field names.Changes
Added
aggregateSummaryItems[TItem, TSummary]generic helper (38 lines)Refactored both functions to use generic helper
FirstReasonfield for missing toolsAdded
TestAggregateSummaryItemsMerged latest changes from main
aggregateDomainStatshelper from main now coexists for domain aggregationExample
Before:
After:
Impact: 56 lines eliminated (38% reduction). Future summary types can reuse the pattern. Successfully integrated with complementary refactoring from main branch.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.