ci(ruff): 改进ruff工作流以区分PR和push事件

- 在PR事件中仅报告格式问题而不失败
- 在push事件中自动修复格式问题并提交
- 改进PR评论内容,包含详细问题和修复方法
- 添加错误处理和现有评论更新逻辑
This commit is contained in:
Wenjie Zhang 2025-12-07 17:55:21 +08:00
parent 3b4d6a2a3e
commit 4eeb4f4029

View File

@ -63,31 +63,80 @@ jobs:
# 5. 运行 Ruff 格式检查(与项目的 make lint 命令一致)
- name: Run Ruff format check
id: ruff-format
# PR 事件中不失败仅报告问题Push 事件中仍然会失败
continue-on-error: ${{ github.event_name == 'pull_request' }}
run: |
set +e # 不立即退出失败
echo "Running ruff check (uv run python -m ruff check .)..."
uv run python -m ruff check .
ruff_check_result=$?
echo "Running ruff format diff check (uv run python -m ruff format src --diff)..."
uv run python -m ruff format src --diff
ruff_format_result=$?
echo "Running import sorting check (uv run python -m ruff check --select I src)..."
uv run python -m ruff check --select I src
ruff_import_result=$?
# 保存检查结果状态
if [ $? -eq 0 ]; then
# 检查是否有任何错误
if [ $ruff_check_result -eq 0 ] && [ $ruff_format_result -eq 0 ] && [ $ruff_import_result -eq 0 ]; then
echo "ruff_format_passed=true" >> $GITHUB_OUTPUT
echo "✅ Ruff format check passed"
else
echo "ruff_format_passed=false" >> $GITHUB_OUTPUT
echo "❌ Ruff format check failed"
echo "❌ Ruff format check failed (this is expected for PR reviews)"
echo "::warning::Ruff format check found issues that should be addressed"
# 汇总错误信息
echo "## Ruff Format Issues Summary" > $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "The following formatting issues were found:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# 运行 ruff check 以获取详细错误信息
echo "### Detailed Issues:" >> $GITHUB_STEP_SUMMARY
uv run python -m ruff check . --output-format=github >> $GITHUB_STEP_SUMMARY 2>&1 || true
echo "" >> $GITHUB_STEP_SUMMARY
echo "To fix these issues locally, run:" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
echo "make format" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
fi
# 6. 针对不同事件类型执行不同操作
- name: Process Ruff Results
if: ${{ github.event_name == 'push' }}
env:
GIT_AUTHOR_NAME: "GitHub Actions"
GIT_AUTHOR_EMAIL: "actions@github.com"
GIT_COMMITTER_NAME: "GitHub Actions"
GIT_COMMITTER_EMAIL: "actions@github.com"
run: |
echo "Processing push event to main branch..."
# 检查当前是否有未提交的修改
if ! git diff --exit-code --quiet; then
echo "⚠️ Warning: There are uncommitted changes before formatting"
echo "The formatting process might need to handle this specially."
fi
# 先检查是否有格式问题
echo "Checking for formatting issues..."
set +e
uv run python -m ruff check . --quiet
has_ruff_issues=$?
set -e
if [ $has_ruff_issues -eq 0 ]; then
echo "✅ No formatting issues found"
exit 0
fi
echo "Formatting issues detected, running automatic fixes..."
# 自动应用格式修复(与项目的 make format 命令一致)
echo "Running uv run ruff format ."
uv run ruff format .
@ -99,17 +148,24 @@ jobs:
uv run python -m ruff check --select I src --fix
# 检查是否有格式变更
git diff --exit-code --name-only || (
echo "Formatting changes detected"
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
echo "Checking for formatting changes..."
if git diff --exit-code --quiet; then
echo "✅ No formatting changes needed"
else
echo "📝 Formatting changes detected"
echo "Changed files:"
git diff --name-only
# 添加所有变更并提交
git add .
git commit -m "style: auto-format with ruff [skip ci]"
echo "📤 Pushing formatting changes..."
git push
echo "✅ Formatting changes committed and pushed"
)
fi
- name: Create PR Comment on Failure
- name: Create PR Comment with Format Issues
if: ${{ github.event_name == 'pull_request' && steps.ruff-format.outputs.ruff_format_passed == 'false' }}
uses: actions/github-script@v7
with:
@ -118,35 +174,81 @@ jobs:
const issue_number = context.issue.number;
const repo = context.repo;
const commentBody = `## ⚠️ Ruff Format Check Failed
// 获取工作流运行 ID 以链接到特定的检查运行
const run_id = process.env.GITHUB_RUN_ID;
const run_url = `https://github.com/${repo.owner}/${repo.repo}/actions/runs/${run_id}`;
The Ruff format check failed on this pull request. Please run the following commands locally to fix formatting issues:
const commentBody = `## 📋 Ruff Format Review
The Ruff format check found some issues in this pull request. **This does not block the PR** - it's just a review of code style:
🔍 **Issues found**:
- Unused imports
- Line length violations (>120 characters)
- Import sorting issues
⚙️ **To fix these issues locally**:
\`\`\`bash
# Format code using project make command
# Run automatic formatting (same as make format)
make format
# Or manually run ruff commands with uv (same as make lint)
uv run ruff check .
uv run python -m ruff format src --diff
uv run python -m ruff check --select I src
\`\`\`
To fix formatting issues:
\`\`\`bash
# Or manually with uv:
uv run ruff format .
uv run ruff check . --fix
uv run python -m ruff check --select I src --fix
\`\`\`
Once formatting issues are resolved, commit and push your changes.`;
📝 **To just check without fixing**:
\`\`\`bash
make lint
\`\`\`
github.rest.issues.createComment({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
body: commentBody
});
🔗 **View detailed linting output**: [Ruff Check Run #${run_id}](${run_url})
---
*Note: This is an automated review for code formatting consistency. The PR can still be merged even with these formatting issues.*`;
// 先尝试更新已有的评论(如果存在)
try {
const comments = await github.rest.issues.listComments({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
});
const botComment = comments.data.find(comment =>
comment.user.login.includes('github-actions') ||
comment.body.includes('Ruff Format Review')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: repo.owner,
repo: repo.repo,
comment_id: botComment.id,
body: commentBody
});
console.log("✅ Updated existing comment");
} else {
await github.rest.issues.createComment({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
body: commentBody
});
console.log("✅ Created new comment");
}
} catch (error) {
console.error("Error managing comment:", error);
// 如果出错,仍然尝试创建新评论
await github.rest.issues.createComment({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
body: commentBody
});
}
- name: Create PR Comment on Success
if: ${{ github.event_name == 'pull_request' && steps.ruff-format.outputs.ruff_format_passed == 'true' }}
@ -157,13 +259,51 @@ jobs:
const issue_number = context.issue.number;
const repo = context.repo;
const commentBody = `## ✅ Ruff Format Check Passed
const commentBody = `## ✅ Ruff Format Review Passed
All Python files follow the required formatting standards. Great work!`;
✅ **All Python files follow the project's formatting standards!**
github.rest.issues.createComment({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
body: commentBody
});
Great work maintaining code consistency! 🎉
---
*Note: This is an automated review for code formatting. Your code follows the project's Ruff configuration.*`;
// 清理可能存在的失败评论(如果之前有失败,现在修复了)
try {
const comments = await github.rest.issues.listComments({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
});
const botComment = comments.data.find(comment =>
comment.user.login.includes('github-actions') ||
comment.body.includes('Ruff Format Review')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: repo.owner,
repo: repo.repo,
comment_id: botComment.id,
body: commentBody
});
console.log("✅ Updated existing comment");
} else {
await github.rest.issues.createComment({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
body: commentBody
});
console.log("✅ Created new comment");
}
} catch (error) {
console.error("Error managing comment:", error);
await github.rest.issues.createComment({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
body: commentBody
});
}