Personal Telegram developer console for GitHub

hardening/ci-zero-test-telegram-token
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
js 147 lines 3.95 KB
Raw
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 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 );
50
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 }));
62 }
63
64 async getRepository(repo) {
65 const r = await this.requestJson(this.repoUrl(repo), 'GitHub repository');
66 return {
67 id: r.name,
68 name: r.name,
69 fullName: r.full_name,
70 private: !!r.private,
71 description: r.description || '',
72 defaultBranch: r.default_branch || 'main',
73 archived: !!r.archived,
74 htmlUrl: r.html_url || ''
75 };
76 }
77
78 async listPullRequests(repo) {
79 const data = await this.requestJson(
80 this.repoUrl(repo, '/pulls?state=all&per_page=10'),
81 'GitHub pull requests'
82 );
83
84 return data.map(pr => ({
85 number: pr.number,
86 title: pr.title,
87 author: pr.user?.login || 'unknown',
88 state: pr.state,
89 merged: !!pr.merged_at,
90 createdAt: pr.created_at,
91 url: pr.html_url
92 }));
93 }
94
95 async listCommits(repo) {
96 const data = await this.requestJson(
97 this.repoUrl(repo, '/commits?per_page=5'),
98 'GitHub commits'
99 );
100
101 return data.map(c => ({
102 sha: c.sha.slice(0, 7),
103 message: String(c.commit?.message || '').split('\n')[0],
104 author: c.author?.login || c.commit?.author?.name || 'Unknown',
105 date: c.commit?.author?.date ? c.commit.author.date.slice(0, 10) : ''
106 }));
107 }
108
109 async listFiles(repo, path = '') {
110 const suffix = path
111 ? `/contents/${path.split('/').map(encodeURIComponent).join('/')}`
112 : '/contents';
113 const data = await this.requestJson(this.repoUrl(repo, suffix), 'GitHub contents');
114
115 if (!Array.isArray(data)) {
116 throw new Error('GitHub contents response is not a directory listing');
117 }
118
119 return data.map(item => ({
120 name: item.name,
121 path: item.path,
122 type: item.type === 'dir' ? 'dir' : 'file',
123 size: item.size ?? 0,
124 sha: item.sha || ''
125 }));
126 }
127
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 ]);
133
134 const runs = runsData.workflow_runs || [];
135 return {
136 repo,
137 defaultBranch: repository.defaultBranch,
138 lastRuns: runs.map(r => ({
139 name: r.name,
140 status: r.status,
141 conclusion: r.conclusion,
142 commit: r.head_commit?.message?.slice(0, 80) || '',
143 url: r.html_url || ''
144 }))
145 };
146 }
147 }