fix: git diff
This commit is contained in:
207
.github/workflows/jsonDataValidation.yml
vendored
207
.github/workflows/jsonDataValidation.yml
vendored
@@ -32,6 +32,32 @@ jobs:
|
||||
pull-requests: write
|
||||
|
||||
steps:
|
||||
- name: Set environment variables based on trigger type
|
||||
id: set_env
|
||||
run: |
|
||||
# 根据触发类型设置环境变量
|
||||
if [ "${{ github.event_name }}" = "pull_request_target" ]; then
|
||||
echo "触发类型: PR触发"
|
||||
echo "trigger_type=pr" >> $GITHUB_OUTPUT
|
||||
echo "validate_path=pr_files" >> $GITHUB_OUTPUT
|
||||
echo "auto_fix=true" >> $GITHUB_OUTPUT
|
||||
echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
|
||||
echo "head_ref=${{ github.event.pull_request.head.ref }}" >> $GITHUB_OUTPUT
|
||||
echo "head_repo=${{ github.event.pull_request.head.repo.full_name }}" >> $GITHUB_OUTPUT
|
||||
echo "base_ref=${{ github.event.pull_request.base.ref }}" >> $GITHUB_OUTPUT
|
||||
echo "base_sha=${{ github.event.pull_request.base.sha }}" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "触发类型: 手动触发"
|
||||
echo "trigger_type=manual" >> $GITHUB_OUTPUT
|
||||
echo "validate_path=${{ github.event.inputs.path }}" >> $GITHUB_OUTPUT
|
||||
echo "auto_fix=${{ github.event.inputs.auto_fix }}" >> $GITHUB_OUTPUT
|
||||
echo "pr_number=${{ github.event.inputs.pr_number }}" >> $GITHUB_OUTPUT
|
||||
echo "head_ref=" >> $GITHUB_OUTPUT
|
||||
echo "head_repo=" >> $GITHUB_OUTPUT
|
||||
echo "base_ref=main" >> $GITHUB_OUTPUT
|
||||
echo "base_sha=" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
@@ -51,6 +77,10 @@ jobs:
|
||||
|
||||
- name: Debug file structure
|
||||
run: |
|
||||
echo "触发类型: ${{ steps.set_env.outputs.trigger_type }}"
|
||||
echo "验证路径: ${{ steps.set_env.outputs.validate_path }}"
|
||||
echo "自动修复: ${{ steps.set_env.outputs.auto_fix }}"
|
||||
echo "PR号: ${{ steps.set_env.outputs.pr_number }}"
|
||||
echo "Current directory: $(pwd)"
|
||||
echo "List files in root:"
|
||||
ls -la
|
||||
@@ -82,7 +112,7 @@ jobs:
|
||||
git remote add upstream $UPSTREAM_REPO
|
||||
|
||||
# 确保origin指向PR的fork仓库
|
||||
if [ "$PR_REPO" != "$MAIN_REPO" ] && [ "${{ github.event_name }}" = "pull_request_target" ]; then
|
||||
if [ "$PR_REPO" != "$MAIN_REPO" ] && [ "${{ steps.set_env.outputs.trigger_type }}" = "pr" ]; then
|
||||
ORIGIN_REPO="https://github.com/${PR_REPO}.git"
|
||||
echo "PR来自fork仓库,设置origin指向: $ORIGIN_REPO"
|
||||
else
|
||||
@@ -102,14 +132,16 @@ jobs:
|
||||
git remote -v
|
||||
|
||||
# 检查是否处于PR环境并切换到正确的分支
|
||||
if [ -n "${{ github.event.pull_request.head.ref }}" ]; then
|
||||
echo "检测到PR,切换到PR分支: ${{ github.event.pull_request.head.ref }}"
|
||||
if [ "${{ steps.set_env.outputs.trigger_type }}" = "pr" ] && [ -n "${{ steps.set_env.outputs.head_ref }}" ]; then
|
||||
echo "检测到PR,切换到PR分支: ${{ steps.set_env.outputs.head_ref }}"
|
||||
if [ "$PR_REPO" != "$MAIN_REPO" ]; then
|
||||
# fork仓库的PR,需要先创建本地分支追踪fork的远程分支
|
||||
git checkout -b "${{ github.event.pull_request.head.ref }}" --track "origin/${{ github.event.pull_request.head.ref }}"
|
||||
git checkout -b "${{ steps.set_env.outputs.head_ref }}" --track "origin/${{ steps.set_env.outputs.head_ref }}" || \
|
||||
git checkout -b "${{ steps.set_env.outputs.head_ref }}" --no-track && \
|
||||
git push --set-upstream origin "${{ steps.set_env.outputs.head_ref }}"
|
||||
else
|
||||
# 同一仓库的PR
|
||||
git checkout "${{ github.event.pull_request.head.ref }}"
|
||||
git checkout "${{ steps.set_env.outputs.head_ref }}" || git checkout -b "${{ steps.set_env.outputs.head_ref }}"
|
||||
fi
|
||||
elif [ -n "${{ github.ref_name }}" ]; then
|
||||
echo "切换到分支: ${{ github.ref_name }}"
|
||||
@@ -117,7 +149,7 @@ jobs:
|
||||
# main分支需要明确指定
|
||||
git checkout upstream/main -b main
|
||||
else
|
||||
git checkout "${{ github.ref_name }}"
|
||||
git checkout "${{ github.ref_name }}" || git checkout -b "${{ github.ref_name }}"
|
||||
fi
|
||||
else
|
||||
echo "创建临时分支"
|
||||
@@ -143,13 +175,10 @@ jobs:
|
||||
sed -i 's/"git"/"upstream"/g' build/validate.py
|
||||
# 修改提示信息
|
||||
sed -i 's/本地文件/PR提交的文件/g' build/validate.py
|
||||
|
||||
echo "验证脚本内容预览:"
|
||||
head -n 20 build/validate.py
|
||||
fi
|
||||
|
||||
- name: Get PR information
|
||||
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' }}
|
||||
- name: Get PR information for workflow_dispatch
|
||||
if: ${{ steps.set_env.outputs.trigger_type == 'manual' && steps.set_env.outputs.pr_number != '' }}
|
||||
id: pr_info
|
||||
uses: actions/github-script@v6
|
||||
with:
|
||||
@@ -159,7 +188,7 @@ jobs:
|
||||
const pr = await github.rest.pulls.get({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
pull_number: parseInt(${{ github.event.inputs.pr_number }})
|
||||
pull_number: parseInt(${{ steps.set_env.outputs.pr_number }})
|
||||
});
|
||||
|
||||
core.setOutput('head_sha', pr.data.head.sha);
|
||||
@@ -167,7 +196,7 @@ jobs:
|
||||
core.setOutput('head_repo', pr.data.head.repo.full_name);
|
||||
core.setOutput('found', 'true');
|
||||
|
||||
console.log(`找到 PR #${{ github.event.inputs.pr_number }}`);
|
||||
console.log(`找到 PR #${{ steps.set_env.outputs.pr_number }}`);
|
||||
console.log(`Head SHA: ${pr.data.head.sha}`);
|
||||
console.log(`Head Ref: ${pr.data.head.ref}`);
|
||||
console.log(`Head Repo: ${pr.data.head.repo.full_name}`);
|
||||
@@ -176,49 +205,104 @@ jobs:
|
||||
const exec = require('child_process').execSync;
|
||||
if (pr.data.head.ref) {
|
||||
console.log(`切换到PR分支: ${pr.data.head.ref}`);
|
||||
exec(`git checkout ${pr.data.head.ref}`);
|
||||
exec(`git checkout ${pr.data.head.ref} || git checkout -b ${pr.data.head.ref}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`获取 PR #${{ github.event.inputs.pr_number }} 信息失败: ${error.message}`);
|
||||
console.log(`获取 PR #${{ steps.set_env.outputs.pr_number }} 信息失败: ${error.message}`);
|
||||
core.setOutput('found', 'false');
|
||||
}
|
||||
|
||||
- name: Get changed files
|
||||
- name: Get changed files for PR trigger
|
||||
id: changed_files
|
||||
if: github.event_name == 'pull_request_target'
|
||||
if: ${{ steps.set_env.outputs.trigger_type == 'pr' }}
|
||||
run: |
|
||||
# 使用upstream/main作为比较基准
|
||||
CHANGED_FILES=$(git diff --name-only upstream/main HEAD | grep -E '^repo/pathing/.*\.json$' || true)
|
||||
echo "Changed JSON files in repo/pathing compared to upstream/main:"
|
||||
echo "$CHANGED_FILES"
|
||||
echo "changed_files=$CHANGED_FILES" >> $GITHUB_OUTPUT
|
||||
# 输出分支信息便于调试
|
||||
echo "当前分支: $(git branch --show-current)"
|
||||
echo "HEAD指向: $(git rev-parse HEAD)"
|
||||
echo "PR基础分支: ${{ steps.set_env.outputs.base_ref }}"
|
||||
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
echo "No JSON files changed in repo/pathing directory"
|
||||
# 确保有upstream/main分支
|
||||
git fetch upstream main
|
||||
echo "Upstream/main SHA: $(git rev-parse upstream/main)"
|
||||
|
||||
# 创建临时变量来存储修改的文件列表
|
||||
CHANGED_FILES=""
|
||||
|
||||
# 方法1:尝试使用git diff检测变化
|
||||
echo "检测方法1: 使用git diff检测"
|
||||
FILES_METHOD_1=$(git diff --name-only upstream/main HEAD | grep -E '^repo/pathing/.*\.json$' || true)
|
||||
if [ -n "$FILES_METHOD_1" ]; then
|
||||
echo "方法1找到的JSON文件:"
|
||||
echo "$FILES_METHOD_1"
|
||||
CHANGED_FILES="$FILES_METHOD_1"
|
||||
else
|
||||
echo "方法1未找到修改的JSON文件"
|
||||
fi
|
||||
|
||||
# 方法2:如果方法1失败,尝试直接查询PR API获取修改的文件
|
||||
if [ -z "$CHANGED_FILES" ] && [ -n "${{ steps.set_env.outputs.pr_number }}" ]; then
|
||||
echo "检测方法2: 使用GitHub API检测"
|
||||
PR_FILES=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
|
||||
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.set_env.outputs.pr_number }}/files" | \
|
||||
jq -r '.[] | select(.filename | test("^repo/pathing/.*\\.json$")) | .filename')
|
||||
|
||||
if [ -n "$PR_FILES" ]; then
|
||||
echo "方法2找到的JSON文件:"
|
||||
echo "$PR_FILES"
|
||||
CHANGED_FILES="$PR_FILES"
|
||||
else
|
||||
echo "方法2未找到修改的JSON文件"
|
||||
fi
|
||||
fi
|
||||
|
||||
# 方法3:如果前两种方法都失败,列出所有repo/pathing中的JSON文件,但限制在最近修改的
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
echo "检测方法3: 列出最近修改的JSON文件"
|
||||
# 列出过去5次提交中修改的JSON文件
|
||||
RECENT_FILES=$(git log -n 5 --name-only --pretty=format: | grep -E '^repo/pathing/.*\.json$' | sort -u || true)
|
||||
|
||||
if [ -n "$RECENT_FILES" ]; then
|
||||
echo "最近修改的JSON文件:"
|
||||
echo "$RECENT_FILES"
|
||||
CHANGED_FILES="$RECENT_FILES"
|
||||
echo "⚠️ 警告: 使用最近修改的文件作为回退方案"
|
||||
else
|
||||
echo "方法3未找到最近修改的JSON文件"
|
||||
fi
|
||||
fi
|
||||
|
||||
# 最后回退方案:直接指定验证目录
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
echo "⚠️ 警告: 所有方法均未检测到修改的JSON文件,将验证整个repo/pathing目录"
|
||||
CHANGED_FILES="repo/pathing"
|
||||
fi
|
||||
|
||||
# 输出结果
|
||||
echo "changed_files=$CHANGED_FILES" >> $GITHUB_OUTPUT
|
||||
echo "最终找到的修改文件:"
|
||||
echo "$CHANGED_FILES"
|
||||
|
||||
- name: Run validation and correction
|
||||
- name: Run validation for PR trigger
|
||||
if: ${{ steps.set_env.outputs.trigger_type == 'pr' }}
|
||||
env:
|
||||
GITHUB_ACTOR: ${{ github.actor }}
|
||||
PR_NUMBER: ${{ github.event.pull_request.number || github.event.inputs.pr_number }}
|
||||
PR_NUMBER: ${{ steps.set_env.outputs.pr_number }}
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
HEAD_REF: ${{ github.event.pull_request.head.ref || steps.pr_info.outputs.head_ref || '' }}
|
||||
PR_REPO: ${{ github.event.pull_request.head.repo.full_name || steps.pr_info.outputs.head_repo || github.repository }}
|
||||
VALIDATE_PATH: ${{ github.event.inputs.path || 'repo/pathing' }}
|
||||
AUTO_FIX: ${{ github.event.inputs.auto_fix || 'true' }}
|
||||
HEAD_REF: ${{ steps.set_env.outputs.head_ref }}
|
||||
PR_REPO: ${{ steps.set_env.outputs.head_repo || github.repository }}
|
||||
CHANGED_FILES: ${{ steps.changed_files.outputs.changed_files }}
|
||||
run: |
|
||||
# 根据触发方式决定验证路径和是否自动修复
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
echo "手动触发模式,验证路径: ${VALIDATE_PATH}"
|
||||
python build/validate.py ${VALIDATE_PATH} $([[ "${AUTO_FIX}" == "true" ]] && echo "--fix")
|
||||
echo "PR 触发模式,验证修改的 JSON 文件"
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
echo "没有找到修改的 JSON 文件,跳过验证"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 检查CHANGED_FILES是否包含整个目录
|
||||
if [ "$CHANGED_FILES" = "repo/pathing" ]; then
|
||||
echo "验证整个目录: repo/pathing"
|
||||
python build/validate.py "repo/pathing" --fix
|
||||
else
|
||||
echo "PR 触发模式,仅验证修改的 JSON 文件"
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
echo "没有找到修改的 JSON 文件,跳过验证"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 单独验证每个修改的文件
|
||||
for file in $CHANGED_FILES; do
|
||||
echo "验证文件: $file"
|
||||
@@ -252,16 +336,55 @@ jobs:
|
||||
else
|
||||
echo "没有文件被修改,无需提交"
|
||||
fi
|
||||
|
||||
- name: Run validation for manual trigger
|
||||
if: ${{ steps.set_env.outputs.trigger_type == 'manual' }}
|
||||
env:
|
||||
GITHUB_ACTOR: ${{ github.actor }}
|
||||
PR_NUMBER: ${{ steps.set_env.outputs.pr_number }}
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
HEAD_REF: ${{ steps.pr_info.outputs.head_ref || '' }}
|
||||
PR_REPO: ${{ steps.pr_info.outputs.head_repo || github.repository }}
|
||||
VALIDATE_PATH: ${{ steps.set_env.outputs.validate_path }}
|
||||
AUTO_FIX: ${{ steps.set_env.outputs.auto_fix }}
|
||||
run: |
|
||||
echo "手动触发模式,验证路径: ${VALIDATE_PATH}"
|
||||
python build/validate.py ${VALIDATE_PATH} $([[ "${AUTO_FIX}" == "true" ]] && echo "--fix")
|
||||
|
||||
# 如果关联了PR,尝试提交更改
|
||||
if [ -n "$PR_NUMBER" ] && [ -n "$HEAD_REF" ] && [ -n "$(git status --porcelain)" ]; then
|
||||
echo "发现修改,提交更改到PR: #$PR_NUMBER"
|
||||
git add .
|
||||
git commit -m "自动修复 JSON 格式和版本号 [ci skip]"
|
||||
|
||||
# 确定当前分支
|
||||
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
echo "当前分支: ${CURRENT_BRANCH}"
|
||||
|
||||
if [ "$CURRENT_BRANCH" = "HEAD" ]; then
|
||||
# 如果在detached HEAD状态,使用正确的方式推送
|
||||
echo "在detached HEAD状态,使用HEAD_REF推送: ${HEAD_REF}"
|
||||
git push origin HEAD:${HEAD_REF}
|
||||
else
|
||||
# 常规推送
|
||||
echo "推送到分支: ${CURRENT_BRANCH}"
|
||||
git push origin ${CURRENT_BRANCH}
|
||||
fi
|
||||
elif [ -n "$(git status --porcelain)" ]; then
|
||||
echo "发现修改,但未关联PR或无法确定分支,跳过提交"
|
||||
else
|
||||
echo "没有文件被修改,无需提交"
|
||||
fi
|
||||
|
||||
- name: Add PR comment
|
||||
if: ${{ github.event_name == 'pull_request_target' || (github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number != '' && steps.pr_info.outputs.found == 'true') }}
|
||||
if: ${{ (steps.set_env.outputs.trigger_type == 'pr') || (steps.set_env.outputs.trigger_type == 'manual' && steps.set_env.outputs.pr_number != '' && steps.pr_info.outputs.found == 'true') }}
|
||||
continue-on-error: true
|
||||
uses: actions/github-script@v6
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
const pr_number = ${{ github.event.pull_request.number || github.event.inputs.pr_number }};
|
||||
const pr_number = ${{ steps.set_env.outputs.pr_number }};
|
||||
|
||||
if (fs.existsSync('validation_notes.md')) {
|
||||
const message = fs.readFileSync('validation_notes.md', 'utf8');
|
||||
|
||||
Reference in New Issue
Block a user