Files
document_analyzer/scripts/_get_gitea_config.py
T
pzhang_zywl a8964db151
CI / test (pull_request) Successful in 18s
fix: 将 Gitea 配置迁移到 ~/.gitea/config.yaml 多账号配置体系
- 新增 _get_gitea_config.py 从 YAML 读取 URL/repo/token
- _common.sh 改为通过 eval python 脚本加载配置
- GITEA_CICD_SETUP.md / DEV_AGENT.md / QE_AGENT.md 更新文档
- CI 工作流改用 ${{ gitea.server_url }} / ${{ gitea.repository }}

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-05 17:17:48 +08:00

82 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Print Gitea config for current user as shell-exportable variables.
Usage (bash):
eval "$(python scripts/_get_gitea_config.py)"
Usage (batch):
for /f "usebackq tokens=1,* delims= " %%a in (
`python scripts/_get_gitea_config.py --batch 2^>nul`
) do set "%%b"
Config: ~/.gitea/config.yaml — multi-profile YAML.
Env: GITEA_USER selects the profile (required).
Fallback: scripts/.env (backwards compat, no GITEA_USER needed).
"""
import os
import sys
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = os.path.expanduser("~/.gitea/config.yaml")
ENV_PATH = os.path.join(SCRIPT_DIR, ".env")
def _read_yaml_config(path):
import yaml
with open(path) as f:
return yaml.safe_load(f) or {}
def main():
use_batch = "--batch" in sys.argv
prefix = "set" if use_batch else "export"
# 1) Primary: ~/.gitea/config.yaml
if os.path.exists(CONFIG_PATH):
user = os.environ.get("GITEA_USER")
if not user:
print(
"Error: GITEA_USER is not set. "
"Choose from: " + ", ".join(_read_yaml_config(CONFIG_PATH).keys()),
file=sys.stderr,
)
sys.exit(1)
config = _read_yaml_config(CONFIG_PATH)
profile = config.get(user)
if not profile:
print(f"Error: user '{user}' not found in {CONFIG_PATH}", file=sys.stderr)
sys.exit(1)
print(f'{prefix} GITEA_URL={profile.get("url", "")}')
print(f'{prefix} GITEA_REPO={profile.get("repo", "")}')
print(f'{prefix} GITEA_API_TOKEN={profile.get("token", "")}')
print(f'{prefix} GITEA_USER={user}')
return
# 2) Fallback: scripts/.env
if os.path.exists(ENV_PATH):
print(f"Warning: {CONFIG_PATH} not found, falling back to {ENV_PATH}",
file=sys.stderr)
with open(ENV_PATH) as f:
for line in f:
line = line.strip()
if line.startswith("export "):
var = line[7:]
if use_batch:
var = var.replace("export ", "set ", 1)
print(var)
if use_batch:
print(f"set GITEA_USER={os.environ.get('GITEA_USER', '')}")
else:
print(f"export GITEA_USER={os.environ.get('GITEA_USER', '')}")
return
print(f"Error: {CONFIG_PATH} not found. Create it or set up scripts/.env.",
file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()