-
Notifications
You must be signed in to change notification settings - Fork 5.5k
154 lines (132 loc) · 5.51 KB
/
Copy pathlint-format.yml
File metadata and controls
154 lines (132 loc) · 5.51 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
name: Lint and Format Check
on:
pull_request:
paths:
- '**/*.py'
- '**/*.ipynb'
- 'pyproject.toml'
- 'uv.lock'
- 'Makefile'
push:
branches: [main]
paths:
- '**/*.py'
- '**/*.ipynb'
permissions:
contents: read
pull-requests: write
jobs:
lint-and-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v4
with:
enable-cache: true
cache-dependency-glob: "uv.lock"
- name: Set up Python 3.11
run: uv python install 3.11
- name: Install dependencies
run: |
uv sync --frozen --all-extras
- name: Get changed files
id: changed-files
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
# For PRs, get files changed compared to base branch
git fetch origin ${{ github.base_ref }}
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD | grep -E '\.(py|ipynb)$' || echo "")
else
# For push to main, check all files
CHANGED_FILES=$(find . -type f \( -name "*.py" -o -name "*.ipynb" \) -not -path "./.venv/*" -not -path "./venv/*" -not -path "*/build/*" -not -path "*/dist/*")
fi
if [ -z "$CHANGED_FILES" ]; then
echo "No Python or notebook files changed"
echo "has_files=false" >> $GITHUB_OUTPUT
echo "" > changed_files.txt
else
echo "Changed files:"
echo "$CHANGED_FILES"
echo "$CHANGED_FILES" > changed_files.txt
echo "has_files=true" >> $GITHUB_OUTPUT
fi
- name: Check formatting with ruff
id: format-check
if: steps.changed-files.outputs.has_files == 'true'
run: |
echo "## Formatting Check Results" >> $GITHUB_STEP_SUMMARY
CHANGED_FILES=$(cat changed_files.txt)
if [ -z "$CHANGED_FILES" ]; then
echo "✅ No files to check" >> $GITHUB_STEP_SUMMARY
echo "has_format_issues=false" >> $GITHUB_OUTPUT
exit 0
fi
if echo "$CHANGED_FILES" | xargs uv run ruff format --check; then
echo "✅ All changed files are properly formatted" >> $GITHUB_STEP_SUMMARY
echo "has_format_issues=false" >> $GITHUB_OUTPUT
else
echo "❌ Some changed files need formatting" >> $GITHUB_STEP_SUMMARY
echo "has_format_issues=true" >> $GITHUB_OUTPUT
exit 1
fi
continue-on-error: true
- name: Run ruff linting
id: lint-check
if: steps.changed-files.outputs.has_files == 'true'
run: |
echo "## Linting Check Results" >> $GITHUB_STEP_SUMMARY
CHANGED_FILES=$(cat changed_files.txt)
if [ -z "$CHANGED_FILES" ]; then
echo "✅ No files to check" >> $GITHUB_STEP_SUMMARY
echo "has_lint_issues=false" >> $GITHUB_OUTPUT
exit 0
fi
if echo "$CHANGED_FILES" | xargs uv run ruff check; then
echo "✅ No linting issues found in changed files" >> $GITHUB_STEP_SUMMARY
echo "has_lint_issues=false" >> $GITHUB_OUTPUT
else
echo "❌ Linting issues found in changed files" >> $GITHUB_STEP_SUMMARY
echo "has_lint_issues=true" >> $GITHUB_OUTPUT
echo "$CHANGED_FILES" | xargs uv run ruff check 2>&1 | tee lint_output.txt || true
exit 1
fi
continue-on-error: true
- name: Post lint/format issues to PR
if: |
github.event_name == 'pull_request' &&
github.event.pull_request.head.repo.full_name == github.repository &&
(steps.format-check.outputs.has_format_issues == 'true' ||
steps.lint-check.outputs.has_lint_issues == 'true')
uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
github_token: ${{ secrets.GITHUB_TOKEN }}
prompt: |
The linting and formatting checks found issues.
Format check status: ${{ steps.format-check.outputs.has_format_issues == 'true' && 'Failed' || 'Passed' }}
Lint check status: ${{ steps.lint-check.outputs.has_lint_issues == 'true' && 'Failed' || 'Passed' }}
${{ steps.lint-check.outputs.has_lint_issues == 'true' && format('Linting issues:\n```\n{0}\n```', steps.lint-check.outputs.lint_output) || '' }}
Create a helpful PR comment that:
- Summarizes the formatting and linting issues
- Explains how to fix them locally using: `make fix`
- Mentions they can check before pushing with: `make check`
- Uses friendly, constructive language
Post using: gh pr comment $PR_NUMBER --body "your comment"
claude_args: |
--allowedTools "Bash(gh pr comment:*)"
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
- name: Fail if issues found
if: |
steps.format-check.outputs.has_format_issues == 'true' ||
steps.lint-check.outputs.has_lint_issues == 'true'
run: |
echo "❌ Linting or formatting issues found. Run 'make fix' locally to resolve."
exit 1
- name: Success
if: |
steps.format-check.outputs.has_format_issues == 'false' &&
steps.lint-check.outputs.has_lint_issues == 'false'
run: |
echo "✅ All linting and formatting checks passed!"