Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 26, 2025

The codebase had 15+ duplicate utility function implementations scattered across pkg/cli, creating maintenance burden and inconsistency.

Changes

Created 4 utility packages:

  • pkg/sliceutil - Contains, ContainsAny, ContainsIgnoreCase
  • pkg/stringutil - Truncate, NormalizeWhitespace, SanitizeErrorMessage
  • pkg/repoutil - SplitRepoSlug, ParseGitHubURL, SanitizeForFilename
  • pkg/mathutil - Min, Max

Migrated 17 files from local implementations to shared packages, removing ~150 lines of duplicate code.

Added comprehensive test coverage:

  • Added 70+ new test cases covering edge cases, unicode handling, and performance benchmarks
  • Achieved 100% code coverage across all 4 utility packages
  • Tests include: large data handling, boundary conditions, real-world examples, commutative/identity properties
  • Added performance benchmarks for all utility functions

Example

Before:

// pkg/cli/deps_outdated.go
func truncate(s string, maxLen int) string {
    if len(s) <= maxLen { return s }
    return s[:maxLen-3] + "..."
}

// pkg/cli/audit_report_render.go  
func truncateString(s string, maxLen int) string {
    if len(s) <= maxLen { return s }
    if maxLen <= 3 { return s[:maxLen] }
    return s[:maxLen-3] + "..."
}

After:

import "github.com/githubnext/gh-aw/pkg/stringutil"

// Both files now use:
stringutil.Truncate(s, maxLen)

All packages include comprehensive tests with 100% coverage. Backward compatibility maintained via wrapper functions where needed (e.g., pkg/cli/repo.SplitRepoSlug).

Testing

  • ✅ All utility packages: 100% code coverage
  • ✅ 70+ test cases including edge cases and benchmarks
  • make fmt - passed
  • make lint - passed (0 issues)
  • ✅ All tests passing
Original prompt

This section details on the original issue you should resolve

<issue_title>[plan] Create common utility packages to eliminate duplicate functions</issue_title>
<issue_description>## Objective

Create standardized utility packages (sliceutil, stringutil, repoutil, mathutil) and migrate 15+ duplicate function implementations from pkg/cli to these shared packages.

Context

Currently, the codebase has duplicate implementations of common utilities scattered across pkg/cli:

  • 5 different "contains" functions
  • 2 truncate functions
  • 4 sanitization functions
  • 4 repository parsing implementations
  • Multiple generic utilities (min, normalize, etc.)

This creates maintenance burden and inconsistency. Consolidating into shared packages will provide a single source of truth.

Implementation Steps

  1. Create pkg/sliceutil/sliceutil.go:

    • Contains(slice []string, item string) bool
    • ContainsAny(s string, substrings ...string) bool
    • ContainsIgnoreCase(s, substr string) bool
  2. Create pkg/stringutil/stringutil.go:

    • Truncate(s string, maxLen int) string (consolidate 2 implementations)
    • NormalizeWhitespace(content string) string (move from update_merge.go)
  3. Create pkg/stringutil/sanitize.go:

    • SanitizeForFilename(s string) string
    • SanitizeErrorMessage(message string) string
    • CleanIdentifier(s string) string
  4. Create pkg/repoutil/repoutil.go:

    • SplitRepoSlug(slug string) (owner, repo string, err error) (canonical version)
    • ParseGitHubURL(url string) (owner, repo string, err error)
    • SanitizeForFilename(slug string) string
  5. Create pkg/mathutil/mathutil.go:

    • Min(a, b int) int
    • Max(a, b int) int
  6. Update all imports in affected files across pkg/cli

  7. Remove duplicate implementations after migration

Files to Modify

Create:

  • pkg/sliceutil/sliceutil.go
  • pkg/sliceutil/sliceutil_test.go
  • pkg/stringutil/stringutil.go
  • pkg/stringutil/stringutil_test.go
  • pkg/stringutil/sanitize.go
  • pkg/stringutil/sanitize_test.go
  • pkg/repoutil/repoutil.go
  • pkg/repoutil/repoutil_test.go
  • pkg/mathutil/mathutil.go
  • pkg/mathutil/mathutil_test.go

Update imports in:

  • pkg/cli/logs_utils.go
  • pkg/cli/docker_images_test.go
  • pkg/cli/commands_utils_test.go
  • pkg/cli/mcp_schema_test.go
  • pkg/cli/mcp_registry_improvements_test.go
  • pkg/cli/deps_outdated.go
  • pkg/cli/audit_report_render.go
  • pkg/cli/trial_command.go
  • pkg/cli/compile_config.go
  • pkg/cli/mcp_registry.go
  • pkg/cli/repo.go
  • pkg/cli/tokens_bootstrap.go
  • pkg/cli/git.go
  • pkg/cli/exec.go
  • pkg/cli/update_merge.go
  • pkg/cli/spec.go
  • pkg/cli/copilot_instructions_test.go

Acceptance Criteria

  • All utility packages created with comprehensive tests
  • All duplicate implementations replaced with imports to new packages
  • All existing tests pass (run make test)
  • No new linter warnings (run make lint)
  • Code formatted correctly (run make fmt)

Testing

Run full test suite after changes:

make fmt
make lint
make test

Estimated Effort

6-8 hours
Related to #7778

AI generated by Plan Command for #7778

Comments on the Issue (you are @copilot in this section)


💡 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.

Copilot AI and others added 2 commits December 26, 2025 18:05
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Create common utility packages to eliminate duplicate functions Create common utility packages to eliminate duplicate functions Dec 26, 2025
Copilot AI requested a review from mnkiefer December 26, 2025 18:35
@pelikhan
Copy link
Contributor

@copilpt merge main and add more tests

@pelikhan
Copy link
Contributor

@copilot merge main and add more tests

Copilot AI and others added 2 commits December 26, 2025 19:08
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 26, 2025

@copilot merge main and add more tests

Completed in bd2912b:

  • Merged main branch successfully
  • Added 70+ comprehensive test cases across all utility packages
  • All packages now have 100% code coverage
  • Added edge case tests, unicode handling, performance benchmarks
  • All tests passing, code formatted and linted

Copilot AI requested a review from pelikhan December 26, 2025 19:20
@pelikhan pelikhan marked this pull request as ready for review December 26, 2025 19:25
@pelikhan pelikhan merged commit 6c48ed4 into main Dec 26, 2025
4 checks passed
@pelikhan pelikhan deleted the copilot/create-common-utility-packages branch December 26, 2025 19:26
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.

[plan] Create common utility packages to eliminate duplicate functions

3 participants