Personal Telegram developer console for GitHub
feat: add pinned repository file readers
davoodmehraban89 committed
Oct 3, 2026 at 12:42 UTC
b3f81fdffc1763b6ebd0d8abd0ff95e104dd00b4
1 file changed
+51
-3
src/github.js
+51
-3
@@ -13,6 +13,21 @@ const fullSha = (value, label = 'Git commit') => {
13
if (!/^[a-f0-9]{40}$/i.test(text)) throw new Error(`Invalid ${label} SHA`);
14
return text;
15
};
16
+const encodedPath = value => String(value || '').split('/').filter(Boolean).map(encodeURIComponent).join('/');
17
+const decodeBase64Utf8 = value => {
18
+ let binary;
19
+ try {
20
+ binary = atob(String(value || '').replace(/\s/g, ''));
21
+ } catch {
22
+ throw new Error('GitHub file content is not valid base64');
23
+ }
24
+ const bytes = Uint8Array.from(binary, char => char.charCodeAt(0));
25
+ try {
26
+ return new TextDecoder('utf-8', { fatal: true }).decode(bytes);
27
+ } catch {
28
+ throw new Error('GitHub file content is not valid UTF-8 text');
29
+ }
30
+};
31
32
export class GitHubService {
33
constructor(token = '', owner = DEFAULT_CONFIG.defaultOwner) {
@@ -192,14 +207,47 @@ export class GitHubService {
207
return (data.check_runs || []).map(check => ({ id: check.id, name: check.name || 'Check', status: check.status || '', conclusion: check.conclusion || null, url: check.html_url || '' }));
208
}
209
195
- async listFiles(repoRef, path = '') {
210
+ async listFiles(repoRef, path = '', { ref } = {}) {
211
const { apiBase } = await this.getRepositoryContext(repoRef);
197
- const suffix = path ? `/contents/${path.split('/').map(encodeURIComponent).join('/')}` : '/contents';
198
- const data = await this.requestJson(`${apiBase}${suffix}`, 'GitHub contents');
212
+ const suffix = path ? `/contents/${encodedPath(path)}` : '/contents';
213
+ const query = ref ? `?ref=${encodeURIComponent(String(ref))}` : '';
214
+ const data = await this.requestJson(`${apiBase}${suffix}${query}`, 'GitHub contents');
215
if (!Array.isArray(data)) throw new Error('GitHub contents response is not a directory listing');
216
return data.map(item => ({ name: item.name, path: item.path, type: item.type === 'dir' ? 'dir' : 'file', size: item.size ?? 0, sha: item.sha || '' }));
217
}
218
219
+ async getBranchSnapshot(repoRef, branch) {
220
+ const branchName = String(branch || '');
221
+ if (!branchName || branchName.includes('..') || branchName.startsWith('/') || branchName.endsWith('/')) {
222
+ throw new Error('Invalid Git branch');
223
+ }
224
+ const { apiBase } = await this.getRepositoryContext(repoRef);
225
+ const branchPath = branchName.split('/').map(encodeURIComponent).join('/');
226
+ const ref = await this.requestJson(`${apiBase}/git/ref/heads/${branchPath}`, 'GitHub branch ref');
227
+ const headSha = fullSha(ref?.object?.sha, 'Git branch head');
228
+ const commit = await this.requestJson(`${apiBase}/git/commits/${headSha}`, 'GitHub branch commit');
229
+ const treeSha = fullSha(commit?.tree?.sha, 'Git tree');
230
+ return { branch: branchName, headSha, treeSha };
231
+ }
232
+
233
+ async getFileAtRef(repoRef, path, ref) {
234
+ const filePath = String(path || '');
235
+ if (!filePath || filePath.startsWith('/') || filePath.includes('\0')) throw new Error('Invalid GitHub file path');
236
+ const commitRef = fullSha(ref);
237
+ const { apiBase } = await this.getRepositoryContext(repoRef);
238
+ const data = await this.requestJson(`${apiBase}/contents/${encodedPath(filePath)}?ref=${commitRef}`, 'GitHub file');
239
+ if (!data || Array.isArray(data) || data.type !== 'file') throw new Error('GitHub contents response is not a file');
240
+ if (data.encoding !== 'base64') throw new Error('GitHub file encoding is not supported');
241
+ const content = decodeBase64Utf8(data.content || '');
242
+ return {
243
+ path: data.path || filePath,
244
+ sha: String(data.sha || ''),
245
+ size: Number(data.size) || 0,
246
+ encoding: 'utf-8',
247
+ content
248
+ };
249
+ }
250
+
251
async listTree(repoRef, sha) {
252
if (!/^[a-f0-9]{40}$/i.test(String(sha || ''))) throw new Error('Invalid Git tree SHA');
253
const { apiBase } = await this.getRepositoryContext(repoRef);