swadhinbiswas feat: implement air-gapped mode, UI fixes, and project management integrations Β· 7/9/2026
e3bd4a1
322 lines Β· 9.3 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
 * Search Commands
 * Search repositories, issues, and PRs from the CLI
 */

import chalk from "chalk";
import { Command } from "commander";
import ora from "ora";
import { getWithAuth } from "../../lib/api.js";
import { getConfig } from "../../lib/config.js";

interface SearchResult<T> {
  total: number;
  items: T[];
}

interface RepoResult {
  id: string;
  fullName: string;
  description?: string;
  visibility: string;
  stars: number;
  forks: number;
  updatedAt: string;
}

interface IssueResult {
  id: string;
  number: number;
  title: string;
  state: string;
  repository: { fullName: string };
  author: { username: string };
  createdAt: string;
}

interface PRResult {
  id: string;
  number: number;
  title: string;
  state: string;
  repository: { fullName: string };
  author: { username: string };
  sourceBranch: string;
  targetBranch: string;
  createdAt: string;
}

export const searchCommands = new Command("search").description(
  "Search repositories, issues, and pull requests",
);

// Search Repos
searchCommands
  .command("repos <query>")
  .alias("repo")
  .description("Search repositories")
  .option("-L, --limit <n>", "Maximum results", "20")
  .option("--sort <field>", "Sort by (stars, forks, updated)", "stars")
  .option("--order <direction>", "Sort order (asc, desc)", "desc")
  .action(async (query: string, options) => {
    const config = getConfig();
    if (!config.token) {
      console.error(chalk.hex("#ff5555")("Not logged in. Run 'och auth login' first."));
      process.exit(1);
    }

    const spinner = ora(`Searching repositories for "${query}"...`).start();

    try {
      const params = new URLSearchParams();
      params.append("q", query);
      params.append("limit", options.limit);
      params.append("sort", options.sort);
      params.append("order", options.order);

      const result = await getWithAuth<{ data: SearchResult<RepoResult> }>(
        `/api/search?type=repos&${params}`,
      );

      spinner.stop();

      if (result.data.items.length === 0) {
        console.log(chalk.hex("#6272a4")(`No repositories found matching "${query}"`));
        return;
      }

      console.log(
        chalk.bold(
          `\nπŸ” Repositories (${result.data.total} total, showing ${result.data.items.length})\n`,
        ),
      );

      for (const repo of result.data.items) {
        const icon =
          repo.visibility === "private"
            ? chalk.hex("#f1fa8c")("πŸ”’")
            : chalk.hex("#50fa7b")("πŸ“¦");
        console.log(`${icon} ${chalk.bold(repo.fullName)}`);
        if (repo.description) {
          console.log(
            chalk.hex("#6272a4")(
              `   ${repo.description.slice(0, 80)}${repo.description.length > 80 ? "..." : ""}`,
            ),
          );
        }
        console.log(
          chalk.hex("#6272a4")(
            `   ⭐ ${repo.stars}  🍴 ${repo.forks}  Updated ${new Date(repo.updatedAt).toLocaleDateString()}`,
          ),
        );
      }

      console.log("");
    } catch (error) {
      spinner.fail("Search failed");
      console.error(
        chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"),
      );
      process.exit(1);
    }
  });

// Search Issues
searchCommands
  .command("issues <query>")
  .alias("issue")
  .description("Search issues")
  .option("-L, --limit <n>", "Maximum results", "20")
  .option("-s, --state <state>", "Filter by state (open, closed, all)", "open")
  .option("-r, --repo <repo>", "Limit to specific repository (owner/name)")
  .action(async (query: string, options) => {
    const config = getConfig();
    if (!config.token) {
      console.error(chalk.hex("#ff5555")("Not logged in. Run 'och auth login' first."));
      process.exit(1);
    }

    const spinner = ora(`Searching issues for "${query}"...`).start();

    try {
      const params = new URLSearchParams();
      params.append("q", query);
      params.append("limit", options.limit);
      if (options.state !== "all") params.append("state", options.state);
      if (options.repo) params.append("repo", options.repo);

      const result = await getWithAuth<{ data: SearchResult<IssueResult> }>(
        `/api/search?type=issues&${params}`,
      );

      spinner.stop();

      if (result.data.items.length === 0) {
        console.log(chalk.hex("#6272a4")(`No issues found matching "${query}"`));
        return;
      }

      console.log(
        chalk.bold(
          `\nπŸ” Issues (${result.data.total} total, showing ${result.data.items.length})\n`,
        ),
      );

      for (const issue of result.data.items) {
        const icon = issue.state === "open" ? chalk.hex("#50fa7b")("●") : chalk.hex("#ff5555")("●");
        console.log(
          `${icon} ${chalk.hex("#8be9fd")(issue.repository.fullName)}#${issue.number} ${issue.title}`,
        );
        console.log(
          chalk.hex("#6272a4")(
            `   @${issue.author.username} β€’ ${new Date(issue.createdAt).toLocaleDateString()}`,
          ),
        );
      }

      console.log("");
    } catch (error) {
      spinner.fail("Search failed");
      console.error(
        chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"),
      );
      process.exit(1);
    }
  });

