Skip to content

[duplicate-code] 🔍 Duplicate Code Detected in workflow output parsers #1950

@dsyme

Description

@dsyme

🔍 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:31
    • pkg/workflow/create_pull_request.go:140
    • pkg/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:62
    • pkg/workflow/create_pull_request.go:168
    • pkg/workflow/create_discussion.go:47
    • pkg/workflow/add_comment.go:131
    • pkg/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

  1. 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.
  2. Introduce a common struct hydration routine

    • Create a helper that hydrates common fields on structs embedding BaseSafeOutputConfig to consolidate repeated code paths.
    • Estimated effort: Medium (coupled with recommendation 1).
    • Benefits: Centralizes validation and makes it easier to modify schema rules.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions