From caa4ce118ccb51912abe90c0d5cf01e36603f00e Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Thu, 10 Sep 2026 20:20:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(ci):=20auto-fix=20=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E8=A1=A5=E5=85=85=20ruff=20check=20--fix=EF=BC=88=E5=90=AB=20F?= =?UTF-8?q?401=20=E7=AD=89=20unsafe=20fix=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - fix_python() 在 black/isort 之后追加 ruff check --fix --unsafe-fixes - 目标范围与 validate_style.sh 对齐:apps packages tests(alembic 被 ruff exclude,scripts 不在检查范围) - ruff 不可用或修复后仍有残余问题时不阻断,剩余 lint 由 style check 继续拦截 - workflow step 名同步更新 解决 F401 等 ruff 可自动修复错误无法自动推修复、开发者反复打回的问题 --- .gitea/workflows/ci-pipeline.yml | 2 +- scripts/ci/auto_fix_formatting.py | 49 +++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/.gitea/workflows/ci-pipeline.yml b/.gitea/workflows/ci-pipeline.yml index c42638cf3..8b997ff13 100755 --- a/.gitea/workflows/ci-pipeline.yml +++ b/.gitea/workflows/ci-pipeline.yml @@ -196,7 +196,7 @@ jobs: - name: Run style checks shell: bash run: bash scripts/ci/validate_style.sh - - name: Auto-fix formatting (black + isort) + - name: Auto-fix formatting (black + isort + ruff) if: failure() shell: sh env: diff --git a/scripts/ci/auto_fix_formatting.py b/scripts/ci/auto_fix_formatting.py index 6873abd3d..ff73d51e8 100755 --- a/scripts/ci/auto_fix_formatting.py +++ b/scripts/ci/auto_fix_formatting.py @@ -1,10 +1,13 @@ #!/usr/bin/env python3 -"""CI中自动修复代码格式(Python: black + isort | Frontend: prettier),并推送回原分支。 +"""CI中自动修复代码格式(Python: black + isort + ruff | Frontend: prettier),并推送回原分支。 - PR事件:所有PR只要Code Quality因格式问题失败,自动修复并push回源分支 - Push事件(develop/main):自动修复并push回原分支,保持主干格式永远正确 - 防循环:修复commit带 [skip ci-format-check] 标记,检测到该标记则跳过修复 -- 只修格式(black/isort/prettier),ruff逻辑类错误不动 +- black/isort/prettier 修格式;ruff check --fix --unsafe-fixes 自动修复 + ruff 可修复的 lint 规则(含 F401 未使用 import 等 unsafe fix) +- ruff 目标范围与 validate_style.sh 的检查范围对齐:apps packages tests + (alembic/scripts 不在 ruff 检查范围内,不做修复) 当code quality检查因格式问题失败时触发。 """ @@ -135,7 +138,7 @@ def get_pr_head_branch(pr_number, api_url, token): def fix_python(target_py_files, scan_mode): - """修复 Python 文件格式 (black + isort)""" + """修复 Python 文件 (black 格式化 + isort 排序 + ruff lint 自动修复)""" if not target_py_files: print("没有需要修复的 Python 文件,跳过") return @@ -146,14 +149,48 @@ def fix_python(target_py_files, scan_mode): result = run(f"python3 -m black {target_str}", check=False) print(result.stdout[-500:] if result.stdout else "") if result.returncode != 0: - print("black执行失败,但继续尝试isort", file=sys.stderr) + print("black执行失败,但继续尝试isort/ruff", file=sys.stderr) print() print("--- isort 排序 ---") result = run(f"python3 -m isort {target_str}", check=False) print(result.stdout[-500:] if result.stdout else "") if result.returncode != 0: - print("isort执行失败", file=sys.stderr) + print("isort执行失败,继续尝试ruff", file=sys.stderr) + + # ruff lint 自动修复 + # 与 validate_style.sh 的检查范围对齐:只修 apps/packages/tests + # (alembic 在 pyproject.toml 中被 exclude,scripts 不在 ruff 检查范围内) + ruff_scopes = ("apps/", "packages/", "tests/") + ruff_files = [f for f in target_py_files if f.startswith(ruff_scopes)] + if scan_mode != "incremental": + ruff_targets = "apps packages tests" + elif ruff_files: + ruff_targets = " ".join(ruff_files) + else: + ruff_targets = "" + + if ruff_targets: + # ruff 由 style job 的 requirements-dev.txt 安装;不可用时跳过(不阻断 black/isort 的修复) + avail = run("python3 -m ruff --version", check=False) + if avail.returncode != 0: + print("ruff 不可用,跳过 ruff 自动修复", file=sys.stderr) + else: + print() + print("--- ruff lint 自动修复 (--fix --unsafe-fixes) ---") + # --unsafe-fixes 用于启用 F401(未使用 import)等 ruff 归类为 unsafe 的自动修复; + # 安全性由修复后重跑的完整 CI(单测/构建/staging 健康检查)兜底 + result = run( + f"python3 -m ruff check {ruff_targets} --fix --unsafe-fixes", + check=False, + ) + print(result.stdout[-1500:] if result.stdout else "") + if result.returncode != 0: + # 可能是仍有不可自动修复的 lint 错误(留待 style check 再次拦截),或修复过程出错 + print("ruff 自动修复后仍有未修复项或执行失败,剩余问题由 style check 继续拦截", file=sys.stderr) + else: + print() + print("增量模式且无 apps/packages/tests 范围内的 Python 变更,跳过 ruff 自动修复") def fix_frontend(target_fe_files, scan_mode, repo_root): @@ -334,7 +371,7 @@ def main(): # 提交修复 run("git clean -fd") run("git add -u") - run('git commit -m "style: auto-format with black + isort + prettier [skip ci-format-check]"') + run('git commit -m "style: auto-format with black + isort + ruff + prettier [skip ci-format-check]"') # 推送(head_branch已从ensure_git_repo获取) print(f"\nPR来源分支: {head_branch}") -- 2.54.0