// Search PRs
searchCommands
  .command("prs <query>")
  .alias("pr")
  .description("Search pull requests")
  .option("-L, --limit <n>", "Maximum results", "20")
  .option(
    "-s, --state <state>",
    "Filter by state (open, closed, merged, all)",
    "open",
  )
  .option("-r, --repo <repo>", "Limit to specific repository (owner/name)")
  .action(async (query: string, options) => {
    const config = getConfig();
    if (!config.token) {
      console.error(chalk.hex("#ff5555")("Not logged in. Run 'och auth login' first."));
      process.exit(1);
    }

    const spinner = ora(`Searching pull requests for "${query}"...`).start();

    try {
      const params = new URLSearchParams();
      params.append("q", query);
      params.append("limit", options.limit);
      if (options.state !== "all") params.append("state", options.state);
      if (options.repo) params.append("repo", options.repo);

      const result = await getWithAuth<{ data: SearchResult<PRResult> }>(
        `/api/search?type=prs&${params}`,
      );

      spinner.stop();

      if (result.data.items.length === 0) {
        console.log(chalk.hex("#6272a4")(`No pull requests found matching "${query}"`));
        return;
      }

      console.log(
        chalk.bold(
          `\nπŸ” Pull Requests (${result.data.total} total, showing ${result.data.items.length})\n`,
        ),
      );

      for (const pr of result.data.items) {
        const icon =
          pr.state === "open"
            ? chalk.hex("#50fa7b")("●")
            : pr.state === "merged"
              ? chalk.hex("#ff79c6")("●")
              : chalk.hex("#ff5555")("●");
        console.log(
          `${icon} ${chalk.hex("#8be9fd")(pr.repository.fullName)}#${pr.number} ${pr.title}`,
        );
        console.log(
          chalk.hex("#6272a4")(
            `   ${pr.sourceBranch} β†’ ${pr.targetBranch} β€’ @${pr.author.username}`,
          ),
        );
      }

      console.log("");
    } catch (error) {
      spinner.fail("Search failed");
      console.error(
        chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"),
      );
      process.exit(1);
    }
  });

// Search Code (bonus)
searchCommands
  .command("code <query>")
  .description("Search code")
  .option("-L, --limit <n>", "Maximum results", "20")
  .option("-r, --repo <repo>", "Limit to specific repository (owner/name)")
  .option("-l, --language <lang>", "Filter by language")
  .action(async (query: string, options) => {
    const config = getConfig();
    if (!config.token) {
      console.error(chalk.hex("#ff5555")("Not logged in. Run 'och auth login' first."));
      process.exit(1);
    }

    const spinner = ora(`Searching code for "${query}"...`).start();

    try {
      const params = new URLSearchParams();
      params.append("q", query);
      params.append("limit", options.limit);
      if (options.repo) params.append("repo", options.repo);
      if (options.language) params.append("language", options.language);

      const result = await getWithAuth<{
        data: SearchResult<{
          path: string;
          repository: { fullName: string };
          matches: { line: number; content: string }[];
        }>;
      }>(`/api/search?type=code&${params}`);

      spinner.stop();

      if (result.data.items.length === 0) {
        console.log(chalk.hex("#6272a4")(`No code found matching "${query}"`));
        return;
      }

      console.log(
        chalk.bold(
          `\nπŸ” Code (${result.data.total} total, showing ${result.data.items.length})\n`,
        ),
      );

      for (const item of result.data.items) {
        console.log(
          `${chalk.hex("#8be9fd")(item.repository.fullName)}/${chalk.bold(item.path)}`,
        );
        for (const match of item.matches.slice(0, 3)) {
          console.log(chalk.hex("#6272a4")(`   ${match.line}: `) + match.content.trim());
        }
        console.log("");
      }
    } catch (error) {
      spinner.fail("Search failed");
      console.error(
        chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"),
      );
      process.exit(1);
    }
  });

export default searchCommands;