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:
@@ -26,6 +26,7 @@ if (function_exists('opcache_invalidate')) { @opcache_invalidate(__FILE__); }
|
||||
|
||||
require_once __DIR__ . '/../core.php';
|
||||
require_once __DIR__ . '/../lsky.php';
|
||||
require_once __DIR__ . '/../gh_api.php'; // 15m44s: ghApiRequest(验活 token)
|
||||
|
||||
$cfg = loadImgJson();
|
||||
if (!$cfg['gh_enabled']) {
|
||||
@@ -46,6 +47,44 @@ $ghToken = 'https://github.com/login/oauth/access_token';
|
||||
|
||||
$code = isset($_GET['code']) ? trim((string)$_GET['code']) : '';
|
||||
|
||||
// ★ 15m44s 变动:token 继承 —— 玩家已授权过且 access_token 仍有效,直接继承,不重弹授权。
|
||||
// 只有真无效(401/过期)才重新走 OAuth。避免"一股脑说无效要重新授权"。
|
||||
// 发起授权前(无 code)先查 gh_auth_tokens 里该玩家的 token,调 GitHub /user 验活:
|
||||
// - 有效 → 302 回 player.php?gh_ok=1&gh_inherit=1(前端据此显示「已授权」,不弹 GitHub)
|
||||
// - 无效/无记录 → 继续走下方 authorize 302 重新授权
|
||||
if ($code === '') {
|
||||
$probePlayer = '';
|
||||
$probeTk = isset($_GET['token']) ? trim((string)$_GET['token']) : '';
|
||||
if ($probeTk !== '') {
|
||||
$pi = validateTokenSilent($probeTk);
|
||||
if ($pi && !empty($pi['player'])) $probePlayer = $pi['player'];
|
||||
}
|
||||
if ($probePlayer === '' && isset($_GET['player'])) $probePlayer = trim((string)$_GET['player']);
|
||||
if ($probePlayer !== '') {
|
||||
$db = getDB();
|
||||
try {
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens (
|
||||
player_name TEXT PRIMARY KEY,
|
||||
gh_login TEXT DEFAULT '',
|
||||
access_token TEXT NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
)");
|
||||
$st = $db->prepare("SELECT access_token, gh_login FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' LIMIT 1");
|
||||
$st->bindValue(':p', $probePlayer, SQLITE3_TEXT);
|
||||
$trow = $st->execute()->fetchArray(SQLITE3_ASSOC);
|
||||
} catch (\Throwable $e) { $trow = null; }
|
||||
if ($trow && !empty($trow['access_token'])) {
|
||||
// 验活:GitHub /user 用该 token,200=有效 401=失效
|
||||
$v = ghApiRequest('GET', '/user', null, $trow['access_token']);
|
||||
if (!empty($v['ok'])) {
|
||||
header('Location: ' . $baseSite . '/player.php?gh_ok=1&gh_inherit=1&gh_user=' . urlencode((string)($trow['gh_login'] ?: '')));
|
||||
exit;
|
||||
}
|
||||
// token 失效 → 继续走下方 authorize 重新授权
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($code !== '') {
|
||||
// ===== 回调:换 token =====
|
||||
$player = isset($_GET['state']) ? trim((string)$_GET['state']) : '';
|
||||
|
||||
+29
-13
@@ -750,13 +750,15 @@ function ticketReply() {
|
||||
}
|
||||
|
||||
// 15m44l: 同步 GitHub issue 评论(非阻塞,未授权/未配置/无对应issue则跳过)
|
||||
$ghSyncOk = false;
|
||||
// 15m44s 新功能:用户回单 → 打「已回复」label
|
||||
$ghSyncOk = false; $ghLabel = false;
|
||||
try {
|
||||
$ghToken = ghTicketToken($player);
|
||||
$ghSyncOk = ghSyncReply($id, $message, $ghToken, $player);
|
||||
} catch (\Throwable $ignored) { $ghSyncOk = false; }
|
||||
$ghLabel = ghSyncLabel($id, 'user_reply', $ghToken);
|
||||
} catch (\Throwable $ignored) { $ghSyncOk = false; $ghLabel = false; }
|
||||
|
||||
success(['gh_synced' => (bool)$ghSyncOk], '回复成功');
|
||||
success(['gh_synced' => (bool)$ghSyncOk, 'gh_label' => (bool)$ghLabel], '回复成功');
|
||||
}
|
||||
|
||||
function ticketWithdraw() {
|
||||
@@ -786,7 +788,15 @@ function ticketWithdraw() {
|
||||
error('撤销失败');
|
||||
}
|
||||
|
||||
success(null, '工单已撤销');
|
||||
// 15m44s 新功能:撤单 → GitHub issue 打「已撤销」label + 关 issue(state_reason=not_planned)
|
||||
$ghLabel = false; $ghClosed = false;
|
||||
try {
|
||||
$tk = ghTicketToken($player);
|
||||
$ghLabel = ghSyncLabel($id, 'withdraw', $tk);
|
||||
$ghClosed = ghSyncClose($id, 'rejected', $tk, $player);
|
||||
} catch (\Throwable $ignored) {}
|
||||
|
||||
success(['gh_label' => (bool)$ghLabel, 'gh_closed' => (bool)$ghClosed], '工单已撤销');
|
||||
}
|
||||
|
||||
// ===== 管理员端 =====
|
||||
@@ -935,13 +945,15 @@ function ticketAdminReply() {
|
||||
}
|
||||
|
||||
// 15m44o: 管理员回复 → 同步 GitHub issue 评论(用提报玩家的 token;非阻塞)
|
||||
$ghSyncOk = false;
|
||||
// 15m44s 新功能:管理员回单 → 打「处理中」label
|
||||
$ghSyncOk = false; $ghLabel = false;
|
||||
try {
|
||||
$tk = ghTicketTokenByTicket($id);
|
||||
$ghSyncOk = ghSyncReply($id, $message, $tk, 'admin');
|
||||
} catch (\Throwable $ignored) { $ghSyncOk = false; }
|
||||
$ghLabel = ghSyncLabel($id, 'admin_reply', $tk);
|
||||
} catch (\Throwable $ignored) { $ghSyncOk = false; $ghLabel = false; }
|
||||
|
||||
success(['gh_synced' => (bool)$ghSyncOk], '回复成功');
|
||||
success(['gh_synced' => (bool)$ghSyncOk, 'gh_label' => (bool)$ghLabel], '回复成功');
|
||||
}
|
||||
|
||||
function ticketReject() {
|
||||
@@ -975,14 +987,16 @@ function ticketReject() {
|
||||
}
|
||||
|
||||
// 15m44o: 驳回 → 同步关 GitHub issue + 留驳回原因评论
|
||||
$ghSyncOk = false;
|
||||
// 15m44s 新功能:驳回 → 打「已驳回」label
|
||||
$ghSyncOk = false; $ghLabel = false;
|
||||
try {
|
||||
$tk = ghTicketTokenByTicket($id);
|
||||
ghSyncReply($id, '🚫 驳回原因:' . $reason, $tk, 'admin');
|
||||
$ghLabel = ghSyncLabel($id, 'rejected', $tk);
|
||||
$ghSyncOk = ghSyncClose($id, 'rejected', $tk, 'admin');
|
||||
} catch (\Throwable $ignored) { $ghSyncOk = false; }
|
||||
} catch (\Throwable $ignored) { $ghSyncOk = false; $ghLabel = false; }
|
||||
|
||||
success(['gh_synced' => (bool)$ghSyncOk], '工单已驳回');
|
||||
success(['gh_synced' => (bool)$ghSyncOk, 'gh_label' => (bool)$ghLabel], '工单已驳回');
|
||||
}
|
||||
|
||||
function ticketAssign() {
|
||||
@@ -1100,13 +1114,15 @@ function ticketAdminComplete() {
|
||||
}
|
||||
|
||||
// 15m44o: 完结 → 同步关 GitHub issue(提报玩家 token)
|
||||
$ghSyncOk = false;
|
||||
// 15m44s 新功能:完结 → 打「已完成」label
|
||||
$ghSyncOk = false; $ghLabel = false;
|
||||
try {
|
||||
$tk = ghTicketTokenByTicket($id);
|
||||
$ghLabel = ghSyncLabel($id, 'completed', $tk);
|
||||
$ghSyncOk = ghSyncClose($id, 'completed', $tk, 'admin');
|
||||
} catch (\Throwable $ignored) { $ghSyncOk = false; }
|
||||
} catch (\Throwable $ignored) { $ghSyncOk = false; $ghLabel = false; }
|
||||
|
||||
success(['gh_synced' => (bool)$ghSyncOk], '已标记完结');
|
||||
success(['gh_synced' => (bool)$ghSyncOk, 'gh_label' => (bool)$ghLabel], '已标记完结');
|
||||
}
|
||||
|
||||
// ===== 服务商端 =====
|
||||
|
||||
+74
-3
@@ -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;
|
||||
|
||||
@@ -15,6 +15,12 @@
|
||||
"public": true,
|
||||
"private": false,
|
||||
"备注": "仓库类型2选1,同时双开默认public"
|
||||
},
|
||||
"tag": {
|
||||
"正常关单": "已完成",
|
||||
"驳回": "已驳回",
|
||||
"已回复": "已回复",
|
||||
"撤销": "已撤销"
|
||||
}
|
||||
},
|
||||
"图像/附件支持": {
|
||||
|
||||
@@ -66,6 +66,14 @@ function loadImgJson() {
|
||||
'gh_client_id' => '',
|
||||
'gh_client_secret' => '',
|
||||
'gh_webhook_secret' => '',
|
||||
// 15m44s: GitHub issue label(tag)映射,动作→label 名(可在 img.json repo.tag 配置)
|
||||
'gh_label_map' => [
|
||||
'正常关单' => '已完成',
|
||||
'驳回' => '已驳回',
|
||||
'已回复' => '已回复',
|
||||
'撤销' => '已撤销',
|
||||
'处理中' => '处理中',
|
||||
],
|
||||
];
|
||||
$file = __DIR__ . '/img.json';
|
||||
if (!is_readable($file)) return $cfg;
|
||||
@@ -121,6 +129,13 @@ function loadImgJson() {
|
||||
if (isset($j['repo']['webhook_secret'])) {
|
||||
$cfg['gh_webhook_secret'] = trim((string)$j['repo']['webhook_secret']);
|
||||
}
|
||||
// 15m44s: 读 repo.tag(动作→label 名 映射),中文键值。有则覆盖默认。
|
||||
if (isset($j['repo']['tag']) && is_array($j['repo']['tag'])) {
|
||||
foreach ($j['repo']['tag'] as $k => $v) {
|
||||
$k = trim((string)$k);
|
||||
if ($k !== '') $cfg['gh_label_map'][$k] = trim((string)$v);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isset($j['repo']['webhook_secret'])) {
|
||||
// 兼容旧顶层 webhook_secret(已部署的生产 img.json 可能是旧结构)
|
||||
|
||||
Reference in New Issue
Block a user