Repository navigation
[duplicate-code] 馃攳 Duplicate Code Detected: Issue/PR Update Scripts Share Same Control Flow聽#4953
Copy link
Copy link
Closed
Description
Analysis of commit 0c768b3
Assignee: @copilot
Summary
pkg/workflow/js/update_issue.cjs and pkg/workflow/js/update_pull_request.cjs reimplement the same staged-preview scaffolding, environment-driven targeting, input validation, and update execution loops with only resource-specific naming differences. Each file duplicates ~150 lines of logic for loading agent output, filtering update items, resolving GH_AW_UPDATE_TARGET, and applying updates, which makes it easy for the scripts to drift apart.
Duplication Details
Pattern: Safe-output update runners
- Severity: Medium
- Occurrences: 2 (issues vs pull requests)
- Locations:
pkg/workflow/js/update_issue.cjs(lines 7-198)pkg/workflow/js/update_pull_request.cjs(lines 7-234)
- Code Sample:
// Shared control flow in both files
const updateTarget = process.env.GH_AW_UPDATE_TARGET || "triggering";
const canUpdateTitle = process.env.GH_AW_UPDATE_TITLE === "true";
const canUpdateBody = process.env.GH_AW_UPDATE_BODY === "true";
...
if (updateTarget === "*") {
if (!updateItem.issue_number && !updateItem.pull_request_number) {
core.info('Target is "*" but no explicit number provided');
continue;
}
targetNumber = parseInt(explicitNumber, 10);
} else if (updateTarget && updateTarget !== "triggering") {
targetNumber = parseInt(updateTarget, 10);
} else {
// derive number from triggering context
}
...
if (!hasUpdates) {
core.info("No valid updates to apply for this item");
continue;
}Impact Analysis
- Maintainability: Every change to staging previews, environment flags, or summary output must be edited in two places, risking divergence.
- Bug Risk: Fixes for targeting bugs, staged-mode behavior, or validation logic may only land in one file, leaving the other broken.
- Code Bloat: ~300 lines of nearly identical logic inflate the workflow js footprint and increase review burden.
Refactoring Recommendations
-
Extract shared helper
- Create a reusable
update_items_runner.cjsthat encapsulates staged preview rendering, target resolution, environment gating, and summary writing, parameterized by resource-specific operations (issue vs PR vs future resources). - Estimated effort: 6-8 hours.
- Benefits: Single source of truth for staged behavior, consistent logging, easier to add new update types.
- Create a reusable
-
Define shared validators
- Move repeated validation helpers (e.g.,
resolveTargetNumber,buildUpdateData) into a utility module consumed by both scripts. - Estimated effort: 3-4 hours.
- Benefits: Simplifies unit tests and reduces copy-paste errors when new fields or operations are introduced.
- Move repeated validation helpers (e.g.,
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: 0c768b3
- Analysis Date: 2025-02-14
AI generated by Duplicate Code Detector
Reactions are currently unavailable
Activity