Skip to content

Refactor duplicate update scripts to use shared helpers#5331

Merged
pelikhan merged 2 commits intomainfrom
copilot/refactor-duplicate-update-scripts
Dec 2, 2025
Merged

Refactor duplicate update scripts to use shared helpers#5331
pelikhan merged 2 commits intomainfrom
copilot/refactor-duplicate-update-scripts

Conversation

Copy link
Contributor

Copilot AI commented Dec 2, 2025

update_issue.cjs and update_pull_request.cjs duplicate ~60 lines of nearly identical staged preview rendering and summary line generation code, differing only in entity labels and whether to show the operation field.

Changes

  • update_runner.cjs: Add factory functions for shared logic

    • createRenderStagedItem(config) - staged preview renderer with configurable entity name, number field, and operation display
    • createGetSummaryLine(config) - summary line generator with configurable entity prefix
  • update_issue.cjs / update_pull_request.cjs: Replace inline functions with factory calls

  • update_runner.test.cjs: Add tests for new factory functions

Example

// Before: 25+ lines duplicated in each file
function renderStagedItem(item, index) {
  let content = `### Issue Update ${index + 1}\n`;
  if (item.issue_number) {
    content += `**Target Issue:** #${item.issue_number}\n\n`;
  } else {
    content += `**Target:** Current issue\n\n`;
  }
  // ... more duplication
}

// After: single config object
const renderStagedItem = createRenderStagedItem({
  entityName: "Issue",
  numberField: "issue_number",
  targetLabel: "Target Issue:",
  currentTargetText: "Current issue",
  includeOperation: false,
});

Fixes #1196

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/user
    • Triggering command: /usr/bin/gh gh api user --jq .login (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] Duplicate update scripts for issues and pull requests</issue_title>
<issue_description># 🔍 Duplicate Code Detected: Issue vs PR Update Scripts

Analysis of commit fe3a592

Assignee: @copilot

Summary

pkg/workflow/js/update_issue.cjs and pkg/workflow/js/update_pull_request.cjs both implement the same staged preview + update plumbing for agentic "update" actions. Each file redefines the same helper functions (context validator, number extractor, staged preview renderer, summary builder, and the runUpdateWorkflow invocation) even though the logic only differs in a few literals and the body operation handling. The shared structure exceeds 60 lines of nearly identical code per file, creating an avoidable maintenance hotspot.

Duplication Details

Pattern: Repeated staged-update scaffolding

  • Severity: Medium
  • Occurrences: 2
  • Locations:
    • pkg/workflow/js/update_issue.cjs:25-100
    • pkg/workflow/js/update_pull_request.cjs:41-153
  • Code Sample:
    // update_issue.cjs
    async function main() {
      return await runUpdateWorkflow({
        itemType: "update_issue",
        displayName: "issue",
        displayNamePlural: "issues",
        numberField: "issue_number",
        outputNumberKey: "issue_number",
        outputUrlKey: "issue_url",
        isValidContext: isIssueContext,
        getContextNumber: getIssueNumber,
        supportsStatus: true,
        supportsOperation: false,
        renderStagedItem,
        executeUpdate: executeIssueUpdate,
        getSummaryLine,
      });
    }
    
    // update_pull_request.cjs
    async function main() {
      return await runUpdateWorkflow({
        itemType: "update_pull_request",
        displayName: "pull request",
        displayNamePlural: "pull requests",
        numberField: "pull_request_number",
        outputNumberKey: "pull_request_number",
        outputUrlKey: "pull_request_url",
        isValidContext: isPRContext,
        getContextNumber: getPRNumber,
        supportsStatus: false,
        supportsOperation: true,
        renderStagedItem,
        executeUpdate: executePRUpdate,
        getSummaryLine,
      });
    }
    The same duplication repeats for renderStagedItem (lines 25-49 vs. 41-64) and getSummaryLine (73-80 vs. 126-133), differing only in entity labels.

Impact Analysis

  • Maintainability: Any change to the staged update flow or logging must be manually mirrored in both files, increasing the chance of drift (e.g., issues support status updates but PRs do not, so future features will be harder to roll out consistently).
  • Bug Risk: Fixes to context detection or summary formatting could land in one script but not the other, leading to inconsistent behavior for workflows that expect parity between issues and PRs.
  • Code Bloat: Each script reimplements ~60 lines of boilerplate even though runUpdateWorkflow already abstracts most of the behavior; the duplicated helpers add noise to the JS directory.

Refactoring Recommendations

  1. Extract shared config for update scripts

    • Create a single factory (e.g., createUpdateScript(config)) that accepts entity-specific settings (itemType, context predicates, staging renderer overrides) and returns the module entrypoint. Both update scripts can then be thin wrappers supplying their config objects.
    • Estimated effort: 4-6 hours.
    • Benefits: One place to change preview rendering, summary formatting, and context validation.
  2. Move shared render/summary helpers into update_runner.cjs or a new utility

    • Provide default implementations for renderStagedItem/getSummaryLine that accept the entity label and field keys, so only PR-specific body handling needs a custom override.
    • Estimated effort: 2-3 hours.
    • Benefits: Reduces drift risk and keeps future update targets (e.g., discussions) trivial to add.

Implementation Checklist

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

Analysis Metadata

  • Analyzed Files: 2
  • Detection Method: Serena semantic code analysis
  • Commit: fe3a592
  • Analysis Date: 2024-??-??

AI generated by Duplicate Code Detector

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


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Add createRenderStagedItem and createGetSummaryLine factory functions
to update_runner.cjs and update both update_issue.cjs and
update_pull_request.cjs to use them.

Fixes #1196

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix duplicate code in issue and pull request update scripts Refactor duplicate update scripts to use shared helpers Dec 2, 2025
Copilot AI requested a review from mnkiefer December 2, 2025 21:36
@pelikhan pelikhan marked this pull request as ready for review December 2, 2025 23:38
@pelikhan pelikhan merged commit 0ae2320 into main Dec 2, 2025
73 checks passed
@pelikhan pelikhan deleted the copilot/refactor-duplicate-update-scripts branch December 2, 2025 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate update scripts for issues and pull requests

3 participants