Personal Telegram developer console for GitHub

feat: add github workspace commit primitives

davoodmehraban89 committed Oct 3, 2026 at 13:22 UTC 8eac40407a54cbd3d0ff7bba73b76b3cb358d5d3
1 file changed +80 -7
src/github.js
+80 -7
@@ -13,6 +13,14 @@ 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 gitBranch = value => {
17 + const branch = String(value || '');
18 + if (!branch || branch.includes('..') || branch.startsWith('/') || branch.endsWith('/') || branch.includes('\0')) {
19 + throw new Error('Invalid Git branch');
20 + }
21 + return branch;
22 +};
23 +const encodedBranch = value => gitBranch(value).split('/').map(encodeURIComponent).join('/');
24 const encodedPath = value => String(value || '').split('/').filter(Boolean).map(encodeURIComponent).join('/');
25 const decodeBase64Utf8 = value => {
26 let binary;
@@ -50,7 +58,7 @@ export class GitHubService {
58 };
59 }
60
53 - async requestJson(url, label, { method = 'GET', body, allowEmpty = false } = {}) {
61 + async requestJson(url, label, { method = 'GET', body, allowEmpty = false, allowNotFound = false } = {}) {
62 const headers = this.getHeaders();
63 const options = { method, headers };
64 if (body !== undefined) {
@@ -64,6 +72,7 @@ export class GitHubService {
72 try { parsed = JSON.parse(raw); } catch { parsed = null; }
73 }
74 if (!res.ok) {
75 + if (allowNotFound && res.status === 404) return null;
76 const providerMessage = parsed?.message || raw.slice(0, 240);
77 const detail = providerMessage ? ` ${providerMessage}` : '';
78 throw new Error(`${label} failed: ${res.status}${detail}`);
@@ -216,14 +225,22 @@ export class GitHubService {
225 return data.map(item => ({ name: item.name, path: item.path, type: item.type === 'dir' ? 'dir' : 'file', size: item.size ?? 0, sha: item.sha || '' }));
226 }
227
228 + async getGitRef(repoRef, branch, { allowMissing = false } = {}) {
229 + const branchName = gitBranch(branch);
230 + const { apiBase } = await this.getRepositoryContext(repoRef);
231 + const data = await this.requestJson(
232 + `${apiBase}/git/ref/heads/${encodedBranch(branchName)}`,
233 + 'GitHub branch ref',
234 + { allowNotFound: allowMissing }
235 + );
236 + if (data === null) return null;
237 + return { branch: branchName, sha: fullSha(data?.object?.sha, 'Git branch head') };
238 + }
239 +
240 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 - }
241 + const branchName = gitBranch(branch);
242 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');
243 + const ref = await this.requestJson(`${apiBase}/git/ref/heads/${encodedBranch(branchName)}`, 'GitHub branch ref');
244 const headSha = fullSha(ref?.object?.sha, 'Git branch head');
245 const commit = await this.requestJson(`${apiBase}/git/commits/${headSha}`, 'GitHub branch commit');
246 const treeSha = fullSha(commit?.tree?.sha, 'Git tree');
@@ -248,6 +265,62 @@ export class GitHubService {
265 };
266 }
267
268 + async createGitBlob(repoRef, text) {
269 + const { apiBase } = await this.getRepositoryContext(repoRef);
270 + const data = await this.requestJson(`${apiBase}/git/blobs`, 'GitHub create blob', {
271 + method: 'POST',
272 + body: { content: String(text ?? ''), encoding: 'utf-8' }
273 + });
274 + return { sha: fullSha(data?.sha, 'Git blob') };
275 + }
276 +
277 + async createGitTree(repoRef, { baseTreeSha, entries } = {}) {
278 + const base = fullSha(baseTreeSha, 'Git base tree');
279 + if (!Array.isArray(entries)) throw new Error('Git tree entries are required');
280 + const tree = entries.map(entry => {
281 + const path = String(entry?.path || '');
282 + if (!path || path.startsWith('/') || path.includes('\0')) throw new Error('Invalid Git tree path');
283 + if (entry?.mode !== '100644' || entry?.type !== 'blob') throw new Error('Unsupported Git tree entry');
284 + return { path, mode: '100644', type: 'blob', sha: fullSha(entry.sha, 'Git blob') };
285 + });
286 + const { apiBase } = await this.getRepositoryContext(repoRef);
287 + const data = await this.requestJson(`${apiBase}/git/trees`, 'GitHub create tree', {
288 + method: 'POST',
289 + body: { base_tree: base, tree }
290 + });
291 + return { sha: fullSha(data?.sha, 'Git tree') };
292 + }
293 +
294 + async createGitCommit(repoRef, { message, treeSha, parentSha, timestamp } = {}) {
295 + const commitMessage = String(message || '');
296 + if (!commitMessage) throw new Error('Git commit message is required');
297 + const tree = fullSha(treeSha, 'Git tree');
298 + const parent = fullSha(parentSha, 'Git parent commit');
299 + const date = new Date(String(timestamp || ''));
300 + if (Number.isNaN(date.getTime()) || date.toISOString() !== String(timestamp)) throw new Error('Invalid Git commit timestamp');
301 + const identity = { name: 'Nexa Dev Bot', email: 'nexa-dev-bot@users.noreply.github.com', date: String(timestamp) };
302 + const { apiBase } = await this.getRepositoryContext(repoRef);
303 + const data = await this.requestJson(`${apiBase}/git/commits`, 'GitHub create commit', {
304 + method: 'POST',
305 + body: { message: commitMessage, tree, parents: [parent], author: identity, committer: identity }
306 + });
307 + return { sha: fullSha(data?.sha, 'Git commit') };
308 + }
309 +
310 + async createGitBranchRef(repoRef, { branch, sha } = {}) {
311 + const branchName = gitBranch(branch);
312 + const commitSha = fullSha(sha, 'Git commit');
313 + const { apiBase } = await this.getRepositoryContext(repoRef);
314 + const data = await this.requestJson(`${apiBase}/git/refs`, 'GitHub create branch ref', {
315 + method: 'POST',
316 + body: { ref: `refs/heads/${branchName}`, sha: commitSha }
317 + });
318 + return {
319 + ref: String(data?.ref || `refs/heads/${branchName}`),
320 + sha: fullSha(data?.object?.sha || commitSha, 'Git commit')
321 + };
322 + }
323 +
324 async listTree(repoRef, sha) {
325 if (!/^[a-f0-9]{40}$/i.test(String(sha || ''))) throw new Error('Invalid Git tree SHA');
326 const { apiBase } = await this.getRepositoryContext(repoRef);