Personal Telegram developer console for GitHub
fix/issues-permission-release-empty-ux
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
| 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('<', '<') |
| 15 | .replaceAll('>', '>') |
| 16 | .replaceAll('"', '"'); |
| 17 | |
| 18 | const short = (value, max = 80) => { |
| 19 | const text = String(value ?? ''); |
| 20 | return text.length > max ? `${text.slice(0, max - 1)}…` : text; |
| 21 | }; |
| 22 | |
| 23 | const numericPair = (value, prefix) => { |
| 24 | const match = String(value || '').match(new RegExp(`^${prefix}:(\\d+):(\\d+)$`)); |
| 25 | if (!match) return null; |
| 26 | return { repoId: Number(match[1]), runId: Number(match[2]) }; |
| 27 | }; |
| 28 | |
| 29 | const numericTriple = (value, prefix) => { |
| 30 | const match = String(value || '').match(new RegExp(`^${prefix}:(\\d+):(\\d+):(\\d+)$`)); |
| 31 | if (!match) return null; |
| 32 | return { |
| 33 | repoId: Number(match[1]), |
| 34 | itemId: Number(match[2]), |
| 35 | page: Math.max(1, Math.min(100, Number(match[3]) || 1)) |
| 36 | }; |
| 37 | }; |
| 38 | |
| 39 | const pagedPair = (value, prefix) => { |
| 40 | const match = String(value || '').match(new RegExp(`^${prefix}:(\\d+):(\\d+)(?::(\\d+))?$`)); |
| 41 | if (!match) return null; |
| 42 | return { |
| 43 | repoId: Number(match[1]), |
| 44 | itemId: Number(match[2]), |
| 45 | page: Math.max(1, Math.min(100, Number(match[3]) || 1)) |
| 46 | }; |
| 47 | }; |
| 48 | |
| 49 | const numericSingle = (value, prefix) => { |
| 50 | const match = String(value || '').match(new RegExp(`^${prefix}:(\\d+)$`)); |
| 51 | return match ? Number(match[1]) : null; |
| 52 | }; |
| 53 | |
| 54 | const pagedSingle = (value, prefix) => { |
| 55 | const match = String(value || '').match(new RegExp(`^${prefix}:(\\d+)(?::(\\d+))?$`)); |
| 56 | if (!match) return null; |
| 57 | return { |
| 58 | repoId: Number(match[1]), |
| 59 | page: Math.max(1, Math.min(100, Number(match[2]) || 1)) |
| 60 | }; |
| 61 | }; |
| 62 | |
| 63 | const compareRef = value => { |
| 64 | const match = String(value || '').match(/^compare:(\d+):([a-f0-9]{40})$/i); |
| 65 | if (!match) return null; |
| 66 | return { repoId: Number(match[1]), sha: match[2] }; |
| 67 | }; |
| 68 | |
| 69 | const workflowRunsPage = value => pagedSingle(value, 'runs'); |
| 70 | |
| 71 | const runIcon = run => { |
| 72 | if (run.conclusion === 'success') return '✅'; |
| 73 | if (run.conclusion === 'failure') return '❌'; |
| 74 | if (run.conclusion === 'cancelled') return '⏹'; |
| 75 | return run.status === 'in_progress' ? '🟡' : '⏳'; |
| 76 | }; |
| 77 | |
| 78 | const stateIcon = value => { |
| 79 | if (value === 'success' || value === 'completed') return '✅'; |
| 80 | if (value === 'failure') return '❌'; |
| 81 | if (value === 'cancelled') return '⏹'; |
| 82 | if (value === 'open') return '🟢'; |
| 83 | if (value === 'closed') return '⚫'; |
| 84 | return '•'; |
| 85 | }; |
| 86 | |
| 87 | const addRepoAdvancedButtons = (view, repoId) => { |
| 88 | const rows = [...(view?.reply_markup?.inline_keyboard || [])]; |
| 89 | const insertAt = Math.max(0, rows.length - 1); |
| 90 | rows.splice(insertAt, 0, |
| 91 | [{ text: '🧪 CI / Actions', callback_data: `runs:${repoId}` }], |
| 92 | [ |
| 93 | { text: '🌿 Branches', callback_data: `branches:${repoId}` }, |
| 94 | { text: '🔎 PRها', callback_data: `prs:${repoId}` } |
| 95 | ], |
| 96 | [ |
| 97 | { text: '🐛 Issues', callback_data: `issues:${repoId}` }, |
| 98 | { text: '🏷 Releases', callback_data: `releases:${repoId}` } |
| 99 | ] |
| 100 | ); |
| 101 | return { |
| 102 | ...view, |
| 103 | reply_markup: { ...(view?.reply_markup || {}), inline_keyboard: rows } |
| 104 | }; |
| 105 | }; |
| 106 | |
| 107 | function listPagination(prefix, repoId, page, count, pageSize) { |
| 108 | const row = []; |
| 109 | if (page > 1) row.push({ text: '⬅️ جدیدتر', callback_data: `${prefix}:${repoId}:${page - 1}` }); |
| 110 | if (count === pageSize && page < 100) row.push({ text: 'قدیمیتر ➡️', callback_data: `${prefix}:${repoId}:${page + 1}` }); |
| 111 | return row.length ? [row] : []; |
| 112 | } |
| 113 | |
| 114 | function explicitPagination(prefix, repoId, page, hasMore) { |
| 115 | const row = []; |
| 116 | if (page > 1) row.push({ text: '⬅️ جدیدتر', callback_data: `${prefix}:${repoId}:${page - 1}` }); |
| 117 | if (hasMore && page < 100) row.push({ text: 'قدیمیتر ➡️', callback_data: `${prefix}:${repoId}:${page + 1}` }); |
| 118 | return row.length ? [row] : []; |
| 119 | } |
| 120 | |
| 121 | function pairPagination(prefix, repoId, itemId, page, count, pageSize) { |
| 122 | const row = []; |
| 123 | if (page > 1) row.push({ text: '⬅️ جدیدتر', callback_data: `${prefix}:${repoId}:${itemId}:${page - 1}` }); |
| 124 | if (count === pageSize && page < 100) row.push({ text: 'قدیمیتر ➡️', callback_data: `${prefix}:${repoId}:${itemId}:${page + 1}` }); |
| 125 | return row.length ? [row] : []; |
| 126 | } |
| 127 | |
| 128 | async function workflowRunsView(github, repoId, page = 1) { |
| 129 | const [repo, runs] = await Promise.all([ |
| 130 | github.getRepository(repoId), |
| 131 | github.listWorkflowRuns(repoId, { perPage: 10, page }) |
| 132 | ]); |
| 133 | const body = runs.length |
| 134 | ? 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') |
| 135 | : '<i>Workflow runی یافت نشد.</i>'; |
| 136 | const refreshCallback = page === 1 ? `runs:${repoId}` : `runs:${repoId}:${page}`; |
| 137 | return { |
| 138 | text: `🧪 <b>CI / Actions — ${escapeHtml(repo.name)}</b>\nصفحه: <b>${page}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 139 | reply_markup: { |
| 140 | inline_keyboard: [ |
| 141 | ...runs.slice(0, 10).map(run => [{ |
| 142 | text: `${runIcon(run)} ${short(run.name, 32)} #${run.runNumber ?? run.id}`, |
| 143 | callback_data: `run:${repoId}:${run.id}` |
| 144 | }]), |
| 145 | ...listPagination('runs', repoId, page, runs.length, 10), |
| 146 | [ |
| 147 | { text: '🔄 بروزرسانی', callback_data: refreshCallback }, |
| 148 | { text: '🔙 پروژه', callback_data: `repo:${repoId}` } |
| 149 | ] |
| 150 | ] |
| 151 | } |
| 152 | }; |
| 153 | } |
| 154 | |
| 155 | async function workflowRunView(github, repoId, runId) { |
| 156 | const [repo, run] = await Promise.all([ |
| 157 | github.getRepository(repoId), |
| 158 | github.getWorkflowRun(repoId, runId) |
| 159 | ]); |
| 160 | const rows = [ |
| 161 | [ |
| 162 | { text: '👷 Jobها', callback_data: `jobs:${repoId}:${runId}` }, |
| 163 | { text: '📦 Artifacts', callback_data: `artifacts:${repoId}:${runId}` } |
| 164 | ] |
| 165 | ]; |
| 166 | if (run.conclusion === 'failure') { |
| 167 | rows.push([{ text: '🔁 اجرای مجدد jobهای ناموفق', callback_data: `rerun:${repoId}:${runId}` }]); |
| 168 | } |
| 169 | if (run.status === 'in_progress' || run.status === 'queued') { |
| 170 | rows.push([{ text: '⏹ لغو Workflow', callback_data: `cancelrun:${repoId}:${runId}` }]); |
| 171 | } |
| 172 | rows.push([ |
| 173 | { text: '🔄 بروزرسانی', callback_data: `run:${repoId}:${runId}` }, |
| 174 | { text: '🔙 CI / Actions', callback_data: `runs:${repoId}` } |
| 175 | ]); |
| 176 | return { |
| 177 | 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>`, |
| 178 | reply_markup: { inline_keyboard: rows } |
| 179 | }; |
| 180 | } |
| 181 | |
| 182 | async function workflowJobsView(github, repoId, runId) { |
| 183 | const [repo, run, jobs] = await Promise.all([ |
| 184 | github.getRepository(repoId), |
| 185 | github.getWorkflowRun(repoId, runId), |
| 186 | github.listWorkflowJobs(repoId, runId) |
| 187 | ]); |
| 188 | const body = jobs.length |
| 189 | ? jobs.slice(0, 20).map(job => { |
| 190 | const jobState = job.conclusion || job.status || 'unknown'; |
| 191 | const steps = (job.steps || []).slice(0, 30) |
| 192 | .map(step => ` ${stateIcon(step.conclusion || step.status)} ${escapeHtml(step.name)} — ${escapeHtml(step.conclusion || step.status || 'unknown')}`) |
| 193 | .join('\n'); |
| 194 | return `<b>${escapeHtml(job.name)}</b> — ${escapeHtml(jobState)}${steps ? `\n${steps}` : ''}`; |
| 195 | }).join('\n\n') |
| 196 | : '<i>Jobی در این Run یافت نشد.</i>'; |
| 197 | return { |
| 198 | text: `👷 <b>Jobهای ${escapeHtml(run.name)} — ${escapeHtml(repo.name)}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 199 | reply_markup: { |
| 200 | inline_keyboard: [ |
| 201 | ...jobs.slice(0, 20).map(job => [{ |
| 202 | text: `${stateIcon(job.conclusion || job.status)} ${short(job.name, 40)}`, |
| 203 | callback_data: `job:${repoId}:${job.id}` |
| 204 | }]), |
| 205 | [{ text: '🔄 بروزرسانی', callback_data: `jobs:${repoId}:${runId}` }, { text: '🔙 Run', callback_data: `run:${repoId}:${runId}` }] |
| 206 | ] |
| 207 | } |
| 208 | }; |
| 209 | } |
| 210 | |
| 211 | async function workflowJobView(github, repoId, jobId) { |
| 212 | const [repo, job] = await Promise.all([github.getRepository(repoId), github.getWorkflowJob(repoId, jobId)]); |
| 213 | const status = job.conclusion || job.status || 'unknown'; |
| 214 | const steps = (job.steps || []).slice(0, 30).map(step => `${stateIcon(step.conclusion || step.status)} ${escapeHtml(step.name)} — ${escapeHtml(step.conclusion || step.status || 'unknown')}`).join('\n'); |
| 215 | const rows = []; |
| 216 | if (job.conclusion === 'failure') rows.push([{ text: '📜 مشاهده log ناموفق', callback_data: `joblog:${repoId}:${jobId}` }]); |
| 217 | rows.push([{ text: '🔄 بروزرسانی', callback_data: `job:${repoId}:${jobId}` }, { text: '🔙 CI / Actions', callback_data: `runs:${repoId}` }]); |
| 218 | return { |
| 219 | text: `👷 <b>${escapeHtml(job.name)} — ${escapeHtml(repo.name)}</b>\nوضعیت: ${stateIcon(status)} <b>${escapeHtml(status)}</b>\n➖➖➖➖➖➖➖➖➖➖\n${steps || '<i>Stepی ثبت نشده.</i>'}`, |
| 220 | reply_markup: { inline_keyboard: rows } |
| 221 | }; |
| 222 | } |
| 223 | |
| 224 | async function workflowJobLogView(github, repoId, jobId) { |
| 225 | const [job, log] = await Promise.all([ |
| 226 | github.getWorkflowJob(repoId, jobId), |
| 227 | github.getWorkflowJobLog(repoId, jobId, { maxChars: 1800 }) |
| 228 | ]); |
| 229 | const escaped = escapeHtml(log.text).slice(-3200); |
| 230 | const note = log.truncated ? '⚠️ فقط بخش پایانی log نمایش داده شده است.\n\n' : ''; |
| 231 | return { |
| 232 | text: `📜 <b>Log — ${escapeHtml(job.name)}</b>\n${note}<pre>${escaped || '(empty log)'}</pre>`, |
| 233 | reply_markup: { inline_keyboard: [[{ text: '🔙 Job', callback_data: `job:${repoId}:${jobId}` }]] } |
| 234 | }; |
| 235 | } |
| 236 | |
| 237 | async function workflowArtifactsView(github, repoId, runId, page = 1) { |
| 238 | const [repo, artifacts] = await Promise.all([ |
| 239 | github.getRepository(repoId), |
| 240 | github.listWorkflowArtifacts(repoId, runId, { perPage: 10, page }) |
| 241 | ]); |
| 242 | const body = artifacts.length |
| 243 | ? artifacts.slice(0, 10).map(item => `📦 <b>${escapeHtml(item.name)}</b>\n ${item.expired ? 'منقضی شده' : 'فعال'} · ${Number(item.size || 0).toLocaleString()} bytes\n expires: ${escapeHtml(item.expiresAt || '-')}`).join('\n\n') |
| 244 | : '<i>Artifactی برای این Run یافت نشد.</i>'; |
| 245 | const refreshCallback = page === 1 ? `artifacts:${repoId}:${runId}` : `artifacts:${repoId}:${runId}:${page}`; |
| 246 | return { |
| 247 | text: `📦 <b>Artifacts — ${escapeHtml(repo.name)}</b>\nصفحه: <b>${page}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 248 | reply_markup: { |
| 249 | inline_keyboard: [ |
| 250 | ...pairPagination('artifacts', repoId, runId, page, artifacts.length, 10), |
| 251 | [{ text: '🔄 بروزرسانی', callback_data: refreshCallback }, { text: '🔙 Run', callback_data: `run:${repoId}:${runId}` }] |
| 252 | ] |
| 253 | } |
| 254 | }; |
| 255 | } |
| 256 | |
| 257 | async function branchesView(github, repoId, page = 1) { |
| 258 | const [repo, branches] = await Promise.all([ |
| 259 | github.getRepository(repoId), |
| 260 | github.listBranches(repoId, { perPage: 20, page }) |
| 261 | ]); |
| 262 | const body = branches.length |
| 263 | ? branches.slice(0, 20).map(branch => `🌿 <b>${escapeHtml(short(branch.name, 70))}</b>\n <code>${escapeHtml(String(branch.sha).slice(0, 10))}</code>${branch.protected ? ' · 🛡 protected' : ''}`).join('\n\n') |
| 264 | : '<i>Branchی یافت نشد.</i>'; |
| 265 | return { |
| 266 | text: `🌿 <b>Branches — ${escapeHtml(repo.name)}</b>\nصفحه: <b>${page}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 267 | reply_markup: { |
| 268 | inline_keyboard: [ |
| 269 | ...branches.slice(0, 20).filter(branch => /^[a-f0-9]{40}$/i.test(String(branch.sha))).map(branch => [{ text: `↔️ ${short(branch.name, 42)}`, callback_data: `compare:${repoId}:${branch.sha}` }]), |
| 270 | ...listPagination('branches', repoId, page, branches.length, 20), |
| 271 | [{ text: '🔙 پروژه', callback_data: `repo:${repoId}` }] |
| 272 | ] |
| 273 | } |
| 274 | }; |
| 275 | } |
| 276 | |
| 277 | async function compareView(github, repoId, sha) { |
| 278 | const [repo, compare] = await Promise.all([github.getRepository(repoId), github.compareWithDefault(repoId, sha)]); |
| 279 | const files = (compare.files || []).slice(0, 12).map(file => `${stateIcon(file.status)} <code>${escapeHtml(short(file.filename, 90))}</code> · +${file.additions}/-${file.deletions}`).join('\n'); |
| 280 | return { |
| 281 | text: `↔️ <b>Compare — ${escapeHtml(repo.name)}</b>\n<code>${escapeHtml(compare.base)}</code> ← <code>${escapeHtml(String(compare.head).slice(0, 10))}</code>\nوضعیت: <b>${escapeHtml(compare.status || '-')}</b>\nAhead: <b>${compare.aheadBy ?? 0}</b> · Behind: <b>${compare.behindBy ?? 0}</b> · Commits: <b>${compare.totalCommits ?? 0}</b>\n➖➖➖➖➖➖➖➖➖➖\n${files || '<i>فایل تغییریافتهای نیست.</i>'}`, |
| 282 | reply_markup: { inline_keyboard: [[{ text: '🔙 Branches', callback_data: `branches:${repoId}` }]] } |
| 283 | }; |
| 284 | } |
| 285 | |
| 286 | async function pullRequestsView(github, repoId, page = 1) { |
| 287 | const [repo, prs] = await Promise.all([github.getRepository(repoId), github.listPullRequests(repoId, { perPage: 10, page })]); |
| 288 | const body = prs.length |
| 289 | ? prs.slice(0, 10).map(pr => `${pr.merged ? '🟣' : stateIcon(pr.state)} <b>#${pr.number} ${escapeHtml(short(pr.title, 100))}</b>\n ${escapeHtml(pr.author)} · ${escapeHtml(pr.merged ? 'merged' : pr.state)}`).join('\n\n') |
| 290 | : '<i>Pull Requestی یافت نشد.</i>'; |
| 291 | return { |
| 292 | text: `🔎 <b>Pull Requests — ${escapeHtml(repo.name)}</b>\nصفحه: <b>${page}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 293 | reply_markup: { |
| 294 | inline_keyboard: [ |
| 295 | ...prs.slice(0, 10).map(pr => [{ text: `#${pr.number} ${short(pr.title, 42)}`, callback_data: `prdetail:${repoId}:${pr.number}` }]), |
| 296 | ...listPagination('prs', repoId, page, prs.length, 10), |
| 297 | [{ text: '🔙 پروژه', callback_data: `repo:${repoId}` }] |
| 298 | ] |
| 299 | } |
| 300 | }; |
| 301 | } |
| 302 | |
| 303 | async function pullRequestDetailView(github, repoId, prNumber) { |
| 304 | const [repo, pr] = await Promise.all([github.getRepository(repoId), github.getPullRequest(repoId, prNumber)]); |
| 305 | const body = escapeHtml(short(pr.body || '', 600)); |
| 306 | return { |
| 307 | text: `🔎 <b>PR #${pr.number} — ${escapeHtml(repo.name)}</b>\n<b>${escapeHtml(pr.title)}</b>\n${pr.merged ? '🟣 merged' : `${stateIcon(pr.state)} ${escapeHtml(pr.state)}`} · ${escapeHtml(pr.author)}\n<code>${escapeHtml(pr.head || '-')}</code> → <code>${escapeHtml(pr.base || '-')}</code>\n+${pr.additions}/-${pr.deletions} · ${pr.changedFiles} files\n➖➖➖➖➖➖➖➖➖➖\n${body || '<i>بدون توضیح.</i>'}`, |
| 308 | reply_markup: { |
| 309 | inline_keyboard: [ |
| 310 | [ |
| 311 | { text: '🧩 فایلها / Diff', callback_data: `prfiles:${repoId}:${prNumber}:1` }, |
| 312 | { text: '✅ Checks', callback_data: `prchecks:${repoId}:${prNumber}:1` } |
| 313 | ], |
| 314 | [{ text: '🔙 PRها', callback_data: `prs:${repoId}` }] |
| 315 | ] |
| 316 | } |
| 317 | }; |
| 318 | } |
| 319 | |
| 320 | async function pullRequestFilesView(github, repoId, prNumber, page = 1) { |
| 321 | const files = await github.listPullRequestFiles(repoId, prNumber, { perPage: 8, page }); |
| 322 | const body = files.length ? files.slice(0, 8).map(file => { |
| 323 | const patch = escapeHtml(short(file.patch || '', 220)); |
| 324 | return `🧩 <code>${escapeHtml(short(file.filename, 100))}</code> · ${escapeHtml(file.status)} · +${file.additions}/-${file.deletions}${patch ? `\n<pre>${patch}</pre>` : ''}`; |
| 325 | }).join('\n\n') : '<i>فایل تغییریافتهای یافت نشد.</i>'; |
| 326 | return { |
| 327 | text: `🧩 <b>PR #${prNumber} Files / Diff</b>\nصفحه: <b>${page}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 328 | reply_markup: { |
| 329 | inline_keyboard: [ |
| 330 | ...pairPagination('prfiles', repoId, prNumber, page, files.length, 8), |
| 331 | [{ text: '🔙 PR', callback_data: `prdetail:${repoId}:${prNumber}` }] |
| 332 | ] |
| 333 | } |
| 334 | }; |
| 335 | } |
| 336 | |
| 337 | async function pullRequestChecksView(github, repoId, prNumber, page = 1) { |
| 338 | const pr = await github.getPullRequest(repoId, prNumber); |
| 339 | const checks = await github.listCommitChecks(repoId, pr.headSha, { perPage: 20, page }); |
| 340 | const body = checks.length |
| 341 | ? checks.slice(0, 20).map(check => `${stateIcon(check.conclusion || check.status)} <b>${escapeHtml(check.name)}</b> — ${escapeHtml(check.conclusion || check.status || 'unknown')}`).join('\n') |
| 342 | : '<i>Checkی یافت نشد.</i>'; |
| 343 | return { |
| 344 | text: `✅ <b>PR #${prNumber} Checks</b>\nصفحه: <b>${page}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 345 | reply_markup: { |
| 346 | inline_keyboard: [ |
| 347 | ...pairPagination('prchecks', repoId, prNumber, page, checks.length, 20), |
| 348 | [{ text: '🔙 PR', callback_data: `prdetail:${repoId}:${prNumber}` }] |
| 349 | ] |
| 350 | } |
| 351 | }; |
| 352 | } |
| 353 | |
| 354 | async function issuesView(github, repoId, page = 1) { |
| 355 | const [repo, issuePage] = await Promise.all([ |
| 356 | github.getRepository(repoId), |
| 357 | github.listIssuesPage(repoId, { page }) |
| 358 | ]); |
| 359 | const issues = issuePage.items || []; |
| 360 | const body = issues.length |
| 361 | ? issues.slice(0, 20).map(issue => `${stateIcon(issue.state)} <b>#${issue.number} ${escapeHtml(short(issue.title, 110))}</b>\n ${escapeHtml(issue.author)} · ${issue.comments} comments`).join('\n\n') |
| 362 | : '<i>Issueی یافت نشد.</i>'; |
| 363 | return { |
| 364 | text: `🐛 <b>Issues — ${escapeHtml(repo.name)}</b>\nصفحه: <b>${page}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 365 | reply_markup: { |
| 366 | inline_keyboard: [ |
| 367 | ...explicitPagination('issues', repoId, page, !!issuePage.hasMore), |
| 368 | [{ text: '🔙 پروژه', callback_data: `repo:${repoId}` }] |
| 369 | ] |
| 370 | } |
| 371 | }; |
| 372 | } |
| 373 | |
| 374 | async function releasesView(github, repoId, page = 1) { |
| 375 | const [repo, releases] = await Promise.all([github.getRepository(repoId), github.listReleases(repoId, { perPage: 10, page })]); |
| 376 | const body = releases.length |
| 377 | ? releases.slice(0, 10).map(release => `🏷 <b>${escapeHtml(release.tag || release.name)}</b>${release.prerelease ? ' · pre-release' : ''}${release.draft ? ' · draft' : ''}\n ${escapeHtml(short(release.name, 110))}\n ${escapeHtml(release.publishedAt || '-')}`).join('\n\n') |
| 378 | : '<i>هیچ Release منتشرشدهای برای این مخزن در GitHub وجود ندارد.</i>'; |
| 379 | return { |
| 380 | text: `🏷 <b>Releases — ${escapeHtml(repo.name)}</b>\nصفحه: <b>${page}</b>\n➖➖➖➖➖➖➖➖➖➖\n${body}`, |
| 381 | reply_markup: { inline_keyboard: [...listPagination('releases', repoId, page, releases.length, 10), [{ text: '🔙 پروژه', callback_data: `repo:${repoId}` }]] } |
| 382 | }; |
| 383 | } |
| 384 | |
| 385 | function workflowActionResultView(repoId, runId, action) { |
| 386 | const rerun = action === 'rerun_failed'; |
| 387 | return { |
| 388 | text: rerun ? '✅ درخواست اجرای مجدد jobهای ناموفق برای GitHub ارسال شد.' : '✅ درخواست لغو Workflow برای GitHub ارسال شد.', |
| 389 | reply_markup: { inline_keyboard: [[{ text: '🧪 مشاهده Run', callback_data: `run:${repoId}:${runId}` }, { text: '🔙 CI / Actions', callback_data: `runs:${repoId}` }]] } |
| 390 | }; |
| 391 | } |
| 392 | |
| 393 | function resolveReadCallback(data) { |
| 394 | const value = String(data || ''); |
| 395 | if (value === 'menu:repos') return { actionId: 'github.repos.list', kind: 'legacy' }; |
| 396 | |
| 397 | const repoId = numericSingle(value, 'repo'); |
| 398 | if (repoId !== null) return { actionId: 'github.repo.detail', kind: 'repo', repoId }; |
| 399 | |
| 400 | const runs = workflowRunsPage(value); |
| 401 | if (runs) return { actionId: 'github.workflow.runs.list', kind: 'runs', ...runs }; |
| 402 | |
| 403 | const run = numericPair(value, 'run'); |
| 404 | if (run) return { actionId: 'github.workflow.run.detail', kind: 'run', ...run }; |
| 405 | |
| 406 | const jobs = numericPair(value, 'jobs'); |
| 407 | if (jobs) return { actionId: 'github.workflow.jobs.list', kind: 'jobs', ...jobs }; |
| 408 | |
| 409 | const job = numericPair(value, 'job'); |
| 410 | if (job) return { actionId: 'github.workflow.job.detail', kind: 'job', repoId: job.repoId, jobId: job.runId }; |
| 411 | |
| 412 | const joblog = numericPair(value, 'joblog'); |
| 413 | if (joblog) return { actionId: 'github.workflow.job.log', kind: 'joblog', repoId: joblog.repoId, jobId: joblog.runId }; |
| 414 | |
| 415 | const artifacts = pagedPair(value, 'artifacts'); |
| 416 | if (artifacts) return { actionId: 'github.workflow.artifacts.list', kind: 'artifacts', repoId: artifacts.repoId, runId: artifacts.itemId, page: artifacts.page }; |
| 417 | |
| 418 | const branches = pagedSingle(value, 'branches'); |
| 419 | if (branches) return { actionId: 'github.branches.list', kind: 'branches', ...branches }; |
| 420 | |
| 421 | const compare = compareRef(value); |
| 422 | if (compare) return { actionId: 'github.compare.read', kind: 'compare', ...compare }; |
| 423 | |
| 424 | const prs = pagedSingle(value, 'prs'); |
| 425 | if (prs) return { actionId: 'github.pr.advanced.list', kind: 'prs', ...prs }; |
| 426 | |
| 427 | const prdetail = numericPair(value, 'prdetail'); |
| 428 | if (prdetail) return { actionId: 'github.pr.detail', kind: 'prdetail', repoId: prdetail.repoId, prNumber: prdetail.runId }; |
| 429 | |
| 430 | const prfiles = numericTriple(value, 'prfiles'); |
| 431 | if (prfiles) return { actionId: 'github.pr.files.list', kind: 'prfiles', repoId: prfiles.repoId, prNumber: prfiles.itemId, page: prfiles.page }; |
| 432 | |
| 433 | const prchecks = numericTriple(value, 'prchecks'); |
| 434 | if (prchecks) return { actionId: 'github.pr.checks.list', kind: 'prchecks', repoId: prchecks.repoId, prNumber: prchecks.itemId, page: prchecks.page }; |
| 435 | |
| 436 | const issues = pagedSingle(value, 'issues'); |
| 437 | if (issues) return { actionId: 'github.issues.list', kind: 'issues', ...issues }; |
| 438 | |
| 439 | const releases = pagedSingle(value, 'releases'); |
| 440 | if (releases) return { actionId: 'github.releases.list', kind: 'releases', ...releases }; |
| 441 | |
| 442 | for (const [prefix, actionId] of CALLBACK_ACTIONS) { |
| 443 | if (value.startsWith(prefix)) return { actionId, kind: 'legacy' }; |
| 444 | } |
| 445 | return null; |
| 446 | } |
| 447 | |
| 448 | function resolveWriteCallback(data) { |
| 449 | const rerun = numericPair(data, 'rerun'); |
| 450 | if (rerun) { |
| 451 | return { |
| 452 | actionId: 'github.workflow.run.rerun_failed', |
| 453 | approval: { input: rerun, label: 'اجرای مجدد jobهای ناموفق' }, |
| 454 | execute: async github => { |
| 455 | await github.rerunFailedWorkflowRun(rerun.repoId, rerun.runId); |
| 456 | return workflowActionResultView(rerun.repoId, rerun.runId, 'rerun_failed'); |
| 457 | } |
| 458 | }; |
| 459 | } |
| 460 | const cancel = numericPair(data, 'cancelrun'); |
| 461 | if (cancel) { |
| 462 | return { |
| 463 | actionId: 'github.workflow.run.cancel', |
| 464 | approval: { input: cancel, label: 'لغو Workflow در حال اجرا' }, |
| 465 | execute: async github => { |
| 466 | await github.cancelWorkflowRun(cancel.repoId, cancel.runId); |
| 467 | return workflowActionResultView(cancel.repoId, cancel.runId, 'cancel'); |
| 468 | } |
| 469 | }; |
| 470 | } |
| 471 | return null; |
| 472 | } |
| 473 | |
| 474 | export function createGitHubPlugin({ github, menu } = {}) { |
| 475 | if (!github) throw new Error('GitHub plugin requires github service'); |
| 476 | if (!menu) throw new Error('GitHub plugin requires menu manager'); |
| 477 | |
| 478 | return { |
| 479 | manifest: githubPluginManifest, |
| 480 | |
| 481 | resolveCommand(command) { |
| 482 | if (command !== '/repos') return null; |
| 483 | return { actionId: 'github.repos.list', execute: () => menu.getReposListView() }; |
| 484 | }, |
| 485 | |
| 486 | resolveCallback(data) { |
| 487 | const write = resolveWriteCallback(data); |
| 488 | if (write) return { actionId: write.actionId, approval: write.approval, execute: () => write.execute(github) }; |
| 489 | |
| 490 | const read = resolveReadCallback(data); |
| 491 | if (!read) return null; |
| 492 | |
| 493 | return { |
| 494 | actionId: read.actionId, |
| 495 | execute: async () => { |
| 496 | if (read.kind === 'repo') return addRepoAdvancedButtons(await menu.renderByCallback(data), read.repoId); |
| 497 | if (read.kind === 'runs') return workflowRunsView(github, read.repoId, read.page); |
| 498 | if (read.kind === 'run') return workflowRunView(github, read.repoId, read.runId); |
| 499 | if (read.kind === 'jobs') return workflowJobsView(github, read.repoId, read.runId); |
| 500 | if (read.kind === 'job') return workflowJobView(github, read.repoId, read.jobId); |
| 501 | if (read.kind === 'joblog') return workflowJobLogView(github, read.repoId, read.jobId); |
| 502 | if (read.kind === 'artifacts') return workflowArtifactsView(github, read.repoId, read.runId, read.page); |
| 503 | if (read.kind === 'branches') return branchesView(github, read.repoId, read.page); |
| 504 | if (read.kind === 'compare') return compareView(github, read.repoId, read.sha); |
| 505 | if (read.kind === 'prs') return pullRequestsView(github, read.repoId, read.page); |
| 506 | if (read.kind === 'prdetail') return pullRequestDetailView(github, read.repoId, read.prNumber); |
| 507 | if (read.kind === 'prfiles') return pullRequestFilesView(github, read.repoId, read.prNumber, read.page); |
| 508 | if (read.kind === 'prchecks') return pullRequestChecksView(github, read.repoId, read.prNumber, read.page); |
| 509 | if (read.kind === 'issues') return issuesView(github, read.repoId, read.page); |
| 510 | if (read.kind === 'releases') return releasesView(github, read.repoId, read.page); |
| 511 | return menu.renderByCallback(data); |
| 512 | } |
| 513 | }; |
| 514 | } |
| 515 | }; |
| 516 | } |