Skip to content

Refactor: Extract shared context helpers from update_issue and update_pull_request#6565

Merged
pelikhan merged 4 commits into
mainfrom
copilot/refactor-duplicate-update-scripts
Dec 15, 2025
Merged

Refactor: Extract shared context helpers from update_issue and update_pull_request#6565
pelikhan merged 4 commits into
mainfrom
copilot/refactor-duplicate-update-scripts

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 15, 2025

Both update_issue.cjs and update_pull_request.cjs duplicated context validation logic (isIssueContext, getIssueNumber, isPRContext, getPRNumber) with 52 lines of identical scaffolding.

Changes

  • Created update_context_helpers.cjs: Extracted context validation functions with proper null handling
  • Reduced duplication: update_issue.cjs 79→61 lines (-23%), update_pull_request.cjs 130→96 lines (-26%)
  • Updated bundler infrastructure: Added helper module to GetJavaScriptSources() and embedded sources

Example

Before:

// Duplicated in both files
function isIssueContext(eventName, _payload) {
  return eventName === "issues" || eventName === "issue_comment";
}
function getIssueNumber(payload) {
  return payload.issue?.number;
}

After:

const { isIssueContext, getIssueNumber } = require("./update_context_helpers.cjs");

Context validation logic is now centralized, with comprehensive test coverage (30 tests) for edge cases including null/undefined payloads and cross-event validation.

Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] 🔍 Duplicate Code Detected: Issue vs PR update scripts</issue_title>
<issue_description>Analysis of commit 1db3d6e

Assignee: @copilot

Summary

The GitHub Actions scripts for updating issues and pull requests share the same workflow scaffolding—context validation, number extraction, staged rendering configuration, and runUpdateWorkflow wiring—implemented twice with minor variations. Future changes to staging/summary behavior will require edits in both files, increasing the chance of inconsistent behavior.

Duplication Details

Pattern: Duplicated update workflow scaffolding

  • Severity: Medium
  • Occurrences: 2
  • Locations:
    • pkg/workflow/js/update_issue.cjs:4-79
    • pkg/workflow/js/update_pull_request.cjs:4-130
  • Code Sample:
const renderStagedItem = createRenderStagedItem({
  entityName: "Issue",
  numberField: "issue_number",
  targetLabel: "Target Issue:",
  currentTargetText: "Current issue",
  includeOperation: false,
});

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,
  });
}

The PR variant replicates this structure with only naming and operation flags changed.

Impact Analysis

  • Maintainability: Two nearly identical scripts double the work for staging/summary changes and increase divergence risk (e.g., context rules, summary wording).
  • Bug Risk: Behavioral fixes (like staged preview tweaks or output key changes) may land in one script but not the other, leading to inconsistent updates.
  • Code Bloat: Boilerplate obscures the truly unique logic (operation handling for PR bodies).

Refactoring Recommendations

  1. Parameterize update runner invocations
    • Extract shared options (staged renderer, summary wiring, base context checks) into a factory in update_runner.cjs, passing per-entity differences (context resolvers, operation support, labels) as config.
    • Estimated effort: 2h; simplifies adding new fields or behaviors.
    • Benefits: Single entry point for staged behavior, output keys, and summary formatting.
  2. Consolidate context helpers
    • Move isIssueContext/getIssueNumber and isPRContext/getPRNumber into a small shared module to avoid drift in event support.
    • Estimated effort: 1h; keeps context validation aligned.

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: 1db3d6e
  • Analysis Date: 2025-12-15T21:05:11+00:00

AI generated by Duplicate Code Detector

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.


Changeset

Ahoy! This treasure was crafted by 🏴‍☠️ Changeset Generator

