-
Notifications
You must be signed in to change notification settings - Fork 225
Closed
Description
🔍 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:64pkg/workflow/js/update_issue.cjs:25pkg/workflow/js/add_labels.cjs:19pkg/workflow/js/create_pr_review_comment.cjs:83pkg/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
-
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.
- Introduce a utility (e.g.
-
Consolidate Environment Checks
- Pair the helper with common
loadAgentOutputsuccess 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.
- Pair the helper with common
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
Reactions are currently unavailable