15m44r gh_api 关单 PATCH 回退 + 非POST发body

- ghSyncClose: 专用 /close 路由失败时退回 canonical PATCH /repos/.../issues/N
  (state=closed + state_reason)。关单不再重复贴「工单已驳回/完结」评论
  (驳回原因评论已由 ticketReject 的 ghSyncReply 贴出)。
- ghApiRequest: 非 POST(PATCH/DELETE) 带 payload 时也要 CURLOPT_POSTFIELDS 发 JSON body,
  旧版只设 CUSTOMREQUEST 不写 body → PATCH 关单空 body 必 400(关单静默失败根因)。
- gh_sync.log 各跳过/成功/失败分支补全日志,定位关单到底卡哪步。
This commit is contained in:
ypsdf1
2026-11-25 14:32:02 +08:00
parent b557fef597
commit 308f76b014
+34 -13
View File
@@ -44,11 +44,20 @@ function ghApiRequest($method, $path, $payload = null, $token = null) {
if (strtoupper($method) === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
$json = json_encode($payload === null ? new stdClass() : $payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$headers[] = 'Content-Type: application/json';
if ($payload !== null) {
$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$headers[] = 'Content-Type: application/json';
}
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
// ★ 15m44r: 非 POST(PATCH/DELETE 等)若带 payload 也要发 JSON body,
// 旧版只设 CUSTOMREQUEST 不写 PREFEREDFIELDS → PATCH 关单空 body 必 400。
if ($payload !== null) {
$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$headers[] = 'Content-Type: application/json';
}
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -207,22 +216,34 @@ function ghTicketIssueNumber($ticketId) {
function ghSyncClose($ticketId, $kind, $token, $sender = 'admin') {
$cfg = ghGetCfg();
if (!$cfg['gh_enabled']) return false;
if ($token === '') return false;
if ($token === '') {
ghLog('close skipped: no token (未授权/无提报玩家 token) ticket=' . $ticketId);
return false;
}
$issueNo = ghTicketIssueNumber($ticketId);
if ($issueNo === '') return false; // 该工单没对应 issue(未同步过)
if ($issueNo === '') {
ghLog('close skipped: empty gh_issue_number ticket=' . $ticketId);
return false;
}
$repo = $cfg['gh_repo'];
$closeComment = ($kind === 'completed')
? GH_TICKET_PREFIX . $ticketId . '][' . $sender . '] ✅ 工单已完结'
: GH_TICKET_PREFIX . $ticketId . '][' . $sender . '] 🚫 工单已驳回';
// 先评论(留痕)
ghApiRequest('POST', '/repos/' . $repo . '/issues/' . $issueNo . '/comments', ['body' => $closeComment], $token);
// ★ 15m44p: 关 issue 带 state_reason —— 完结→completed(GitHub显示done) / 驳回→not_planned(显示not planned)
// 旧版只传 state=closed 没带 reason,GitHub 不会改成 done/not-planned 语义。
// ★ 15m44r: 关单不再重复贴「工单已驳回」评论(驳回原因评论已由 ticketReject 的
// ghSyncReply 贴出,完结场景也没有单独原因评论可贴,再贴一条纯属噪音)。
// ★ 15m44p/15m44r: 关 issue 带 state_reason —— 完结→completed(done)/驳回→not_planned。
$stateReason = ($kind === 'rejected') ? 'not_planned' : 'completed';
// 首选专用 /close 路由;若失败(部分新版 OAuth token 该子路由 404/405),
// 退回 canonical PATCH /issues/{n}(state=closed),这是 GitHub 官方推荐的主路径。
$r = ghApiRequest('POST', '/repos/' . $repo . '/issues/' . $issueNo . '/close',
['state' => 'closed', 'state_reason' => $stateReason], $token);
if (!$r['ok']) {
ghLog('close issue #' . $issueNo . ' failed: ' . $r['msg']);
$r2 = ghApiRequest('PATCH', '/repos/' . $repo . '/issues/' . $issueNo,
['state' => 'closed', 'state_reason' => $stateReason], $token);
$via = 'patch';
$r = $r2;
if (!$r2['ok']) {
ghLog('close issue #' . $issueNo . ' FAILED close=' . $r['msg'] . ' patch=' . $r2['msg']);
} else {
ghLog('close issue #' . $issueNo . ' ok via=' . $via . ' kind=' . $kind . ' reason=' . $stateReason . ' status=' . $r['status']);
}
} else {
ghLog('close issue #' . $issueNo . ' ok kind=' . $kind . ' reason=' . $stateReason . ' status=' . $r['status']);
}