Copilot AI and others added 2 commits December 15, 2025 21:16
- Created update_context_helpers.cjs with shared context validation functions
- Extracted isIssueContext, getIssueNumber, isPRContext, getPRNumber
- Updated update_issue.cjs to use shared helpers (79→61 lines, 23% reduction)
- Updated update_pull_request.cjs to use shared helpers (130→96 lines, 26% reduction)
- Added comprehensive test coverage for all context helper functions
- All 2217 JavaScript tests passing

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
- Added update_context_helpers.cjs to GetJavaScriptSources map
- Required for bundler to resolve the new shared context helper module
- All unit tests passing including TestStagedPreviewInlined

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicate code in issue and PR update scripts Refactor: Extract shared context helpers from update_issue and update_pull_request Dec 15, 2025
Copilot AI requested a review from mnkiefer December 15, 2025 21:37
@pelikhan pelikhan added the smoke label Dec 15, 2025
@pelikhan pelikhan marked this pull request as ready for review December 15, 2025 22:14
@github-actions
Copy link
Copy Markdown
Contributor

📰 BREAKING: Smoke Copilot is now investigating this pull request. Sources say the story is developing...

@github-actions
Copy link
Copy Markdown
Contributor

🔮 The ancient spirits stir... Smoke Codex awakens to divine this pull request...

@github-actions
Copy link
Copy Markdown
Contributor

💥 WHOOSH! Smoke Claude springs into action on this pull request! [Panel 1 begins...]

@github-actions
Copy link
Copy Markdown
Contributor

📰🚀🔍👀📡🕵️ https://github.com/githubnext/gh-aw/actions/runs/20249232828

@github-actions
Copy link
Copy Markdown
Contributor

📰 BREAKING: Smoke Copilot Playwright is now investigating this pull request. Sources say the story is developing...

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 15, 2025

🎉 Yo ho ho! Changeset Generator found the treasure and completed successfully! ⚓💰

31185ad

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Results

Status: ✅ PASS

cc @pelikhan

📰 BREAKING: Report filed by Smoke Copilot fer issue #6565 🗺️

@github-actions
Copy link
Copy Markdown
Contributor

Copilot Engine Smoke Test Results

Last 2 Merged PRs:

Test Results:

  • ✅ GitHub MCP Testing (retrieved PRs)
  • ✅ File Writing Testing (created /tmp/gh-aw/agent/smoke-test-copilot-20249232828.txt)
  • ✅ Bash Tool Testing (verified file creation)
  • ✅ Serena MCP Testing (queried classes)
  • ✅ Safe Input gh Tool Testing (listed 3 issues)

Overall Status: PASS

📰🔥📋 https://github.com/githubnext/gh-aw/actions/runs/20249232828 fer issue #6565 🗺️

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Results (Claude Sonnet 4.5)

Recent PRs:

Test Results:

  • ✅ GitHub MCP: Retrieved last 2 merged PRs
  • ✅ File Writing: Created test file successfully
  • ✅ Bash Tool: Verified file creation
  • ✅ Playwright MCP: Navigated to GitHub, title verified
  • ✅ Cache Memory: Created and verified cache file
  • ❌ Safe Input gh Tool: Tool not available (missing_tool needed)

Overall: PASS (5/6 tests successful)

💥 [THE END] — Illustrated by Smoke Claude fer issue #6565 🗺️

@github-actions
Copy link
Copy Markdown
Contributor

PRs:

  • [docs] Consolidate network permissions documentation
  • Add validation for jobs.secrets to enforce GitHub Actions secrets expressions
    Tests: MCP ✅ | file ✅ | Playwright ✅ | cache ✅ | safeinputs-gh ❌ (tool unavailable)
    Overall: FAIL

🔮 The oracle has spoken through Smoke Codex fer issue #6565 🗺️

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Results

Playwright MCP: Successfully navigated to github.com, title contains "GitHub"
Cache Memory: File created at /tmp/gh-aw/cache-memory/smoke-test-20249232842.txt
Safe Input gh: Retrieved 3 issues successfully

Status: PASS

📰 BREAKING: Report filed by Smoke Copilot Playwright fer issue #6565 🗺️

@pelikhan pelikhan merged commit 4b38a3d into main Dec 15, 2025
34 checks passed
@pelikhan pelikhan deleted the copilot/refactor-duplicate-update-scripts branch December 15, 2025 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] 🔍 Duplicate Code Detected: Issue vs PR update scripts

3 participants