Skip to content

Commit

Permalink
Configure and run pre-commit (#699)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Héctor Gómez <[email protected]>
  • Loading branch information
3 people authored Nov 14, 2024
1 parent 29be684 commit b994143
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ venv/
.coverage*
htmlcov/
data/
.flake8
.pre-commit-config.yaml
docker-compose.override.yml
docker-compose.override.yml.example
Expand Down
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
108 changes: 26 additions & 82 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
27 changes: 14 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
[tool.isort]
profile = "black"
multi_line_output = 3

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "config.settings"
11 changes: 3 additions & 8 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
web3==6.20.2

0 comments on commit b994143

Please sign in to comment.