|
| 1 | +/// <reference path="../env.d.ts" /> |
| 2 | +import { tool } from "@opencode-ai/plugin" |
| 3 | +import DESCRIPTION from "./github-pr-search.txt" |
| 4 | + |
| 5 | +async function githubFetch(endpoint: string, options: RequestInit = {}) { |
| 6 | + const response = await fetch(`https://api.github.com${endpoint}`, { |
| 7 | + ...options, |
| 8 | + headers: { |
| 9 | + Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, |
| 10 | + Accept: "application/vnd.github+json", |
| 11 | + "Content-Type": "application/json", |
| 12 | + ...options.headers, |
| 13 | + }, |
| 14 | + }) |
| 15 | + if (!response.ok) { |
| 16 | + throw new Error(`GitHub API error: ${response.status} ${response.statusText}`) |
| 17 | + } |
| 18 | + return response.json() |
| 19 | +} |
| 20 | + |
| 21 | +interface PR { |
| 22 | + title: string |
| 23 | + html_url: string |
| 24 | +} |
| 25 | + |
| 26 | +export default tool({ |
| 27 | + description: DESCRIPTION, |
| 28 | + args: { |
| 29 | + query: tool.schema.string().describe("Search query for PR titles and descriptions"), |
| 30 | + limit: tool.schema.number().describe("Maximum number of results to return").default(10), |
| 31 | + offset: tool.schema.number().describe("Number of results to skip for pagination").default(0), |
| 32 | + }, |
| 33 | + async execute(args) { |
| 34 | + const owner = "sst" |
| 35 | + const repo = "opencode" |
| 36 | + |
| 37 | + const page = Math.floor(args.offset / args.limit) + 1 |
| 38 | + const searchQuery = encodeURIComponent(`${args.query} repo:${owner}/${repo} type:pr state:open`) |
| 39 | + const result = await githubFetch( |
| 40 | + `/search/issues?q=${searchQuery}&per_page=${args.limit}&page=${page}&sort=updated&order=desc`, |
| 41 | + ) |
| 42 | + |
| 43 | + if (result.total_count === 0) { |
| 44 | + return `No PRs found matching "${args.query}"` |
| 45 | + } |
| 46 | + |
| 47 | + const prs = result.items as PR[] |
| 48 | + const formatted = prs.map((pr) => `${pr.title}\n${pr.html_url}`).join("\n\n") |
| 49 | + |
| 50 | + return `Found ${result.total_count} PRs (showing ${prs.length}):\n\n${formatted}` |
| 51 | + }, |
| 52 | +}) |
0 commit comments