Personal Telegram developer console for GitHub
feat/plugin-foundation-v1
audit/commit-drilldown
audit/finalize-workspace-menu-security
audit/ops-actionability
audit/pr-menu-consolidation
audit/status-menu-consolidation
audit/telegram-menu-e2e
audit/telegram-menu-e2e-wave2
chore/actions-node24-runtime
cleanup/remove-diag
design/cloudflare-ops-read
docs/control-plane-2-7-design
docs/guide-v2-1-sync
docs/menu-audit-final-sync
docs/phase2-acceptance-closeout
docs/phase2-multifile-design
feat/bootstrap-worker
feat/cloudflare-ops-read
feat/cp2-multifile-final
feat/cp2-multifile-workspace
feat/cp3-github-write-final
feat/cp4-cloudflare-ops-final
feat/cp5-cicd-control-plane
feat/cp6-incident-manager
feat/cp7-automation-engine
feat/deep-file-browser
feat/final-delivery-16-20
feat/github-advanced-read
feat/in-app-guide-v2
feat/navigation-recovery-ux-v2
feat/ops-console-v2-1
feat/persistent-menu-code-input
feat/phase2-multifile-workspace
feat/phase2-multifile-workspace-v2
feat/plugin-foundation-v1
feat/safe-github-ops
feat/self-register-webhook
feat/studio-g0-foundation
feat/telegram-main-inline-menu
feat/telegram-pr-merge
feat/ux-platform-8-12
feat/v3a-readonly-copilot
feat/workspace-file-upload
feature/mobile-repo-browser
feature/multi-file-change-set
fix/approval-expiry-feedback
fix/callback-resilience-and-docs
fix/ci-actions-pagination
fix/cloudflare-fetch-binding
fix/editor-request-body-cap
fix/editor-safe-file-options
fix/issues-permission-release-empty-ux
fix/main-menu-callback-fallback
fix/phase2-file-editor-selection
fix/phase2-final-ux
fix/phase2-workspace-ref-persistence
fix/production-auto-deploy
fix/provider-write-error-feedback
fix/repository-id-navigation-fallback
fix/repository-pr-navigation
fix/telegram-callback-updates
fix/telegram-runtime-diagnostics
fix/workspace-editor-flow-v2
fix/workspace-recovery-validation
hardening/ci-zero-test-telegram-token
hardening/pr1-destructive-totp
hardening/pr2-kill-switch
main
multifile-workspace
nexa/edit-BwJEIIY3PW
nexa/edit-IPHT8irO7b
nexa/edit-b4Od3BIMYd
p2-multifile
phase2-20261005
phase2-20261005-a
phase2-20261005-b
phase2-20261005-c
phase2-20261005-d
phase2-20261005-e
phase2-20261005-f
phase2-20261005-g
phase2-20261005-h
phase2-20261005-i
phase2-20261005-j
phase2-20261005-k
phase2-20261005-l
phase2-20261005-m
phase2-20261005-n
phase2-20261005-o
phase2-20261005-p
phase2-20261005-q
phase2-20261005-r
phase2-20261005-s
phase2-20261005-t
phase2-20261005-u
phase2-20261005-v
phase2-20261005-w
phase2-20261005-x
phase2-20261005-y
phase2-20261005-z
phase2-acceptance-closeout
phase2-add-file-hotfix
phase2-change-set
phase2-design-20261005
phase2-design-final
phase2-implementation
phase2-multifile-implementation
phase2-multifile-spec
phase2-spec-final
phase2-workspace
phase2/multifile-workspace
plan/nexa-studio-v2-reviewed
security/remove-telegram-setup
spec/repository-workspace-editor
v3a/multifile
work/multifile
| 1 | import { DEFAULT_CONFIG } from './config.js'; |
| 2 | |
| 3 | export class GitHubService { |
| 4 | constructor(token = '', owner = DEFAULT_CONFIG.defaultOwner) { |
| 5 | this.token = token; |
| 6 | this.owner = owner; |
| 7 | this.baseUrl = 'https://api.github.com'; |
| 8 | } |
| 9 | |
| 10 | requireToken() { |
| 11 | if (!this.token) { |
| 12 | throw new Error('GITHUB_TOKEN is not configured'); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | getHeaders() { |
| 17 | this.requireToken(); |
| 18 | return { |
| 19 | 'User-Agent': 'Nexa-Dev-Bot-v1.1', |
| 20 | 'Accept': 'application/vnd.github+json', |
| 21 | 'Authorization': `Bearer ${this.token}`, |
| 22 | 'X-GitHub-Api-Version': '2022-11-28' |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | async requestJson(url, label) { |
| 27 | const res = await fetch(url, { headers: this.getHeaders() }); |
| 28 | if (!res.ok) { |
| 29 | let detail = ''; |
| 30 | try { |
| 31 | const body = await res.json(); |
| 32 | detail = body?.message ? ` ${body.message}` : ''; |
| 33 | } catch { |
| 34 | detail = ''; |
| 35 | } |
| 36 | throw new Error(`${label} failed: ${res.status}${detail}`); |
| 37 | } |
| 38 | return res.json(); |
| 39 | } |
| 40 | |
| 41 | repoUrl(repo, suffix = '') { |
| 42 | return `${this.baseUrl}/repos/${encodeURIComponent(this.owner)}/${encodeURIComponent(repo)}${suffix}`; |
| 43 | } |
| 44 | |
| 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 | |
| 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 | |
| 69 | mapRepository(r) { |
| 70 | return { |
| 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, |
| 78 | htmlUrl: r.html_url || '', |
| 79 | stars: r.stargazers_count ?? 0, |
| 80 | forks: r.forks_count ?? 0 |
| 81 | }; |
| 82 | } |
| 83 | |
| 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( |
| 107 | `${apiBase}/pulls?state=all&per_page=10`, |
| 108 | 'GitHub pull requests' |
| 109 | ); |
| 110 | |
| 111 | return data.map(pr => ({ |
| 112 | number: pr.number, |
| 113 | title: pr.title, |
| 114 | author: pr.user?.login || 'unknown', |
| 115 | state: pr.state, |
| 116 | merged: !!pr.merged_at, |
| 117 | createdAt: pr.created_at, |
| 118 | url: pr.html_url |
| 119 | })); |
| 120 | } |
| 121 | |
| 122 | async listCommits(repoRef) { |
| 123 | const { apiBase } = await this.getRepositoryContext(repoRef); |
| 124 | const data = await this.requestJson( |
| 125 | `${apiBase}/commits?per_page=5`, |
| 126 | 'GitHub commits' |
| 127 | ); |
| 128 | |
| 129 | return data.map(c => ({ |
| 130 | sha: c.sha.slice(0, 7), |
| 131 | message: String(c.commit?.message || '').split('\n')[0], |
| 132 | author: c.author?.login || c.commit?.author?.name || 'Unknown', |
| 133 | date: c.commit?.author?.date ? c.commit.author.date.slice(0, 10) : '' |
| 134 | })); |
| 135 | } |
| 136 | |
| 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'; |
| 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'); |
| 146 | } |
| 147 | |
| 148 | return data.map(item => ({ |
| 149 | name: item.name, |
| 150 | path: item.path, |
| 151 | type: item.type === 'dir' ? 'dir' : 'file', |
| 152 | size: item.size ?? 0, |
| 153 | sha: item.sha || '' |
| 154 | })); |
| 155 | } |
| 156 | |
| 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 { |
| 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, |
| 212 | conclusion: r.conclusion, |
| 213 | commit: r.head_commit?.message?.slice(0, 80) || '', |
| 214 | url: r.html_url || '' |
| 215 | })) |
| 216 | }; |
| 217 | } |
| 218 | } |