Refactor: Extract shared entity search helper from expired cleanup scripts #12820
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
close_expired_issues.cjs,close_expired_pull_requests.cjs, andclose_expired_discussions.cjseach contained ~100 lines of identical GraphQL pagination and filtering logic, differing only in entity-specific field names.Changes
Created
expired_entity_search_helpers.cjssearchEntitiesWithExpiration()function accepting entity configurationRefactored three cleanup scripts
close_expired_issues.cjs: 382 → 287 lines (-95)close_expired_pull_requests.cjs: 381 → 286 lines (-95)close_expired_discussions.cjs: 490 → 383 lines (-107)Added comprehensive test coverage
expired_entity_search_helpers.test.cjsUsage
Impact
Original prompt
This section details on the original issue you should resolve
<issue_title>Duplicate Code: Expired Entity Cleanup in close_expired_* scripts</issue_title>
<issue_description># 🔍 Duplicate Code Detected: Expired Entity Cleanup Logic
Analysis of commit 1659ea6
Assignee:
@copilotSummary
The expired-entity cleanup scripts for issues, pull requests, and discussions duplicate the same search/filter loop, expiration matching, and pagination logic. The three files are structurally identical in large sections (>70 lines each) with only entity-specific fields and API endpoints differing.
Duplication Details
Pattern: Expired entity GraphQL search + filter loop
actions/setup/js/close_expired_issues.cjs(lines 22-123)actions/setup/js/close_expired_pull_requests.cjs(lines 22-123)actions/setup/js/close_expired_discussions.cjs(lines 22-139)Impact Analysis
Refactoring Recommendations
Extract shared pagination + filtering helper
actions/setup/js/expiration_helpers.cjs(or a newexpired_entity_search.cjs) that accepts entity type metadata (query fragment, node mapping, label/ID fields).Normalize logging + stats structure
{items, stats}shape, with optional dedupe callback for discussions.Implementation Checklist
Analysis Metadata
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.