Skip to content

Duplicate Code: close_issue and close_pull_request handlers reimplement the same close-flow logic #25627

@github-actions

Description

@github-actions

Overview

A significant near-duplicate implementation exists between the safe-output handlers for closing issues and closing pull requests.

The duplicated flow includes configuration parsing, max-count gating, comment-body resolution, staged-mode preview handling, label/title filtering, close execution, and verbose error/log handling.

Critical Findings

  • Pattern: Near-identical close-flow pipeline implemented in separate modules
  • Severity: Medium
  • Occurrences: 2 primary handlers, plus overlapping utility logic duplicated against an existing helper module
  • Threshold Check: Exceeds >10 duplicated lines by a wide margin (roughly 100+ structurally similar lines)

Duplication Details

The two handlers implement highly similar end-to-end logic in their main() returned handler functions:

  • Parse/validate target number
  • Resolve comment text fallback and sanitize
  • Apply required labels/title-prefix filters
  • Handle staged mode preview
  • Post comment
  • Close target entity
  • Return structured success/failure payload

Additionally, helper logic like buildCommentBody, label filtering, and title-prefix filtering is duplicated in close_pull_request.cjs:318 and already exists in close_entity_helpers.cjs:47.

Code Evidence (high-level)
  • close_issue.cjs main processing pipeline: lines 84-301
  • close_pull_request.cjs main processing pipeline: lines 85-310
  • Existing generalized helper pipeline (processCloseEntityItems): lines 214-383 in close_entity_helpers.cjs

Impact Analysis

  • Maintainability: Bug fixes and behavior changes must be repeated across handlers, increasing drift risk.
  • Bug Risk: Inconsistent behavior can emerge over time (e.g., error handling and comment/footer behavior).
  • Code Bloat: Existing abstraction (close_entity_helpers.cjs) is underused while duplicate handler logic continues to grow.

Recommendations

  1. Migrate close_issue.cjs and close_pull_request.cjs to a shared entity-close runner in close_entity_helpers.cjs.
  2. Keep only entity-specific callbacks in each handler (getDetails, addComment, closeEntity) and centralize common flow.
  3. Reuse shared helper functions for comment-body construction and filters; remove duplicated local implementations.

Implementation Checklist

  • Define a unified close-flow contract in close_entity_helpers.cjs
  • Refactor close_pull_request.cjs to consume shared flow
  • Refactor close_issue.cjs to consume shared flow (including cross-repo handling where needed)
  • Align staged preview behavior and result payload format
  • Update/add handler tests for parity and regression protection

Analysis Metadata

  • Commit: 68515c18ed1602fb1eb44e24e35718e93c6c8eb8
  • Detection Method: Serena symbol analysis + structural diff inspection
  • Eligible changed files: 931 (.go/.cjs, excluding test/workflow patterns)
  • Workflow Run: §24241739247

Generated by Duplicate Code Detector ·

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions