Personal Telegram developer console for GitHub

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