-
Notifications
You must be signed in to change notification settings - Fork 1.1k
309 lines (259 loc) · 10.8 KB
/
Copy pathruff.yml
File metadata and controls
309 lines (259 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# GitHub Actions workflow for Ruff code formatting and linting
# 自动运行 Ruff 格式检查和修复
#
# 触发条件:
# 1. Push 到 main 分支时:自动格式化并提交
# 2. Pull Request 时:检查格式并标记问题(不自动提交)
name: Ruff Format Check
on:
push:
branches:
- main
paths:
- '**.py'
- 'pyproject.toml'
- 'Makefile'
- '.github/workflows/ruff.yml'
pull_request:
branches:
- main
paths:
- '**.py'
- 'pyproject.toml'
- 'Makefile'
# 设置写入权限,允许自动提交格式化代码
permissions:
contents: write
pull-requests: write
jobs:
ruff:
name: Ruff Format & Lint
runs-on: ubuntu-latest
steps:
# 1. 检出代码
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
# 2. 安装 uv(项目使用 uv 作为包管理器)
- name: Install uv
uses: astral-sh/setup-uv@v4
with:
version: "0.7.2"
# 3. 设置 Python 环境
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
# 4. 安装 ruff 依赖(根据项目配置,ruff 在 dev 依赖组中)
- name: Install dependencies
run: |
uv sync --group dev
# 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 [ $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 (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 .
echo "Running uv run ruff check . --fix"
uv run ruff check . --fix
echo "Running uv run python -m ruff check --select I src --fix"
uv run python -m ruff check --select I src --fix
# 检查是否有格式变更
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 with Format Issues
if: ${{ github.event_name == 'pull_request' && steps.ruff-format.outputs.ruff_format_passed == 'false' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const issue_number = context.issue.number;
const repo = context.repo;
// 获取工作流运行 ID 以链接到特定的检查运行
const run_id = process.env.GITHUB_RUN_ID;
const run_url = `https://github.com/${repo.owner}/${repo.repo}/actions/runs/${run_id}`;
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
# Run automatic formatting (same as make format)
make format
# Or manually with uv:
uv run ruff format .
uv run ruff check . --fix
uv run python -m ruff check --select I src --fix
\`\`\`
📝 **To just check without fixing**:
\`\`\`bash
make lint
\`\`\`
🔗 **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' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const issue_number = context.issue.number;
const repo = context.repo;
const commentBody = `## ✅ Ruff Format Review Passed
✅ **All Python files follow the project's formatting standards!**
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
});
}