Skip to content

Duplicate Code: Codemod frontmatter line-walk/replace pattern #18049

@github-actions

Description

@github-actions

🔍 Duplicate Code Detected: Codemod frontmatter line-walk/replace pattern

Analysis of commit e750213

Assignee: @copilot

Summary

Multiple codemod implementations repeat the same frontmatter line-walk, block-tracking, replace, and reconstruction pattern. This is duplicated across many pkg/cli/codemod_*.go files and exceeds the duplication threshold.

Duplication Details

Pattern: Frontmatter line walk with block tracking and replacement

  • Severity: Medium
  • Occurrences: 3+ (seen across many codemods)
  • Locations:
    • pkg/cli/codemod_slash_command.go (lines 9-89)
    • pkg/cli/codemod_assign_to_agent.go (lines 12-125)
    • pkg/cli/codemod_permissions.go (lines 12-85)
  • Code Sample:
frontmatterLines, markdown, err := parseFrontmatterLines(content)
if err != nil {
	return content, false, err
}

var modified bool
result := make([]string, len(frontmatterLines))

for i, line := range frontmatterLines {
	trimmedLine := strings.TrimSpace(line)
	// block tracking and replacement logic...
	result[i] = line
}

if !modified {
	return content, false, nil
}

newContent := reconstructContent(result, markdown)
return newContent, true, nil

Impact Analysis

  • Maintainability: Changes to block-walk behavior (indent handling, comment skipping, etc.) must be made in many files.
  • Bug Risk: Inconsistent fixes across codemods can create subtle YAML parsing differences.
  • Code Bloat: Repeated 50–100 line loops across codemods increases file size and review overhead.

Refactoring Recommendations

  1. Extract a shared helper in pkg/cli/codemod_yaml_utils.go

    • Provide a utility that takes frontmatter lines, a block key, and a line transform callback.
    • Estimated effort: medium (2–4 hours)
    • Benefits: single source of truth for block-walk behavior and indentation handling.
  2. Standardize codemod apply flow

    • Build a small scaffold helper like applyFrontmatterLineTransform(content, fn) to handle parse/reconstruct boilerplate.
    • Estimated effort: low to medium (1–2 hours)
    • Benefits: removes repeated parse/reconstruct code and ensures consistent error handling.

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 3 core codemod files (pattern appears in many more)
  • Detection Method: Serena semantic code analysis
  • Commit: e750213
  • Analysis Date: February 24, 2026

Generated by Duplicate Code Detector

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions