diff --git a/scripts/ci/auto_fix_formatting.py b/scripts/ci/auto_fix_formatting.py index 74baed74e..aa899fe16 100755 --- a/scripts/ci/auto_fix_formatting.py +++ b/scripts/ci/auto_fix_formatting.py @@ -23,6 +23,49 @@ def run(cmd, check=True, capture=True, cwd=None): return result +def ensure_git_repo(api_url, repo, token, pr_number): + """确保当前目录是git仓库。 + + checkout脚本用tarball方式下载代码,没有.git目录。 + 这里自动初始化git仓库、拉取远端分支,确保后续git操作可用。 + tarball内容与PR HEAD完全一致,checkout后git status为clean。 + """ + if os.path.exists(".git"): + return + + print("检测到tarball checkout(无.git目录),自动初始化git仓库...") + + # 构造带认证的远端URL + server_url = api_url.rsplit("/api/v1", 1)[0] + remote_url = f"{server_url.replace('https://', f'https://x-access-token:{token}@')}/{repo}.git" + + # 获取PR的源分支 + pr_api_url = f"{api_url}/repos/{repo}/pulls/{pr_number}" + req = urllib.request.Request(pr_api_url, headers={"Authorization": f"token {token}"}) + with urllib.request.urlopen(req) as resp: + pr = json.loads(resp.read()) + head_branch = pr["head"]["ref"] + + print(f"PR源分支: {head_branch}") + + # 初始化git并拉取远端分支 + run("git init -q") + run(f"git remote add origin {remote_url}") + run("git config user.name \"CI Bot\"") + run("git config user.email \"ci-bot@xiaoxiajianji.com\"") + + print("拉取远端分支...") + run(f"git fetch --depth=1 origin {head_branch}") + run(f"git checkout -B {head_branch} FETCH_HEAD") + + # tarball内容与HEAD一致,checkout后应该是clean状态 + result = run("git status --porcelain") + if result.stdout.strip(): + print(f"⚠️ 注意:tarball与HEAD有差异 ({len(result.stdout.strip().splitlines())} 个文件)") + else: + print("✅ git仓库就绪,工作区clean") + + def get_changed_files(pr_number, api_url, token): """获取PR中变更的文件列表""" url = f"{api_url}/pulls/{pr_number}/files?limit=100" @@ -91,9 +134,7 @@ def fix_frontend(target_fe_files, scan_mode, repo_root): if scan_mode == "incremental": # 增量模式:只格式化变更的前端文件 - # 转换为相对于 apps/web 的路径或用绝对路径 target_str = " ".join(target_fe_files) - # 从项目根目录运行,prettier 会找配置文件 cmd = f"{prettier_bin} --write {target_str}" else: # 全量模式:格式化整个前端目录 @@ -129,6 +170,9 @@ def main(): sys.exit(1) repo_root = os.getcwd() + + # 确保git仓库可用(tarball checkout模式下自动初始化) + ensure_git_repo(api_url, repo, token, pr_number) print("=== 检测到代码格式问题,尝试自动修复 ===") print(f"PR #{pr_number}") @@ -136,20 +180,9 @@ def main(): # 前端文件扩展名 fe_extensions = ( - ".ts", - ".tsx", - ".js", - ".jsx", - ".css", - ".scss", - ".less", - ".json", - ".html", - ".md", - ".yaml", - ".yml", + ".ts", ".tsx", ".js", ".jsx", ".css", ".scss", ".less", + ".json", ".html", ".md", ".yaml", ".yml", ) - # Python 文件扩展名 py_extensions = (".py",) # 确定要修复的文件范围 @@ -160,15 +193,13 @@ def main(): print(f"增量模式: {len(target_py_files)} 个Python文件, {len(target_fe_files)} 个前端文件") else: target_py_files = ["alembic", "apps", "packages", "tests", "scripts"] - # 全量模式下 prettier 在前端目录内部运行,无需传文件列表 - target_fe_files = ["apps/web"] # 标记为有前端文件需要处理 + target_fe_files = ["apps/web"] print("全量模式,修复所有文件") # Python 格式化 fix_python(target_py_files, scan_mode) # 前端格式化 - # 全量模式下直接传 web 目录标记 if scan_mode != "incremental": fix_frontend(["apps/web"], scan_mode, repo_root) else: @@ -186,10 +217,6 @@ def main(): for line in result.stdout.strip().split("\n"): print(f" {line}") - # 配置git - run('git config user.name "CI Bot"') - run('git config user.email "ci-bot@xiaoxiajianji.com"') - # 提交修复 run("git add -A") run('git commit -m "style: auto-format with black + isort + prettier [ci skip]"')