fix(ci): auto-fix强制checkout到PR源分支,避免merge commit差异
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 5s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Failing after 17s
AI Code Review / AI Code Review (pull_request) Failing after 37s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 3m49s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Successful in 3m2s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 5m4s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 7m38s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 2m45s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 11m18s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 20s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 5s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Failing after 17s
AI Code Review / AI Code Review (pull_request) Failing after 37s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 3m49s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Successful in 3m2s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 5m4s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 7m38s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 2m45s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 11m18s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 20s
This commit is contained in:
@@ -24,11 +24,11 @@ def run(cmd, check=True, capture=True, cwd=None):
|
||||
|
||||
|
||||
def ensure_git_repo(api_url, repo, token, pr_number):
|
||||
"""确保当前目录是git仓库。
|
||||
"""确保当前目录是git仓库,并切换到PR源分支。
|
||||
|
||||
checkout脚本用tarball方式下载代码,没有.git目录。
|
||||
这里自动初始化git仓库,用read-tree将远端HEAD载入index,
|
||||
不修改工作区文件(tarball内容与PR HEAD一致)。
|
||||
checkout脚本用tarball方式下载代码(PR merge后的commit),没有.git目录。
|
||||
这里自动初始化git仓库,fetch PR源分支并强制checkout,
|
||||
使工作区变为PR源分支的代码,确保后续格式化修复基于源分支。
|
||||
"""
|
||||
if os.path.exists(".git"):
|
||||
return
|
||||
@@ -48,31 +48,30 @@ def ensure_git_repo(api_url, repo, token, pr_number):
|
||||
|
||||
print(f"PR源分支: {head_branch}")
|
||||
|
||||
# 初始化git(不创建初始commit)
|
||||
# 初始化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("拉取远端分支...")
|
||||
# fetch源分支(浅克隆,只要最新commit)
|
||||
print("fetch源分支...")
|
||||
run(f"git fetch --depth=1 origin {head_branch}")
|
||||
|
||||
# 用read-tree载入index,不碰工作区文件
|
||||
# 这样工作区保持tarball内容,index是远端HEAD的状态
|
||||
# 由于tarball = HEAD,git status应为clean
|
||||
run("git read-tree FETCH_HEAD")
|
||||
# 强制checkout到源分支(覆盖tarball内容)
|
||||
# tarball是merge后的commit,源分支才是我们要修改并推送的目标
|
||||
print("切换到源分支...")
|
||||
run(f"git checkout -B {head_branch} FETCH_HEAD")
|
||||
|
||||
# 设置HEAD指向本地分支
|
||||
run(f"git symbolic-ref HEAD refs/heads/{head_branch}")
|
||||
|
||||
# 验证状态
|
||||
result = run("git status --porcelain")
|
||||
if result.stdout.strip():
|
||||
n = len(result.stdout.strip().splitlines())
|
||||
print(f"⚠️ 工作区与HEAD有差异 ({n} 个文件)")
|
||||
print(f"⚠️ 工作区有 {n} 个未追踪文件")
|
||||
else:
|
||||
print("✅ git仓库就绪,工作区clean")
|
||||
|
||||
return head_branch
|
||||
|
||||
|
||||
def get_changed_files(pr_number, api_url, token):
|
||||
"""获取PR中变更的文件列表"""
|
||||
@@ -180,7 +179,8 @@ def main():
|
||||
repo_root = os.getcwd()
|
||||
|
||||
# 确保git仓库可用(tarball checkout模式下自动初始化)
|
||||
ensure_git_repo(api_url, repo, token, pr_number)
|
||||
# 返回PR源分支名,供后续推送使用
|
||||
head_branch = ensure_git_repo(api_url, repo, token, pr_number)
|
||||
|
||||
print("=== 检测到代码格式问题,尝试自动修复 ===")
|
||||
print(f"PR #{pr_number}")
|
||||
@@ -239,8 +239,7 @@ def main():
|
||||
run("git add -A")
|
||||
run('git commit -m "style: auto-format with black + isort + prettier [ci skip]"')
|
||||
|
||||
# 获取来源分支并推送
|
||||
head_branch = get_pr_head_branch(pr_number, f"{api_url}/repos/{repo}", token)
|
||||
# 推送(head_branch已从ensure_git_repo获取)
|
||||
print(f"\nPR来源分支: {head_branch}")
|
||||
|
||||
run(f'git push origin "HEAD:{head_branch}"')
|
||||
|
||||
Reference in New Issue
Block a user