|
| 1 | +# This workflow will install Python dependencies, run tests and lint with a variety of Python versions |
| 2 | +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions |
| 3 | + |
| 4 | +name: Latest commit |
| 5 | + |
| 6 | +env: |
| 7 | + CACHE_VERSION: 6 |
| 8 | + DEFAULT_PYTHON: "3.9" |
| 9 | + PRE_COMMIT_HOME: ~/.cache/pre-commit |
| 10 | + |
| 11 | +on: |
| 12 | + schedule: |
| 13 | + - cron: "2 4 * * 0" # weekly |
| 14 | + workflow_dispatch: |
| 15 | + push: |
| 16 | +# pull_request: |
| 17 | + |
| 18 | +jobs: |
| 19 | + # Prepare default python version environment |
| 20 | + prepare: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + name: Prepare |
| 23 | + steps: |
| 24 | + - name: Check out committed code |
| 25 | + uses: actions/checkout@v3 |
| 26 | + - name: Set up Python ${{ env.DEFAULT_PYTHON }} |
| 27 | + id: python |
| 28 | + uses: actions/setup-python@v4 |
| 29 | + with: |
| 30 | + python-version: ${{ env.DEFAULT_PYTHON }} |
| 31 | + - name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment |
| 32 | + id: cache-venv |
| 33 | + uses: actions/cache@v3 |
| 34 | + with: |
| 35 | + path: venv |
| 36 | + key: >- |
| 37 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ |
| 38 | + steps.python.outputs.python-version }}-${{ |
| 39 | + hashFiles('requirements_test.txt') }}-${{ |
| 40 | + hashFiles('setup.py') }} |
| 41 | + restore-keys: | |
| 42 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ steps.python.outputs.python-version }}-${{ hashFiles('requirements_test.txt') }}-${{ hashFiles('setup.py') }}- |
| 43 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ steps.python.outputs.python-version }}-${{ hashFiles('requirements_test.txt') }} |
| 44 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ steps.python.outputs.python-version }}- |
| 45 | + - name: Create Python virtual environment |
| 46 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 47 | + run: | |
| 48 | + pip install virtualenv --upgrade |
| 49 | + python -m venv venv |
| 50 | + . venv/bin/activate |
| 51 | + pip install -U pip setuptools wheel |
| 52 | + pip install -r requirements_test.txt -r requirements_commit.txt |
| 53 | + - name: Restore pre-commit environment from cache |
| 54 | + id: cache-precommit |
| 55 | + uses: actions/cache@v3 |
| 56 | + with: |
| 57 | + path: ${{ env.PRE_COMMIT_HOME }} |
| 58 | + key: | |
| 59 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }} |
| 60 | + restore-keys: | |
| 61 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-pre-commit- |
| 62 | + - name: Install pre-commit dependencies |
| 63 | + if: steps.cache-precommit.outputs.cache-hit != 'true' |
| 64 | + run: | |
| 65 | + . venv/bin/activate |
| 66 | + pre-commit install-hooks |
| 67 | +
|
| 68 | + black: |
| 69 | + runs-on: ubuntu-latest |
| 70 | + name: Black check and force |
| 71 | + needs: prepare |
| 72 | + steps: |
| 73 | + - name: Check out committed code |
| 74 | + uses: actions/checkout@v3 |
| 75 | + with: |
| 76 | + persist-credentials: false |
| 77 | + - name: Set up Python ${{ env.DEFAULT_PYTHON }} |
| 78 | + id: python |
| 79 | + uses: actions/setup-python@v4 |
| 80 | + with: |
| 81 | + python-version: ${{ env.DEFAULT_PYTHON }} |
| 82 | + - name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment |
| 83 | + id: cache-venv |
| 84 | + uses: actions/cache@v3 |
| 85 | + with: |
| 86 | + path: venv |
| 87 | + key: >- |
| 88 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ |
| 89 | + steps.python.outputs.python-version }}-${{ |
| 90 | + hashFiles('requirements_test.txt') }}-${{ |
| 91 | + hashFiles('setup.py') }} |
| 92 | + - name: Fail job if Python cache restore failed |
| 93 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 94 | + run: | |
| 95 | + echo "Failed to restore Python ${{ env.DEFAULT_PYTHON }} virtual environment from cache" |
| 96 | + exit 1 |
| 97 | + - name: Run black --check --exclude venv . |
| 98 | + run: | |
| 99 | + . venv/bin/activate |
| 100 | + black --check --exclude venv . |
| 101 | + - name: If needed, commit black changes to the pull request |
| 102 | + if: failure() |
| 103 | + run: | |
| 104 | + . venv/bin/activate |
| 105 | + black --exclude venv . |
| 106 | + git config --global user.name 'autoblack' |
| 107 | + git config --global user.email '[email protected]' |
| 108 | + git remote set-url origin https://x-access-token:${{ secrets.PAT_CT }}@github.com/$GITHUB_REPOSITORY |
| 109 | + git checkout $GITHUB_HEAD_REF |
| 110 | + git commit -am "fixup: ${GITHUB_REF##*/} Python code reformatted using Black" |
| 111 | + git push origin ${GITHUB_REF##*/} |
| 112 | +
|
| 113 | + commitcheck: |
| 114 | + runs-on: ubuntu-latest |
| 115 | + name: Check commit |
| 116 | + needs: black |
| 117 | + steps: |
| 118 | + - name: Check out committed code |
| 119 | + uses: actions/checkout@v3 |
| 120 | + - name: Set up Python ${{ env.DEFAULT_PYTHON }} |
| 121 | + id: python |
| 122 | + uses: actions/setup-python@v4 |
| 123 | + with: |
| 124 | + python-version: ${{ env.DEFAULT_PYTHON }} |
| 125 | + - name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment |
| 126 | + id: cache-venv |
| 127 | + uses: actions/cache@v3 |
| 128 | + with: |
| 129 | + path: venv |
| 130 | + key: >- |
| 131 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ |
| 132 | + steps.python.outputs.python-version }}-${{ |
| 133 | + hashFiles('requirements_test.txt') }}-${{ |
| 134 | + hashFiles('setup.py') }} |
| 135 | + - name: Fail job if Python cache restore failed |
| 136 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 137 | + run: | |
| 138 | + echo "Failed to restore Python ${{ env.DEFAULT_PYTHON }} virtual environment from cache" |
| 139 | + exit 1 |
| 140 | + - name: Restore pre-commit environment from cache |
| 141 | + id: cache-precommit |
| 142 | + uses: actions/cache@v3 |
| 143 | + with: |
| 144 | + path: ${{ env.PRE_COMMIT_HOME }} |
| 145 | + key: | |
| 146 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }} |
| 147 | + - name: Fail job if cache restore failed |
| 148 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 149 | + run: | |
| 150 | + echo "Failed to restore pre-commit environment from cache" |
| 151 | + exit 1 |
| 152 | + - name: Verify commit |
| 153 | + run: | |
| 154 | + . venv/bin/activate |
| 155 | + pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual bandit |
| 156 | + pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual black |
| 157 | + pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual codespell |
| 158 | + pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual flake8 |
| 159 | + #pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual isort |
| 160 | + pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual mypy |
| 161 | + pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual userdata |
| 162 | + pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual yamllint |
| 163 | +
|
| 164 | + prepare-test-cache: |
| 165 | + runs-on: ubuntu-latest |
| 166 | + name: Create pytest cache for Python ${{ matrix.python-version }} |
| 167 | + needs: commitcheck |
| 168 | + strategy: |
| 169 | + matrix: |
| 170 | + python-version: [3.9, "3.10"] |
| 171 | + steps: |
| 172 | + - name: Check out committed code |
| 173 | + uses: actions/checkout@v3 |
| 174 | + - name: Set up Python ${{ matrix.python-version }} |
| 175 | + id: python |
| 176 | + uses: actions/setup-python@v4 |
| 177 | + with: |
| 178 | + python-version: ${{ matrix.python-version }} |
| 179 | + - name: Restore full Python ${{ matrix.python-version }} virtual environment |
| 180 | + id: cache-venv |
| 181 | + uses: actions/cache@v3 |
| 182 | + with: |
| 183 | + path: venv |
| 184 | + key: >- |
| 185 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-${{ |
| 186 | + matrix.python-version }}-${{ hashFiles('requirements_test.txt') |
| 187 | + }}-${{ hashFiles('setup.py') }} |
| 188 | + restore-keys: | |
| 189 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-${{ matrix.python-version }}-${{ hashFiles('requirements_test.txt') }}-${{ hashFiles('setup.py') }} |
| 190 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-${{ matrix.python-version }}-${{ hashFiles('requirements_test.txt') }} |
| 191 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-${{ matrix.python-version }}- |
| 192 | + - name: Create full Python ${{ matrix.python-version }} virtual environment |
| 193 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 194 | + run: | |
| 195 | + python -m venv venv |
| 196 | + . venv/bin/activate |
| 197 | + pip install -U pip setuptools wheel |
| 198 | + #pip install -r requirements_test.txt |
| 199 | + # 20220124 Mimic setup_test.sh |
| 200 | + pip install --upgrade -r requirements_test.txt -c https://raw.githubusercontent.com/home-assistant/core/dev/homeassistant/package_constraints.txt -r https://raw.githubusercontent.com/home-assistant/core/dev/requirements_test.txt -r https://raw.githubusercontent.com/home-assistant/core/dev/requirements_test_pre_commit.txt |
| 201 | + pip install --upgrade pytest-asyncio |
| 202 | +
|
| 203 | + pytest: |
| 204 | + runs-on: ubuntu-latest |
| 205 | + name: Run pytest using Python ${{ matrix.python-version }} |
| 206 | + needs: prepare-test-cache |
| 207 | + strategy: |
| 208 | + matrix: |
| 209 | + python-version: [3.9, "3.10"] |
| 210 | + |
| 211 | + steps: |
| 212 | + - name: Check out committed code |
| 213 | + uses: actions/checkout@v3 |
| 214 | + - name: Set up Python ${{ matrix.python-version }} |
| 215 | + id: python |
| 216 | + uses: actions/setup-python@v4 |
| 217 | + with: |
| 218 | + python-version: ${{ matrix.python-version }} |
| 219 | + - name: Restore full Python ${{ matrix.python-version }} virtual environment |
| 220 | + id: cache-venv |
| 221 | + uses: actions/cache@v3 |
| 222 | + with: |
| 223 | + path: venv |
| 224 | + key: >- |
| 225 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-venv-${{ |
| 226 | + matrix.python-version }}-${{ hashFiles('requirements_test.txt') |
| 227 | + }}-${{ hashFiles('setup.py') }} |
| 228 | + - name: Fail job if Python cache restore failed |
| 229 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 230 | + run: | |
| 231 | + echo "Failed to restore Python virtual environment from cache" |
| 232 | + exit 1 |
| 233 | + - name: Run all tests |
| 234 | + run: | |
| 235 | + . venv/bin/activate |
| 236 | + pytest --log-level info tests/*.py --cov='.' |
| 237 | + - name: Upload coverage artifact |
| 238 | + uses: actions/upload-artifact@v3 |
| 239 | + with: |
| 240 | + name: coverage-${{ matrix.python-version }} |
| 241 | + path: .coverage |
| 242 | + |
| 243 | + mypy: |
| 244 | + runs-on: ubuntu-latest |
| 245 | + name: Run mypy |
| 246 | + needs: pytest |
| 247 | + steps: |
| 248 | + - name: Check out committed code |
| 249 | + uses: actions/checkout@v3 |
| 250 | + with: |
| 251 | + persist-credentials: false |
| 252 | + - name: Set up Python ${{ env.DEFAULT_PYTHON }} |
| 253 | + id: python |
| 254 | + uses: actions/setup-python@v4 |
| 255 | + with: |
| 256 | + python-version: ${{ env.DEFAULT_PYTHON }} |
| 257 | + - name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment |
| 258 | + id: cache-venv |
| 259 | + uses: actions/cache@v3 |
| 260 | + with: |
| 261 | + path: venv |
| 262 | + key: >- |
| 263 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ |
| 264 | + steps.python.outputs.python-version }}-${{ |
| 265 | + hashFiles('requirements_test.txt') }}-${{ |
| 266 | + hashFiles('setup.py') }} |
| 267 | + - name: Fail job if Python cache restore failed |
| 268 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 269 | + run: | |
| 270 | + echo "Failed to restore Python ${{ env.DEFAULT_PYTHON }} virtual environment from cache" |
| 271 | + exit 1 |
| 272 | + - name: Run mypy |
| 273 | + run: | |
| 274 | + . venv/bin/activate |
| 275 | + pip list | grep -i mypy |
| 276 | + mypy plugwise/ |
| 277 | +
|
| 278 | + shellcheck: |
| 279 | + name: Shellcheck |
| 280 | + runs-on: ubuntu-latest |
| 281 | + steps: |
| 282 | + - uses: actions/checkout@v3 |
| 283 | + - name: Run ShellCheck |
| 284 | + uses: ludeeus/action-shellcheck@master |
| 285 | + |
| 286 | + coverage: |
| 287 | + name: Process test coverage |
| 288 | + runs-on: ubuntu-latest |
| 289 | + needs: pytest |
| 290 | + steps: |
| 291 | + - name: Check out code from GitHub |
| 292 | + uses: actions/checkout@v3 |
| 293 | + - name: Set up Python ${{ env.DEFAULT_PYTHON }} |
| 294 | + id: python |
| 295 | + uses: actions/setup-python@v4 |
| 296 | + with: |
| 297 | + python-version: ${{ env.DEFAULT_PYTHON }} |
| 298 | + - name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment |
| 299 | + id: cache-venv |
| 300 | + uses: actions/cache@v3 |
| 301 | + with: |
| 302 | + path: venv |
| 303 | + key: >- |
| 304 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ |
| 305 | + steps.python.outputs.python-version }}-${{ |
| 306 | + hashFiles('requirements_test.txt') }}-${{ |
| 307 | + hashFiles('setup.py') }} |
| 308 | + - name: Fail job if Python cache restore failed |
| 309 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 310 | + run: | |
| 311 | + echo "Failed to restore Python virtual environment from cache" |
| 312 | + exit 1 |
| 313 | + - name: Download all coverage artifacts |
| 314 | + uses: actions/download-artifact@v3 |
| 315 | + - name: Combine coverage results |
| 316 | + run: | |
| 317 | + . venv/bin/activate |
| 318 | + coverage combine coverage*/.coverage* |
| 319 | + coverage report --fail-under=94 |
| 320 | + coverage xml |
| 321 | + - name: Upload coverage to Codecov |
| 322 | + uses: codecov/codecov-action@v3 |
| 323 | + |
| 324 | + test-publishing: |
| 325 | + name: Build and publish Python 🐍 distributions 📦 to TestPyPI |
| 326 | + runs-on: ubuntu-latest |
| 327 | + needs: [coverage, mypy] |
| 328 | + steps: |
| 329 | + - name: Check out code from GitHub |
| 330 | + uses: actions/checkout@v3 |
| 331 | + - name: Set up Python ${{ env.DEFAULT_PYTHON }} |
| 332 | + id: python |
| 333 | + uses: actions/setup-python@v4 |
| 334 | + with: |
| 335 | + python-version: ${{ env.DEFAULT_PYTHON }} |
| 336 | + - name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment |
| 337 | + id: cache-venv |
| 338 | + uses: actions/cache@v3 |
| 339 | + with: |
| 340 | + path: venv |
| 341 | + key: >- |
| 342 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ |
| 343 | + steps.python.outputs.python-version }}-${{ |
| 344 | + hashFiles('requirements_test.txt') }}-${{ |
| 345 | + hashFiles('setup.py') }} |
| 346 | + - name: Fail job if Python cache restore failed |
| 347 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 348 | + run: | |
| 349 | + echo "Failed to restore Python virtual environment from cache" |
| 350 | + exit 1 |
| 351 | + - name: Build a distribution |
| 352 | + run: >- |
| 353 | + python setup.py sdist |
| 354 | + - name: Publish distribution 📦 to Test PyPI |
| 355 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 356 | + continue-on-error: true |
| 357 | + with: |
| 358 | + password: ${{ secrets.testpypi_token }} |
| 359 | + repository_url: https://test.pypi.org/legacy/ |
| 360 | + skip_existing: true |
| 361 | + |
| 362 | + complexity: |
| 363 | + name: Process test complexity |
| 364 | + runs-on: ubuntu-latest |
| 365 | + needs: coverage |
| 366 | + steps: |
| 367 | + - name: Check out code from GitHub |
| 368 | + uses: actions/checkout@v3 |
| 369 | + - name: Set up Python ${{ env.DEFAULT_PYTHON }} |
| 370 | + id: python |
| 371 | + uses: actions/setup-python@v4 |
| 372 | + with: |
| 373 | + python-version: ${{ env.DEFAULT_PYTHON }} |
| 374 | + - name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment |
| 375 | + id: cache-venv |
| 376 | + uses: actions/cache@v3 |
| 377 | + with: |
| 378 | + path: venv |
| 379 | + key: >- |
| 380 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ |
| 381 | + steps.python.outputs.python-version }}-${{ |
| 382 | + hashFiles('requirements_test.txt') }}-${{ |
| 383 | + hashFiles('setup.py') }} |
| 384 | + - name: Fail job if Python cache restore failed |
| 385 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 386 | + run: | |
| 387 | + echo "Failed to restore Python virtual environment from cache" |
| 388 | + exit 1 |
| 389 | + - name: Run complexity report (click to view details) |
| 390 | + run: | |
| 391 | + . venv/bin/activate |
| 392 | + echo "Showing complexity higher or equal to 'C'" |
| 393 | + radon cc plugwise/smile.py plugwise/helper.py tests/test_smile.py -s -nc --no-assert |
0 commit comments