Skip to content

Commit ce40583

Browse files
committed
Optimzed lint_pr.yml
1 parent b14815c commit ce40583

File tree

1 file changed

+69
-25
lines changed

1 file changed

+69
-25
lines changed

.github/workflows/lint_pr.yml

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
name: lint_pull_request
22
on: [pull_request, push]
33
jobs:
4-
lint_pull_request:
4+
check_changes:
55
runs-on: ubuntu-24.04
6+
outputs:
7+
has_python_changes: ${{ steps.changed-files.outputs.has_python_changes }}
8+
files: ${{ steps.changed-files.outputs.files }}
69
steps:
710
- uses: actions/checkout@v3
811
with:
@@ -30,48 +33,89 @@ jobs:
3033
- name: PR information
3134
if: ${{ github.event_name == 'pull_request' && steps.changed-files.outputs.has_python_changes == 'false' }}
3235
run: echo "This PR contains no Python changes, but still requires manual approval."
36+
37+
lint:
38+
needs: check_changes
39+
if: ${{ needs.check_changes.outputs.has_python_changes == 'true' }}
40+
runs-on: ubuntu-24.04
41+
strategy:
42+
fail-fast: false
43+
matrix:
44+
tool: [flake8, format, mypy, pytest, pyupgrade, tox]
45+
steps:
46+
- uses: actions/checkout@v3
47+
with:
48+
fetch-depth: 0
3349

3450
- uses: actions/setup-python@v4
35-
if: ${{ steps.changed-files.outputs.has_python_changes == 'true' }}
3651
with:
3752
python-version: 3.12
3853

54+
- uses: actions/cache@v3
55+
with:
56+
path: ~/.cache/pip
57+
key: ${{ runner.os }}-pip-${{ hashFiles('requirements-dev.txt') }}
58+
restore-keys: |
59+
${{ runner.os }}-pip-
60+
3961
- name: Install dependencies
40-
if: ${{ steps.changed-files.outputs.has_python_changes == 'true' }}
4162
run: |
4263
python -m pip install --upgrade pip
4364
pip install -r requirements-dev.txt
44-
65+
66+
# Flake8 linting
4567
- name: Lint with flake8
46-
if: ${{ steps.changed-files.outputs.has_python_changes == 'true' }}
68+
if: ${{ matrix.tool == 'flake8' }}
69+
id: flake8
4770
run: |
48-
echo "Linting files: ${{ steps.changed-files.outputs.files }}"
49-
flake8 ${{ steps.changed-files.outputs.files }} --count --show-source --statistics
50-
51-
- name: Format check with isort and black
52-
if: ${{ steps.changed-files.outputs.has_python_changes == 'true' }}
71+
echo "Linting files: ${{ needs.check_changes.outputs.files }}"
72+
flake8 ${{ needs.check_changes.outputs.files }} --count --show-source --statistics
73+
74+
# Format checking with isort and black
75+
- name: Format check
76+
if: ${{ matrix.tool == 'format' }}
77+
id: format
5378
run: |
54-
echo "Checking format with isort for: ${{ steps.changed-files.outputs.files }}"
55-
isort --profile black --check ${{ steps.changed-files.outputs.files }}
56-
echo "Checking format with black for: ${{ steps.changed-files.outputs.files }}"
57-
black --check ${{ steps.changed-files.outputs.files }}
58-
79+
echo "Checking format with isort for: ${{ needs.check_changes.outputs.files }}"
80+
isort --profile black --check ${{ needs.check_changes.outputs.files }}
81+
echo "Checking format with black for: ${{ needs.check_changes.outputs.files }}"
82+
black --check ${{ needs.check_changes.outputs.files }}
83+
84+
# Type checking with mypy
5985
- name: Type check with mypy
60-
if: ${{ steps.changed-files.outputs.has_python_changes == 'true' }}
86+
if: ${{ matrix.tool == 'mypy' }}
87+
id: mypy
6188
run: |
62-
echo "Type checking: ${{ steps.changed-files.outputs.files }}"
63-
mypy --ignore-missing-imports ${{ steps.changed-files.outputs.files }}
64-
89+
echo "Type checking: ${{ needs.check_changes.outputs.files }}"
90+
mypy --ignore-missing-imports ${{ needs.check_changes.outputs.files }}
91+
92+
# Run tests with pytest
6593
- name: Run tests with pytest
66-
if: ${{ steps.changed-files.outputs.has_python_changes == 'true' }}
94+
if: ${{ matrix.tool == 'pytest' }}
95+
id: pytest
6796
run: |
6897
pytest ./patterns
6998
pytest --doctest-modules ./patterns || true
70-
99+
100+
# Check Python version compatibility
71101
- name: Check Python version compatibility
72-
if: ${{ steps.changed-files.outputs.has_python_changes == 'true' }}
73-
run: pyupgrade --py312-plus ${{ steps.changed-files.outputs.files }}
74-
102+
if: ${{ matrix.tool == 'pyupgrade' }}
103+
id: pyupgrade
104+
run: pyupgrade --py312-plus ${{ needs.check_changes.outputs.files }}
105+
106+
# Run tox
75107
- name: Run tox
76-
if: ${{ steps.changed-files.outputs.has_python_changes == 'true' }}
108+
if: ${{ matrix.tool == 'tox' }}
109+
id: tox
77110
run: tox
111+
112+
summary:
113+
needs: [check_changes, lint]
114+
if: ${{ always() && needs.check_changes.outputs.has_python_changes == 'true' }}
115+
runs-on: ubuntu-24.04
116+
steps:
117+
- name: Summarize results
118+
run: |
119+
echo "## Pull Request Lint Results" >> $GITHUB_STEP_SUMMARY
120+
echo "Linting has completed for all Python files changed in this PR." >> $GITHUB_STEP_SUMMARY
121+
echo "See individual job logs for detailed results." >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)