Skip to content

[duplicate-code] 🔍 Duplicate Code Detected: Staged Mode Preview Rendering #3274

@github-actions

Description

@github-actions

🔍 Duplicate Code Detected: Staged Mode Preview Rendering

Analysis of commit da2590c

Assignee: @copilot

Summary

Multiple agent workflow handlers duplicate the same "staged mode" preview logic: they filter staged items, build a Markdown summary with identical control flow, and bail out early. The structure is nearly identical across issue creation, updates, label application, PR review commenting, and push-to-branch workflows. Any behavioural change (new fields, formatting fixes, bug fixes) must be applied in five places today.

Duplication Details

Pattern: Staged Mode Preview Blocks in JS Handlers

  • Severity: Medium
  • Occurrences: 5
  • Locations:
    • pkg/workflow/js/create_issue.cjs:64
    • pkg/workflow/js/update_issue.cjs:25
    • pkg/workflow/js/add_labels.cjs:19
    • pkg/workflow/js/create_pr_review_comment.cjs:83
    • pkg/workflow/js/push_to_pull_request_branch.cjs:140
  • Code Sample:
if (isStaged) {
  let summaryContent = "## 🎭 Staged Mode: Create Issues Preview\n\n";
  summaryContent += "The following issues would be created if staged mode was disabled:\n\n";
  for (let i = 0; i < createIssueItems.length; i++) {
    const item = createIssueItems[i];
    summaryContent += `### Issue ${i + 1}\n`;
    summaryContent += `**Title:** ${item.title || "No title provided"}\n\n`;
    if (item.body) {
      summaryContent += `**Body:**\n${item.body}\n\n`;
    }
    if (item.labels && item.labels.length > 0) {
      summaryContent += `**Labels:** ${item.labels.join(", ")}\n\n`;
    }
    summaryContent += "---\n\n";
  }
  await core.summary.addRaw(summaryContent).write();
  core.info("📝 Issue creation preview written to step summary");
  return;
}

Impact Analysis

  • Maintainability: Any tweak to staged-mode behaviour (formatting, localisation, accessibility) has to be replicated manually across five files, increasing the maintenance burden.
  • Bug Risk: Divergent fixes are likely—several handlers already drift in messaging, which hints at future inconsistencies if one path is updated without the rest.
  • Code Bloat: Each module carries ~15–20 lines of boilerplate that could be centralised, making these files longer and harder to scan.

Refactoring Recommendations

  1. Extract Shared Preview Helper

    • Introduce a utility (e.g. pkg/workflow/js/staged_preview.cjs) that takes the staged items and a renderer callback to generate Markdown.
    • Estimated effort: 0.5–1 day, mainly for designing a flexible interface and updating call sites.
    • Benefits: Single source of truth for staged summaries, easier formatting changes, reduced code size.
  2. Consolidate Environment Checks

    • Pair the helper with common loadAgentOutput success handling to remove redundant checks.
    • Estimated effort: additional 0.5 day to refactor callers safely.
    • Benefits: Prevents future copy/paste logic for staged-mode detection and reduces chances of divergent behaviour.

Implementation Checklist

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

Analysis Metadata

  • Analyzed Files: 5
  • Detection Method: Serena semantic code analysis
  • Commit: da2590c
  • Analysis Date: 2025-11-05

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