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 222 lines 7.83 KB
Raw
1 import { correlateDeliveryState } from '../../core/cicd-control-plane.js';
2 import { cloudflarePluginManifest } from './manifest.js';
3
4 const escapeHtml = (value) =>
5 String(value ?? '')
6 .replaceAll('&', '&')
7 .replaceAll('<', '&lt;')
8 .replaceAll('>', '&gt;')
9 .replaceAll('"', '&quot;');
10 const mark = (value) => (value ? '✅' : '❌');
11 const VERSION_ID = /^[0-9a-f-]{36}$/i;
12
13 function unavailableView() {
14 return {
15 text: '☁️ <b>Cloudflare Worker</b>\n❌ Provider Cloudflare پیکربندی نشده است.\nهیچ وضعیت موفق ساختگی نمایش داده نمی‌شود.',
16 reply_markup: {
17 inline_keyboard: [
18 [{ text: '🔄 بررسی مجدد', callback_data: 'cf:status' }],
19 [{ text: '🔙 ابزارها', callback_data: 'menu:tools' }]
20 ]
21 }
22 };
23 }
24
25 function snapshotView(snapshot) {
26 const deployment = snapshot.deployment,
27 version = snapshot.version,
28 obs = snapshot.observability || {};
29 const traffic = deployment?.versions?.length
30 ? deployment.versions
31 .map(
32 (i) =>
33 `${escapeHtml(i.versionId || '-')} — ${Number(i.percentage || 0)}%`
34 )
35 .join('\n')
36 : 'استقرار فعالی گزارش نشده است.';
37 const bindings = snapshot.bindings?.length
38 ? snapshot.bindings
39 .slice(0, 20)
40 .map(
41 (i) =>
42 `• <code>${escapeHtml(i.name || '-')}</code> — ${escapeHtml(i.type || '-')}`
43 )
44 .join('\n')
45 : '<i>Bindingی گزارش نشده است.</i>';
46 return {
47 text: `☁️ <b>Cloudflare Worker — ${escapeHtml(snapshot.workerName || '-')}</b>
48 ➖➖➖➖➖➖➖➖➖➖
49 <b>Deployment</b>
50 ID: <code>${escapeHtml(deployment?.id || '-')}</code>
51 Created: <code>${escapeHtml(deployment?.createdOn || '-')}</code>
52 Traffic:
53 ${traffic}
54
55 <b>Version</b>
56 ID: <code>${escapeHtml(version?.id || '-')}</code>
57
58 <b>Bindings</b>
59 ${bindings}
60
61 <b>Observability</b>
62 • Observability: ${mark(obs.enabled)}
63 • Logs: ${mark(obs.logsEnabled)}
64 • Invocation logs: ${mark(obs.invocationLogs)}
65 • Traces: ${mark(obs.tracesEnabled)}
66
67 Secret values نمایش داده نمی‌شوند.`,
68 reply_markup: {
69 inline_keyboard: [
70 [{ text: '🚦 CI/CD Control Plane', callback_data: 'cf:delivery' }],
71 [{ text: '📚 نسخه‌ها / Rollback', callback_data: 'cf:versions' }],
72 [{ text: '🔄 بروزرسانی', callback_data: 'cf:status' }],
73 [{ text: '🔙 ابزارها', callback_data: 'menu:tools' }]
74 ]
75 }
76 };
77 }
78
79 function versionsView(versions = []) {
80 const rows = versions
81 .slice(0, 10)
82 .filter((v) => VERSION_ID.test(v.id))
83 .map((v) => [
84 {
85 text: `🚀 ${v.id.slice(0, 8)} · ${String(v.createdOn || '').slice(0, 10)}`,
86 callback_data: `cfdeploy:${v.id}`
87 }
88 ]);
89 return {
90 text: `☁️ <b>Cloudflare Versions</b>
91 هر Deploy/Rollback یک عملیات DESTRUCTIVE با Safe Approval است.
92 ➖➖➖➖➖➖➖➖➖➖
93 ${
94 versions
95 .slice(0, 10)
96 .map(
97 (v) =>
98 `• <code>${escapeHtml(v.id)}</code>\n ${escapeHtml(v.createdOn || '-')} · ${escapeHtml(v.triggeredBy || '-')}`
99 )
100 .join('\n\n') || '<i>نسخه‌ای گزارش نشده است.</i>'
101 }`,
102 reply_markup: {
103 inline_keyboard: [
104 ...rows,
105 [{ text: '🔄 بروزرسانی', callback_data: 'cf:versions' }],
106 [{ text: '🔙 Cloudflare', callback_data: 'cf:status' }]
107 ]
108 }
109 };
110 }
111
112 export function createCloudflarePlugin({ cloudflare, github } = {}) {
113 return {
114 manifest: cloudflarePluginManifest,
115 resolveCallback(data) {
116 const value = String(data || '');
117 if (value === 'cf:status')
118 return {
119 actionId: 'cloudflare.worker.status',
120 kind: 'worker-status',
121 async execute() {
122 if (!cloudflare?.isConfigured?.()) return unavailableView();
123 return snapshotView(await cloudflare.getWorkerSnapshot());
124 }
125 };
126 if (value === 'cf:versions')
127 return {
128 actionId: 'cloudflare.worker.versions',
129 kind: 'versions',
130 async execute() {
131 if (!cloudflare?.isConfigured?.()) return unavailableView();
132 return versionsView(await cloudflare.listVersions());
133 }
134 };
135 if (value === 'cf:delivery')
136 return {
137 actionId: 'cloudflare.delivery.status',
138 kind: 'delivery-status',
139 async execute() {
140 if (!cloudflare?.isConfigured?.() || !github)
141 return {
142 text: '🚦 <b>Delivery Control Plane</b>\n❌ Provider evidence is not configured.',
143 reply_markup: {
144 inline_keyboard: [
145 [{ text: '🔙 Cloudflare', callback_data: 'cf:status' }]
146 ]
147 }
148 };
149 const repos = await github.listRepositories();
150 const repo =
151 repos.find((item) => item.name === cloudflare.workerName) ||
152 repos[0];
153 if (!repo)
154 throw new Error('GitHub repository evidence is unavailable');
155 const [commits, runs, runtime] = await Promise.all([
156 github.listCommits(repo.id),
157 github.listWorkflowRuns(repo.id, { perPage: 20, page: 1 }),
158 cloudflare.getWorkerSnapshot()
159 ]);
160 const targetSha = String(commits[0]?.fullSha || '');
161 const ci =
162 runs.find(
163 (run) =>
164 run.name === 'CI' &&
165 run.branch === (repo.defaultBranch || 'main') &&
166 run.fullSha === targetSha
167 ) || null;
168 const deploy =
169 runs.find(
170 (run) =>
171 /Deploy Cloudflare Worker/i.test(run.name) &&
172 run.branch === (repo.defaultBranch || 'main') &&
173 run.fullSha === targetSha
174 ) || null;
175 const state = correlateDeliveryState({
176 targetSha,
177 ci: ci ? { ...ci, sha: ci.fullSha } : null,
178 deploy: deploy ? { ...deploy, sha: deploy.fullSha } : null,
179 runtime
180 });
181 const label =
182 state.state === 'success'
183 ? 'SUCCESS / موفق'
184 : state.state.toUpperCase();
185 return {
186 text: `🚦 <b>CI/CD Control Plane</b>\nState: <b>${label}</b>\nSHA: <code>${escapeHtml(targetSha.slice(0, 12) || '-')}</code>\nCI: <b>${escapeHtml(ci?.conclusion || ci?.status || 'unknown')}</b>\nDeploy: <b>${escapeHtml(deploy?.conclusion || deploy?.status || 'unknown')}</b>\nRuntime: <code>${escapeHtml(state.versionId || runtime?.deployment?.versions?.[0]?.versionId || '-')}</code>\n\n${escapeHtml(state.reason)}`,
187 reply_markup: {
188 inline_keyboard: [
189 [{ text: '🔄 بروزرسانی', callback_data: 'cf:delivery' }],
190 [{ text: '🔙 Cloudflare', callback_data: 'cf:status' }]
191 ]
192 }
193 };
194 }
195 };
196 const m = value.match(/^cfdeploy:([0-9a-f-]{36})$/i);
197 if (m) {
198 const versionId = m[1];
199 return {
200 actionId: 'cloudflare.worker.deploy_version',
201 approval: {
202 input: { versionId },
203 label: `Deploy/Rollback Cloudflare ${versionId.slice(0, 8)}`,
204 cancelCallback: 'cf:versions'
205 },
206 async execute() {
207 const result = await cloudflare.deployVersion(versionId);
208 return {
209 text: `✅ Cloudflare version <code>${escapeHtml(result.versionId)}</code> با 100% traffic تأیید شد.`,
210 reply_markup: {
211 inline_keyboard: [
212 [{ text: '☁️ وضعیت', callback_data: 'cf:status' }]
213 ]
214 }
215 };
216 }
217 };
218 }
219 return null;
220 }
221 };
222 }