fix(ci): 修复auto-fix在tarball checkout模式下git不可用的问题
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m9s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 1m13s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m39s
AI Code Review / AI Code Review (pull_request) Successful in 3m34s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 5m47s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been cancelled
CI/CD Pipeline / Frontend Unit Tests (pull_request) Failing after 1395h58m38s
CI/CD Pipeline / Deploy Production (pull_request) Failing after 1395h58m53s
CI/CD Pipeline / Build Production Worker Image (pull_request) Failing after 1395h58m54s
CI/CD Pipeline / Build Production API Image (pull_request) Failing after 1395h58m55s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Failing after 1395h58m57s
CI/CD Pipeline / Build Staging API Image (pull_request) Failing after 1395h58m58s
CI/CD Pipeline / Check if frontend-only change (pull_request) Has been skipped
CI/CD Pipeline / Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Failing after 1396h31m4s
CI/CD Pipeline / Build Production Web Image (pull_request) Failing after 1396h31m2s
CI/CD Pipeline / Production Browser E2E (pull_request) Failing after 1396h30m59s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m9s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 1m13s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m39s
AI Code Review / AI Code Review (pull_request) Successful in 3m34s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 5m47s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been cancelled
CI/CD Pipeline / Frontend Unit Tests (pull_request) Failing after 1395h58m38s
CI/CD Pipeline / Deploy Production (pull_request) Failing after 1395h58m53s
CI/CD Pipeline / Build Production Worker Image (pull_request) Failing after 1395h58m54s
CI/CD Pipeline / Build Production API Image (pull_request) Failing after 1395h58m55s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Failing after 1395h58m57s
CI/CD Pipeline / Build Staging API Image (pull_request) Failing after 1395h58m58s
CI/CD Pipeline / Check if frontend-only change (pull_request) Has been skipped
CI/CD Pipeline / Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Failing after 1396h31m4s
CI/CD Pipeline / Build Production Web Image (pull_request) Failing after 1396h31m2s
CI/CD Pipeline / Production Browser E2E (pull_request) Failing after 1396h30m59s
This commit is contained in:
@@ -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]"')
|
||||
|
||||
Reference in New Issue
Block a user