opencode-gh-search 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts ADDED
@@ -0,0 +1,498 @@
1
+ import { execFile } from "node:child_process"
2
+ import { existsSync, readdirSync } from "node:fs"
3
+ import { mkdir, readFile, writeFile } from "node:fs/promises"
4
+ import { homedir } from "node:os"
5
+ import { basename, join } from "node:path"
6
+ import { promisify } from "node:util"
7
+ import { Plugin } from "@opencode/plugin"
8
+ import { GhSearch, exactness, matches, matchRepos, parseQuery, type Cursor, type Item, type Kind, type Page, type Repo, type Resolved, type Viewer } from "./rpc.js"
9
+
10
+ const run = promisify(execFile)
11
+
12
+ // The desktop app's server may not inherit the login shell's PATH.
13
+ const env = {
14
+ ...process.env,
15
+ PATH: [process.env.PATH, "/opt/homebrew/bin", "/usr/local/bin"].filter(Boolean).join(":"),
16
+ }
17
+
18
+ async function repoRoot(dir: string): Promise<string | undefined> {
19
+ try {
20
+ const { stdout } = await run("git", ["rev-parse", "--show-toplevel"], { cwd: dir, env })
21
+ return stdout.trim() || undefined
22
+ } catch {
23
+ return undefined
24
+ }
25
+ }
26
+
27
+ /** Repos under `dir`, descending into folders that are not repos themselves, up to `depth` levels. */
28
+ function childRepos(dir: string, depth = 3): Repo[] {
29
+ const repos: Repo[] = []
30
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
31
+ if (!entry.isDirectory() || entry.name.startsWith(".")) continue
32
+ const path = join(dir, entry.name)
33
+ if (existsSync(join(path, ".git"))) repos.push({ name: entry.name, dir: path })
34
+ else if (depth > 1) repos.push(...childRepos(path, depth - 1))
35
+ }
36
+ return repos.sort((a, b) => a.name.localeCompare(b.name))
37
+ }
38
+
39
+ async function resolve(directory: string, input: string): Promise<Resolved> {
40
+ const text = input.trim()
41
+ const root = await repoRoot(directory)
42
+ if (root) return { repos: [{ name: basename(root), dir: root }], query: text }
43
+
44
+ const repos = childRepos(directory)
45
+ const [first = "", ...rest] = text.split(/\s+/)
46
+ if (!first.startsWith("@") || first.length < 2) return { repos, query: text }
47
+ return { repos: matchRepos(repos, (repo) => repo.name, first.slice(1)), query: rest.join(" ") }
48
+ }
49
+
50
+ const names = new Map<string, string>()
51
+
52
+ async function nameWithOwner(dir: string): Promise<string> {
53
+ try {
54
+ return await lookupName(dir)
55
+ } catch {
56
+ return ""
57
+ }
58
+ }
59
+
60
+ async function lookupName(dir: string): Promise<string> {
61
+ const cached = names.get(dir)
62
+ if (cached) return cached
63
+ // Read from the origin remote: much faster than `gh repo view` when searching many repos.
64
+ const { stdout } = await run("git", ["remote", "get-url", "origin"], { cwd: dir, env })
65
+ const name = stdout.trim().match(/github\.com[:/](.+?\/.+?)(?:\.git)?$/)?.[1] ?? ""
66
+ names.set(dir, name)
67
+ return name
68
+ }
69
+
70
+ let token: string | undefined
71
+
72
+ async function githubToken(): Promise<string> {
73
+ if (!token) token = (await run("gh", ["auth", "token"], { env })).stdout.trim()
74
+ return token
75
+ }
76
+
77
+ /** GitHub asked us to slow down (secondary rate limit, or the hourly quota ran out). */
78
+ class RateLimited extends Error {
79
+ constructor(readonly retryMs: number) {
80
+ super(`GitHub rate limit; retrying in ${Math.ceil(retryMs / 1000)}s`)
81
+ }
82
+ }
83
+
84
+ // GitHub's secondary rate limit trips on bursts of concurrent requests, e.g. syncing every repo in a folder at once.
85
+ const MAX_CONCURRENT = 2
86
+ let running = 0
87
+ const waiting: (() => void)[] = []
88
+
89
+ async function graphql<T>(query: string, variables: Record<string, unknown> = {}): Promise<T> {
90
+ if (running >= MAX_CONCURRENT) await new Promise<void>((resolve) => waiting.push(resolve))
91
+ running++
92
+ try {
93
+ return await request<T>(query, variables)
94
+ } finally {
95
+ running--
96
+ waiting.shift()?.()
97
+ }
98
+ }
99
+
100
+ async function request<T>(query: string, variables: Record<string, unknown>, attempt = 0): Promise<T> {
101
+ const response = await fetch("https://api.github.com/graphql", {
102
+ method: "POST",
103
+ headers: { authorization: `bearer ${await githubToken()}`, "content-type": "application/json" },
104
+ body: JSON.stringify({ query, variables }),
105
+ })
106
+ if (response.status === 401) token = undefined
107
+ // Heavy pages can time out with a non-JSON 502/504 page; retry those a couple of times.
108
+ if (response.status >= 500 && attempt < 2) return request(query, variables, attempt + 1)
109
+ if (response.status === 403 || response.status === 429) {
110
+ const retryAfter = Number(response.headers.get("retry-after"))
111
+ const reset = Number(response.headers.get("x-ratelimit-reset"))
112
+ const exhausted = response.headers.get("x-ratelimit-remaining") === "0"
113
+ // GitHub's guidance: honour retry-after, else wait for the reset when the quota is spent, else a minute.
114
+ const ms = retryAfter ? retryAfter * 1000 : exhausted && reset ? reset * 1000 - Date.now() : 60_000
115
+ const text = await response.text()
116
+ if (retryAfter || exhausted || /rate limit/i.test(text)) throw new RateLimited(Math.max(ms, 1000))
117
+ throw new Error(`GitHub ${response.status}: ${messageOf(text) ?? response.statusText}`)
118
+ }
119
+ const text = await response.text()
120
+ let body: { data?: T; errors?: { message: string }[] }
121
+ try {
122
+ body = JSON.parse(text)
123
+ } catch {
124
+ throw new Error(`GitHub ${response.status} ${response.statusText}`.trim())
125
+ }
126
+ if (!body.data) throw new Error(body.errors?.map((e) => e.message).join("; ") || `GitHub ${response.status}`)
127
+ return body.data
128
+ }
129
+
130
+ function messageOf(text: string): string | undefined {
131
+ try {
132
+ return (JSON.parse(text) as { message?: string }).message
133
+ } catch {
134
+ return undefined
135
+ }
136
+ }
137
+
138
+ const VIEWER_MS = 10 * 60_000
139
+ let current: { at: number; viewer: Promise<Viewer> } | undefined
140
+
141
+ /**
142
+ * The signed-in GitHub user and their teams, for `@me` and the row highlights. Teams come from every org you're
143
+ * in (needs the `read:org` scope; without it, team requests just don't count as yours).
144
+ */
145
+ function viewer(): Promise<Viewer> {
146
+ if (current && Date.now() - current.at < VIEWER_MS) return current.viewer
147
+ type Data = {
148
+ viewer: { login: string; organizations: { nodes: { login: string; teams: { nodes: { slug: string }[] } }[] } }
149
+ }
150
+ const lookup = async (): Promise<Viewer> => {
151
+ const { login } = (await graphql<{ viewer: { login: string } }>("query { viewer { login } }")).viewer
152
+ try {
153
+ const data = await graphql<Data>(
154
+ `query($login: String!) { viewer { login organizations(first: 100) { nodes { login
155
+ teams(first: 100, userLogins: [$login]) { nodes { slug } } } } } }`,
156
+ { login },
157
+ )
158
+ const teams = data.viewer.organizations.nodes.flatMap((org) => org.teams.nodes.map((t) => `${org.login}/${t.slug}`))
159
+ return { login, teams }
160
+ } catch {
161
+ return { login, teams: [] }
162
+ }
163
+ }
164
+ const entry = { at: Date.now(), viewer: lookup() }
165
+ current = entry
166
+ entry.viewer.catch(() => {
167
+ if (current === entry) current = undefined
168
+ })
169
+ return entry.viewer.catch(() => ({ login: "", teams: [] }))
170
+ }
171
+
172
+ // GitHub search only matches whole words, so each repo's PRs and issues are kept in a local index and searched here.
173
+ // The first sync pages through every item; later syncs stop at the first item not updated since the last one.
174
+
175
+ const CACHE_DIR = join(homedir(), ".cache", "opencode-gh-search")
176
+ const RESYNC_MS = 60_000
177
+ const RETRY_MS = 15_000
178
+
179
+ // Bump when the stored fields change, so old caches are fetched again in full.
180
+ const INDEX_VERSION = 7
181
+
182
+ interface Index {
183
+ version?: number
184
+ items: Record<number, Item>
185
+ /** Every item has been fetched once. */
186
+ complete: boolean
187
+ /** Where the first full sync continues after an interruption. */
188
+ resume?: string
189
+ /** updatedAt of the newest item seen by the last finished sync. */
190
+ synced?: string
191
+ }
192
+
193
+ interface Node {
194
+ number: number
195
+ title: string
196
+ url: string
197
+ state: string
198
+ updatedAt: string
199
+ createdAt: string
200
+ closedAt: string | null
201
+ author: { login: string } | null
202
+ labels: { nodes: { name: string }[] }
203
+ assignees: { nodes: { login: string }[] }
204
+ isDraft?: boolean
205
+ headRefName?: string
206
+ baseRefName?: string
207
+ mergedAt?: string | null
208
+ reviewDecision?: "APPROVED" | "CHANGES_REQUESTED" | "REVIEW_REQUIRED" | null
209
+ latestReviews?: { nodes: { author: { login: string; __typename: string } | null; state: string }[] }
210
+ reviewRequests?: { nodes: { requestedReviewer: { login?: string; slug?: string } | null }[] }
211
+ }
212
+
213
+ const COMMON = "number title url state updatedAt createdAt closedAt author { login } labels(first: 20) { nodes { name } } assignees(first: 10) { nodes { login } }"
214
+
215
+ /** How each kind is fetched from GitHub's GraphQL API. */
216
+ const SOURCES: Record<Kind, { connection: string; fields: string }> = {
217
+ pr: { connection: "pullRequests", fields: `${COMMON} isDraft headRefName baseRefName mergedAt reviewDecision latestReviews(first: 20) { nodes { author { login __typename } state } } reviewRequests(first: 10) { nodes { requestedReviewer { ... on User { login } ... on Team { slug } } } }` },
218
+ issue: { connection: "issues", fields: COMMON },
219
+ }
220
+
221
+ function toItem(kind: Kind, repo: string, node: Node): Item {
222
+ const state = node.state === "OPEN" && node.isDraft ? "draft" : node.state.toLowerCase()
223
+ return {
224
+ kind,
225
+ number: node.number,
226
+ title: node.title,
227
+ state,
228
+ repo,
229
+ author: node.author?.login ?? "",
230
+ url: node.url,
231
+ labels: node.labels.nodes.map((label) => label.name),
232
+ assignees: node.assignees.nodes.map((user) => user.login),
233
+ createdAt: node.createdAt,
234
+ ...(node.mergedAt ? { mergedAt: node.mergedAt } : {}),
235
+ ...(node.closedAt && !node.mergedAt ? { closedAt: node.closedAt } : {}),
236
+ ...(kind === "pr"
237
+ ? {
238
+ branch: node.headRefName,
239
+ base: node.baseRefName,
240
+ review: (node.reviewDecision?.toLowerCase() ?? "none") as Item["review"],
241
+ reviews: Object.fromEntries(
242
+ (node.latestReviews?.nodes ?? []).filter((r) => r.author).map((r) => [r.author!.login, r.state.toLowerCase()]),
243
+ ),
244
+ bots: (node.latestReviews?.nodes ?? []).filter((r) => r.author?.__typename === "Bot").map((r) => r.author!.login),
245
+ reviewers: (node.reviewRequests?.nodes ?? [])
246
+ .map(({ requestedReviewer: r }) => r?.login ?? r?.slug ?? "")
247
+ .filter(Boolean),
248
+ }
249
+ : {}),
250
+ }
251
+ }
252
+
253
+ const indexes = new Map<string, Promise<Index>>()
254
+ const syncing = new Map<string, Promise<void>>()
255
+ // When each index may sync next: RESYNC_MS after a sync, later after an error.
256
+ const nextSync = new Map<string, number>()
257
+ const syncErrors = new Map<string, string>()
258
+
259
+ const key = (kind: Kind, repo: string) => `${kind}:${repo}`
260
+
261
+ function cacheFile(kind: Kind, repo: string) {
262
+ return join(CACHE_DIR, `${repo.replace("/", "__")}.${kind}.json`)
263
+ }
264
+
265
+ /** One shared index per kind and repo, so a sync and concurrent searches see the same object. */
266
+ function load(kind: Kind, repo: string): Promise<Index> {
267
+ let index = indexes.get(key(kind, repo))
268
+ if (!index) {
269
+ index = read(kind, repo)
270
+ indexes.set(key(kind, repo), index)
271
+ }
272
+ return index
273
+ }
274
+
275
+ async function read(kind: Kind, repo: string): Promise<Index> {
276
+ try {
277
+ const index = JSON.parse(await readFile(cacheFile(kind, repo), "utf8")) as Index
278
+ if (index.version === INDEX_VERSION) return index
279
+ // An older format: keep showing its items while a full sync replaces them.
280
+ return { version: INDEX_VERSION, items: index.items ?? {}, complete: false }
281
+ } catch {}
282
+ return { version: INDEX_VERSION, items: {}, complete: false }
283
+ }
284
+
285
+ async function save(kind: Kind, repo: string, index: Index) {
286
+ await mkdir(CACHE_DIR, { recursive: true })
287
+ await writeFile(cacheFile(kind, repo), JSON.stringify(index))
288
+ }
289
+
290
+ async function sync(kind: Kind, repo: string): Promise<void> {
291
+ const index = await load(kind, repo)
292
+ const { connection, fields } = SOURCES[kind]
293
+ const query = `query($owner: String!, $name: String!, $after: String) {
294
+ repository(owner: $owner, name: $name) {
295
+ ${connection}(first: 50, after: $after, orderBy: { field: UPDATED_AT, direction: DESC }) {
296
+ pageInfo { hasNextPage endCursor }
297
+ nodes { ${fields} }
298
+ }
299
+ }
300
+ }`
301
+ const [owner, name] = repo.split("/")
302
+ let after = index.complete ? undefined : index.resume
303
+ let newest: string | undefined
304
+ let pages = 0
305
+ for (;;) {
306
+ type Connection = { pageInfo: { hasNextPage: boolean; endCursor: string }; nodes: Node[] }
307
+ const data = await graphql<{ repository: Record<string, Connection> }>(query, { owner, name, after })
308
+ const { nodes, pageInfo } = data.repository[connection]
309
+ let reachedSynced = false
310
+ for (const node of nodes) {
311
+ newest ??= node.updatedAt
312
+ if (index.complete && index.synced && node.updatedAt <= index.synced) {
313
+ reachedSynced = true
314
+ break
315
+ }
316
+ const mergeable = index.items[node.number]?.mergeable
317
+ index.items[node.number] = { ...toItem(kind, repo, node), ...(mergeable !== undefined ? { mergeable } : {}) }
318
+ }
319
+ pages++
320
+ if (reachedSynced || !pageInfo.hasNextPage) break
321
+ after = pageInfo.endCursor
322
+ if (!index.complete) index.resume = after
323
+ if (pages % 5 === 0) await save(kind, repo, index)
324
+ }
325
+ if (!index.complete || !index.synced || (newest && newest > index.synced)) index.synced = newest ?? index.synced
326
+ index.complete = true
327
+ index.resume = undefined
328
+ if (kind === "pr") await syncMergeable(repo, index)
329
+ await save(kind, repo, index)
330
+ }
331
+
332
+ /**
333
+ * Whether each open PR is ready to merge. Kept out of the main sync: GitHub computes it per PR, which makes large
334
+ * pages time out, and it changes with CI without touching `updatedAt`. Best effort: a failure keeps the last values.
335
+ */
336
+ async function syncMergeable(repo: string, index: Index) {
337
+ const query = `query($owner: String!, $name: String!, $after: String) {
338
+ repository(owner: $owner, name: $name) {
339
+ pullRequests(first: 20, after: $after, states: OPEN) {
340
+ pageInfo { hasNextPage endCursor }
341
+ nodes { number mergeStateStatus }
342
+ }
343
+ }
344
+ }`
345
+ const [owner, name] = repo.split("/")
346
+ try {
347
+ for (let after: string | undefined; ; ) {
348
+ type Connection = { pageInfo: { hasNextPage: boolean; endCursor: string }; nodes: { number: number; mergeStateStatus: string }[] }
349
+ const data = await graphql<{ repository: { pullRequests: Connection } }>(query, { owner, name, after })
350
+ const { nodes, pageInfo } = data.repository.pullRequests
351
+ for (const node of nodes) {
352
+ const item = index.items[node.number]
353
+ // UNKNOWN means GitHub hasn't computed it yet; keep what we had.
354
+ if (item && node.mergeStateStatus !== "UNKNOWN") item.mergeable = node.mergeStateStatus === "CLEAN"
355
+ }
356
+ if (!pageInfo.hasNextPage) break
357
+ after = pageInfo.endCursor
358
+ }
359
+ } catch {}
360
+ }
361
+
362
+ /** Starts a sync when none is running and its next sync time has come. */
363
+ function refresh(kind: Kind, repo: string) {
364
+ const k = key(kind, repo)
365
+ if (syncing.has(k) || Date.now() < (nextSync.get(k) ?? 0)) return
366
+ nextSync.set(k, Date.now() + RESYNC_MS)
367
+ const job = sync(kind, repo)
368
+ .then(() => void syncErrors.delete(k))
369
+ .catch((error) => {
370
+ // Back off instead of retrying on the next keystroke; a first sync resumes where it stopped.
371
+ if (error instanceof RateLimited) {
372
+ nextSync.set(k, Date.now() + error.retryMs)
373
+ syncErrors.set(k, `Indexing ${repo} paused: ${error.message}`)
374
+ return
375
+ }
376
+ nextSync.set(k, Date.now() + RETRY_MS)
377
+ syncErrors.set(k, `Indexing ${repo} failed: ${error instanceof Error ? error.message : String(error)}`)
378
+ })
379
+ .finally(() => syncing.delete(k))
380
+ syncing.set(k, job)
381
+ }
382
+
383
+ const rank: Record<string, number> = { open: 0, draft: 1, merged: 2, closed: 3 }
384
+
385
+ const active = (item: Item) => item.state === "open" || item.state === "draft"
386
+
387
+ /** A stack is a chain of open PRs where each one is based on the branch of the one below it. */
388
+ function withStacks(index: Index): Item[] {
389
+ const list = Object.values(index.items)
390
+ const byBranch = new Map(list.filter(active).map((item) => [item.branch, item]))
391
+ const parent = (item: Item) => (active(item) && item.base ? byBranch.get(item.base) : undefined)
392
+ const children = new Map<number, Item[]>()
393
+ for (const item of list) {
394
+ const below = parent(item)
395
+ if (below) children.set(below.number, [...(children.get(below.number) ?? []), item])
396
+ }
397
+ const height = (item: Item, seen = new Set<number>()): number => {
398
+ if (seen.has(item.number)) return 0
399
+ seen.add(item.number)
400
+ return 1 + Math.max(0, ...(children.get(item.number) ?? []).map((child) => height(child, seen)))
401
+ }
402
+ return list.map((item) => {
403
+ const below = parent(item)
404
+ if (!below && !children.has(item.number)) return item
405
+ let position = 1
406
+ let root = item
407
+ const seen = new Set([item.number])
408
+ for (let p = parent(root); p && !seen.has(p.number); p = parent(root)) {
409
+ seen.add(p.number)
410
+ root = p
411
+ position++
412
+ }
413
+ const stack: Item["stack"] = { position, size: height(root) }
414
+ if (below) stack.parent = below.number
415
+ return { ...item, stack }
416
+ })
417
+ }
418
+
419
+ /** Plugin options from `opencode.json`. */
420
+ interface Settings {
421
+ /** Leave out reviews by bot accounts (e.g. Copilot) from the review status. Default true. */
422
+ ignoreBotReviews: boolean
423
+ /** Further reviewer logins to leave out, e.g. a CI account that isn't a GitHub App. */
424
+ ignoreReviewers: string[]
425
+ }
426
+
427
+ let settings: Settings = { ignoreBotReviews: true, ignoreReviewers: [] }
428
+
429
+ /** Drops ignored reviewers from an item's reviews. The index keeps them, so changing the settings needs no resync. */
430
+ function withoutIgnored(item: Item): Item {
431
+ if (!item.reviews) return item
432
+ const ignored = new Set([
433
+ ...settings.ignoreReviewers.map((login) => login.toLowerCase()),
434
+ ...(settings.ignoreBotReviews ? (item.bots ?? []).map((login) => login.toLowerCase()) : []),
435
+ ])
436
+ if (ignored.size === 0) return item
437
+ return { ...item, reviews: Object.fromEntries(Object.entries(item.reviews).filter(([login]) => !ignored.has(login.toLowerCase()))) }
438
+ }
439
+
440
+ async function search(kind: Kind, dirs: string[], query: string, size: number, start?: Cursor): Promise<Page> {
441
+ const repos = [...new Set((await Promise.all(dirs.map(nameWithOwner))).filter(Boolean))]
442
+ repos.forEach((repo) => refresh(kind, repo))
443
+ const indexed = await Promise.all(repos.map((repo) => load(kind, repo)))
444
+ const me = await viewer()
445
+ const terms = parseQuery(query, kind)
446
+ const all = indexed
447
+ .flatMap((index) => (kind === "pr" ? withStacks(index) : Object.values(index.items)))
448
+ .filter((item) => matches(item, terms, me))
449
+ .map((item) => ({ item, exact: exactness(item, terms) }))
450
+ .sort(
451
+ (a, b) =>
452
+ b.exact - a.exact ||
453
+ rank[a.item.state] - rank[b.item.state] ||
454
+ b.item.number - a.item.number ||
455
+ a.item.repo.localeCompare(b.item.repo),
456
+ )
457
+ .map(({ item }) => item)
458
+ const offset = start?.offset ?? 0
459
+ const items = all.slice(offset, offset + size).map(withoutIgnored)
460
+ const indexing = repos.some((repo) => syncing.has(key(kind, repo)))
461
+ const errors = repos.map((repo) => syncErrors.get(key(kind, repo))).filter((e): e is string => !!e)
462
+ const result: Page = { repos, items, total: all.length, indexing, viewer: me }
463
+ if (errors.length) result.error = errors.join("; ")
464
+ if (offset + size < all.length) result.next = { offset: offset + size }
465
+ return result
466
+ }
467
+
468
+ export default Plugin.define({
469
+ id: "gh-search",
470
+ async setup(ctx) {
471
+ settings = {
472
+ ignoreBotReviews: ctx.options.ignoreBotReviews !== false,
473
+ ignoreReviewers: Array.isArray(ctx.options.ignoreReviewers) ? ctx.options.ignoreReviewers.map(String) : [],
474
+ }
475
+ const registration = await ctx.rpc.register(GhSearch, {
476
+ resolve: async (input) => {
477
+ const { directory, input: text } = input as { directory: string; input: string }
478
+ return resolve(directory, text)
479
+ },
480
+ search: async (input, context) => {
481
+ const { kind, dirs, query, size, cursor } = input as {
482
+ kind: Kind
483
+ dirs: string[]
484
+ query: string
485
+ size: number
486
+ cursor?: Cursor
487
+ }
488
+ try {
489
+ return await search(kind, dirs, query, size, cursor)
490
+ } catch (error) {
491
+ const message = error instanceof Error ? error.message : String(error)
492
+ return context.error("gh_failed", "GitHub search failed", { message })
493
+ }
494
+ },
495
+ })
496
+ return () => registration.dispose()
497
+ },
498
+ })