Skip to content

Commit 74cb603

Browse files
committed
.github/workflows: Add Action to check linting and formatting
- Runs on PRs; if linting and formatting fixes found it writes a comment describing how to apply and detailing the diff
1 parent 55ba565 commit 74cb603

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

.github/workflows/ruff.yml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
name: Ruff
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
paths:
7+
- '**.py'
8+
- 'pyproject.toml'
9+
- '.github/workflows/ruff.yml'
10+
11+
jobs:
12+
ruff:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: '3.11'
21+
22+
- name: Install Ruff
23+
run: pip install ruff
24+
25+
- name: Run Ruff format check
26+
run: ruff format --check . --diff
27+
28+
- name: Run Ruff linter
29+
run: ruff check . --output-format=github
30+
31+
# Optional: Create a comment on PR with formatting suggestions
32+
ruff-suggestions:
33+
if: github.event_name == 'pull_request' && failure()
34+
needs: ruff
35+
runs-on: ubuntu-latest
36+
permissions:
37+
pull-requests: write
38+
steps:
39+
- uses: actions/checkout@v4
40+
41+
- name: Set up Python
42+
uses: actions/setup-python@v5
43+
with:
44+
python-version: '3.11'
45+
46+
- name: Install Ruff
47+
run: pip install ruff
48+
49+
- name: Generate fix suggestions
50+
run: |
51+
echo "## 🎨 Ruff Formatting & Linting Report" >> suggestions.md
52+
echo "" >> suggestions.md
53+
echo "Run the following commands locally to fix issues:" >> suggestions.md
54+
echo "" >> suggestions.md
55+
echo '```bash' >> suggestions.md
56+
echo "ruff format ." >> suggestions.md
57+
echo "ruff check . --fix" >> suggestions.md
58+
echo '```' >> suggestions.md
59+
echo "" >> suggestions.md
60+
61+
# Show what would be changed
62+
echo "### Formatting changes needed:" >> suggestions.md
63+
echo '```diff' >> suggestions.md
64+
ruff format --check . --diff >> suggestions.md 2>&1 || true
65+
echo '```' >> suggestions.md
66+
echo "" >> suggestions.md
67+
68+
# Show linting issues
69+
echo "### Linting issues:" >> suggestions.md
70+
echo '```' >> suggestions.md
71+
ruff check . >> suggestions.md 2>&1 || true
72+
echo '```' >> suggestions.md
73+
74+
- name: Comment PR
75+
uses: actions/github-script@v7
76+
with:
77+
script: |
78+
const fs = require('fs');
79+
const suggestions = fs.readFileSync('suggestions.md', 'utf8');
80+
81+
// Find existing comment
82+
const { data: comments } = await github.rest.issues.listComments({
83+
owner: context.repo.owner,
84+
repo: context.repo.repo,
85+
issue_number: context.issue.number,
86+
});
87+
88+
const botComment = comments.find(comment =>
89+
comment.user.type === 'Bot' &&
90+
comment.body.includes('🎨 Ruff Formatting & Linting Report')
91+
);
92+
93+
if (botComment) {
94+
// Update existing comment
95+
await github.rest.issues.updateComment({
96+
owner: context.repo.owner,
97+
repo: context.repo.repo,
98+
comment_id: botComment.id,
99+
body: suggestions
100+
});
101+
} else {
102+
// Create new comment
103+
await github.rest.issues.createComment({
104+
owner: context.repo.owner,
105+
repo: context.repo.repo,
106+
issue_number: context.issue.number,
107+
body: suggestions
108+
});
109+
}

0 commit comments

Comments
 (0)