Skip to content

[duplicate-code] Duplicate Code: nested tools.* frontmatter map navigation guards in codemods #69117

Open

Description

@github-actions

🔍 Duplicate Code Detected: Nested Frontmatter Map Navigation Guards

Analysis of commit 5bf0b44 (github/gh-aw)

Assignee: @copilot

Summary

Multiple codemod detector functions duplicate the same defensive cascade to navigate nested frontmatter maps (frontmatter["tools"] -> map[string]any -> ["github"] -> map[string]any -> field). Each repeats the value, ok := x.(map[string]any); if !ok { return false } pattern 2-3 levels deep. No shared accessor exists in pkg/cli/yaml_frontmatter_utils.go for this.

Duplication Details

Pattern: multi-level map type-assertion guard

  • Severity: Medium
  • Occurrences: 8 files share toolsAny, hasTools := frontmatter["tools"]
  • Locations:
    • pkg/cli/codemod_github_repos.go (lines 39-62, hasDeprecatedGitHubReposField)
    • pkg/cli/codemod_github_app.go
    • pkg/cli/codemod_allowed_repos_current.go
    • pkg/cli/codemod_toolset_singular.go
    • pkg/cli/codemod_difc_proxy.go (lines 92-106)
    • pkg/cli/codemod_dependabot_permissions.go
    • pkg/cli/codemod_mount_as_clis.go
    • pkg/cli/codemod_serena_import.go
  • Code Sample:
    toolsAny, hasTools := frontmatter["tools"]
    if !hasTools { return false }
    toolsMap, ok := toolsAny.(map[string]any)
    if !ok { return false }
    githubAny, hasGitHub := toolsMap["github"]
    if !hasGitHub { return false }
    githubMap, ok := githubAny.(map[string]any)
    if !ok { return false }

Impact Analysis

  • Maintainability: Any change to how nested tool config is accessed (e.g. supporting a string shorthand) must touch 8 sites.
  • Bug Risk: Subtle divergences already exist (some return the map, some only check existence).
  • Code Bloat: ~10 lines of boilerplate per detector.

Refactoring Recommendations

  1. Add a generic nested-map accessor to pkg/cli/yaml_frontmatter_utils.go, e.g. nestedMap(frontmatter map[string]any, keys ...string) (map[string]any, bool) returning the leaf map, so each detector becomes m, ok := nestedMap(fm, "tools", "github"); if !ok { return false }.
    • Estimated effort: low (2-3h)
    • Benefits: eliminates repeated type-assertion ladders; consistent nil/absent handling.
  2. Optionally add nestedValue for leaf scalar lookups used by the same detectors.

Implementation Checklist

  • Review duplication findings
  • Add nestedMap helper + tests
  • Migrate the 8 detectors
  • Verify unit tests pass
Analysis Metadata
  • Analyzed Files: pkg/cli/codemod_*.go
  • Detection Method: Serena activation + grep/semantic cross-reference
  • Commit: 5bf0b44

Generated by 🔍 Duplicate Code Detector · pi · opus48 · 139.4 AIC · ⌖ 42.8 AIC · ⊞ 1.6K · ◷

  • expires on Oct 17, 2026, 1:56 PM UTC-08:00

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions