|
| 1 | +name: Python Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [ opened, synchronize, reopened ] |
| 6 | + push: |
| 7 | + branches: [ master ] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: static-python-check-${{ github.ref }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + security-events: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + detect: |
| 20 | + name: Python Changes Detection |
| 21 | + runs-on: ubuntu-latest |
| 22 | + outputs: |
| 23 | + python_changed: ${{ steps.changes.outputs.python_changed }} |
| 24 | + steps: |
| 25 | + - name: Checkout repository |
| 26 | + uses: actions/checkout@v5 |
| 27 | + with: |
| 28 | + persist-credentials: false |
| 29 | + fetch-depth: 0 |
| 30 | + |
| 31 | + - name: Check if Python files changed |
| 32 | + id: changes |
| 33 | + shell: bash |
| 34 | + run: | |
| 35 | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then |
| 36 | + RANGE="${{ github.event.pull_request.base.sha }}...${{ github.sha }}" |
| 37 | + else |
| 38 | + RANGE="${{ github.sha }}~1...${{ github.sha }}" |
| 39 | + fi |
| 40 | + if git diff --name-only "$RANGE" -- '*.py' | grep -q .; then |
| 41 | + echo "python_changed=true" >> "$GITHUB_OUTPUT" |
| 42 | + else |
| 43 | + echo "python_changed=false" >> "$GITHUB_OUTPUT" |
| 44 | + fi |
| 45 | +
|
| 46 | + pylint-analysis: |
| 47 | + name: Pylint Static Code Analysis |
| 48 | + needs: detect |
| 49 | + if: needs.detect.outputs.python_changed == 'true' |
| 50 | + runs-on: ubuntu-latest |
| 51 | + steps: |
| 52 | + - name: Checkout repository |
| 53 | + uses: actions/checkout@v5 |
| 54 | + with: |
| 55 | + persist-credentials: false |
| 56 | + fetch-depth: 0 |
| 57 | + |
| 58 | + - name: Set up Python |
| 59 | + uses: actions/setup-python@v6 |
| 60 | + with: |
| 61 | + python-version: '3.13' |
| 62 | + cache: 'pip' |
| 63 | + |
| 64 | + - name: Install dependencies |
| 65 | + run: pip install -r requirements.txt |
| 66 | + |
| 67 | + - name: Analyze code with Pylint |
| 68 | + id: analyze-code |
| 69 | + run: | |
| 70 | + pylint_score=$(pylint $(git ls-files '*.py')| grep 'rated at' | awk '{print $7}' | cut -d'/' -f1) |
| 71 | + echo "PYLINT_SCORE=$pylint_score" >> $GITHUB_ENV |
| 72 | +
|
| 73 | + - name: Check Pylint score |
| 74 | + run: | |
| 75 | + if (( $(echo "$PYLINT_SCORE < 9.5" | bc -l) )); then |
| 76 | + echo "Failure: Pylint score is below 9.5 (project score: $PYLINT_SCORE)." |
| 77 | + exit 1 |
| 78 | + else |
| 79 | + echo "Success: Pylint score is above 9.5 (project score: $PYLINT_SCORE)." |
| 80 | + fi |
| 81 | +
|
| 82 | + black-check: |
| 83 | + name: Black Format Check |
| 84 | + needs: detect |
| 85 | + if: needs.detect.outputs.python_changed == 'true' |
| 86 | + runs-on: ubuntu-latest |
| 87 | + steps: |
| 88 | + - name: Checkout repository |
| 89 | + uses: actions/checkout@v5 |
| 90 | + with: |
| 91 | + persist-credentials: false |
| 92 | + fetch-depth: 0 |
| 93 | + |
| 94 | + - name: Set up Python |
| 95 | + uses: actions/setup-python@v6 |
| 96 | + with: |
| 97 | + python-version: '3.13' |
| 98 | + cache: 'pip' |
| 99 | + |
| 100 | + - name: Install dependencies |
| 101 | + run: pip install -r requirements.txt |
| 102 | + |
| 103 | + - name: Check code format with Black |
| 104 | + id: check-format |
| 105 | + run: black --check $(git ls-files '*.py') |
| 106 | + |
| 107 | + pytest-test: |
| 108 | + name: Pytest Unit Tests with Coverage |
| 109 | + needs: detect |
| 110 | + if: needs.detect.outputs.python_changed == 'true' |
| 111 | + runs-on: ubuntu-latest |
| 112 | + steps: |
| 113 | + - name: Checkout repository |
| 114 | + uses: actions/checkout@v5 |
| 115 | + with: |
| 116 | + persist-credentials: false |
| 117 | + fetch-depth: 0 |
| 118 | + |
| 119 | + - uses: actions/setup-python@v6 |
| 120 | + with: |
| 121 | + python-version: '3.13' |
| 122 | + cache: 'pip' |
| 123 | + |
| 124 | + - name: Install Python dependencies |
| 125 | + run: pip install -r requirements.txt |
| 126 | + |
| 127 | + - name: Check code coverage with Pytest |
| 128 | + run: pytest --cov=. -v tests/ --cov-fail-under=80 |
| 129 | + |
| 130 | + mypy-check: |
| 131 | + name: Mypy Type Check |
| 132 | + needs: detect |
| 133 | + if: needs.detect.outputs.python_changed == 'true' |
| 134 | + runs-on: ubuntu-latest |
| 135 | + steps: |
| 136 | + - name: Checkout repository |
| 137 | + uses: actions/checkout@v5 |
| 138 | + with: |
| 139 | + persist-credentials: false |
| 140 | + fetch-depth: 0 |
| 141 | + |
| 142 | + - name: Set up Python |
| 143 | + uses: actions/setup-python@v6 |
| 144 | + with: |
| 145 | + python-version: '3.13' |
| 146 | + cache: 'pip' |
| 147 | + |
| 148 | + - name: Install dependencies |
| 149 | + run: pip install -r requirements.txt |
| 150 | + |
| 151 | + - name: Check types with Mypy |
| 152 | + id: check-types |
| 153 | + run: mypy . |
| 154 | + |
| 155 | + noop: |
| 156 | + name: No Operation |
| 157 | + needs: detect |
| 158 | + if: needs.detect.outputs.python_changed != 'true' |
| 159 | + runs-on: ubuntu-latest |
| 160 | + steps: |
| 161 | + - run: echo "No changes in the *.py files — passing." |
0 commit comments