15m44s 工单 GitHub 三项:token继承+label打标+webhook联动关单

需求1(token继承): 玩家授权过且 access_token 仍有效→gh_oauth.php 发起授权前
  先查 gh_auth_tokens 调 /user 验活,有效则 302 回 player.php?gh_inherit=1 直接继承
  不弹 GitHub;真失效才重新走 OAuth。不再一股脑报'无效要重新授权'。

需求2(label打标): 撤单/用户回/管理回/驳回/完结 5 个动作调 ghSyncLabel,按
  img.json repo.tag 映射(正常关单→已完成/驳回→已驳回/已回复/撤销)给对应 issue
  打 label,并摘掉互斥状态类 label。gh_label_map 可配置,缺省中文兜底。

需求3(webhook联动关单): ghHandleWebhook issue 关闭事件按 state_reason 联动 PHP:
  completed(done)→已完结;not_planned→已驳回;缺省 done。

附件占位符 [图片N] 未替换根因=历史单(OPcache 跑旧编译)+漏清 OPcache,本轮部署
后会清 OPcache,新建单正常替换。
This commit is contained in:
ypsdf1
2026-11-25 15:06:11 +08:00
parent 308f76b014
commit 08f85284ee
5 changed files with 164 additions and 17 deletions
+74 -3
View File
@@ -250,6 +250,69 @@ function ghSyncClose($ticketId, $kind, $token, $sender = 'admin') {
return $r['ok'];
}
/**
* 15m44s 新功能:按工单动作给对应 GitHub issue 打 label(从 repo 实际 label 列表取,保证名字存在)。
* $action: 'withdraw'→已撤销 | 'user_reply'→已回复 | 'admin_reply'→处理中
* | 'rejected'→已驳回 | 'completed'→已完成
* 逻辑:拉 /labels 拿 repo 现有 label;命中目标中文名则加;同时把互斥的状态类 label 摘掉
* (例如完结时摘掉 已驳回/已撤销/已回复/处理中,避免一个 issue 挂着多个终态)。
* 非阻塞:失败只 ghLog 不影响主流程。返回 bool(是否成功打上目标 label)。
*/
function ghSyncLabel($ticketId, $action, $token) {
$cfg = ghGetCfg();
if (!$cfg['gh_enabled']) return false;
if ($token === '') { ghLog('label skipped: no token action=' . $action . ' ticket=' . $ticketId); return false; }
$issueNo = ghTicketIssueNumber($ticketId);
if ($issueNo === '') { ghLog('label skipped: empty issue action=' . $action); return false; }
$repo = $cfg['gh_repo'];
// ★ 15m44s: 目标 label 名从 img.json 的 gh_label_map 读(repo.tag 可配),动作→中文 label 名。
// $action: 'withdraw'→撤销 | 'user_reply'→已回复 | 'admin_reply'→处理中
// | 'rejected'→驳回 | 'completed'→正常关单
$map = (isset($cfg['gh_label_map']) && is_array($cfg['gh_label_map'])) ? $cfg['gh_label_map'] : [];
$actionKeyMap = [
'withdraw' => '撤销',
'user_reply' => '已回复',
'admin_reply' => '处理中',
'rejected' => '驳回',
'completed' => '正常关单',
];
$key = isset($actionKeyMap[$action]) ? $actionKeyMap[$action] : '';
$target = (isset($map[$key]) && $map[$key] !== '') ? $map[$key] : '';
if ($target === '') {
// 未配置该动作映射 → 用默认中文 label 名兜底
$defaultMap = ['撤销'=>'已撤销','已回复'=>'已回复','处理中'=>'处理中','驳回'=>'已驳回','正常关单'=>'已完成'];
$target = isset($defaultMap[$key]) ? $defaultMap[$key] : '';
}
if ($target === '') return false;
// 互斥组:状态类 label(打某个时摘掉同组其它),非状态类(如 申诉状)保留
$stateLabels = ['待处理', '处理中', '已回复', '已完成', '已驳回', '已撤销'];
// 拉 repo 现有 label,确认目标 label 存在(不存在则跳过,避免 422)
$lr = ghApiRequest('GET', '/repos/' . $repo . '/labels', null, $token);
$existingNames = [];
if (!empty($lr['ok']) && is_array($lr['data'])) {
foreach ($lr['data'] as $lb) { if (isset($lb['name'])) $existingNames[] = (string)$lb['name']; }
}
if (!in_array($target, $existingNames, true)) {
ghLog('label skipped: target "' . $target . '" not in repo labels action=' . $action);
return false;
}
// 1) 摘掉同组其它 label(先摘后加)
foreach ($stateLabels as $sl) {
if ($sl === $target) continue;
if (in_array($sl, $existingNames, true)) {
ghApiRequest('DELETE', '/repos/' . $repo . '/issues/' . $issueNo . '/labels/' . rawurlencode($sl), null, $token);
}
}
// 2) 加目标 label
$r = ghApiRequest('POST', '/repos/' . $repo . '/issues/' . $issueNo . '/labels', ['labels' => [$target]], $token);
if (!$r['ok']) {
ghLog('label add failed action=' . $action . ' target=' . $target . ': ' . $r['msg']);
} else {
ghLog('label ok action=' . $action . ' target=' . $target . ' issue=' . $issueNo);
}
return $r['ok'];
}
/**
* 处理 GitHub Webhook 事件(gh_webhook.php 调用)。
* 只关心 issue 的 评论 / 关闭 / 重新打开;识别 [工单#id] 前缀定位 PHP 工单,回推 web_ticket_replies。
@@ -316,20 +379,28 @@ function ghHandleWebhook($payload, $event) {
return true;
}
// issue 关闭事件:GitHub 侧关闭 issue → PHP 标记 completed(若仍开放)
// issue 关闭事件:GitHub 侧关闭 issue → 按 state_reason 联动 PHP 状态
// 15m44s 新功能:closed-done(completed) → PHP 已完结;closed-not planned(not_planned) → PHP 已驳回。
if ($event === 'issues' && isset($issue['state']) && $issue['state'] === 'closed') {
$reason = isset($issue['state_reason']) ? (string)$issue['state_reason'] : '';
// 用户定调:done(completed)=已完结;not planned(not_planned)=已驳回;其它/无 reason 默认已完结
$phpStatus = ($reason === 'not_planned') ? 'rejected' : 'completed';
$now = time();
$closeMsg = '🔒 GitHub issue #' . $issueNo . ' 已关闭';
$closeMsg = ($phpStatus === 'rejected')
? '🔒 GitHub issue #' . $issueNo . ' 已关闭(not planned)→ 工单标记为已驳回'
: '🔒 GitHub issue #' . $issueNo . ' 已关闭(done)→ 工单标记为已完结';
try {
$ins = $db->prepare("INSERT INTO web_ticket_replies (ticket_id, sender, role, message, created_at) VALUES (:tid, 'gh:issue', 'admin', :msg, :now)");
$ins->bindValue(':tid', $ticketId, SQLITE3_INTEGER);
$ins->bindValue(':msg', $closeMsg, SQLITE3_TEXT);
$ins->bindValue(':now', $now, SQLITE3_INTEGER);
$ins->execute();
$upd = $db->prepare("UPDATE web_tickets SET status = 'completed', updated_at = :now WHERE id = :id");
$upd = $db->prepare("UPDATE web_tickets SET status = :status, updated_at = :now WHERE id = :id");
$upd->bindValue(':status', $phpStatus, SQLITE3_TEXT);
$upd->bindValue(':now', $now, SQLITE3_INTEGER);
$upd->bindValue(':id', $ticketId, SQLITE3_INTEGER);
$upd->execute();
ghLog('webhook issue#'. $issueNo . ' closed reason=' . $reason . ' → php status=' . $phpStatus . ' ticket=' . $ticketId);
} catch (\Throwable $e) {
ghLog('webhook close issue: ' . $e->getMessage());
return false;