-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathgithub-api.ts
More file actions
196 lines (170 loc) · 4.9 KB
/
github-api.ts
File metadata and controls
196 lines (170 loc) · 4.9 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
import { execFile } from "node:child_process";
import type { ExtractedContent } from "./extract.js";
import type { GitHubUrlInfo } from "./github-extract.js";
const MAX_TREE_ENTRIES = 200;
const MAX_INLINE_FILE_CHARS = 100_000;
let ghAvailable: boolean | null = null;
let ghHintShown = false;
export async function checkGhAvailable(): Promise<boolean> {
if (ghAvailable !== null) return ghAvailable;
return new Promise((resolve) => {
execFile("gh", ["--version"], { timeout: 5000 }, (err) => {
ghAvailable = !err;
resolve(ghAvailable);
});
});
}
export function showGhHint(): void {
if (!ghHintShown) {
ghHintShown = true;
console.error("[pi-web-access] Install `gh` CLI for better GitHub repo access including private repos.");
}
}
export async function checkRepoSize(owner: string, repo: string): Promise<number | null> {
if (!(await checkGhAvailable())) return null;
return new Promise((resolve) => {
execFile("gh", ["api", `repos/${owner}/${repo}`, "--jq", ".size"], { timeout: 10000 }, (err, stdout) => {
if (err) {
resolve(null);
return;
}
const kb = parseInt(stdout.trim(), 10);
resolve(Number.isNaN(kb) ? null : kb);
});
});
}
async function getDefaultBranch(owner: string, repo: string): Promise<string | null> {
if (!(await checkGhAvailable())) return null;
return new Promise((resolve) => {
execFile("gh", ["api", `repos/${owner}/${repo}`, "--jq", ".default_branch"], { timeout: 10000 }, (err, stdout) => {
if (err) {
resolve(null);
return;
}
const branch = stdout.trim();
resolve(branch || null);
});
});
}
async function fetchTreeViaApi(owner: string, repo: string, ref: string): Promise<string | null> {
if (!(await checkGhAvailable())) return null;
return new Promise((resolve) => {
execFile(
"gh",
["api", `repos/${owner}/${repo}/git/trees/${ref}?recursive=1`, "--jq", ".tree[].path"],
{ timeout: 15000, maxBuffer: 5 * 1024 * 1024 },
(err, stdout) => {
if (err) {
resolve(null);
return;
}
const paths = stdout.trim().split("\n").filter(Boolean);
if (paths.length === 0) {
resolve(null);
return;
}
const truncated = paths.length > MAX_TREE_ENTRIES;
const display = paths.slice(0, MAX_TREE_ENTRIES).join("\n");
resolve(truncated ? display + `\n... (${paths.length} total entries)` : display);
},
);
});
}
async function fetchReadmeViaApi(owner: string, repo: string, ref: string): Promise<string | null> {
if (!(await checkGhAvailable())) return null;
return new Promise((resolve) => {
execFile(
"gh",
["api", `repos/${owner}/${repo}/readme?ref=${ref}`, "--jq", ".content"],
{ timeout: 10000 },
(err, stdout) => {
if (err) {
resolve(null);
return;
}
try {
const decoded = Buffer.from(stdout.trim(), "base64").toString("utf-8");
resolve(decoded.length > 8192 ? decoded.slice(0, 8192) + "\n\n[README truncated at 8K chars]" : decoded);
} catch {
resolve(null);
}
},
);
});
}
async function fetchFileViaApi(owner: string, repo: string, path: string, ref: string): Promise<string | null> {
if (!(await checkGhAvailable())) return null;
return new Promise((resolve) => {
execFile(
"gh",
["api", `repos/${owner}/${repo}/contents/${path}?ref=${ref}`, "--jq", ".content"],
{ timeout: 10000, maxBuffer: 2 * 1024 * 1024 },
(err, stdout) => {
if (err) {
resolve(null);
return;
}
try {
resolve(Buffer.from(stdout.trim(), "base64").toString("utf-8"));
} catch {
resolve(null);
}
},
);
});
}
export async function fetchViaApi(
url: string,
owner: string,
repo: string,
info: GitHubUrlInfo,
sizeNote?: string,
): Promise<ExtractedContent | null> {
const ref = info.ref || (await getDefaultBranch(owner, repo));
if (!ref) return null;
const lines: string[] = [];
if (sizeNote) {
lines.push(sizeNote);
lines.push("");
}
if (info.type === "blob" && info.path) {
const content = await fetchFileViaApi(owner, repo, info.path, ref);
if (!content) return null;
lines.push(`## ${info.path}`);
if (content.length > MAX_INLINE_FILE_CHARS) {
lines.push(content.slice(0, MAX_INLINE_FILE_CHARS));
lines.push(`\n[File truncated at 100K chars]`);
} else {
lines.push(content);
}
return {
url,
title: `${owner}/${repo} - ${info.path}`,
content: lines.join("\n"),
error: null,
};
}
const [tree, readme] = await Promise.all([
fetchTreeViaApi(owner, repo, ref),
fetchReadmeViaApi(owner, repo, ref),
]);
if (!tree && !readme) return null;
if (tree) {
lines.push("## Structure");
lines.push(tree);
lines.push("");
}
if (readme) {
lines.push("## README.md");
lines.push(readme);
lines.push("");
}
lines.push("This is an API-only view. Clone the repo or use `read`/`bash` for deeper exploration.");
const title = info.path ? `${owner}/${repo} - ${info.path}` : `${owner}/${repo}`;
return {
url,
title,
content: lines.join("\n"),
error: null,
};
}