import { native } from "./tauri"; import type { CheckInfo, CommentInfo, CommitInfo, GlobalConfig, PrSummary, ProposedInlineComment, ReviewInfo, TimelineEventInfo, } from "../types"; export type MergeMethod = "squash" | "merge" | "rebase"; export interface ReviewThreadInfo { /** GraphQL node id — needed for resolve/unresolve mutations */ id: string; isResolved: boolean; /** REST databaseIds of the thread's comments */ commentIds: number[]; } /** * Minimal GitHub REST v3 client working against github.com and GitHub * Enterprise. All requests go through the native HTTP command (no CORS, * supports self-signed GHE certs). */ export class GitHubClient { readonly apiBase: string; readonly webBase: string; private token: string; private insecure: boolean; login = ""; constructor(cfg: Pick) { const url = cfg.githubUrl.replace(/\/+$/, ""); this.webBase = url; this.apiBase = url === "https://github.com" || url === "https://www.github.com" ? "https://api.github.com" : `${url}/api/v3`; this.token = cfg.token; this.insecure = cfg.insecureTls; this.login = cfg.login; } private async raw( method: string, path: string, opts: { body?: unknown; accept?: string } = {} ): Promise<{ status: number; body: string; headers: [string, string][] }> { const url = path.startsWith("http") ? path : this.apiBase + path; const headers: [string, string][] = [ ["Authorization", `Bearer ${this.token}`], ["Accept", opts.accept ?? "application/vnd.github+json"], ["X-GitHub-Api-Version", "2022-11-28"], ]; if (opts.body !== undefined) headers.push(["Content-Type", "application/json"]); const resp = await native.httpRequest({ method, url, headers, body: opts.body !== undefined ? JSON.stringify(opts.body) : undefined, insecure: this.insecure, }); if (resp.status >= 400) { let msg = resp.body; try { msg = JSON.parse(resp.body).message ?? resp.body; } catch { /* keep raw */ } throw new Error(`GitHub ${method} ${path} → ${resp.status}: ${msg}`); } return resp; } private async json(method: string, path: string, body?: unknown): Promise { const resp = await this.raw(method, path, { body }); return resp.body ? (JSON.parse(resp.body) as T) : (undefined as T); } /** GET with Link-header pagination. Default cap ~500 items. */ private async paged(path: string, accept?: string, maxPages = 5): Promise { const out: T[] = []; let url: string | null = path + (path.includes("?") ? "&" : "?") + "per_page=100"; for (let i = 0; i < maxPages && url; i++) { const resp = await this.raw("GET", url, { accept }); out.push(...(JSON.parse(resp.body) as T[])); const link = resp.headers.find(([k]) => k.toLowerCase() === "link")?.[1] ?? ""; const next = /<([^>]+)>;\s*rel="next"/.exec(link); url = next ? next[1] : null; } return out; } /** Server-rendered bodies: includes body_html with signed asset URLs and *