-
Notifications
You must be signed in to change notification settings - Fork 34
Refactor: Eliminate duplicate code and organize validation into npm.go and pip.go #1857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Created generic collectPackagesFromWorkflow helper to consolidate package collection logic - Created validatePythonPackagesWithPip helper to unify pip validation loops - Reduced extractNpxPackages, extractPipPackages, extractUvPackages to single-line wrappers - Reduced validatePipPackages and validateUvPackagesWithPip duplication - Added comprehensive tests for new helper function - Net reduction: 68 lines (-118 lines, +50 lines) - All existing tests pass Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
- Add pipCmd parameter to validatePythonPackagesWithPip - Restore pip/pip3 fallback logic in validatePipPackages - Ensures validation works on systems with only pip3 installed - All tests pass Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
pkg/workflow/validation.go
Outdated
|
|
||
| // validatePythonPackagesWithPip is a generic helper that validates Python packages using pip index. | ||
| // It accepts a package list and a package type name for consistent error messaging. | ||
| func (c *Compiler) validatePythonPackagesWithPip(packages []string, packageType string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot move to pip.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to pip.go in commit 7b8bbf7
pkg/workflow/validation.go
Outdated
| } | ||
|
|
||
| // validatePipPackages validates that pip packages are available on PyPI | ||
| func (c *Compiler) validatePipPackages(workflowData *WorkflowData) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot move to pip.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to pip.go in commit 7b8bbf7
pkg/workflow/validation.go
Outdated
| } | ||
|
|
||
| // validateUvPackagesWithPip validates uv packages using pip index | ||
| func (c *Compiler) validateUvPackagesWithPip(packages []string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot move to pip.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to pip.go in commit 7b8bbf7
pkg/workflow/validation.go
Outdated
| } | ||
|
|
||
| // extractNpxPackages extracts npx package names from workflow data | ||
| func extractNpxPackages(workflowData *WorkflowData) []string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot move to npm.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to npm.go in commit 7b8bbf7
pkg/workflow/validation.go
Outdated
| } | ||
|
|
||
| // extractNpxFromCommands extracts npx package names from command strings | ||
| func extractNpxFromCommands(commands string) []string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot move to npm.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to npm.go in commit 7b8bbf7
pkg/workflow/validation.go
Outdated
|
|
||
| return packages | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot move to pip.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to pip.go in commit 7b8bbf7
- Pass pipCmd parameter to validateUvPackagesWithPip - Check for pip3 when pip is not available in validateUvPackages - Ensures uv package validation works on systems with only pip3 - All tests pass Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
- Move npm/npx-related functions to npm.go - Move pip/uv-related functions to pip.go - Keep shared helper collectPackagesFromWorkflow in validation.go - Remove unused os/exec import from validation.go - All tests pass Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
|
Agentic Changeset Generator triggered by this pull request. |
Summary
This PR refactors
pkg/workflow/validation.goto eliminate ~94 lines of duplicate code across package collection and validation logic, and organizes the code into dedicated files by package manager type.Problem
Two significant duplication patterns existed:
1. Package Collection Scaffolding (3 occurrences)
extractNpxPackages,extractPipPackages, andextractUvPackagesshared identical collection logic with ~54 lines of duplicated code per function:The only difference between these functions was the command parser function they called (
extractNpxFromCommands,extractPipFromCommands,extractUvFromCommands).2. Pip Validation Loop (2 occurrences)
validatePipPackagesandvalidateUvPackagesWithPipshared ~40 lines of identical validation logic:Solution
Created Generic Helper Functions
collectPackagesFromWorkflow- Generic package collection with deduplication:seenmapvalidatePythonPackagesWithPip- Unified validation logic:pipandpip3commandsRefactored Functions
All extraction functions became single-line wrappers:
Validation functions simplified while preserving pip/pip3 fallback logic:
Code Organization
New Files Created:
npm.go(69 lines) - Contains all npm/npx-related validation functions:validateNpxPackagesextractNpxPackagesextractNpxFromCommandspip.go(201 lines) - Contains all pip/uv-related validation functions:validatePythonPackagesWithPipvalidatePipPackagesvalidateUvPackagesvalidateUvPackagesWithPipextractPipPackagesextractPipFromCommandsextractUvPackagesextractUvFromCommandsvalidation.go- Reduced from 460 to 214 lines:collectPackagesFromWorkflowhelperos/execimportImpact
Code Metrics:
Benefits:
Testing:
collectPackagesFromWorkflowFuture Enhancements
The generic
collectPackagesFromWorkflowhelper and modular file organization make it easy to add new package managers (e.g.,gem,cargo,composer) without duplicating collection logic.Fixes #1856
Original prompt
This section details on the original issue you should resolve
<issue_title>[duplicate-code] 🔍 Duplicate Code Detected</issue_title>
<issue_description># 🔍 Duplicate Code Detected
Analysis of commit 2d83ebf
Assignee:
@copilotSummary
Duplicate package collection and validation logic landed in
pkg/workflow/validation.go. Three helpers repeat the same loops to gather package names, and two validation helpers duplicate the same pip-check workflow. The drift risk is high because these blocks must stay in sync when the workflow schema evolves.Duplication Details
Pattern 1: Repeated package collection scaffolding
pkg/workflow/validation.go(lines 303-356)pkg/workflow/validation.go(lines 381-414)pkg/workflow/validation.go(lines 449-482)extractPipPackagesandextractUvPackages, differing only by the command parsing helper. Any change to the workflow structure has to be replicated in all three blocks.Pattern 2: Pip-backed package validation flow duplicated
pkg/workflow/validation.go(lines 182-223)pkg/workflow/validation.go(lines 273-299)validatePipPackagesandvalidateUvPackagesWithPiploops share the same structure, logging, and warning messaging with only naming differences.Impact Analysis
Refactoring Recommendations
Factor shared collectors
collectPackages(workflowData, extractor, includeTools bool)and pass in the command parser (e.g.,extractNpxFromCommands).Unify pip validation loop
validatePythonPackages(packages []string, cmdName string)that handles the common loop and logging, with callers supplying the command label.Implementation Checklist
Analysis Metadata
Comments on the Issue (you are @copilot in this section)
Fixes #1856
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.