ISEP#47: 補齊 8 支 Python 也從 env/.env 取 token,不再從 remote URL 抽

c9128 複驗指出:雲端那次只改了 6 支 shell 的 sed 寫法,漏了 8 支 Python
的 re.search(r"//[^:/]+:([^@]+)@") 寫法。remote 清乾淨後這 8 支會靜默拿到
空字串 → 401,正是票上 2026-08-13「被讀成找不到/不存在」那種壞法;雲端有
環境變數所以測不出本機(只有 .env)的壞法。

- 新增 hooks/lib/gitea_token.py:Python 端唯一 token 來源,與
  scripts/lib/gitea-token.sh 同一解析順序(env → $CLAUDE_PROJECT_DIR/.env
  → git 根的 .env),絕不從 remote URL 抽。
- 8 支改用它:scripts/ticket、debt-worklist、milestone-account、pr-verdict、
  release-manifest、isep-nag、isep-notify、hooks/lib/release_chain.py。
  release_chain 的 import 做成防禦式(旁邊沒有 gitea_token.py 也能載入,
  讀 tag/release 一律匿名),token_from_env_or_remote 保留簽章、改走新來源。
- 測試:新增 scripts/test-gitea-token.sh(16 條,Python+shell 兩份實作、
  解析順序、髒 remote 不准解出、8 支都走得過 token 步);docs/TESTING.md A44。
  beacon 測試的 mkplugin 一併複製 gitea_token.py(release_chain 現在會 import 它)。

驗過(remote 乾淨+無環境變數+.env 有 token):ticket mine/isep-nag/
isep-notify/milestone-account/pr-verdict/debt-worklist 都拿得到 token、
走得過 token 步。回歸:test-gitea-token 16/16、milestone-account 23/23、
release-manifest 21/21、debt-worklist 47/47、release-check 14/14、
release-ship 14/14、ticket-pick 84/84、ticket-triage 80/80、
ticket-repo-arg 7/7、ticket-handoff-writeback 49/49、
ticket-where-seen 17/17、isep-notify-botapi 10/10、
beacon 33/33、pr-verdict-guard 52/52。

README scripts 數在本樹實數=68(main 66→branch 67 加 git-credential-gitea.sh
→本次 68 加 test-gitea-token.sh;README 舊值 65 在 main 上就已 stale)。
hooks 仍 62,hooks-inventory.md 不動。

版本號待總管定(改到的是會被載入的 hooks/lib 與 scripts,需升版才會傳到安裝的那份)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SwZx8MkV84Cz2Mf4sDU4if
This commit is contained in:
Claude
2026-09-19 03:59:51 +00:00
parent ad10159dfe
commit 76adc749b0
13 changed files with 249 additions and 139 deletions
+4 -16
View File
@@ -41,6 +41,8 @@ import urllib.request
HOST = os.environ.get("MILESTONE_ACCOUNT_HOST", "https://git.uncle6.me")
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
sys.path.insert(0, os.path.join(HERE, "..", "hooks", "lib"))
from gitea_token import gitea_token as _gitea_token # 唯一 token 來源(ISEP#47):env→.env,不碰 remote URL
def _project_roots():
"""跟 `hooks/lib/mainline.py::project_roots()` 同一把尺(那裡是唯一定義;載不到才退回同形狀的內建)。"""
try:
@@ -112,22 +114,8 @@ def die(msg, code=2):
def token():
root = os.environ.get("CLAUDE_PROJECT_DIR") or HERE
host = HOST.split("//")[-1].rstrip("/")
try:
out = subprocess.run(["git", "-C", root, "remote", "-v"],
capture_output=True, text=True, timeout=20).stdout
except Exception:
out = ""
for line in out.splitlines():
if host in line:
m = re.search(r"//[^:/]+:([^@]+)@", line)
if m:
return m.group(1)
for env in ("GITEA_TOKEN_CLAUDE_CODE", "GITEA_TOKEN"):
if os.environ.get(env):
return os.environ[env]
return ""
# 來源=env.envISEP#47),不碰 remote URL。拿不到回 ""(呼叫者自己判空)。
return _gitea_token() or ""
_FX = None