From 684d3f4f31445a4c3cea1e1c49873fe608bce78b Mon Sep 17 00:00:00 2001 From: Jeffery Date: Fri, 21 Aug 2026 08:23:27 +0000 Subject: [PATCH] =?UTF-8?q?feat(gitea):=20gitea.sh=20=E8=AA=8D=E8=AD=89?= =?UTF-8?q?=E5=A4=B1=E6=95=97=E6=99=82=E8=87=AA=E5=8B=95=E9=80=80=E5=9B=9E?= =?UTF-8?q?=20tea=20CLI=20=E7=99=BB=E5=85=A5=20token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit What(改了什麼) - tools/gitea.sh 新增 tea_token():解析 ~/.config/tea/config.yml,優先取 default: true 的登入 token,否則取第一個。 - 啟動時 GITEA_TOKEN 未設定或為空,改用 tea token;兩者皆無才維持原本的必填錯誤。 - req() 遇 HTTP 401/403 且 tea token 存在又與當前 token 不同時,換用 tea token 重試一次,之後不再重試。 - 檔頭用法註解補上退回機制的說明。 Why(為什麼改) - 使用者規則:api token 401/403 時,若 tea CLI 已登入就取其 token 重試,避免因 GITEA_TOKEN 過期或未設定就直接中斷流程。 How(怎麼改) - 以 awk 解析 tea 設定檔的 logins 清單,抓各登入的 token 與 default 旗標。 - req() 改為 while 迴圈包住 curl,retried 旗標保證最多重試一次;HTTP >= 400 仍以 exit 4 收場。 - 實測:sh -n 語法檢查通過;GITEA_TOKEN 為空時經 tea token 成功;GITEA_TOKEN=deadbeef 時走 401 重試路徑成功;無 tea 設定且 token 為空時維持原錯誤訊息。 Who(影響哪個功能/使用者) - 所有經 tools/gitea.sh 呼叫 Gitea API 的技能(wiki、repo-sync、jsc-git pr 等)與使用者:token 缺失或失效時多一層自動救援,行為只在原本會失敗的情境改變。 Co-Authored-By: Claude Fable 5 --- tools/gitea.sh | 50 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/tools/gitea.sh b/tools/gitea.sh index a31d21b..7b73169 100755 --- a/tools/gitea.sh +++ b/tools/gitea.sh @@ -17,24 +17,52 @@ # gitea.sh repo-set / [website] # 設定 repo 描述與網頁 # gitea.sh api [json-file] # 原始 API 呼叫(path 以 /repos/... 起始) # 環境變數: GITEA_HOST(例 https://gitea.jsc.idv.tw)、GITEA_TOKEN +# GITEA_TOKEN 未設定,或請求遇 401/403 時,自動退回 tea CLI 的登入 token +# (~/.config/tea/config.yml,優先取 default: true 的登入);兩者皆無才失敗。 set -eu +tea_token() { # 取 tea CLI 設定檔的 token(優先 default: true 的登入,否則第一個) + cfg="${HOME}/.config/tea/config.yml" + [ -f "$cfg" ] || return 1 + t=$(awk ' + /^ *- / { if (def && tok != "") { print tok; found=1; exit } tok=""; def=0 } + /^ *token: */ { line=$0; sub(/^ *token: */,"",line); gsub(/"/,"",line) + tok=line; if (first=="") first=line } + /^ *default: *true/ { def=1 } + END { if (!found) { if (def && tok != "") print tok; else print first } } + ' "$cfg") + [ -n "$t" ] && printf '%s\n' "$t" +} + : "${GITEA_HOST:?GITEA_HOST is required}" -: "${GITEA_TOKEN:?GITEA_TOKEN is required}" +TEA_TOKEN=$(tea_token 2>/dev/null || true) +if [ -z "${GITEA_TOKEN:-}" ]; then + if [ -n "$TEA_TOKEN" ]; then GITEA_TOKEN="$TEA_TOKEN"; else + : "${GITEA_TOKEN:?GITEA_TOKEN is required}" + fi +fi case "$GITEA_HOST" in http://*|https://*) HOST="$GITEA_HOST" ;; *) HOST="https://$GITEA_HOST" ;; esac API="${HOST%/}/api/v1" -req() { # METHOD path [json-file] -> body(HTTP >= 400 時 exit 4) +req() { # METHOD path [json-file] -> body(HTTP >= 400 時 exit 4;401/403 以 tea token 重試一次) method="$1"; path="$2"; body_file="${3:-}" - if [ -n "$body_file" ]; then - out=$(curl -sS -w '\n%{http_code}' -X "$method" \ - -H "Authorization: token $GITEA_TOKEN" -H "Content-Type: application/json" \ - --data-binary "@$body_file" "$API$path") - else - out=$(curl -sS -w '\n%{http_code}' -X "$method" \ - -H "Authorization: token $GITEA_TOKEN" "$API$path") - fi - code=$(printf '%s' "$out" | tail -n1) + retried=0 + while :; do + if [ -n "$body_file" ]; then + out=$(curl -sS -w '\n%{http_code}' -X "$method" \ + -H "Authorization: token $GITEA_TOKEN" -H "Content-Type: application/json" \ + --data-binary "@$body_file" "$API$path") + else + out=$(curl -sS -w '\n%{http_code}' -X "$method" \ + -H "Authorization: token $GITEA_TOKEN" "$API$path") + fi + code=$(printf '%s' "$out" | tail -n1) + if [ "$retried" -eq 0 ] && { [ "$code" = 401 ] || [ "$code" = 403 ]; } \ + && [ -n "$TEA_TOKEN" ] && [ "$TEA_TOKEN" != "$GITEA_TOKEN" ]; then + GITEA_TOKEN="$TEA_TOKEN"; retried=1; continue + fi + break + done printf '%s\n' "$out" | sed '$d' [ "$code" -lt 400 ] }