Skip to content

Commit

Permalink
Merge pull request: feat/add-linter-workflow
Browse files Browse the repository at this point in the history
Feat/add linter workflow
  • Loading branch information
ayoub3bidi committed Feb 18, 2024
2 parents 2cb243a + 49b0497 commit cbcd04c
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 14 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Linter

on:
push:
branches:
- main
- develop
pull_request:
branches:
- main
- develop

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Create environment file
run: |
echo "POSTGRES_HOST=${{ secrets.POSTGRES_HOST }}" > .env
echo "POSTGRES_PORT=${{ secrets.POSTGRES_PORT }}" >> .env
echo "POSTGRES_DB=${{ secrets.POSTGRES_DB }}" >> .env
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> .env
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> .env
- name: Run linter
run: |
setsid ./ci/lint.sh
14 changes: 13 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
stages:
- test
- lint

unit_test:
stage: test
Expand All @@ -21,4 +22,15 @@ integration_test:
- /^(main|develop.*)$/
- merge_requests
tags:
- imagesbuilder
- imagesbuilder

lint:
stage: lint
script:
- setsid ./ci/lint.sh
only:
refs:
- /^(main|develop.*)$/
- merge_requests
tags:
- imagesbuilder
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ FROM api AS unit_tests

WORKDIR /app/src

CMD ["python", "-m", "unittest", "discover", "-s", "./unit_tests", "-p", "test_*.py", "-v"]
CMD ["python", "-m", "unittest", "discover", "-s", "./unit_tests", "-p", "test_*.py", "-v"]

FROM api AS linter

WORKDIR /app/src

CMD ["ruff", "check", "--fix", "."]
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ Here's how to run the integration test locally:
```shell
docker-compose up --build --abort-on-container-exit mercury_unit_tests
```

## Linter

Having a fast linter can help avoiding coding style problems, and potentially avoid future bugs that takes long hours to fix.
For the linter we're working with [ruff](https://astral.sh/ruff), a very fast linter written in Rust.

Here's how to run the linter test locally:

```shell
docker-compose up --build --abort-on-container-exit mercury_linter
```

-------

## Contributions
Expand Down
3 changes: 3 additions & 0 deletions ci/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

docker-compose up --build --abort-on-container-exit mercury_linter
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ services:
target: unit_tests
env_file:
- .env
mercury_linter:
image: cwcloud-linter:latest
build:
context: .
dockerfile: ./Dockerfile
target: linter
env_file:
- .env

volumes:
mercurydb:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ bcrypt
python-jose[cryptography]
passlib[bcrypt]
python-multipart
pytest
pytest
ruff
4 changes: 2 additions & 2 deletions src/controllers/admin/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def update_user(user_id, payload, db):
user.update({"email": payload.email})
if payload.password:
user.update({"password": get_password_hash(payload.password)})
if payload.is_admin != None:
if payload.is_admin is not None:
user.update({"is_admin": payload.is_admin})
if payload.disabled != None:
if payload.disabled is not None:
User.disabled = user.disabled
user.update({"disabled": payload.disabled})

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/user/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from utils.variables import is_not_empty

def register(payload, db):
if (validate_email(payload.email) == False):
if (validate_email(payload.email) is False):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid email")
if (validate_password(payload.password) == False):
if (validate_password(payload.password) is False):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid password")
user = db.query(User).filter(User.email == payload.email).first()
if user:
Expand Down
10 changes: 5 additions & 5 deletions src/integration_tests/test_admin_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
v = os.getenv("VERSION")

def test_get_users():
if (client != None):
if (client is not None):
response = client.get(f"/{v}/admin/user/all", headers=headers)
assert response.status_code == 200
assert len(response.json()) > 0

def test_get_user_by_id():
if (client != None):
if (client is not None):
response = client.get(f"/{v}/admin/user/1", headers=headers)
assert response.status_code == 200
assert response.json()["id"] == 1

def test_add_user():
if (client != None):
if (client is not None):
response = client.post(f"/{v}/admin/user/register", json={"username": "test_user", "email": "[email protected]", "password": "GoodPassword123", "is_admin": True, "disabled": False }, headers=headers)
assert response.status_code == 201
assert response.json()["username"] == "test_user"

def test_delete_user():
if (client != None):
if (client is not None):
response = client.delete(f"/{v}/admin/user/1", headers=headers)
assert response.text == None
assert response.text is None
2 changes: 1 addition & 1 deletion src/integration_tests/test_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
v = os.getenv("VERSION")

def test_get_health():
if (client != None):
if (client is not None):
response = client.get(f"/{v}/health")
assert response.status_code == 200
assert response.json() == { 'status': 'ok', 'alive': True }
2 changes: 1 addition & 1 deletion src/middleware/auth_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ async def get_current_active_user(current_user: UserRegisterSchema = Depends(get
return current_user

async def get_current_admin_user(current_user: UserRegisterSchema = Depends(get_current_user)):
if current_user.is_admin == False:
if current_user.is_admin is False:
raise HTTPException(status_code=400, detail="User is not admin")
return current_user

0 comments on commit cbcd04c

Please sign in to comment.