Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 30, 2026

Problem

close_expired_issues.cjs, close_expired_pull_requests.cjs, and close_expired_discussions.cjs each contained ~100 lines of identical GraphQL pagination and filtering logic, differing only in entity-specific field names.

Changes

Created expired_entity_search_helpers.cjs

  • Generic searchEntitiesWithExpiration() function accepting entity configuration
  • Cursor-based GraphQL pagination with agentic workflow + expiration marker filtering
  • Configurable deduplication (enabled for discussions to handle GraphQL edge case)

Refactored 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

  • 11 test cases in expired_entity_search_helpers.test.cjs
  • Covers pagination, filtering, deduplication, edge cases

Usage

const { searchEntitiesWithExpiration } = require("./expired_entity_search_helpers.cjs");

// Issues/PRs
const { items, stats } = await searchEntitiesWithExpiration(github, owner, repo, {
  entityType: "issues",
  graphqlField: "issues",
  resultKey: "issues",
});

// Discussions (with deduplication)
const { items, stats } = await searchEntitiesWithExpiration(github, owner, repo, {
  entityType: "discussions",
  graphqlField: "discussions",
  resultKey: "discussions",
  enableDedupe: true,
});

Impact

  • Single point of maintenance for search/pagination logic
  • Consistent behavior across all entity types
  • Easy to extend to additional entity types
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: @copilot

Summary

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

  • Severity: Medium
  • Occurrences: 3
  • Locations:
    • 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)
  • Code Sample:
function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function searchIssuesWithExpiration(github, owner, repo) {
  const issues = [];
  let hasNextPage = true;
  let cursor = null;
  let pageCount = 0;
  let totalScanned = 0;

  core.info(`Starting GraphQL search for open issues in ${owner}/${repo}`);

  while (hasNextPage) {
    pageCount++;
    core.info(`Fetching page ${pageCount} of open issues (cursor: ${cursor || "initial"})`);

    const query = `
      query($owner: String!, $repo: String!, $cursor: String) {
        repository(owner: $owner, name: $repo) {
          issues(first: 100, after: $cursor, states: [OPEN]) {
            pageInfo { hasNextPage endCursor }
            nodes { id number title url body createdAt }
          }
        }
      }
    `;

    const result = await github.graphql(query, { owner, repo, cursor });
    if (!result || !result.repository || !result.repository.issues) {
      core.warning(`GraphQL query returned no data at page ${pageCount}`);
      break;
    }

    const nodes = result.repository.issues.nodes || [];
    totalScanned += nodes.length;

    let agenticCount = 0;
    let withExpirationCount = 0;

    for (const issue of nodes) {
      const agenticPattern = /^> AI generated by/m;
      const isAgenticWorkflow = issue.body && agenticPattern.test(issue.body);
      if (!isAgenticWorkflow) continue;

      const match = issue.body ? issue.body.match(EXPIRATION_PATTERN) : null;
      if (match) {
        withExpirationCount++;
        core.info(`  Found issue #${issue.number} with expiration marker: "${match[1]}" - ${issue.title}`);
        issues.push(issue);
      }
    }

    core.info(`Page ${pageCount} summary: ${agenticCount} agentic issues, ${withExpirationCount} with expiration markers`);
    hasNextPage = result.repository.issues.pageInfo.hasNextPage;
    cursor = result.repository.issues.pageInfo.endCursor;
  }

  core.info(`Search complete: Scanned ${totalScanned} issues across ${pageCount} pages, found ${issues.length} with expiration markers`);
  return { issues, stats: { pageCount, totalScanned } };
}

Impact Analysis

  • Maintainability: Changes to pagination, filtering rules, or logging must be replicated across three files.
  • Bug Risk: Divergence is already visible (discussions adds duplicate ID handling). Future changes could accidentally drift.
  • Code Bloat: ~70+ lines repeated per file, increasing review surface area.

Refactoring Recommendations

  1. Extract shared pagination + filtering helper

    • Extract a common helper in actions/setup/js/expiration_helpers.cjs (or a new expired_entity_search.cjs) that accepts entity type metadata (query fragment, node mapping, label/ID fields).
    • Estimated effort: medium (2-4 hours)
    • Benefits: single place for pagination and agentic + expiration matching.
  2. Normalize logging + stats structure

    • Return a normalized {items, stats} shape, with optional dedupe callback for discussions.
    • Estimated effort: low (1-2 hours)
    • Benefits: reduces drift and makes future additions (e.g., projects) straightforward.

Implementation Checklist

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

Analysis Metadata

  • Analyzed Files: 3
  • Detection Method: Serena semantic code analysis + targeted diff review
  • Commit: 1659ea6
  • Analysis Date: 2026-01-30

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.

- Create shared helper function `searchEntitiesWithExpiration()` in new file
- Reduce close_expired_issues.cjs by ~95 lines (287 lines now)
- Reduce close_expired_pull_requests.cjs by ~95 lines (286 lines now)
- Reduce close_expired_discussions.cjs by ~107 lines (383 lines now)
- Total reduction: ~300 lines of duplicate code replaced with 160-line shared helper
- Add comprehensive test suite with 11 test cases covering all scenarios
- All tests passing

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicate code in expired entity cleanup scripts Refactor: Extract shared entity search helper from expired cleanup scripts Jan 30, 2026
Copilot AI requested a review from pelikhan January 30, 2026 19:47
@pelikhan pelikhan marked this pull request as ready for review January 30, 2026 22:28
@pelikhan pelikhan merged commit f650b7f into main Jan 30, 2026
145 checks passed
@pelikhan pelikhan deleted the copilot/refactor-expired-entity-cleanup branch January 30, 2026 22:38
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: Expired Entity Cleanup in close_expired_* scripts

2 participants