File size: 4,004 Bytes
38be44d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { Octokit } from '@octokit/rest';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import prompts from 'prompts';

if (!process.env.GITHUB_TOKEN) {
  console.error('Error: GITHUB_TOKEN environment variable is required.');
  process.exit(1);
}

const argv = yargs(hideBin(process.argv))
  .option('query', {
    alias: 'q',
    type: 'string',
    description:
      'Search query to find duplicate issues (e.g. "function response parts")',
    demandOption: true,
  })
  .option('canonical', {
    alias: 'c',
    type: 'number',
    description: 'The canonical issue number to duplicate others to',
    demandOption: true,
  })
  .option('pr', {
    type: 'string',
    description:
      'Optional Pull Request URL or ID to mention in the closing comment',
  })
  .option('owner', {
    type: 'string',
    default: 'google-gemini',
    description: 'Repository owner',
  })
  .option('repo', {
    type: 'string',
    default: 'gemini-cli',
    description: 'Repository name',
  })
  .option('dry-run', {
    alias: 'd',
    type: 'boolean',
    default: false,
    description: 'Run without making actual changes (read-only mode)',
  })
  .option('auto', {
    type: 'boolean',
    default: false,
    description:
      'Automatically close all duplicates without prompting (batch mode)',
  })
  .help()
  .parse();

const octokit = new Octokit({
  auth: process.env.GITHUB_TOKEN,
});

const { query, canonical, pr, owner, repo, dryRun, auto } = argv;

// Construct the full search query ensuring it targets the specific repo and open issues
const fullSearchQuery = `repo:${owner}/${repo} is:issue is:open ${query}`;

async function run() {
  console.log(`Searching for issues matching: ${fullSearchQuery}`);
  if (dryRun) {
    console.log('--- DRY RUN MODE: No changes will be made ---');
  }

  try {
    const issues = await octokit.paginate(
      octokit.rest.search.issuesAndPullRequests,
      {
        q: fullSearchQuery,
      },
    );

    console.log(`Found ${issues.length} issues.`);

    for (const issue of issues) {
      if (issue.number === canonical) {
        console.log(`Skipping canonical issue #${issue.number}`);
        continue;
      }

      console.log(
        `Processing issue #${issue.number}: ${issue.title} (by @${issue.user?.login})`,
      );

      if (!auto && !dryRun) {
        const response = await prompts({
          type: 'confirm',
          name: 'value',
          message: `Close issue #${issue.number} "${issue.title}" created by @${issue.user?.login}?`,
          initial: true,
        });

        if (!response.value) {
          console.log(`Skipping issue #${issue.number}`);
          continue;
        }
      }

      let commentBody = `Closing this issue as a duplicate of #${canonical}.`;
      if (pr) {
        commentBody += ` Please note that this issue should be resolved by PR ${pr}.`;
      }

      try {
        if (!dryRun) {
          // Add comment
          await octokit.rest.issues.createComment({
            owner,
            repo,
            issue_number: issue.number,
            body: commentBody,
          });
          console.log(`  Added comment.`);

          // Close issue
          await octokit.rest.issues.update({
            owner,
            repo,
            issue_number: issue.number,
            state: 'closed',
            state_reason: 'duplicate',
          });
          console.log(`  Closed issue.`);
        } else {
          console.log(`  [DRY RUN] Would add comment: "${commentBody}"`);
          console.log(`  [DRY RUN] Would close issue #${issue.number}`);
        }
      } catch (error) {
        console.error(
          `  Failed to process issue #${issue.number}:`,
          error.message,
        );
      }
    }
  } catch (error) {
    console.error('Error searching for issues:', error.message);
    process.exit(1);
  }
}

run().catch(console.error);