Personal Telegram developer console for GitHub

feat/studio-g0-foundation
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 195 lines 7.49 KB
Raw
1 const WORKSPACE_CALLBACK =
2 /^(?:ws|wfile|wtext|wmsg|wdiff|wready|wcancel):([A-Za-z0-9_-]{10})$/;
3 const WORKSPACE_ID = /^[A-Za-z0-9_-]{10}$/;
4 const INTENT_ID = /^[A-Za-z0-9_-]{8}$/;
5
6 const escapeHtml = (value) =>
7 String(value ?? '')
8 .replaceAll('&', '&')
9 .replaceAll('<', '&lt;')
10 .replaceAll('>', '&gt;')
11 .replaceAll('"', '&quot;');
12
13 function legacyFailureView(data, error) {
14 const reason = String(error?.message || 'Provider read failed');
15 const permissionDenied =
16 /403|resource not accessible by personal access token/i.test(reason);
17 const repoMatch = String(data || '').match(/^[^:]+:(\d+)/);
18 const fallback = repoMatch ? `repo:${repoMatch[1]}` : 'menu:main';
19 const isIssues = String(data || '').startsWith('issues:');
20 const detail = permissionDenied
21 ? isIssues
22 ? 'GitHub درخواست Issues را با خطای 403 رد کرد. در Fine-grained token، مجوز Repository permissions → Issues را روی Read قرار دهید.'
23 : 'GitHub این درخواست خواندنی را با خطای 403 رد کرد. مجوز Read مربوط به این بخش در توکن فعلی کافی نیست.'
24 : 'دریافت اطلاعات از سرویس با خطا مواجه شد. هیچ تغییری انجام نشد.';
25
26 return {
27 text: `⚠️ <b>دریافت اطلاعات با خطا مواجه شد</b>
28 ➖➖➖➖➖➖➖➖➖➖
29 ${detail}
30
31 این خطا هیچ عملیات تغییردهنده‌ای اجرا نکرد.`,
32 reply_markup: {
33 inline_keyboard: [
34 [
35 {
36 text: repoMatch ? '↩️ بازگشت به پروژه' : '↩️ منوی اصلی',
37 callback_data: fallback
38 }
39 ]
40 ]
41 },
42 validationError: false
43 };
44 }
45
46 export async function buildCallbackFailureView({
47 data,
48 error,
49 ownerId,
50 workspaces
51 } = {}) {
52 const callback = String(data || '');
53 const reason = String(error?.message || 'Provider read failed');
54 const match = callback.match(WORKSPACE_CALLBACK);
55 if (!match) return legacyFailureView(callback, error);
56
57 const workspaceId = match[1];
58 let workspace = null;
59 if (
60 ownerId !== undefined &&
61 ownerId !== null &&
62 typeof workspaces?.getWorkspace === 'function'
63 ) {
64 try {
65 workspace = await workspaces.getWorkspace({
66 ownerId: String(ownerId),
67 workspaceId
68 });
69 } catch {
70 // Recovery remains available even when durable context lookup itself fails.
71 }
72 }
73
74 const repoId = Number(workspace?.repoId);
75 const hasRepo = Number.isSafeInteger(repoId);
76 const missingCommitMessage = /^Commit message is required$/i.test(reason);
77 const longCommitMessage =
78 /^Commit message must be 120 characters or fewer$/i.test(reason);
79 const noDraftChange = /^No draft change to commit$/i.test(reason);
80 const validationError =
81 missingCommitMessage || longCommitMessage || noDraftChange;
82
83 let title = '⚠️ <b>عملیات Workspace متوقف شد</b>';
84 let detail = workspace
85 ? 'Workspace و Draft فعلی محفوظ هستند. می‌توانید دوباره تلاش کنید یا به Draft برگردید.'
86 : 'عملیات متوقف شد. برای بازیابی وضعیت، دوباره تلاش کنید یا به منوی اصلی برگردید.';
87 if (missingCommitMessage) {
88 title = '✍️ <b>پیام Commit لازم است</b>';
89 detail =
90 'قبل از «آماده Commit»، یک پیام Commit ثبت کنید. هیچ تغییری در GitHub انجام نشده است.';
91 } else if (longCommitMessage) {
92 title = '✍️ <b>پیام Commit بیش از حد طولانی است</b>';
93 detail =
94 'پیام Commit باید حداکثر 120 کاراکتر باشد. هیچ تغییری در GitHub انجام نشده است.';
95 } else if (noDraftChange) {
96 title = '📝 <b>تغییری برای Commit وجود ندارد</b>';
97 detail =
98 'محتوای Draft با نسخهٔ اصلی یکسان است. ابتدا Draft را تغییر دهید؛ هیچ تغییری در GitHub انجام نشده است.';
99 }
100
101 const rows = [];
102 if (missingCommitMessage || longCommitMessage) {
103 rows.push([
104 { text: '✍️ ثبت پیام Commit', callback_data: `wmsg:${workspaceId}` }
105 ]);
106 }
107 rows.push([{ text: '🔄 تلاش مجدد', callback_data: callback }]);
108 rows.push([{ text: '📝 ادامه Draft', callback_data: `ws:${workspaceId}` }]);
109 if (hasRepo)
110 rows.push([
111 { text: '📦 بازگشت به پروژه', callback_data: `repo:${repoId}` }
112 ]);
113 rows.push([{ text: '🏠 منوی اصلی', callback_data: 'menu:main' }]);
114
115 return {
116 text: `${title}\n➖➖➖➖➖➖➖➖➖➖\n${detail}`,
117 reply_markup: { inline_keyboard: rows },
118 validationError
119 };
120 }
121
122 export async function buildApprovedActionFailedView({
123 payload,
124 error,
125 ownerId,
126 workspaces
127 } = {}) {
128 const reason = String(error?.message || 'GitHub operation failed');
129 const permissionDenied =
130 /403|resource not accessible by personal access token/i.test(reason);
131 const repoId = Number(payload?.input?.repoId);
132 const runId = Number(payload?.input?.runId);
133 const workspaceAction =
134 payload?.actionId === 'github.workspace.commit_branch';
135 const intentId = String(payload?.input?.intentId || '');
136
137 let workspaceId = '';
138 if (
139 workspaceAction &&
140 INTENT_ID.test(intentId) &&
141 ownerId !== undefined &&
142 ownerId !== null &&
143 typeof workspaces?.getIntent === 'function'
144 ) {
145 try {
146 const intent = await workspaces.getIntent({
147 ownerId: String(ownerId),
148 intentId
149 });
150 const candidate = String(intent?.workspaceId || '');
151 if (WORKSPACE_ID.test(candidate)) workspaceId = candidate;
152 } catch {
153 // Provider failure feedback must still render even if durable recovery lookup fails.
154 }
155 }
156
157 const detail = workspaceAction
158 ? permissionDenied
159 ? 'تأیید شما معتبر بود، اما GitHub درخواست Commit شاخه را با خطای 403 رد کرد. دسترسی Contents WRITE توکن فعلی را بررسی کنید.'
160 : `تأیید شما معتبر بود، اما Commit شاخه انجام نشد: ${escapeHtml(reason)}`
161 : permissionDenied
162 ? 'تأیید شما معتبر بود، اما GitHub درخواست را با خطای 403 رد کرد. دسترسی توکن فعلی برای GitHub Actions WRITE کافی نیست.'
163 : `تأیید شما معتبر بود، اما GitHub عملیات را موفق اعلام نکرد: ${escapeHtml(reason)}`;
164
165 const rows = [];
166 if (workspaceAction && workspaceId) {
167 rows.push([
168 { text: '🔄 درخواست تأیید تازه', callback_data: `wready:${workspaceId}` }
169 ]);
170 rows.push([{ text: '📝 ادامه Draft', callback_data: `ws:${workspaceId}` }]);
171 } else if (
172 !workspaceAction &&
173 Number.isSafeInteger(repoId) &&
174 Number.isSafeInteger(runId)
175 ) {
176 rows.push([
177 { text: '↩️ بازگشت به Run', callback_data: `run:${repoId}:${runId}` }
178 ]);
179 }
180 if (Number.isSafeInteger(repoId)) {
181 rows.push([
182 { text: '📦 بازگشت به پروژه', callback_data: `repo:${repoId}` }
183 ]);
184 }
185 rows.push([{ text: '🏠 منوی اصلی', callback_data: 'menu:main' }]);
186
187 return {
188 text: `⚠️ <b>اجرای عملیات در GitHub ناموفق بود</b>
189 ➖➖➖➖➖➖➖➖➖➖
190 ${detail}
191
192 این تأیید یک‌بار مصرف شده است؛ برای تلاش مجدد باید تأیید تازه ایجاد شود.`,
193 reply_markup: { inline_keyboard: rows }
194 };
195 }