-
Notifications
You must be signed in to change notification settings - Fork 264
Closed
Description
🔍 Duplicate Code Detected
Analysis of commit 2554f15
Assignee: @copilot
Summary
Repeated configuration parsing logic was introduced across multiple workflow output compilers. The same label parsing and target repository validation blocks appear in several files, which increases cognitive load and makes it easy to miss updates when the schema evolves.
Duplication Details
Pattern 1: Duplicate label parsing blocks in output compilers
- Severity: Medium
- Occurrences: 3
- Locations:
pkg/workflow/create_issue.go:31pkg/workflow/create_pull_request.go:140pkg/workflow/push_to_pull_request_branch.go:180
- Code Sample:
// Parse labels if labels, exists := configMap["labels"]; exists { if labelsArray, ok := labels.([]any); ok { var labelStrings []string for _, label := range labelsArray { if labelStr, ok := label.(string); ok { labelStrings = append(labelStrings, labelStr) } } config.Labels = labelStrings } }
Pattern 2: Duplicate target repository validation
- Severity: Medium
- Occurrences: 5
- Locations:
pkg/workflow/create_issue.go:62pkg/workflow/create_pull_request.go:168pkg/workflow/create_discussion.go:47pkg/workflow/add_comment.go:131pkg/workflow/create_pr_review_comment.go:130
- Code Sample:
if targetRepoSlug, exists := configMap["target-repo"]; exists { if targetRepoStr, ok := targetRepoSlug.(string); ok { if targetRepoStr == "*" { return nil // Invalid configuration } config.TargetRepoSlug = targetRepoStr } }
Impact Analysis
- Maintainability: Updating validation or schema requirements requires touching many files, increasing the chance of inconsistencies.
- Bug Risk: Fixes applied to one output type may not propagate to others, leading to mismatched behavior across generated workflows.
- Code Bloat: The duplicated blocks add nearly identical logic across five files, making the compiler harder to reason about.
Refactoring Recommendations
-
Extract shared parsing helpers
- Move label parsing, title-prefix handling, and target repo validation into shared helper functions (e.g.,
parseLabels(configMap map[string]any) []string). - Estimated effort: Medium (2-3 hours to refactor and update call sites).
- Benefits: Ensures consistency and reduces future maintenance cost.
- Move label parsing, title-prefix handling, and target repo validation into shared helper functions (e.g.,
-
Introduce a common struct hydration routine
- Create a helper that hydrates common fields on structs embedding
BaseSafeOutputConfigto consolidate repeated code paths. - Estimated effort: Medium (coupled with recommendation 1).
- Benefits: Centralizes validation and makes it easier to modify schema rules.
- Create a helper that hydrates common fields on structs embedding
Implementation Checklist
- Review duplication findings
- Prioritize refactoring tasks
- Create refactoring plan
- Implement changes
- Update tests
- Verify no functionality broken
Analysis Metadata
- Analyzed Files: 6
- Detection Method: Serena semantic code analysis
- Commit: 2554f15
- Analysis Date: 2024-04-14
AI generated by Duplicate Code Detector
Reactions are currently unavailable