Personal Telegram developer console for GitHub

feat: add GitHub workflow operator views and routes

davoodmehraban89 committed Oct 3, 2026 at 05:46 UTC e1171adc86ad291e99f5e7e068cea0dd6d092943
1 file changed +199 -8
src/plugins/github/plugin.js
+199 -8
@@ -1,7 +1,6 @@
1 import { githubPluginManifest } from './manifest.js';
2
3 const CALLBACK_ACTIONS = Object.freeze([
4 - ['repo:', 'github.repo.detail'],
4 ['files:', 'github.files.list'],
5 ['tree:', 'github.tree.read'],
6 ['blob:', 'github.blob.read'],
@@ -10,14 +9,189 @@ const CALLBACK_ACTIONS = Object.freeze([
9 ['status:', 'github.status.read']
10 ]);
11
13 -function resolveCallbackAction(data) {
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 || '');
15 - if (value === 'menu:repos') return 'github.repos.list';
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) {
18 - if (value.startsWith(prefix)) return actionId;
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
@@ -37,12 +211,29 @@ export function createGitHubPlugin({ github, menu } = {}) {
211 },
212
213 resolveCallback(data) {
40 - const actionId = resolveCallbackAction(data);
41 - if (!actionId) return null;
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 {
44 - actionId,
45 - execute: () => menu.renderByCallback(data)
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 };