From b9941433e4c53ffd5349e0df932296d41b28fbb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ux=C3=ADo?= Date: Thu, 14 Nov 2024 13:32:30 +0100 Subject: [PATCH] Configure and run pre-commit (#699) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Configure and run pre-commit * Require web3 library explicitly * Change dependencies * Reorder dependencies, remove black, flake8 and isort * Reorder requirements.txt * Remove unused pre-commit repos * Run Python 3.12 on CI * Remove flake8 and black references from the project * Sort requirements.txt * Update cla.yml --------- Co-authored-by: Hector Gómez Varela Co-authored-by: Héctor Gómez --- .dev.env | 2 +- .dockerignore | 1 - .flake8 | 2 - .github/ISSUE_TEMPLATE/feature_request.md | 2 +- .github/workflows/test.yml | 108 ++++++---------------- .pre-commit-config.yaml | 27 +++--- README.md | 15 +-- pyproject.toml | 4 - requirements-dev.txt | 11 +-- requirements.txt | 10 +- 10 files changed, 54 insertions(+), 128 deletions(-) delete mode 100644 .flake8 diff --git a/.dev.env b/.dev.env index c8aa8e34..8d21e6bc 100644 --- a/.dev.env +++ b/.dev.env @@ -113,7 +113,7 @@ DEFAULT_FILE_STORAGE=django.core.files.storage.FileSystemStorage # eg.: AWS_S3_CUSTOM_DOMAIN=cdn.mydomain.com #AWS_S3_CUSTOM_DOMAIN= -# CSRF protection +# CSRF protection # See https://docs.djangoproject.com/en/dev/ref/settings/#csrf-trusted-origins CSRF_TRUSTED_ORIGINS="http://localhost:${NGINX_HOST_PORT}" #CSRF_TRUSTED_ORIGINS="https://safe-config.staging.gnosisdev.com,http://safe-config.staging.gnosisdev.com" diff --git a/.dockerignore b/.dockerignore index 9463f227..78b705a8 100644 --- a/.dockerignore +++ b/.dockerignore @@ -10,7 +10,6 @@ venv/ .coverage* htmlcov/ data/ -.flake8 .pre-commit-config.yaml docker-compose.override.yml docker-compose.override.yml.example diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 791f075d..00000000 --- a/.flake8 +++ /dev/null @@ -1,2 +0,0 @@ -[flake8] -max-line-length = 119 diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index e0def9c6..ad8da635 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -11,7 +11,7 @@ assignees: '' A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] **Provide references to the feature you are implementing that requires this change** -Provide at least one of the following: +Provide at least one of the following: - Links to epics in your repository - Images taken from mocks - Gitbook or any form of written documentation links, etc. Any of these alternatives will help us contextualise your request. diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4556c9fe..77027194 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,81 +9,25 @@ on: push: pull_request: release: - types: [ released ] + types: [released] jobs: - flake8: - name: flake8 + linting: runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - uses: actions/cache@v4 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements-dev.txt') }} - restore-keys: | - ${{ runner.os }}-pip- - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: '3.12.2' - - name: Install dependencies - run: | - pip install -U wheel setuptools - pip install -r requirements-dev.txt - - name: flake8 - uses: liskin/gh-problem-matcher-wrap@v3 - with: - linters: flake8 - run: flake8 + strategy: + matrix: + python-version: ["3.12"] - isort: - runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v4 - - uses: actions/cache@v4 + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements-dev.txt') }} - restore-keys: | - ${{ runner.os }}-pip- - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: '3.12.2' + python-version: ${{ matrix.python-version }} - name: Install dependencies - run: | - pip install -U wheel setuptools - pip install -r requirements-dev.txt - - name: isort - uses: liskin/gh-problem-matcher-wrap@v3 - with: - linters: isort - run: isort --check --profile black src/ - - black: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - uses: actions/cache@v4 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements-dev.txt') }} - restore-keys: | - ${{ runner.os }}-pip- - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: '3.12.2' - - name: Install dependencies - run: | - pip install -U wheel setuptools - pip install -r requirements-dev.txt - - name: black - run: black --check src/ + run: pip install pre-commit + - name: Run pre-commit + run: pre-commit run --all-files django-check: runs-on: ubuntu-latest @@ -102,12 +46,12 @@ jobs: ports: - 5432:5432 env: - SECRET_KEY: 'insecure_key_for_dev' + SECRET_KEY: "insecure_key_for_dev" POSTGRES_HOST: localhost POSTGRES_PORT: 5432 - AWS_ACCESS_KEY_ID: 'example-aws-access-key-id' - AWS_SECRET_ACCESS_KEY: 'example-aws-secret-access-key' - AWS_STORAGE_BUCKET_NAME: 'example-aws-storage-bucket-name' + AWS_ACCESS_KEY_ID: "example-aws-access-key-id" + AWS_SECRET_ACCESS_KEY: "example-aws-secret-access-key" + AWS_STORAGE_BUCKET_NAME: "example-aws-storage-bucket-name" steps: - name: Check out repository code uses: actions/checkout@v4 @@ -120,7 +64,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v5 with: - python-version: '3.12.2' + python-version: "3.12.2" - name: Install dependencies run: | pip install -U wheel setuptools @@ -140,7 +84,7 @@ jobs: docker-publish-staging: if: (github.event_name == 'push' && github.ref == 'refs/heads/main') - needs: [ flake8, isort, black, django-check ] + needs: [linting, django-check] runs-on: ubuntu-latest steps: - name: Checkout @@ -188,7 +132,7 @@ jobs: docker-publish-release: if: (github.event_name == 'release' && github.event.action == 'released') - needs: [ flake8, isort, black, django-check ] + needs: [linting, django-check] runs-on: ubuntu-latest steps: - name: Checkout @@ -241,10 +185,10 @@ jobs: needs: [docker-publish-staging] if: github.event_name == 'push' && github.ref == 'refs/heads/main' steps: - - uses: actions/checkout@v4 - - name: Deploy Staging - run: bash scripts/autodeploy.sh - env: - AUTODEPLOY_URL: ${{ secrets.AUTODEPLOY_URL }} - AUTODEPLOY_TOKEN: ${{ secrets.AUTODEPLOY_TOKEN }} - TARGET_ENV: "staging" + - uses: actions/checkout@v4 + - name: Deploy Staging + run: bash scripts/autodeploy.sh + env: + AUTODEPLOY_URL: ${{ secrets.AUTODEPLOY_URL }} + AUTODEPLOY_TOKEN: ${{ secrets.AUTODEPLOY_TOKEN }} + TARGET_ENV: "staging" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d03cfb5c..49c47b8f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,15 +1,16 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks repos: - - repo: https://github.com/pycqa/isort - rev: 5.12.0 + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 hooks: - - id: isort - args: [ "--profile", "black", "--filter-files" ] - - repo: https://github.com/psf/black - rev: 24.3.0 - hooks: - - id: black - language_version: python3 - - repo: https://github.com/pycqa/flake8 - rev: 7.0.0 - hooks: - - id: flake8 + - id: check-docstring-first + - id: check-merge-conflict + - id: debug-statements + - id: detect-private-key + - id: requirements-txt-fixer + - id: trailing-whitespace + - id: end-of-file-fixer + types: [python] + - id: check-yaml + - id: check-added-large-files diff --git a/README.md b/README.md index a3b18922..9a815688 100644 --- a/README.md +++ b/README.md @@ -73,19 +73,10 @@ pytest src ## Code Style Formatter and Linter -[Black](https://black.readthedocs.io/en/stable/), [Flake8](https://flake8.pycqa.org/en/latest/) and [isort](https://pycqa.github.io/isort/) are the tools used to validate the style of the changes -being pushed. You can refer to the documentation -of these tools to check how to integrate them with your editor/IDE. +Code formatting and linting are enforced before every commit using `pre-commit`. -```shell -isort --profile black src # sorts imports according to the isort spec with a profile compatible with Black -black src # formats the files in the src folder using Black -flake8 src # runs flake8 Linter in the src folder -``` - -There's also a pre-commit hook that you can install locally via `pre-commit` so that it formats the files changed on each commit automatically: +To manually trigger the checks, run: ```shell -pre-commit install # installs commit hook under .git/hooks/pre-commit -git commit # Initially this can take a couple minutes to setup the environment (which will be reused in following commits) +pre-commit run --all-files ``` diff --git a/pyproject.toml b/pyproject.toml index f9084f2b..3e842c8d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,2 @@ -[tool.isort] -profile = "black" -multi_line_output = 3 - [tool.pytest.ini_options] DJANGO_SETTINGS_MODULE = "config.settings" diff --git a/requirements-dev.txt b/requirements-dev.txt index 79e2cbd2..617faa1c 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,16 +1,11 @@ -r requirements.txt -black==24.10.0 coverage==7.6.4 +django-stubs==5.1.1 +djangorestframework-stubs==3.15.1 factory-boy==3.3.1 Faker==30.8.1 -flake8==7.1.1 -isort==5.13.2 +mypy==1.13.0 pre-commit==4.0.1 pytest-django==4.9.0 responses==0.25.3 - -# mypy and PEP 561 stubs -mypy==1.13.0 -django-stubs==5.1.1 -djangorestframework-stubs==3.15.1 types-requests==2.32.0.20241016 diff --git a/requirements.txt b/requirements.txt index 1653b5c0..63442a23 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,19 +1,21 @@ boto3==1.35.57 Django==5.1.2 django-cors-headers==4.6.0 -djangorestframework==3.15.2 -djangorestframework-camel-case==1.4.2 django-storages==1.14.4 django-stubs-ext==5.1.1 +djangorestframework==3.15.2 +djangorestframework-camel-case==1.4.2 drf-yasg[validation]==1.21.7 -safe-eth-py[django]==5.8.0 +eth-typing==4.1.0 +eth-utils==4.1.1 gunicorn==23.0.0 Pillow==11.0.0 psycopg2-binary==2.9.10 requests==2.32.3 +safe-eth-py[django]==5.8.0 # Without pin, safe-eth-py 5.8.0 installs >= 7 which breaks django-check job # > ImportError: cannot import name 'geth_poa_middleware' from 'web3.middleware' # TODO: Remove when safe-eth-py updates web3 to >= 7 # https://github.com/safe-global/safe-eth-py/pull/1315 -web3==6.20.2 \ No newline at end of file +web3==6.20.2