Skip to content

Commit 4eeb4f4

Browse files
committed
ci(ruff): 改进ruff工作流以区分PR和push事件
- 在PR事件中仅报告格式问题而不失败 - 在push事件中自动修复格式问题并提交 - 改进PR评论内容,包含详细问题和修复方法 - 添加错误处理和现有评论更新逻辑
1 parent 3b4d6a2 commit 4eeb4f4

File tree

1 file changed

+175
-35
lines changed

1 file changed

+175
-35
lines changed

.github/workflows/ruff.yml

Lines changed: 175 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,80 @@ jobs:
6363
# 5. 运行 Ruff 格式检查(与项目的 make lint 命令一致)
6464
- name: Run Ruff format check
6565
id: ruff-format
66+
# PR 事件中不失败,仅报告问题;Push 事件中仍然会失败
67+
continue-on-error: ${{ github.event_name == 'pull_request' }}
6668
run: |
69+
set +e # 不立即退出失败
70+
6771
echo "Running ruff check (uv run python -m ruff check .)..."
6872
uv run python -m ruff check .
73+
ruff_check_result=$?
6974
7075
echo "Running ruff format diff check (uv run python -m ruff format src --diff)..."
7176
uv run python -m ruff format src --diff
77+
ruff_format_result=$?
7278
7379
echo "Running import sorting check (uv run python -m ruff check --select I src)..."
7480
uv run python -m ruff check --select I src
81+
ruff_import_result=$?
7582
76-
# 保存检查结果状态
77-
if [ $? -eq 0 ]; then
83+
# 检查是否有任何错误
84+
if [ $ruff_check_result -eq 0 ] && [ $ruff_format_result -eq 0 ] && [ $ruff_import_result -eq 0 ]; then
7885
echo "ruff_format_passed=true" >> $GITHUB_OUTPUT
7986
echo "✅ Ruff format check passed"
8087
else
8188
echo "ruff_format_passed=false" >> $GITHUB_OUTPUT
82-
echo "❌ Ruff format check failed"
89+
echo "❌ Ruff format check failed (this is expected for PR reviews)"
90+
echo "::warning::Ruff format check found issues that should be addressed"
91+
92+
# 汇总错误信息
93+
echo "## Ruff Format Issues Summary" > $GITHUB_STEP_SUMMARY
94+
echo "" >> $GITHUB_STEP_SUMMARY
95+
echo "The following formatting issues were found:" >> $GITHUB_STEP_SUMMARY
96+
echo "" >> $GITHUB_STEP_SUMMARY
97+
98+
# 运行 ruff check 以获取详细错误信息
99+
echo "### Detailed Issues:" >> $GITHUB_STEP_SUMMARY
100+
uv run python -m ruff check . --output-format=github >> $GITHUB_STEP_SUMMARY 2>&1 || true
101+
102+
echo "" >> $GITHUB_STEP_SUMMARY
103+
echo "To fix these issues locally, run:" >> $GITHUB_STEP_SUMMARY
104+
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
105+
echo "make format" >> $GITHUB_STEP_SUMMARY
106+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
83107
fi
84108
85109
# 6. 针对不同事件类型执行不同操作
86110
- name: Process Ruff Results
87111
if: ${{ github.event_name == 'push' }}
112+
env:
113+
GIT_AUTHOR_NAME: "GitHub Actions"
114+
GIT_AUTHOR_EMAIL: "[email protected]"
115+
GIT_COMMITTER_NAME: "GitHub Actions"
116+
GIT_COMMITTER_EMAIL: "[email protected]"
88117
run: |
89118
echo "Processing push event to main branch..."
90119
120+
# 检查当前是否有未提交的修改
121+
if ! git diff --exit-code --quiet; then
122+
echo "⚠️ Warning: There are uncommitted changes before formatting"
123+
echo "The formatting process might need to handle this specially."
124+
fi
125+
126+
# 先检查是否有格式问题
127+
echo "Checking for formatting issues..."
128+
set +e
129+
uv run python -m ruff check . --quiet
130+
has_ruff_issues=$?
131+
set -e
132+
133+
if [ $has_ruff_issues -eq 0 ]; then
134+
echo "✅ No formatting issues found"
135+
exit 0
136+
fi
137+
138+
echo "Formatting issues detected, running automatic fixes..."
139+
91140
# 自动应用格式修复(与项目的 make format 命令一致)
92141
echo "Running uv run ruff format ."
93142
uv run ruff format .
@@ -99,17 +148,24 @@ jobs:
99148
uv run python -m ruff check --select I src --fix
100149
101150
# 检查是否有格式变更
102-
git diff --exit-code --name-only || (
103-
echo "Formatting changes detected"
104-
git config user.name "GitHub Actions"
105-
git config user.email "[email protected]"
151+
echo "Checking for formatting changes..."
152+
if git diff --exit-code --quiet; then
153+
echo "✅ No formatting changes needed"
154+
else
155+
echo "📝 Formatting changes detected"
156+
echo "Changed files:"
157+
git diff --name-only
158+
159+
# 添加所有变更并提交
106160
git add .
107161
git commit -m "style: auto-format with ruff [skip ci]"
162+
163+
echo "📤 Pushing formatting changes..."
108164
git push
109165
echo "✅ Formatting changes committed and pushed"
110-
)
166+
fi
111167
112-
- name: Create PR Comment on Failure
168+
- name: Create PR Comment with Format Issues
113169
if: ${{ github.event_name == 'pull_request' && steps.ruff-format.outputs.ruff_format_passed == 'false' }}
114170
uses: actions/github-script@v7
115171
with:
@@ -118,35 +174,81 @@ jobs:
118174
const issue_number = context.issue.number;
119175
const repo = context.repo;
120176
121-
const commentBody = `## ⚠️ Ruff Format Check Failed
177+
// 获取工作流运行 ID 以链接到特定的检查运行
178+
const run_id = process.env.GITHUB_RUN_ID;
179+
const run_url = `https://github.com/${repo.owner}/${repo.repo}/actions/runs/${run_id}`;
122180
123-
The Ruff format check failed on this pull request. Please run the following commands locally to fix formatting issues:
181+
const commentBody = `## 📋 Ruff Format Review
124182
125-
\`\`\`bash
126-
# Format code using project make command
127-
make format
183+
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:
128184
129-
# Or manually run ruff commands with uv (same as make lint)
130-
uv run ruff check .
131-
uv run python -m ruff format src --diff
132-
uv run python -m ruff check --select I src
133-
\`\`\`
185+
🔍 **Issues found**:
186+
- Unused imports
187+
- Line length violations (>120 characters)
188+
- Import sorting issues
189+
190+
⚙️ **To fix these issues locally**:
134191
135-
To fix formatting issues:
136192
\`\`\`bash
193+
# Run automatic formatting (same as make format)
194+
make format
195+
196+
# Or manually with uv:
137197
uv run ruff format .
138198
uv run ruff check . --fix
139199
uv run python -m ruff check --select I src --fix
140200
\`\`\`
141201
142-
Once formatting issues are resolved, commit and push your changes.`;
202+
📝 **To just check without fixing**:
203+
\`\`\`bash
204+
make lint
205+
\`\`\`
206+
207+
🔗 **View detailed linting output**: [Ruff Check Run #${run_id}](${run_url})
143208
144-
github.rest.issues.createComment({
145-
owner: repo.owner,
146-
repo: repo.repo,
147-
issue_number: issue_number,
148-
body: commentBody
149-
});
209+
---
210+
*Note: This is an automated review for code formatting consistency. The PR can still be merged even with these formatting issues.*`;
211+
212+
// 先尝试更新已有的评论(如果存在)
213+
try {
214+
const comments = await github.rest.issues.listComments({
215+
owner: repo.owner,
216+
repo: repo.repo,
217+
issue_number: issue_number,
218+
});
219+
220+
const botComment = comments.data.find(comment =>
221+
comment.user.login.includes('github-actions') ||
222+
comment.body.includes('Ruff Format Review')
223+
);
224+
225+
if (botComment) {
226+
await github.rest.issues.updateComment({
227+
owner: repo.owner,
228+
repo: repo.repo,
229+
comment_id: botComment.id,
230+
body: commentBody
231+
});
232+
console.log("✅ Updated existing comment");
233+
} else {
234+
await github.rest.issues.createComment({
235+
owner: repo.owner,
236+
repo: repo.repo,
237+
issue_number: issue_number,
238+
body: commentBody
239+
});
240+
console.log("✅ Created new comment");
241+
}
242+
} catch (error) {
243+
console.error("Error managing comment:", error);
244+
// 如果出错,仍然尝试创建新评论
245+
await github.rest.issues.createComment({
246+
owner: repo.owner,
247+
repo: repo.repo,
248+
issue_number: issue_number,
249+
body: commentBody
250+
});
251+
}
150252
151253
- name: Create PR Comment on Success
152254
if: ${{ github.event_name == 'pull_request' && steps.ruff-format.outputs.ruff_format_passed == 'true' }}
@@ -157,13 +259,51 @@ jobs:
157259
const issue_number = context.issue.number;
158260
const repo = context.repo;
159261
160-
const commentBody = `## ✅ Ruff Format Check Passed
262+
const commentBody = `## ✅ Ruff Format Review Passed
263+
264+
✅ **All Python files follow the project's formatting standards!**
265+
266+
Great work maintaining code consistency! 🎉
267+
268+
---
269+
*Note: This is an automated review for code formatting. Your code follows the project's Ruff configuration.*`;
270+
271+
// 清理可能存在的失败评论(如果之前有失败,现在修复了)
272+
try {
273+
const comments = await github.rest.issues.listComments({
274+
owner: repo.owner,
275+
repo: repo.repo,
276+
issue_number: issue_number,
277+
});
161278
162-
All Python files follow the required formatting standards. Great work!`;
279+
const botComment = comments.data.find(comment =>
280+
comment.user.login.includes('github-actions') ||
281+
comment.body.includes('Ruff Format Review')
282+
);
163283
164-
github.rest.issues.createComment({
165-
owner: repo.owner,
166-
repo: repo.repo,
167-
issue_number: issue_number,
168-
body: commentBody
169-
});
284+
if (botComment) {
285+
await github.rest.issues.updateComment({
286+
owner: repo.owner,
287+
repo: repo.repo,
288+
comment_id: botComment.id,
289+
body: commentBody
290+
});
291+
console.log("✅ Updated existing comment");
292+
} else {
293+
await github.rest.issues.createComment({
294+
owner: repo.owner,
295+
repo: repo.repo,
296+
issue_number: issue_number,
297+
body: commentBody
298+
});
299+
console.log("✅ Created new comment");
300+
}
301+
} catch (error) {
302+
console.error("Error managing comment:", error);
303+
await github.rest.issues.createComment({
304+
owner: repo.owner,
305+
repo: repo.repo,
306+
issue_number: issue_number,
307+
body: commentBody
308+
});
309+
}

0 commit comments

Comments
 (0)