Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[ci/cd]: add ruff text formatter #1450

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/formatter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Code Format Checker
on:
push:
branches:
- '*'
pull_request:
branches:
- '*'

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

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.10'

- name: Install Ruff
run: |
python -m pip install --upgrade pip
pip install ruff

- name: Run Ruff Format
run: |
ruff format --line-length 120 --check . --exclude xray_api,app/db/migrations/versions
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Backend is built using FastAPI and uses SQLAlchemy as the ORM for database opera
### Python Code Formatting
To maintain consistency in the codebase, we require all code to be formatted using
```bash
autopep8 <file> --max-line-length 120
ruff format . --line-length 120
```

## Frontend
Expand Down
8 changes: 2 additions & 6 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
redoc_url="/redoc" if DOCS else None,
)

scheduler = BackgroundScheduler(
{"apscheduler.job_defaults.max_instances": 20}, timezone="UTC"
)
scheduler = BackgroundScheduler({"apscheduler.job_defaults.max_instances": 20}, timezone="UTC")
logger = logging.getLogger("uvicorn.error")

app.add_middleware(
Expand Down Expand Up @@ -53,9 +51,7 @@ def on_startup():
paths = [f"{r.path}/" for r in app.routes]
paths.append("/api/")
if f"/{XRAY_SUBSCRIPTION_PATH}/" in paths:
raise ValueError(
f"you can't use /{XRAY_SUBSCRIPTION_PATH}/ as subscription path it reserved for {app.title}"
)
raise ValueError(f"you can't use /{XRAY_SUBSCRIPTION_PATH}/ as subscription path it reserved for {app.title}")
scheduler.start()


Expand Down
43 changes: 23 additions & 20 deletions app/dashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,39 @@
from fastapi.staticfiles import StaticFiles

base_dir = Path(__file__).parent
build_dir = base_dir / 'build'
statics_dir = build_dir / 'statics'
build_dir = base_dir / "build"
statics_dir = build_dir / "statics"


def build():
proc = subprocess.Popen(
['npm', 'run', 'build', '--', '--outDir', build_dir, '--assetsDir', 'statics'],
env={**os.environ, 'VITE_BASE_API': VITE_BASE_API},
cwd=base_dir
["npm", "run", "build", "--", "--outDir", build_dir, "--assetsDir", "statics"],
env={**os.environ, "VITE_BASE_API": VITE_BASE_API},
cwd=base_dir,
)
proc.wait()
with open(build_dir / 'index.html', 'r') as file:
with open(build_dir / "index.html", "r") as file:
html = file.read()
with open(build_dir / '404.html', 'w') as file:
with open(build_dir / "404.html", "w") as file:
file.write(html)


def run_dev():
proc = subprocess.Popen(
['npm', 'run', 'dev', '--', '--host', '0.0.0.0', '--clearScreen', 'false', '--base', os.path.join(DASHBOARD_PATH, '')],
env={**os.environ, 'VITE_BASE_API': VITE_BASE_API},
cwd=base_dir
[
"npm",
"run",
"dev",
"--",
"--host",
"0.0.0.0",
"--clearScreen",
"false",
"--base",
os.path.join(DASHBOARD_PATH, ""),
],
env={**os.environ, "VITE_BASE_API": VITE_BASE_API},
cwd=base_dir,
)

atexit.register(proc.terminate)
Expand All @@ -39,16 +50,8 @@ def run_build():
if not build_dir.is_dir():
build()

app.mount(
DASHBOARD_PATH,
StaticFiles(directory=build_dir, html=True),
name="dashboard"
)
app.mount(
'/statics/',
StaticFiles(directory=statics_dir, html=True),
name="statics"
)
app.mount(DASHBOARD_PATH, StaticFiles(directory=build_dir, html=True), name="dashboard")
app.mount("/statics/", StaticFiles(directory=statics_dir, html=True), name="statics")


@app.on_event("startup")
Expand Down
42 changes: 29 additions & 13 deletions app/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,35 @@ def get_db(): # Dependency
yield db


from .crud import (create_admin, create_notification_reminder, # noqa
create_user, delete_notification_reminder, get_admin,
get_admins, get_jwt_secret_key, get_notification_reminder,
get_or_create_inbound, get_system_usage,
get_tls_certificate, get_user, get_user_by_id, get_users,
get_users_count, remove_admin, remove_user, revoke_user_sub,
set_owner, update_admin, update_user, update_user_status, reset_user_by_next,
update_user_sub, start_user_expire, get_admin_by_id,
get_admin_by_telegram_id)
from .crud import (
create_admin,
create_notification_reminder, # noqa
create_user,
delete_notification_reminder,
get_admin,
get_admins,
get_jwt_secret_key,
get_notification_reminder,
get_or_create_inbound,
get_system_usage,
get_tls_certificate,
get_user,
get_user_by_id,
get_users,
get_users_count,
remove_admin,
remove_user,
revoke_user_sub,
set_owner,
update_admin,
update_user,
update_user_status,
reset_user_by_next,
update_user_sub,
start_user_expire,
get_admin_by_id,
get_admin_by_telegram_id,
)

from .models import JWT, System, User # noqa

Expand Down Expand Up @@ -60,18 +80,14 @@ def get_db(): # Dependency
"get_admins",
"get_admin_by_id",
"get_admin_by_telegram_id",

"create_notification_reminder",
"get_notification_reminder",
"delete_notification_reminder",

"GetDB",
"get_db",

"User",
"System",
"JWT",

"Base",
"Session",
]
16 changes: 8 additions & 8 deletions app/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from config import (SQLALCHEMY_DATABASE_URL, SQLALCHEMY_POOL_SIZE,
SQLIALCHEMY_MAX_OVERFLOW)
from config import (
SQLALCHEMY_DATABASE_URL,
SQLALCHEMY_POOL_SIZE,
SQLIALCHEMY_MAX_OVERFLOW,
)

IS_SQLITE = SQLALCHEMY_DATABASE_URL.startswith('sqlite')
IS_SQLITE = SQLALCHEMY_DATABASE_URL.startswith("sqlite")

if IS_SQLITE:
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False}
)
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
else:
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
pool_size=SQLALCHEMY_POOL_SIZE,
max_overflow=SQLIALCHEMY_MAX_OVERFLOW,
pool_recycle=3600,
pool_timeout=10
pool_timeout=10,
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Expand Down
Loading