15m44w 续5: 横条根因(gh_auth_tokens建表TEXT PK改VARCHAR)+webhook撤销联动+诊断端点

① 横条: gh_auth_tokens 建表声明 TEXT PRIMARY KEY 在 MySQL 下不合法(TEXT 不能做 PK),
   改 VARCHAR(500) 对齐线上 caoyuanphp 实际 schema(gh_cfg.php+gh_oauth.php 两处)。
   gh_cfg.php 验活加 ghLog 日志(status/ok/token前缀)便于定位断点。
② webhook 撤销联动(用户定调): issue 关闭事件 state_reason=completed/done 且 label 含「已撤销」
   → PHP 工单撤销(withdrawn)。原只分 not_planned→rejected/其它→completed, 现加 已撤销+done→withdrawn。
③ 新增 api/gh_diag.php 临时诊断端点(验活全链路逐步输出, 定位横条断点)。
This commit is contained in:
ypsdf1
2026-11-25 19:47:23 +08:00
parent fb541038d9
commit 7acbcf6198
4 changed files with 93 additions and 12 deletions
+14 -3
View File
@@ -30,18 +30,29 @@ if ($cfg['gh_enabled'] && $probeTk !== '') {
if ($probePlayer !== '') {
try {
$db = getDB();
// ★ 15m44w 续4:建表用 VARCHAR(500) 对齐线上 caoyuanphp.gh_auth_tokens 实际 schema
// (旧版 TEXT PRIMARY KEY 在 MySQL 下 TEXT 不能做 PK 报 1101; 表已存在时 IF NOT EXISTS
// 跳过不抛, 但为双模式兼容(老 SQLite 库也建 VARCHAR 无害)改 VARCHAR(500))。
$db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens (
player_name TEXT PRIMARY KEY, gh_login TEXT DEFAULT '',
player_name VARCHAR(500) PRIMARY KEY, gh_login VARCHAR(500) DEFAULT '',
access_token TEXT NOT NULL, updated_at INTEGER NOT NULL)");
$st = $db->prepare("SELECT access_token FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1");
$st = $db->prepare("SELECT access_token, gh_login FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1");
$st->bindValue(':p', $probePlayer, SQLITE3_TEXT);
$trow = $st->execute()->fetchArray(SQLITE3_ASSOC);
if ($trow && !empty($trow['access_token'])) {
// 验活:/user 加唯一 query 破 CF 缓存(同 gh_oauth.php 15m44v 修法)
$v = ghApiRequest('GET', '/user?x=nocache_' . (int)microtime(true) . mt_rand(100000, 999999), null, $trow['access_token']);
$ghAuthorized = !empty($v['ok']);
// ★ 诊断日志:验活结果写 gh_sync.log,定位横条消失断点(status/ok/token 前缀)
ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8)
. ' status=' . (int)($v['status'] ?? -1) . ' ok=' . ($v['ok'] ? 'y' : 'n'));
} else {
ghLog('cfg verify player=' . $probePlayer . ' no token row in gh_auth_tokens');
}
} catch (\Throwable $e) { $ghAuthorized = false; }
} catch (\Throwable $e) {
ghLog('cfg verify exception: ' . $e->getMessage());
$ghAuthorized = false;
}
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
/**
* 15m44w 续4 临时诊断端点:gh_cfg.php 验活全链路逐步输出,定位横条消失断点。
* 带 ?token= 跑:反解玩家名 → 查 gh_auth_tokens → 验活 /user → gh_authorized 判定。
* 每步 status/结果都打印,找出哪步断了。验完可删(不影响主流程)。
*/
while (ob_get_level() > 0) { @ob_end_clean(); }
ob_start();
header('Content-Type: application/json; charset=utf-8');
header('X-Content-Type-Options: nosniff');
header('Cache-Control: no-store');
require_once __DIR__ . '/../core.php';
require_once __DIR__ . '/../lsky.php';
require_once __DIR__ . '/../gh_api.php';
$out = [];
$probeTk = isset($_GET['token']) ? trim((string)$_GET['token']) : '';
$out['step0_token'] = ($probeTk === '') ? '(no token passed)' : ('len=' . strlen($probeTk) . ' prefix=' . substr($probeTk, 0, 6));
// 1) 反解玩家名
$pi = validateTokenSilent($probeTk);
$out['step1_validateTokenSilent'] = $pi ? ('player=' . $pi['player'] . ' purpose=' . $pi['purpose']) : 'false';
$probePlayer = ($pi && !empty($pi['player'])) ? $pi['player'] : '';
$out['step2_player'] = ($probePlayer === '') ? '(empty)' : $probePlayer;
if ($probePlayer !== '') {
// 3) 查 gh_auth_tokens
$db = getDB();
$out['step3_db_class'] = get_class($db);
try {
// 不建表(避免 TEXT PK 在 MySQL 抛),直接查表(表已存在 varchar PK)
$st = $db->prepare("SELECT access_token, gh_login, updated_at FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1");
$st->bindValue(':p', $probePlayer, SQLITE3_TEXT);
$res = $st->execute();
$trow = $res->fetchArray(SQLITE3_ASSOC);
$out['step4_token_found'] = $trow ? ('token_prefix=' . substr((string)$trow['access_token'], 0, 8) . ' gh_login=' . $trow['gh_login'] . ' updated=' . $trow['updated_at']) : '(no row)';
if ($trow && !empty($trow['access_token'])) {
// 5) 验活
$v = ghApiRequest('GET', '/user?x=nocache_' . (int)microtime(true) . mt_rand(100000, 999999), null, $trow['access_token']);
$out['step5_verify'] = ['status' => (int)($v['status'] ?? -1), 'ok' => !empty($v['ok']), 'msg' => mb_substr((string)($v['msg'] ?? ''), 0, 120)];
}
} catch (\Throwable $e) {
$out['step4_exception'] = get_class($e) . ': ' . $e->getMessage();
}
} else {
$out['step4'] = 'skipped (no player)';
}
success($out, 'diag');
+5 -4
View File
@@ -64,8 +64,8 @@ if ($code === '') {
$db = getDB();
try {
$db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens (
player_name TEXT PRIMARY KEY,
gh_login TEXT DEFAULT '',
player_name VARCHAR(500) PRIMARY KEY,
gh_login VARCHAR(500) DEFAULT '',
access_token TEXT NOT NULL,
updated_at INTEGER NOT NULL
)");
@@ -144,9 +144,10 @@ if ($code !== '') {
$upsertOk = false;
try {
// MySQL 路径(caoyuanphp.gh_auth_tokens 主键 player_name)
// ★ 15m44w 续4:建表用 VARCHAR(500) 对齐线上 caoyuanphp 实际 schema(MySQL TEXT 不能做 PK)。
$db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens (
player_name TEXT PRIMARY KEY,
gh_login TEXT DEFAULT '',
player_name VARCHAR(500) PRIMARY KEY,
gh_login VARCHAR(500) DEFAULT '',
access_token TEXT NOT NULL,
updated_at INTEGER NOT NULL
)");
+24 -5
View File
@@ -445,16 +445,35 @@ function ghHandleWebhook($payload, $event) {
return true;
}
// issue 关闭事件:GitHub 侧关闭 issue → 按 state_reason 联动 PHP 状态
// 15m44s 新功能:closed-done(completed) → PHP 已完结;closed-not planned(not_planned) → PHP 已驳回。
// issue 关闭事件:GitHub 侧关闭 issue → 按 state_reason + label 联动 PHP 状态
// 15m44s:done(completed)→已完结;not planned(not_planned)→已驳回;其它/无 reason 默认已完结
// ★ 15m44w 续4(用户定调):closed-done(state_reason=completed/done) 且 label 含「已撤销」
// → PHP 工单撤销(withdrawn)。撤销场景:PHP 撤销工单时关 issue 贴「已撤销」标,
// GitHub 侧该 issue 后续被关(done)且挂着已撤销标 → 回推 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';
// 15m44w 续4:done + 已撤销 label → withdrawn
$issueLabels = [];
if (isset($issue['labels']) && is_array($issue['labels'])) {
foreach ($issue['labels'] as $lb) {
// label 可能是对象 {name:...} 或字符串(webhook payload 通常是对象)
$nm = (is_array($lb) && isset($lb['name'])) ? (string)$lb['name'] : (string)$lb;
if ($nm !== '') $issueLabels[] = $nm;
}
}
$isWithdrawLabel = in_array('已撤销', $issueLabels, true);
if ($isWithdrawLabel && in_array($reason, ['completed', 'done', ''], true)) {
$phpStatus = 'withdrawn';
} else {
// 用户定调:done(completed)=已完结;not planned(not_planned)=已驳回;其它/无 reason 默认已完结
$phpStatus = ($reason === 'not_planned') ? 'rejected' : 'completed';
}
$now = time();
$closeMsg = ($phpStatus === 'rejected')
? '🔒 GitHub issue #' . $issueNo . ' 已关闭(not planned)→ 工单标记为已驳回'
: '🔒 GitHub issue #' . $issueNo . ' 已关闭(done)→ 工单标记为已完结';
: (($phpStatus === 'withdrawn')
? '↩️ GitHub issue #' . $issueNo . ' 已关闭(done)且标签「已撤销」→ 工单标记为已撤销'
: '🔒 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);