Personal Telegram developer console for GitHub

feat: resolve repositories by numeric id and expose trees/blobs

davoodmehraban89 committed Oct 2, 2026 at 23:03 UTC 21b9a97d115621eaaafda6ee0d7d4da155b90c34
1 file changed +104 -33
src/github.js
+104 -33
@@ -42,42 +42,69 @@ export class GitHubService {
42 return `${this.baseUrl}/repos/${encodeURIComponent(this.owner)}/${encodeURIComponent(repo)}${suffix}`;
43 }
44
45 - async listRepositories() {
46 - const data = await this.requestJson(
47 - `${this.baseUrl}/user/repos?sort=updated&per_page=20&affiliation=owner,collaborator,organization_member`,
48 - 'GitHub repositories'
49 - );
45 + repositoryLookupUrl(repoRef) {
46 + const value = String(repoRef || '');
47 + if (/^\d+$/.test(value)) {
48 + return `${this.baseUrl}/repositories/${value}`;
49 + }
50 + return this.repoUrl(value);
51 + }
52
51 - return data.map(r => ({
52 - id: r.name,
53 - name: r.name,
54 - fullName: r.full_name,
55 - title: `${r.name} ${r.private ? '🔒' : '🌐'}`,
56 - private: !!r.private,
57 - stars: r.stargazers_count ?? 0,
58 - forks: r.forks_count ?? 0,
59 - description: r.description || '',
60 - defaultBranch: r.default_branch || 'main'
61 - }));
53 + repositoryApiBase(fullName) {
54 + const parts = String(fullName || '').split('/').filter(Boolean);
55 + if (parts.length !== 2) {
56 + throw new Error('GitHub repository full_name is invalid');
57 + }
58 + return `${this.baseUrl}/repos/${parts.map(encodeURIComponent).join('/')}`;
59 + }
60 +
61 + async getRepositoryContext(repoRef) {
62 + const raw = await this.requestJson(this.repositoryLookupUrl(repoRef), 'GitHub repository');
63 + return {
64 + raw,
65 + apiBase: this.repositoryApiBase(raw.full_name)
66 + };
67 }
68
64 - async getRepository(repo) {
65 - const r = await this.requestJson(this.repoUrl(repo), 'GitHub repository');
69 + mapRepository(r) {
70 return {
67 - id: r.name,
71 + id: String(r.id ?? r.name),
72 name: r.name,
73 fullName: r.full_name,
74 private: !!r.private,
75 description: r.description || '',
76 defaultBranch: r.default_branch || 'main',
77 archived: !!r.archived,
74 - htmlUrl: r.html_url || ''
78 + htmlUrl: r.html_url || '',
79 + stars: r.stargazers_count ?? 0,
80 + forks: r.forks_count ?? 0
81 };
82 }
83
78 - async listPullRequests(repo) {
84 + async listRepositories() {
85 + const data = await this.requestJson(
86 + `${this.baseUrl}/user/repos?sort=updated&per_page=20&affiliation=owner,collaborator,organization_member`,
87 + 'GitHub repositories'
88 + );
89 +
90 + return data.map(r => {
91 + const repo = this.mapRepository(r);
92 + return {
93 + ...repo,
94 + title: `${repo.name} ${repo.private ? '🔒' : '🌐'}`
95 + };
96 + });
97 + }
98 +
99 + async getRepository(repoRef) {
100 + const { raw } = await this.getRepositoryContext(repoRef);
101 + return this.mapRepository(raw);
102 + }
103 +
104 + async listPullRequests(repoRef) {
105 + const { apiBase } = await this.getRepositoryContext(repoRef);
106 const data = await this.requestJson(
80 - this.repoUrl(repo, '/pulls?state=all&per_page=10'),
107 + `${apiBase}/pulls?state=all&per_page=10`,
108 'GitHub pull requests'
109 );
110
@@ -92,9 +119,10 @@ export class GitHubService {
119 }));
120 }
121
95 - async listCommits(repo) {
122 + async listCommits(repoRef) {
123 + const { apiBase } = await this.getRepositoryContext(repoRef);
124 const data = await this.requestJson(
97 - this.repoUrl(repo, '/commits?per_page=5'),
125 + `${apiBase}/commits?per_page=5`,
126 'GitHub commits'
127 );
128
@@ -106,11 +134,12 @@ export class GitHubService {
134 }));
135 }
136
109 - async listFiles(repo, path = '') {
137 + async listFiles(repoRef, path = '') {
138 + const { apiBase } = await this.getRepositoryContext(repoRef);
139 const suffix = path
140 ? `/contents/${path.split('/').map(encodeURIComponent).join('/')}`
141 : '/contents';
113 - const data = await this.requestJson(this.repoUrl(repo, suffix), 'GitHub contents');
142 + const data = await this.requestJson(`${apiBase}${suffix}`, 'GitHub contents');
143
144 if (!Array.isArray(data)) {
145 throw new Error('GitHub contents response is not a directory listing');
@@ -125,16 +154,58 @@ export class GitHubService {
154 }));
155 }
156
128 - async getRepoStatus(repo) {
129 - const [repository, runsData] = await Promise.all([
130 - this.getRepository(repo),
131 - this.requestJson(this.repoUrl(repo, '/actions/runs?per_page=3'), 'GitHub Actions runs')
132 - ]);
157 + async listTree(repoRef, sha) {
158 + if (!/^[a-f0-9]{40}$/i.test(String(sha || ''))) {
159 + throw new Error('Invalid Git tree SHA');
160 + }
161 +
162 + const { apiBase } = await this.getRepositoryContext(repoRef);
163 + const data = await this.requestJson(
164 + `${apiBase}/git/trees/${encodeURIComponent(sha)}`,
165 + 'GitHub tree'
166 + );
167 +
168 + return (data.tree || [])
169 + .filter(item => item.type === 'tree' || item.type === 'blob')
170 + .map(item => ({
171 + name: item.path,
172 + path: item.path,
173 + type: item.type === 'tree' ? 'dir' : 'file',
174 + size: item.size ?? 0,
175 + sha: item.sha || ''
176 + }));
177 + }
178 +
179 + async getBlob(repoRef, sha) {
180 + if (!/^[a-f0-9]{40}$/i.test(String(sha || ''))) {
181 + throw new Error('Invalid Git blob SHA');
182 + }
183 +
184 + const { apiBase } = await this.getRepositoryContext(repoRef);
185 + const data = await this.requestJson(
186 + `${apiBase}/git/blobs/${encodeURIComponent(sha)}`,
187 + 'GitHub blob'
188 + );
189 +
190 + return {
191 + encoding: data.encoding || '',
192 + size: data.size ?? 0,
193 + content: data.content || ''
194 + };
195 + }
196 +
197 + async getRepoStatus(repoRef) {
198 + const { raw, apiBase } = await this.getRepositoryContext(repoRef);
199 + const runsData = await this.requestJson(
200 + `${apiBase}/actions/runs?per_page=3`,
201 + 'GitHub Actions runs'
202 + );
203
204 const runs = runsData.workflow_runs || [];
205 return {
136 - repo,
137 - defaultBranch: repository.defaultBranch,
206 + repo: String(repoRef),
207 + repoName: raw.name,
208 + defaultBranch: raw.default_branch || 'main',
209 lastRuns: runs.map(r => ({
210 name: r.name,
211 status: r.status,