Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 5, 2025

Summary

Successfully refactored duplicate staged mode preview rendering code across 5 JavaScript handlers into a shared utility module.

Changes Made

New Files

  • pkg/workflow/js/staged_preview.cjs - Shared utility module (31 lines)
  • pkg/workflow/js/staged_preview.test.cjs - Test suite (9 comprehensive tests)

Updated Files

  • pkg/workflow/js/create_issue.cjs - Now uses shared utility (-10 lines)
  • pkg/workflow/js/update_issue.cjs - Now uses shared utility (-4 lines)
  • pkg/workflow/js/add_labels.cjs - Now uses shared utility (+1 line but cleaner)
  • pkg/workflow/js/create_pr_review_comment.cjs - Now uses shared utility (-1 line)
  • pkg/workflow/js/push_to_pull_request_branch.cjs - Now uses shared utility (-1 line)
  • pkg/workflow/js/create_issue.test.cjs - Updated test expectations
  • pkg/workflow/js.go - Added staged_preview.cjs to embedded sources

Latest Updates

  • Added core.info logging for full summary content (addresses @pelikhan feedback)
  • Added staged_preview.cjs to compiler bundler sources (addresses @pelikhan feedback)
  • Merged main branch, formatted code, and recompiled workflows (addresses @pelikhan feedback)

Verification

  • All 710 JavaScript tests passing (2 new tests from main branch)
  • All Go unit tests passing
  • Build successful
  • JavaScript formatting verified with prettier
  • JavaScript linting passed
  • Bundler correctly includes staged_preview.cjs
  • All 68 workflows successfully recompiled

Benefits

  • Maintainability: Single source of truth for staged mode previews
  • Consistency: All handlers use the same preview structure
  • Testability: Dedicated test suite with 9 test cases
  • Flexibility: Easy to extend with new preview types
  • Code Quality: Cleaner, more maintainable codebase
  • Visibility: Full preview content now logged for better debugging
  • Up-to-date: Merged with latest main branch changes

Statistics

  • Net code change: +131 insertions, -116 deletions
  • Production code: +21 lines (handlers simplified, new utility added, bundler updated)
  • Test code: +349 lines (comprehensive test coverage)
  • Total tests: 710 passing (including 9 new utility tests + 2 from main)
Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] 🔍 Duplicate Code Detected: Staged Mode Preview Rendering</issue_title>
<issue_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: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</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 5, 2025 21:32
- Created shared staged_preview.cjs utility module with generateStagedPreview function
- Updated 5 handler files to use shared utility instead of duplicate code
- Reduced code by ~4 lines net (110 insertions, 114 deletions)
- Added comprehensive tests for shared utility (9 test cases)
- Fixed create_issue.test.cjs to match new behavior
- All 708 tests passing
- Formatting and linting verified

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
All tests passing (708/708)
- JavaScript tests: 38 files, 708 tests
- Go unit tests: All passing
- Build: Successful
- Formatting: Verified
- Linting: Passed

Benefits achieved:
- Eliminated duplicate code in 5 handlers
- Created reusable utility with 9 test cases
- Improved maintainability and consistency
- Foundation for future enhancements

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicate staged mode preview rendering code Refactor: Extract duplicate staged mode preview rendering into shared utility Nov 5, 2025
Copilot AI requested a review from pelikhan November 5, 2025 21:37
@pelikhan
Copy link
Contributor

pelikhan commented Nov 5, 2025

@copilot render summary to core.info as well

- Log full summary content to core.info for better visibility
- Update tests to verify summary content logging
- Addresses feedback from @pelikhan

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 5, 2025

@copilot render summary to core.info as well

Done in 0c1f83a. The staged preview summary content is now logged to core.info in addition to being written to the step summary.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2025

Agentic Changeset Generator triggered by this pull request.

@pelikhan
Copy link
Contributor

pelikhan commented Nov 5, 2025

...

@pelikhan
Copy link
Contributor

pelikhan commented Nov 5, 2025

@copilot You must update the compiler and bundled JavaScript files to inline the new file.

See error: https://github.com/githubnext/gh-aw/actions/runs/19117338988/job/54629867586#step:4:1

- Embed staged_preview.cjs file in js.go
- Add to GetJavaScriptSources() map for bundling
- Fixes bundling error for scripts that require staged_preview

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 5, 2025

@copilot You must update the compiler and bundled JavaScript files to inline the new file.

See error: https://github.com/githubnext/gh-aw/actions/runs/19117338988/job/54629867586#step:4:1

Fixed in f02cbbe. Added staged_preview.cjs to the embedded JavaScript sources in pkg/workflow/js.go and included it in the GetJavaScriptSources() map so it's available during bundling.

@pelikhan pelikhan added smoke and removed smoke labels Nov 5, 2025
@pelikhan pelikhan marked this pull request as ready for review November 5, 2025 22:13
Copilot AI review requested due to automatic review settings November 5, 2025 22:13
@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2025

Agentic Changeset Generator triggered by this pull request.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors duplicate staged mode preview generation logic across multiple workflow scripts by introducing a reusable generateStagedPreview utility function. The refactoring reduces code duplication and standardizes how staged mode previews are generated and displayed.

Key changes:

  • Introduces a new staged_preview.cjs module with a generateStagedPreview function
  • Refactors five workflow scripts to use the new utility instead of inline logic
  • Updates test expectations to match the new standardized preview messages

Reviewed Changes

Copilot reviewed 28 out of 28 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/workflow/js/staged_preview.cjs New utility module providing reusable generateStagedPreview function
pkg/workflow/js/staged_preview.test.cjs Comprehensive test suite for the new utility
pkg/workflow/js/update_issue.cjs Refactored to use generateStagedPreview
pkg/workflow/js/push_to_pull_request_branch.cjs Refactored to use generateStagedPreview
pkg/workflow/js/create_pr_review_comment.cjs Refactored to use generateStagedPreview
pkg/workflow/js/create_issue.cjs Refactored to use generateStagedPreview
pkg/workflow/js/add_labels.cjs Refactored to use generateStagedPreview
pkg/workflow/js/create_issue.test.cjs Updated test assertion for new message format
pkg/workflow/js.go Registered new script in Go embed mappings
.github/workflows/*.lock.yml Auto-generated workflow files updated with new function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@pelikhan
Copy link
Contributor

pelikhan commented Nov 5, 2025

@copilot merge main, format and recompile, fix tests

Copilot AI and others added 2 commits November 5, 2025 22:37
- Merged latest changes from main branch
- Ran code formatting (make fmt)
- Recompiled all workflow lock files (make recompile)
- All 710 JavaScript tests passing
- All Go unit tests passing
- Build successful

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 5, 2025

@copilot merge main, format and recompile, fix tests

Done in 5e8eff2. Merged main branch, formatted code with make fmt, recompiled all workflows with make recompile, and verified all tests pass (710 JavaScript tests, all Go unit tests).

@pelikhan pelikhan merged commit 8342771 into main Nov 6, 2025
4 checks passed
@pelikhan pelikhan deleted the copilot/remove-duplicate-staged-mode-code branch November 6, 2025 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants