Personal Telegram developer console for GitHub

fix/approval-expiry-feedback
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 257 lines 9.13 KB
Raw
1 import { githubPluginManifest } from './manifest.js';
2
3 const CALLBACK_ACTIONS = Object.freeze([
4 ['files:', 'github.files.list'],
5 ['tree:', 'github.tree.read'],
6 ['blob:', 'github.blob.read'],
7 ['pr:', 'github.pr.list'],
8 ['commits:', 'github.commits.list'],
9 ['status:', 'github.status.read']
10 ]);
11
12 const escapeHtml = value => String(value ?? '')
13 .replaceAll('&', '&')
14 .replaceAll('<', '&lt;')
15 .replaceAll('>', '&gt;')
16 .replaceAll('"', '&quot;');
17
18 const numericPair = (value, prefix) => {
19 const match = String(value || '').match(new RegExp(`^${prefix}:(\\d+):(\\d+)$`));
20 if (!match) return null;
21 return { repoId: Number(match[1]), runId: Number(match[2]) };
22 };
23
24 const numericSingle = (value, prefix) => {
25 const match = String(value || '').match(new RegExp(`^${prefix}:(\\d+)$`));
26 return match ? Number(match[1]) : null;
27 };
28
29 const workflowRunsPage = value => {
30 const match = String(value || '').match(/^runs:(\d+)(?::(\d+))?$/);
31 if (!match) return null;
32 const page = Math.max(1, Math.min(100, Number(match[2]) || 1));
33 return { repoId: Number(match[1]), page };
34 };
35
36 const runIcon = run => {
37 if (run.conclusion === 'success') return '✅';
38 if (run.conclusion === 'failure') return '❌';
39 if (run.conclusion === 'cancelled') return '⏹';
40 return run.status === 'in_progress' ? '🟡' : '⏳';
41 };
42
43 const addCiButton = (view, repoId) => {
44 const rows = [...(view?.reply_markup?.inline_keyboard || [])];
45 const ciRow = [{ text: '🧪 CI / Actions', callback_data: `runs:${repoId}` }];
46 const insertAt = Math.max(0, rows.length - 1);
47 rows.splice(insertAt, 0, ciRow);
48 return {
49 ...view,
50 reply_markup: {
51 ...(view?.reply_markup || {}),
52 inline_keyboard: rows
53 }
54 };
55 };
56
57 async function workflowRunsView(github, repoId, page = 1) {
58 const [repo, runs] = await Promise.all([
59 github.getRepository(repoId),
60 github.listWorkflowRuns(repoId, { perPage: 10, page })
61 ]);
62 const body = runs.length
63 ? runs.slice(0, 10).map(run => `${runIcon(run)} <b>${escapeHtml(run.name)}</b> #${run.runNumber ?? '-'}\n └ ${escapeHtml(run.branch || '-')} · <code>${escapeHtml(run.sha || '-')}</code> · ${escapeHtml(run.conclusion || run.status || 'unknown')}`).join('\n\n')
64 : '<i>Workflow runی یافت نشد.</i>';
65
66 const pagination = [];
67 if (page > 1) {
68 pagination.push({ text: '⬅️ جدیدتر', callback_data: `runs:${repoId}:${page - 1}` });
69 }
70 if (runs.length === 10 && page < 100) {
71 pagination.push({ text: 'قدیمی‌تر ➡️', callback_data: `runs:${repoId}:${page + 1}` });
72 }
73 const refreshCallback = page === 1 ? `runs:${repoId}` : `runs:${repoId}:${page}`;
74
75 return {
76 text: `🧪 <b>CI / Actions — ${escapeHtml(repo.name)}</b>\nصفحه: <b>${page}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`,
77 reply_markup: {
78 inline_keyboard: [
79 ...runs.slice(0, 10).map(run => [{
80 text: `${runIcon(run)} ${run.name} #${run.runNumber ?? run.id}`,
81 callback_data: `run:${repoId}:${run.id}`
82 }]),
83 ...(pagination.length ? [pagination] : []),
84 [
85 { text: '🔄 بروزرسانی', callback_data: refreshCallback },
86 { text: '🔙 پروژه', callback_data: `repo:${repoId}` }
87 ]
88 ]
89 }
90 };
91 }
92
93 async function workflowRunView(github, repoId, runId) {
94 const [repo, run] = await Promise.all([
95 github.getRepository(repoId),
96 github.getWorkflowRun(repoId, runId)
97 ]);
98 const rows = [
99 [{ text: '👷 Jobها', callback_data: `jobs:${repoId}:${runId}` }]
100 ];
101 if (run.conclusion === 'failure') {
102 rows.push([{ text: '🔁 اجرای مجدد jobهای ناموفق', callback_data: `rerun:${repoId}:${runId}` }]);
103 }
104 if (run.status === 'in_progress' || run.status === 'queued') {
105 rows.push([{ text: '⏹ لغو Workflow', callback_data: `cancelrun:${repoId}:${runId}` }]);
106 }
107 rows.push([
108 { text: '🔄 بروزرسانی', callback_data: `run:${repoId}:${runId}` },
109 { text: '🔙 CI / Actions', callback_data: `runs:${repoId}` }
110 ]);
111
112 return {
113 text: `🧪 <b>${escapeHtml(run.name)} — ${escapeHtml(repo.name)}</b>\n➖➖➖➖➖➖➖➖➖➖\nوضعیت: ${runIcon(run)} <b>${escapeHtml(run.conclusion || run.status || 'unknown')}</b>\nRun: <code>#${escapeHtml(run.runNumber ?? run.id)}</code>\nBranch: <code>${escapeHtml(run.branch || '-')}</code>\nSHA: <code>${escapeHtml(run.sha || '-')}</code>\nTrigger: <code>${escapeHtml(run.event || '-')}</code>`,
114 reply_markup: { inline_keyboard: rows }
115 };
116 }
117
118 async function workflowJobsView(github, repoId, runId) {
119 const [repo, run, jobs] = await Promise.all([
120 github.getRepository(repoId),
121 github.getWorkflowRun(repoId, runId),
122 github.listWorkflowJobs(repoId, runId)
123 ]);
124 const body = jobs.length
125 ? jobs.slice(0, 20).map(job => {
126 const jobState = job.conclusion || job.status || 'unknown';
127 const steps = (job.steps || []).slice(0, 30)
128 .map(step => ` ${step.conclusion === 'success' ? '✅' : step.conclusion === 'failure' ? '❌' : '•'} ${escapeHtml(step.name)} — ${escapeHtml(step.conclusion || step.status || 'unknown')}`)
129 .join('\n');
130 return `<b>${escapeHtml(job.name)}</b> — ${escapeHtml(jobState)}${steps ? `\n${steps}` : ''}`;
131 }).join('\n\n')
132 : '<i>Jobی در این Run یافت نشد.</i>';
133
134 return {
135 text: `👷 <b>Jobهای ${escapeHtml(run.name)} — ${escapeHtml(repo.name)}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`,
136 reply_markup: {
137 inline_keyboard: [[
138 { text: '🔄 بروزرسانی', callback_data: `jobs:${repoId}:${runId}` },
139 { text: '🔙 Run', callback_data: `run:${repoId}:${runId}` }
140 ]]
141 }
142 };
143 }
144
145 function workflowActionResultView(repoId, runId, action) {
146 const rerun = action === 'rerun_failed';
147 return {
148 text: rerun
149 ? '✅ درخواست اجرای مجدد jobهای ناموفق برای GitHub ارسال شد.'
150 : '✅ درخواست لغو Workflow برای GitHub ارسال شد.',
151 reply_markup: {
152 inline_keyboard: [[
153 { text: '🧪 مشاهده Run', callback_data: `run:${repoId}:${runId}` },
154 { text: '🔙 CI / Actions', callback_data: `runs:${repoId}` }
155 ]]
156 }
157 };
158 }
159
160 function resolveReadCallback(data) {
161 const value = String(data || '');
162 if (value === 'menu:repos') return { actionId: 'github.repos.list', kind: 'legacy' };
163
164 const repoId = numericSingle(value, 'repo');
165 if (repoId !== null) return { actionId: 'github.repo.detail', kind: 'repo', repoId };
166
167 const runs = workflowRunsPage(value);
168 if (runs) return { actionId: 'github.workflow.runs.list', kind: 'runs', ...runs };
169
170 const run = numericPair(value, 'run');
171 if (run) return { actionId: 'github.workflow.run.detail', kind: 'run', ...run };
172
173 const jobs = numericPair(value, 'jobs');
174 if (jobs) return { actionId: 'github.workflow.jobs.list', kind: 'jobs', ...jobs };
175
176 for (const [prefix, actionId] of CALLBACK_ACTIONS) {
177 if (value.startsWith(prefix)) return { actionId, kind: 'legacy' };
178 }
179 return null;
180 }
181
182 function resolveWriteCallback(data) {
183 const rerun = numericPair(data, 'rerun');
184 if (rerun) {
185 return {
186 actionId: 'github.workflow.run.rerun_failed',
187 approval: {
188 input: rerun,
189 label: 'اجرای مجدد jobهای ناموفق'
190 },
191 execute: async github => {
192 await github.rerunFailedWorkflowRun(rerun.repoId, rerun.runId);
193 return workflowActionResultView(rerun.repoId, rerun.runId, 'rerun_failed');
194 }
195 };
196 }
197
198 const cancel = numericPair(data, 'cancelrun');
199 if (cancel) {
200 return {
201 actionId: 'github.workflow.run.cancel',
202 approval: {
203 input: cancel,
204 label: 'لغو Workflow در حال اجرا'
205 },
206 execute: async github => {
207 await github.cancelWorkflowRun(cancel.repoId, cancel.runId);
208 return workflowActionResultView(cancel.repoId, cancel.runId, 'cancel');
209 }
210 };
211 }
212 return null;
213 }
214
215 export function createGitHubPlugin({ github, menu } = {}) {
216 if (!github) throw new Error('GitHub plugin requires github service');
217 if (!menu) throw new Error('GitHub plugin requires menu manager');
218
219 return {
220 manifest: githubPluginManifest,
221
222 resolveCommand(command) {
223 if (command !== '/repos') return null;
224 return {
225 actionId: 'github.repos.list',
226 execute: () => menu.getReposListView()
227 };
228 },
229
230 resolveCallback(data) {
231 const write = resolveWriteCallback(data);
232 if (write) {
233 return {
234 actionId: write.actionId,
235 approval: write.approval,
236 execute: () => write.execute(github)
237 };
238 }
239
240 const read = resolveReadCallback(data);
241 if (!read) return null;
242
243 return {
244 actionId: read.actionId,
245 execute: async () => {
246 if (read.kind === 'repo') {
247 return addCiButton(await menu.renderByCallback(data), read.repoId);
248 }
249 if (read.kind === 'runs') return workflowRunsView(github, read.repoId, read.page);
250 if (read.kind === 'run') return workflowRunView(github, read.repoId, read.runId);
251 if (read.kind === 'jobs') return workflowJobsView(github, read.repoId, read.runId);
252 return menu.renderByCallback(data);
253 }
254 };
255 }
256 };
257 }