15m44o 双向同步补全: 管理员回复/完结/驳回同步GitHub + @gh附件渲染修复

This commit is contained in:
ypsdf1
2026-11-25 13:22:47 +08:00
parent c21b35c676
commit fccc150bcb
3 changed files with 87 additions and 7 deletions
+34
View File
@@ -172,6 +172,40 @@ function ghSyncReply($ticketId, $message, $token, $sender) {
return $r['ok'];
}
/** 取工单对应的 GitHub issue 号(无则返回 '') */
function ghTicketIssueNumber($ticketId) {
try {
$db = getDB();
$st = $db->prepare("SELECT gh_issue_number FROM web_tickets WHERE id = :id");
$st->bindValue(':id', (int)$ticketId, SQLITE3_INTEGER);
$row = $st->execute()->fetchArray(SQLITE3_ASSOC);
return ($row && !empty($row['gh_issue_number'])) ? (string)$row['gh_issue_number'] : '';
} catch (\Throwable $e) {
return '';
}
}
/**
* 15m44o: PHP 完结/驳回 → GitHub 关 issue(+ 一条状态说明评论)。
* $kind: 'completed'(完结)| 'rejected'(驳回)。带前缀防 Webhook 循环。
*/
function ghSyncClose($ticketId, $kind, $token, $sender = 'admin') {
$cfg = ghGetCfg();
if (!$cfg['gh_enabled']) return false;
if ($token === '') return false;
$issueNo = ghTicketIssueNumber($ticketId);
if ($issueNo === '') return false; // 该工单没对应 issue(未同步过)
$repo = $cfg['gh_repo'];
$closeComment = ($kind === 'completed')
? GH_TICKET_PREFIX . $ticketId . '][' . $sender . '] ✅ 工单已完结'
: GH_TICKET_PREFIX . $ticketId . '][' . $sender . '] 🚫 工单已驳回';
// 先评论(留痕)再关 issue
ghApiRequest('POST', '/repos/' . $repo . '/issues/' . $issueNo . '/comments', ['body' => $closeComment], $token);
$r = ghApiRequest('POST', '/repos/' . $repo . '/issues/' . $issueNo . '/close', ['state' => 'closed'], $token);
if (!$r['ok']) ghLog('close issue #' . $issueNo . ' failed: ' . $r['msg']);
return $r['ok'];
}
/**
* 处理 GitHub Webhook 事件(gh_webhook.php 调用)。
* 只关心 issue 的 评论 / 关闭 / 重新打开;识别 [工单#id] 前缀定位 PHP 工单,回推 web_ticket_replies。