Skip to content

Commit fb5d048

Browse files
committed
feat: add automated code quality tools and pre-commit hooks
- Add pre-commit configuration with black, isort, and ruff - Add auto-format GitHub Actions workflow to auto-fix PRs - Update pyproject.toml with tool configurations - Add Makefile commands for pre-commit management - Add comprehensive CODE_QUALITY.md documentation Benefits: - Automatically formats code on every commit - Auto-fixes formatting issues in PRs via GitHub Actions - Ensures consistent code style across the project - Reduces manual formatting work Usage: make pre-commit-install # One-time setup git commit # Auto-formats on commit
1 parent e159dc9 commit fb5d048

File tree

5 files changed

+470
-2
lines changed

5 files changed

+470
-2
lines changed

.github/CODE_QUALITY.md

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
# 代码质量和自动格式化指南
2+
3+
本项目使用多种工具来保证代码质量和一致性。
4+
5+
## 🎯 快速开始
6+
7+
### 1. 安装 Pre-commit Hooks (推荐)
8+
9+
这是**最简单的方式**,可以在每次 `git commit` 时自动格式化代码:
10+
11+
```bash
12+
# 安装 hooks
13+
make pre-commit-install
14+
15+
# 或直接使用
16+
uv run pre-commit install
17+
```
18+
19+
**安装后,每次提交时会自动:**
20+
21+
- ✅ 格式化代码 (black)
22+
- ✅ 排序 imports (isort)
23+
- ✅ 删除行尾空格
24+
- ✅ 检查 YAML/TOML 语法
25+
- ✅ 运行 ruff linter
26+
27+
### 2. 手动格式化代码
28+
29+
```bash
30+
# 格式化所有代码
31+
make format
32+
33+
# 或单独运行
34+
uv run black .
35+
uv run isort .
36+
```
37+
38+
### 3. 检查代码(不修改)
39+
40+
```bash
41+
# 检查格式但不修改
42+
make check
43+
44+
#
45+
uv run black --check .
46+
uv run isort --check-only .
47+
```
48+
49+
## 🔧 工具说明
50+
51+
### Black - 代码格式化器
52+
53+
配置在 `pyproject.toml`:
54+
55+
```toml
56+
[tool.black]
57+
line-length = 88
58+
target-version = ['py310', 'py311', 'py312', 'py313']
59+
```
60+
61+
使用:
62+
63+
```bash
64+
# 格式化所有文件
65+
uv run black .
66+
67+
# 检查但不修改
68+
uv run black --check .
69+
70+
# 格式化特定文件
71+
uv run black services/hyperliquid_services.py
72+
```
73+
74+
### isort - Import 排序
75+
76+
配置在 `pyproject.toml`:
77+
78+
```toml
79+
[tool.isort]
80+
profile = "black"
81+
line_length = 88
82+
```
83+
84+
使用:
85+
86+
```bash
87+
# 排序所有文件
88+
uv run isort .
89+
90+
# 检查但不修改
91+
uv run isort --check-only .
92+
```
93+
94+
### Ruff - 快速 Linter
95+
96+
配置在 `pyproject.toml`:
97+
98+
```toml
99+
[tool.ruff.lint]
100+
select = ["E", "W", "F", "I", "B", "C4", "UP"]
101+
```
102+
103+
使用:
104+
105+
```bash
106+
# 运行 linter
107+
uv run ruff check .
108+
109+
# 自动修复
110+
uv run ruff check . --fix
111+
```
112+
113+
### Pre-commit - Git Hooks
114+
115+
配置在 `.pre-commit-config.yaml`
116+
117+
使用:
118+
119+
```bash
120+
# 安装 hooks
121+
uv run pre-commit install
122+
123+
# 手动运行所有文件
124+
uv run pre-commit run --all-files
125+
126+
# 更新 hooks 到最新版本
127+
uv run pre-commit autoupdate
128+
129+
# 跳过 hooks 提交(不推荐)
130+
git commit --no-verify -m "message"
131+
```
132+
133+
## 🤖 自动化流程
134+
135+
### GitHub Actions - 自动格式化
136+
137+
PR 提交后,会自动运行 `.github/workflows/auto-format.yml`:
138+
139+
1. 自动格式化代码
140+
2. 如果有变更,自动提交并推送
141+
3. 在 PR 中添加评论通知
142+
143+
### GitHub Actions - CI 检查
144+
145+
PR 必须通过 `.github/workflows/ci.yml` 的所有检查:
146+
147+
-`test (3.10)` - Python 3.10 测试
148+
-`test (3.11)` - Python 3.11 测试
149+
-`test (3.12)` - Python 3.12 测试
150+
-`test (3.13)` - Python 3.13 测试
151+
-`lint` - 代码质量检查
152+
-`build` - 包构建
153+
154+
## 📋 开发工作流
155+
156+
### 推荐流程
157+
158+
```bash
159+
# 1. 安装 pre-commit (只需一次)
160+
make pre-commit-install
161+
162+
# 2. 开发代码
163+
# ... 编写代码 ...
164+
165+
# 3. 提交代码 (会自动格式化)
166+
git add .
167+
git commit -m "feat: add new feature"
168+
169+
# 4. 如果 pre-commit 修改了文件
170+
git add .
171+
git commit -m "feat: add new feature"
172+
173+
# 5. 推送到 GitHub
174+
git push origin feature-branch
175+
176+
# 6. 创建 PR - CI 会自动检查
177+
```
178+
179+
### 手动流程(如果不使用 pre-commit)
180+
181+
```bash
182+
# 1. 开发代码
183+
# ... 编写代码 ...
184+
185+
# 2. 格式化代码
186+
make format
187+
188+
# 3. 检查是否通过
189+
make check
190+
191+
# 4. 提交
192+
git add .
193+
git commit -m "feat: add new feature"
194+
git push
195+
```
196+
197+
## 🚨 常见问题
198+
199+
### Q: Pre-commit 失败怎么办?
200+
201+
A: Pre-commit 失败后会自动修复文件,只需重新添加并提交:
202+
203+
```bash
204+
# 1. pre-commit 运行并修复文件
205+
git add .
206+
207+
# 2. 再次提交
208+
git commit -m "your message"
209+
```
210+
211+
### Q: 如何跳过 pre-commit 检查?
212+
213+
A: 不推荐,但紧急情况下可以:
214+
215+
```bash
216+
git commit --no-verify -m "emergency fix"
217+
```
218+
219+
### Q: CI 失败说代码格式不对?
220+
221+
A: 运行格式化并重新提交:
222+
223+
```bash
224+
make format
225+
git add .
226+
git commit -m "style: format code"
227+
git push
228+
```
229+
230+
### Q: 如何在 VS Code 中自动格式化?
231+
232+
A: 安装扩展并配置:
233+
234+
1. 安装扩展:
235+
236+
- Black Formatter
237+
- isort
238+
239+
2.`.vscode/settings.json` 中:
240+
241+
```json
242+
{
243+
"python.formatting.provider": "black",
244+
"editor.formatOnSave": true,
245+
"[python]": {
246+
"editor.codeActionsOnSave": {
247+
"source.organizeImports": true
248+
}
249+
}
250+
}
251+
```
252+
253+
### Q: 如何更新 pre-commit hooks?
254+
255+
A: 定期运行:
256+
257+
```bash
258+
make pre-commit-update
259+
```
260+
261+
## 📚 相关资源
262+
263+
- [Black 文档](https://black.readthedocs.io/)
264+
- [isort 文档](https://pycqa.github.io/isort/)
265+
- [Ruff 文档](https://docs.astral.sh/ruff/)
266+
- [Pre-commit 文档](https://pre-commit.com/)
267+
268+
## 🎯 最佳实践
269+
270+
1.**始终使用 pre-commit** - 最简单的方式
271+
2.**提交前检查** - `make check`
272+
3.**保持工具更新** - `make pre-commit-update`
273+
4.**不要跳过检查** - 除非紧急情况
274+
5.**配置 IDE** - 保存时自动格式化
275+
276+
---
277+
278+
**快速命令参考:**
279+
280+
```bash
281+
make pre-commit-install # 安装 pre-commit hooks
282+
make format # 格式化代码
283+
make check # 检查代码
284+
make pre-commit-all # 运行所有 pre-commit 检查
285+
```

.github/workflows/auto-format.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Auto Format
2+
3+
on:
4+
pull_request:
5+
branches: [ main, develop ]
6+
7+
jobs:
8+
format:
9+
name: Auto format code
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
pull-requests: write
14+
15+
steps:
16+
- uses: actions/checkout@v5
17+
with:
18+
ref: ${{ github.head_ref }}
19+
token: ${{ secrets.GITHUB_TOKEN }}
20+
21+
- name: Install uv
22+
uses: astral-sh/setup-uv@v7
23+
with:
24+
version: "latest"
25+
26+
- name: Set up Python
27+
run: uv python install 3.12
28+
29+
- name: Install dependencies
30+
run: |
31+
uv sync --all-extras --dev
32+
33+
- name: Run black
34+
run: |
35+
uv run black .
36+
37+
- name: Run isort
38+
run: |
39+
uv run isort .
40+
41+
- name: Check for changes
42+
id: check_changes
43+
run: |
44+
if [ -n "$(git status --porcelain)" ]; then
45+
echo "has_changes=true" >> $GITHUB_OUTPUT
46+
else
47+
echo "has_changes=false" >> $GITHUB_OUTPUT
48+
fi
49+
50+
- name: Commit and push changes
51+
if: steps.check_changes.outputs.has_changes == 'true'
52+
run: |
53+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
54+
git config --local user.name "github-actions[bot]"
55+
git add .
56+
git commit -m "style: auto-format code with black and isort [skip ci]"
57+
git push
58+
59+
- name: Comment on PR
60+
if: steps.check_changes.outputs.has_changes == 'true'
61+
uses: actions/github-script@v7
62+
with:
63+
script: |
64+
github.rest.issues.createComment({
65+
issue_number: context.issue.number,
66+
owner: context.repo.owner,
67+
repo: context.repo.repo,
68+
body: '✨ Code has been automatically formatted with `black` and `isort`.'
69+
})

.pre-commit-config.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Pre-commit hooks 配置
2+
# 安装: uv run pre-commit install
3+
# 运行: uv run pre-commit run --all-files
4+
5+
repos:
6+
# Black - Python 代码格式化
7+
- repo: https://github.com/psf/black
8+
rev: 24.10.0
9+
hooks:
10+
- id: black
11+
language_version: python3.12
12+
13+
# isort - Import 排序
14+
- repo: https://github.com/PyCQA/isort
15+
rev: 5.13.2
16+
hooks:
17+
- id: isort
18+
args: ["--profile", "black"]
19+
20+
# 基础检查
21+
- repo: https://github.com/pre-commit/pre-commit-hooks
22+
rev: v5.0.0
23+
hooks:
24+
- id: trailing-whitespace # 删除行尾空格
25+
- id: end-of-file-fixer # 确保文件以换行符结束
26+
- id: check-yaml # 检查 YAML 语法
27+
- id: check-added-large-files # 防止提交大文件
28+
args: ['--maxkb=1000']
29+
- id: check-merge-conflict # 检查合并冲突标记
30+
- id: check-toml # 检查 TOML 语法
31+
- id: mixed-line-ending # 统一行尾格式
32+
args: ['--fix=lf']
33+
34+
# Ruff - 快速 Python linter
35+
- repo: https://github.com/astral-sh/ruff-pre-commit
36+
rev: v0.8.4
37+
hooks:
38+
- id: ruff
39+
args: [--fix, --exit-non-zero-on-fix]

0 commit comments

Comments
 (0)