forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
245 lines (216 loc) · 10.1 KB
/
lint_pr.yml
File metadata and controls
245 lines (216 loc) · 10.1 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
name: lint_pull_request
on: [pull_request, push]
jobs:
check_changes:
runs-on: ubuntu-24.04
outputs:
has_python_changes: ${{ steps.changed-files.outputs.has_python_changes }}
files: ${{ steps.changed-files.outputs.files }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # To get all history for git diff commands
- name: Get changed Python files
id: changed-files
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
# For PRs, compare against base branch
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRT origin/${{ github.base_ref }} HEAD | grep '\.py$' | grep -v "^setup\.py$" || echo "")
# Check if setup.py specifically changed
SETUP_PY_CHANGED=$(git diff --name-only --diff-filter=ACMRT origin/${{ github.base_ref }} HEAD | grep "^setup\.py$" || echo "")
if [ ! -z "$SETUP_PY_CHANGED" ]; then
CHANGED_FILES="$CHANGED_FILES $SETUP_PY_CHANGED"
fi
else
# For pushes, use the before/after SHAs
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.before }} ${{ github.event.after }} | grep '\.py$' | grep -v "^setup\.py$" || echo "")
# Check if setup.py specifically changed
SETUP_PY_CHANGED=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.before }} ${{ github.event.after }} | grep "^setup\.py$" || echo "")
if [ ! -z "$SETUP_PY_CHANGED" ]; then
CHANGED_FILES="$CHANGED_FILES $SETUP_PY_CHANGED"
fi
fi
# Check if any Python files were changed and set the output accordingly
if [ -z "$CHANGED_FILES" ]; then
echo "No Python files changed"
echo "has_python_changes=false" >> $GITHUB_OUTPUT
echo "files=" >> $GITHUB_OUTPUT
else
echo "Changed Python files: $CHANGED_FILES"
echo "has_python_changes=true" >> $GITHUB_OUTPUT
echo "files=$CHANGED_FILES" >> $GITHUB_OUTPUT
fi
- name: PR information
if: ${{ github.event_name == 'pull_request' }}
run: |
if [[ "${{ steps.changed-files.outputs.has_python_changes }}" == "false" ]]; then
echo "This PR contains no Python changes, but still requires manual approval."
else
echo "This PR contains Python changes that will be linted."
fi
lint:
needs: check_changes
if: ${{ needs.check_changes.outputs.has_python_changes == 'true' }}
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
tool: [flake8, format, mypy, pytest, pyupgrade, tox]
steps:
# Additional check to ensure we have Python files before proceeding
- name: Verify Python changes
run: |
if [[ "${{ needs.check_changes.outputs.has_python_changes }}" != "true" ]]; then
echo "No Python files were changed. Skipping linting."
exit 0
fi
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-python@v4
with:
python-version: 3.12
- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements-dev.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
# Flake8 linting
- name: Lint with flake8
if: ${{ matrix.tool == 'flake8' }}
id: flake8
run: |
echo "Linting files: ${{ needs.check_changes.outputs.files }}"
flake8 ${{ needs.check_changes.outputs.files }} --count --show-source --statistics
# Format checking with isort and black
- name: Format check
if: ${{ matrix.tool == 'format' }}
id: format
run: |
echo "Checking format with isort for: ${{ needs.check_changes.outputs.files }}"
isort --profile black --check ${{ needs.check_changes.outputs.files }}
echo "Checking format with black for: ${{ needs.check_changes.outputs.files }}"
black --check ${{ needs.check_changes.outputs.files }}
# Type checking with mypy
- name: Type check with mypy
if: ${{ matrix.tool == 'mypy' }}
id: mypy
run: |
echo "Type checking: ${{ needs.check_changes.outputs.files }}"
mypy --ignore-missing-imports ${{ needs.check_changes.outputs.files }}
# Run tests with pytest
- name: Run tests with pytest
if: ${{ matrix.tool == 'pytest' }}
id: pytest
run: |
echo "Running pytest discovery..."
python -m pytest --collect-only -v
# First run any test files that correspond to changed files
echo "Running tests for changed files..."
changed_files="${{ needs.check_changes.outputs.files }}"
# Extract module paths from changed files
modules=()
for file in $changed_files; do
# Convert file path to module path (remove .py and replace / with .)
if [[ $file == patterns/* ]]; then
module_path=${file%.py}
module_path=${module_path//\//.}
modules+=("$module_path")
fi
done
# Run tests for each module
for module in "${modules[@]}"; do
echo "Testing module: $module"
python -m pytest -xvs tests/ -k "$module" || true
done
# Then run doctests on the changed files
echo "Running doctests for changed files..."
for file in $changed_files; do
if [[ $file == *.py ]]; then
echo "Running doctest for $file"
python -m pytest --doctest-modules -v $file || true
fi
done
# Check Python version compatibility
- name: Check Python version compatibility
if: ${{ matrix.tool == 'pyupgrade' }}
id: pyupgrade
run: pyupgrade --py312-plus ${{ needs.check_changes.outputs.files }}
# Run tox
- name: Run tox
if: ${{ matrix.tool == 'tox' }}
id: tox
run: |
echo "Running tox integration for changed files..."
changed_files="${{ needs.check_changes.outputs.files }}"
# Create a temporary tox configuration that extends the original one
echo "[tox]" > tox_pr.ini
echo "envlist = py312" >> tox_pr.ini
echo "skip_missing_interpreters = true" >> tox_pr.ini
echo "[testenv]" >> tox_pr.ini
echo "setenv =" >> tox_pr.ini
echo " COVERAGE_FILE = .coverage.{envname}" >> tox_pr.ini
echo "deps =" >> tox_pr.ini
echo " -r requirements-dev.txt" >> tox_pr.ini
echo "allowlist_externals =" >> tox_pr.ini
echo " pytest" >> tox_pr.ini
echo " coverage" >> tox_pr.ini
echo "commands =" >> tox_pr.ini
# Always run a baseline test with coverage to ensure we have some data
echo " pytest -xvs --cov=patterns tests/ --cov-report=term-missing || true" >> tox_pr.ini
# Add coverage-focused test commands
echo " # Run specific tests for changed files" >> tox_pr.ini
for file in $changed_files; do
if [[ $file == *.py ]]; then
# Run coverage tests for implementation files
if [[ $file == patterns/* ]]; then
module_name=$(basename $file .py)
pattern_dir=$(dirname $file | cut -d'/' -f2)
echo " # Testing $file" >> tox_pr.ini
# First check if there's a specific test for this module
echo " pytest -xvs --cov=patterns --cov-append tests/${pattern_dir}/test_${module_name}.py 2>/dev/null || true" >> tox_pr.ini
# Also run more general tests for the directory
echo " pytest -xvs --cov=patterns --cov-append tests/${pattern_dir}/ -k \"${module_name}\" || true" >> tox_pr.ini
fi
# Run test files directly if modified
if [[ $file == tests/* ]]; then
echo " pytest -xvs --cov=patterns --cov-append $file || true" >> tox_pr.ini
fi
fi
done
# Run doctests on pattern files
echo " # Run doctests" >> tox_pr.ini
for file in $changed_files; do
if [[ $file == patterns/*.py ]]; then
echo " pytest --doctest-modules -v --cov=patterns --cov-append $file || true" >> tox_pr.ini
fi
done
# Add coverage report command (only executed if there's coverage data)
echo " coverage combine || true" >> tox_pr.ini
echo " coverage report || echo 'No coverage data available'" >> tox_pr.ini
# Run tox with the custom configuration
echo "Running tox with custom PR configuration..."
cat tox_pr.ini
tox -c tox_pr.ini || true # Don't fail the build if tox fails
summary:
needs: [check_changes, lint]
# Run summary in all cases, regardless of whether lint job ran
if: ${{ always() }}
runs-on: ubuntu-24.04
steps:
- name: Summarize results
run: |
echo "## Pull Request Lint Results" >> $GITHUB_STEP_SUMMARY
if [[ "${{ needs.check_changes.outputs.has_python_changes }}" == "true" ]]; then
echo "Linting has completed for all Python files changed in this PR." >> $GITHUB_STEP_SUMMARY
echo "See individual job logs for detailed results." >> $GITHUB_STEP_SUMMARY
else
echo "No Python files were changed in this PR. Linting was skipped." >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "⚠️ **Note:** This PR still requires manual approval regardless of linting results." >> $GITHUB_STEP_SUMMARY