Personal Telegram developer console for GitHub

fix(cp3): repair advanced write callback wrapper

davoodmehraban89 committed Oct 5, 2026 at 06:00 UTC af3f24c310fb655468c1ccf8c7beb9867e795761
1 file changed +161 -30
src/plugins/github/plugin.js
+161 -30
@@ -1,36 +1,167 @@
1 import { createGitHubPlugin as createLegacyGitHubPlugin } from './plugin-legacy.js';
2 -const FULL_SHA = /^[a-f0-9]{40}$/i;
3 -const from36 = value => /^[0-9a-z]+$/i.test(String(value || '')) ? Number.parseInt(value, 36) : NaN;
4 -const mergeRef = value => { const m = String(value || '').match(/^mergepr:([0-9a-z]+):([0-9a-z]+):([a-f0-9]{40})$/i); if (!m) return null; const repoId = from36(m[1]); const prNumber = from36(m[2]); return Number.isSafeInteger(repoId) && Number.isSafeInteger(prNumber) && prNumber > 0 ? { repoId, prNumber, headSha: m[3].toLowerCase() } : null; };
2
6 -const cp3Pair = (value, prefix) => { const m = String(value || '').match(new RegExp(`^${prefix}:(\\d+):(\\d+)import { createGitHubPlugin as createLegacyGitHubPlugin } from './plugin-legacy.js';
3 const FULL_SHA = /^[a-f0-9]{40}$/i;
4 const from36 = value => /^[0-9a-z]+$/i.test(String(value || '')) ? Number.parseInt(value, 36) : NaN;
9 -)); return m ? { repoId: Number(m[1]), runId: Number(m[2]) } : null; };
10 -const appendBeforeBack = (view, row) => { const rows = [...(view?.reply_markup?.inline_keyboard || [])]; rows.splice(Math.max(0, rows.length - 1), 0, row); return { ...view, reply_markup: { ...(view.reply_markup || {}), inline_keyboard: rows } }; };
5 +const mergeRef = value => {
6 + const m = String(value || '').match(/^mergepr:([0-9a-z]+):([0-9a-z]+):([a-f0-9]{40})$/i);
7 + if (!m) return null;
8 + const repoId = from36(m[1]);
9 + const prNumber = from36(m[2]);
10 + return Number.isSafeInteger(repoId) && Number.isSafeInteger(prNumber) && prNumber > 0
11 + ? { repoId, prNumber, headSha: m[3].toLowerCase() }
12 + : null;
13 +};
14 +
15 +const cp3Pair = (value, prefix) => {
16 + const m = String(value || '').match(new RegExp('^' + prefix + ':(\\d+):(\\d+)$'));
17 + return m ? { repoId: Number(m[1]), runId: Number(m[2]) } : null;
18 +};
19 +
20 +const appendBeforeBack = (view, row) => {
21 + const rows = [...(view?.reply_markup?.inline_keyboard || [])];
22 + rows.splice(Math.max(0, rows.length - 1), 0, row);
23 + return { ...view, reply_markup: { ...(view.reply_markup || {}), inline_keyboard: rows } };
24 +};
25 +
26 export function createGitHubPlugin(options = {}) {
12 - const { github } = options; const base = createLegacyGitHubPlugin(options);
13 - return { ...base, resolveCallback(data, context = {}) {
14 - const dispatch = cp3Pair(data, 'dispatchwf');
15 - if (dispatch) return { actionId: 'github.workflow.dispatch', approval: { input: { repoId: dispatch.repoId, runId: dispatch.runId }, label: 'اجرای Workflow', cancelCallback: `run:${dispatch.repoId}:${dispatch.runId}` }, execute: async () => { const run = await github.getWorkflowRun(dispatch.repoId, dispatch.runId); if (!Number.isSafeInteger(Number(run.workflowId)) || !run.branch) throw new Error('Workflow dispatch target is unavailable'); await github.dispatchWorkflow(dispatch.repoId, run.workflowId, { ref: run.branch, inputs: {} }); return { text: '✅ درخواست اجرای Workflow برای GitHub ارسال شد.', reply_markup: { inline_keyboard: [[{ text: '🧪 مشاهده Run', callback_data: `run:${dispatch.repoId}:${dispatch.runId}` }]] } }; } };
16 - const rerunJob = cp3Pair(data, 'rerunjob');
17 - if (rerunJob) return { actionId: 'github.workflow.job.rerun', approval: { input: { repoId: rerunJob.repoId, runId: rerunJob.runId, jobId: rerunJob.runId }, label: 'اجرای مجدد Job', cancelCallback: `job:${rerunJob.repoId}:${rerunJob.runId}` }, execute: async () => { const job = await github.getWorkflowJob(rerunJob.repoId, rerunJob.runId); if (!['failure','cancelled'].includes(String(job.conclusion || ''))) throw new Error('Workflow job is not eligible for rerun'); await github.rerunJob(rerunJob.repoId, rerunJob.runId); return { text: '✅ درخواست اجرای مجدد Job برای GitHub ارسال شد.', reply_markup: { inline_keyboard: [[{ text: '👷 مشاهده Job', callback_data: `job:${rerunJob.repoId}:${rerunJob.runId}` }]] } }; } };
18 - const merge = mergeRef(data);
19 - if (merge) return { actionId: 'github.pull_request.merge', approval: { input: merge, label: `Merge PR #${merge.prNumber}`, cancelCallback: `prdetail:${merge.repoId}:${merge.prNumber}` }, execute: async () => {
20 - const current = await github.getPullRequest(merge.repoId, merge.prNumber);
21 - if (current.merged || current.state !== 'open' || current.draft) throw new Error('Pull request is not mergeable');
22 - if (!FULL_SHA.test(String(current.headSha || '')) || String(current.headSha).toLowerCase() !== merge.headSha) throw new Error('Pull request head changed; fresh approval is required');
23 - const result = await github.mergePullRequest(merge.repoId, merge.prNumber, { expectedHeadSha: merge.headSha, mergeMethod: 'squash' });
24 - if (!result?.merged) throw new Error(result?.message || 'GitHub did not merge the pull request');
25 - return { text: `✅ <b>PR #${merge.prNumber} با موفقیت Merge شد.</b>`, reply_markup: { inline_keyboard: [[{ text: '🔙 PRها', callback_data: `prs:${merge.repoId}` }]] } };
26 - } };
27 - const route = base.resolveCallback(data, context); if (!route || typeof route.execute !== 'function') return route;
28 - const detail = String(data || '').match(/^prdetail:(\d+):(\d+)$/);
29 - const runDetail = String(data || '').match(/^run:(\d+):(\d+)$/);
30 - const jobDetail = String(data || '').match(/^job:(\d+):(\d+)$/);
31 - if (detail) { const repoId = Number(detail[1]); const prNumber = Number(detail[2]); return { ...route, execute: async () => { const view = await route.execute(); const pr = await github.getPullRequest(repoId, prNumber); if (pr?.state !== 'open' || pr?.merged || pr?.draft || !FULL_SHA.test(String(pr?.headSha || ''))) return view; const callback = `mergepr:${repoId.toString(36)}:${prNumber.toString(36)}:${String(pr.headSha).toLowerCase()}`; if (callback.length > 64) return view; return appendBeforeBack(view, [{ text: '🔀 Merge امن (Squash)', callback_data: callback }]); } }; }
32 - if (runDetail) { const repoId = Number(runDetail[1]); const runId = Number(runDetail[2]); return { ...route, execute: async () => { const view = await route.execute(); const run = await github.getWorkflowRun(repoId, runId); const callback = `dispatchwf:${repoId}:${runId}`; if (!Number.isSafeInteger(Number(run.workflowId)) || !run.branch || callback.length > 64) return view; return appendBeforeBack(view, [{ text: '▶️ اجرای دوباره Workflow', callback_data: callback }]); } }; }
33 - if (jobDetail) { const repoId = Number(jobDetail[1]); const jobId = Number(jobDetail[2]); return { ...route, execute: async () => { const view = await route.execute(); const job = await github.getWorkflowJob(repoId, jobId); const callback = `rerunjob:${repoId}:${jobId}`; if (!['failure','cancelled'].includes(String(job.conclusion || '')) || callback.length > 64) return view; return appendBeforeBack(view, [{ text: '🔁 اجرای مجدد Job', callback_data: callback }]); } }; }
34 - return route;
35 - } };
27 + const { github } = options;
28 + const base = createLegacyGitHubPlugin(options);
29 +
30 + return {
31 + ...base,
32 + resolveCallback(data, context = {}) {
33 + const dispatch = cp3Pair(data, 'dispatchwf');
34 + if (dispatch) {
35 + return {
36 + actionId: 'github.workflow.dispatch',
37 + approval: {
38 + input: { repoId: dispatch.repoId, runId: dispatch.runId },
39 + label: 'اجرای Workflow',
40 + cancelCallback: `run:${dispatch.repoId}:${dispatch.runId}`
41 + },
42 + execute: async () => {
43 + const run = await github.getWorkflowRun(dispatch.repoId, dispatch.runId);
44 + if (!Number.isSafeInteger(Number(run.workflowId)) || !run.branch) {
45 + throw new Error('Workflow dispatch target is unavailable');
46 + }
47 + await github.dispatchWorkflow(dispatch.repoId, run.workflowId, { ref: run.branch, inputs: {} });
48 + return {
49 + text: '✅ درخواست اجرای Workflow برای GitHub ارسال شد.',
50 + reply_markup: {
51 + inline_keyboard: [[{ text: '🧪 مشاهده Run', callback_data: `run:${dispatch.repoId}:${dispatch.runId}` }]]
52 + }
53 + };
54 + }
55 + };
56 + }
57 +
58 + const rerunJob = cp3Pair(data, 'rerunjob');
59 + if (rerunJob) {
60 + return {
61 + actionId: 'github.workflow.job.rerun',
62 + approval: {
63 + input: { repoId: rerunJob.repoId, runId: rerunJob.runId, jobId: rerunJob.runId },
64 + label: 'اجرای مجدد Job',
65 + cancelCallback: `job:${rerunJob.repoId}:${rerunJob.runId}`
66 + },
67 + execute: async () => {
68 + const job = await github.getWorkflowJob(rerunJob.repoId, rerunJob.runId);
69 + if (!['failure', 'cancelled'].includes(String(job.conclusion || ''))) {
70 + throw new Error('Workflow job is not eligible for rerun');
71 + }
72 + await github.rerunJob(rerunJob.repoId, rerunJob.runId);
73 + return {
74 + text: '✅ درخواست اجرای مجدد Job برای GitHub ارسال شد.',
75 + reply_markup: {
76 + inline_keyboard: [[{ text: '👷 مشاهده Job', callback_data: `job:${rerunJob.repoId}:${rerunJob.runId}` }]]
77 + }
78 + };
79 + }
80 + };
81 + }
82 +
83 + const merge = mergeRef(data);
84 + if (merge) {
85 + return {
86 + actionId: 'github.pull_request.merge',
87 + approval: {
88 + input: merge,
89 + label: `Merge PR #${merge.prNumber}`,
90 + cancelCallback: `prdetail:${merge.repoId}:${merge.prNumber}`
91 + },
92 + execute: async () => {
93 + const current = await github.getPullRequest(merge.repoId, merge.prNumber);
94 + if (current.merged || current.state !== 'open' || current.draft) throw new Error('Pull request is not mergeable');
95 + if (!FULL_SHA.test(String(current.headSha || '')) || String(current.headSha).toLowerCase() !== merge.headSha) {
96 + throw new Error('Pull request head changed; fresh approval is required');
97 + }
98 + const result = await github.mergePullRequest(merge.repoId, merge.prNumber, {
99 + expectedHeadSha: merge.headSha,
100 + mergeMethod: 'squash'
101 + });
102 + if (!result?.merged) throw new Error(result?.message || 'GitHub did not merge the pull request');
103 + return {
104 + text: `✅ <b>PR #${merge.prNumber} با موفقیت Merge شد.</b>`,
105 + reply_markup: { inline_keyboard: [[{ text: '🔙 PRها', callback_data: `prs:${merge.repoId}` }]] }
106 + };
107 + }
108 + };
109 + }
110 +
111 + const route = base.resolveCallback(data, context);
112 + if (!route || typeof route.execute !== 'function') return route;
113 +
114 + const detail = String(data || '').match(/^prdetail:(\d+):(\d+)$/);
115 + const runDetail = String(data || '').match(/^run:(\d+):(\d+)$/);
116 + const jobDetail = String(data || '').match(/^job:(\d+):(\d+)$/);
117 +
118 + if (detail) {
119 + const repoId = Number(detail[1]);
120 + const prNumber = Number(detail[2]);
121 + return {
122 + ...route,
123 + execute: async () => {
124 + const view = await route.execute();
125 + const pr = await github.getPullRequest(repoId, prNumber);
126 + if (pr?.state !== 'open' || pr?.merged || pr?.draft || !FULL_SHA.test(String(pr?.headSha || ''))) return view;
127 + const callback = `mergepr:${repoId.toString(36)}:${prNumber.toString(36)}:${String(pr.headSha).toLowerCase()}`;
128 + if (callback.length > 64) return view;
129 + return appendBeforeBack(view, [{ text: '🔀 Merge امن (Squash)', callback_data: callback }]);
130 + }
131 + };
132 + }
133 +
134 + if (runDetail) {
135 + const repoId = Number(runDetail[1]);
136 + const runId = Number(runDetail[2]);
137 + return {
138 + ...route,
139 + execute: async () => {
140 + const view = await route.execute();
141 + const run = await github.getWorkflowRun(repoId, runId);
142 + const callback = `dispatchwf:${repoId}:${runId}`;
143 + if (!Number.isSafeInteger(Number(run.workflowId)) || !run.branch || callback.length > 64) return view;
144 + return appendBeforeBack(view, [{ text: '▶️ اجرای دوباره Workflow', callback_data: callback }]);
145 + }
146 + };
147 + }
148 +
149 + if (jobDetail) {
150 + const repoId = Number(jobDetail[1]);
151 + const jobId = Number(jobDetail[2]);
152 + return {
153 + ...route,
154 + execute: async () => {
155 + const view = await route.execute();
156 + const job = await github.getWorkflowJob(repoId, jobId);
157 + const callback = `rerunjob:${repoId}:${jobId}`;
158 + if (!['failure', 'cancelled'].includes(String(job.conclusion || '')) || callback.length > 64) return view;
159 + return appendBeforeBack(view, [{ text: '🔁 اجرای مجدد Job', callback_data: callback }]);
160 + }
161 + };
162 + }
163 +
164 + return route;
165 + }
166 + };
167 }