diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json index b0d0b21..0de1c44 100644 --- a/.claude-plugin/plugin.json +++ b/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "jsc-gitea", - "version": "0.0.1", + "version": "0.0.2", "description": "Gitea API 工具、Wiki 讀寫與存取庫批次同步", "skills": "./skills", "author": { diff --git a/.codex-plugin/plugin.json b/.codex-plugin/plugin.json index e42cfcb..5c7f23e 100644 --- a/.codex-plugin/plugin.json +++ b/.codex-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "jsc-gitea", - "version": "0.0.1", + "version": "0.0.2", "description": "Gitea API 工具、Wiki 讀寫與存取庫批次同步", "skills": "./skills" } diff --git a/README.md b/README.md index 974c9f2..65baa07 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ gitea.sh api [json-file] ### `wiki` -Gitea wiki 頁讀寫的統一入口:依頁面類型(QUESTION / PLAN / ANALYZE / MAINTAIN / REPO / LOG)解析 wiki 所在的 `{owner}/{repo}`,未設定環境變數時以決策樹詢問。 +Gitea wiki 頁讀寫的統一入口:依頁面類型(QUESTION / PLAN / ANALYZE / MAINTAIN / REPO / LOG)解析 wiki 所在的 `{owner}/{repo}`,未設定環境變數時以決策樹詢問。頁面內容以圖表優先(mermaid 圖、markdown 表格),純文字為最後手段。 ### `repo-sync` @@ -54,7 +54,7 @@ Gitea wiki 頁讀寫的統一入口:依頁面類型(QUESTION / PLAN / ANALYZ | 變數 | 用途 | 未設定時 | | --- | --- | --- | | `GITEA_HOST` | Gitea 站台(可省略 scheme,預設 https) | 詢問使用者 | -| `GITEA_TOKEN` | Gitea API token | 詢問使用者 | +| `GITEA_TOKEN` | Gitea API token;缺少或遇 401/403 時自動退回 tea CLI 登入 token | 詢問使用者 | | `JSC_WIKI_REPO_{TYPE}` | 各類型 wiki 頁的 `{owner}/{repo}`;TYPE = QUESTION / PLAN / ANALYZE / MAINTAIN / REPO / LOG | 退回 `JSC_WIKI_REPO` | | `JSC_WIKI_REPO` | 共用預設的 wiki `{owner}/{repo}` | 詢問使用者 | diff --git a/plugin.json b/plugin.json index 0f65b1e..add943c 100644 --- a/plugin.json +++ b/plugin.json @@ -1,6 +1,6 @@ { "name": "jsc-gitea", - "version": "0.0.1", + "version": "0.0.2", "description": "Gitea API 工具、Wiki 讀寫與存取庫批次同步", "skills": "./skills/" } diff --git a/skills/wiki/SKILL.md b/skills/wiki/SKILL.md index 8e85c5a..efab159 100644 --- a/skills/wiki/SKILL.md +++ b/skills/wiki/SKILL.md @@ -1,6 +1,6 @@ --- name: wiki -description: Read or write a Gitea wiki page through tools/gitea.sh with GITEA_TOKEN. Resolve the wiki repo per page type (QUESTION/PLAN/ANALYZE/MAINTAIN/REPO/LOG) via gitea.sh wiki-repo, falling back to JSC_WIKI_REPO or a decision-tree question. Used by jsc-ask, jsc-sdlc, and jsc-log for all wiki pages; not for repo code files. +description: Read or write a Gitea wiki page through tools/gitea.sh with GITEA_TOKEN. Resolve the wiki repo per page type (QUESTION/PLAN/ANALYZE/MAINTAIN/REPO/LOG) via gitea.sh wiki-repo, falling back to JSC_WIKI_REPO or a decision-tree question. Page content is chart-first - prefer mermaid diagrams and markdown tables over plain prose. Used by jsc-ask, jsc-sdlc, and jsc-log for all wiki pages; not for repo code files. --- # wiki — read and write Gitea wiki pages @@ -27,3 +27,5 @@ Different page types can live in different `{owner}/{repo}` repos, classified by 1. Page names must follow the wiki naming table in the skill guidelines (see `jsc-meta/references/guidelines.md`). 2. To update a contents page (`*_CONTENTS`): `wiki-get` it first, apply the template to append or modify, then `wiki-put` the whole page back. Never overwrite entries owned by others. 3. Write all wiki content in UTF-8 Traditional Chinese, per the STE100 output rule. +4. Prefer visual forms for page content: use mermaid diagrams (flowchart, sequence, gantt, pie) and markdown tables wherever the information allows. Plain running text is the last resort, kept short. +5. Authentication fallback is built into `tools/gitea.sh`: on a missing GITEA_TOKEN or a 401/403 response it retries with the tea CLI login token automatically, so only report an auth failure when both paths fail. 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 ] }