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>
pelikhan
reviewed
Oct 17, 2025
|
|
||
| // 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) { |
Contributor
Author
There was a problem hiding this comment.
Moved to pip.go in commit 7b8bbf7
| } | ||
|
|
||
| // validatePipPackages validates that pip packages are available on PyPI | ||
| func (c *Compiler) validatePipPackages(workflowData *WorkflowData) error { |
Contributor
Author
There was a problem hiding this comment.
Moved to pip.go in commit 7b8bbf7
| @@ -272,41 +278,24 @@ func (c *Compiler) validateUvPackages(workflowData *WorkflowData) error { | |||
|
|
|||
| // validateUvPackagesWithPip validates uv packages using pip index | |||
| func (c *Compiler) validateUvPackagesWithPip(packages []string) error { | |||
Contributor
Author
There was a problem hiding this comment.
Moved to pip.go in commit 7b8bbf7
| } | ||
|
|
||
| // extractNpxPackages extracts npx package names from workflow data | ||
| func extractNpxPackages(workflowData *WorkflowData) []string { |
Contributor
Author
There was a problem hiding this comment.
Moved to npm.go in commit 7b8bbf7
| } | ||
|
|
||
| // extractNpxFromCommands extracts npx package names from command strings | ||
| func extractNpxFromCommands(commands string) []string { |
Contributor
Author
There was a problem hiding this comment.
Moved to npm.go in commit 7b8bbf7
| @@ -379,38 +375,7 @@ func extractNpxFromCommands(commands string) []string { | |||
|
|
|||
Contributor
Author
There was a problem hiding this comment.
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>
Copilot
AI
changed the title
[WIP] Refactor duplicate package collection and validation logic
Refactor: Eliminate duplicate code in validation.go
Oct 17, 2025
- 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>
Copilot
AI
changed the title
Refactor: Eliminate duplicate code in validation.go
Refactor: Eliminate duplicate code and organize validation into npm.go and pip.go
Oct 17, 2025
pelikhan
approved these changes
Oct 17, 2025
Contributor
|
Agentic Changeset Generator triggered by this pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.