Skip to content

Commit 740089e

Browse files
Merge pull request #1 from Utkarsh-Patel-13/backend/ruff-linter-formatter
Backend/ruff linter formatter
2 parents 505a005 + c39de63 commit 740089e

90 files changed

Lines changed: 8474 additions & 6415 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/code-quality.yml

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
name: Code Quality Checks
2+
3+
on:
4+
pull_request:
5+
branches: [main, dev]
6+
types: [opened, synchronize, reopened, ready_for_review]
7+
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: true
11+
12+
jobs:
13+
file-quality:
14+
name: File Quality Checks
15+
runs-on: ubuntu-latest
16+
if: github.event.pull_request.draft == false
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Fetch base branch
25+
run: |
26+
# Ensure we have the base branch reference for comparison
27+
git fetch origin ${{ github.base_ref }}:${{ github.base_ref }} 2>/dev/null || git fetch origin ${{ github.base_ref }} 2>/dev/null || true
28+
29+
- name: Set up Python
30+
uses: actions/setup-python@v5
31+
with:
32+
python-version: '3.12'
33+
34+
- name: Install pre-commit
35+
run: pip install pre-commit
36+
37+
- name: Cache pre-commit hooks
38+
uses: actions/cache@v4
39+
with:
40+
path: ~/.cache/pre-commit
41+
key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
42+
restore-keys: |
43+
pre-commit-
44+
45+
- name: Install hook environments (cache)
46+
run: pre-commit install-hooks
47+
48+
- name: Run file quality checks on changed files
49+
run: |
50+
# Get list of changed files and run specific hooks on them
51+
if git show-ref --verify --quiet refs/heads/${{ github.base_ref }}; then
52+
BASE_REF="${{ github.base_ref }}"
53+
elif git show-ref --verify --quiet refs/remotes/origin/${{ github.base_ref }}; then
54+
BASE_REF="origin/${{ github.base_ref }}"
55+
else
56+
echo "Base branch reference not found, running file quality hooks on all files"
57+
pre-commit run --all-files check-yaml check-json check-toml check-merge-conflict check-added-large-files debug-statements check-case-conflict
58+
exit 0
59+
fi
60+
61+
echo "Running file quality hooks on changed files against $BASE_REF"
62+
63+
# Run each hook individually on changed files
64+
SKIP=detect-secrets,bandit,ruff,ruff-format,prettier,eslint,typescript-check-web,typescript-check-extension,commitizen \
65+
pre-commit run --from-ref $BASE_REF --to-ref HEAD || exit_code=$?
66+
67+
# Exit with the same code as pre-commit
68+
exit ${exit_code:-0}
69+
70+
security-scan:
71+
name: Security Scan
72+
runs-on: ubuntu-latest
73+
if: github.event.pull_request.draft == false
74+
75+
steps:
76+
- name: Checkout code
77+
uses: actions/checkout@v4
78+
with:
79+
fetch-depth: 0
80+
81+
- name: Fetch base branch
82+
run: |
83+
git fetch origin ${{ github.base_ref }}:${{ github.base_ref }} 2>/dev/null || git fetch origin ${{ github.base_ref }} 2>/dev/null || true
84+
85+
- name: Set up Python
86+
uses: actions/setup-python@v5
87+
with:
88+
python-version: '3.12'
89+
90+
- name: Install pre-commit
91+
run: pip install pre-commit
92+
93+
- name: Cache pre-commit hooks
94+
uses: actions/cache@v4
95+
with:
96+
path: ~/.cache/pre-commit
97+
key: pre-commit-security-${{ hashFiles('.pre-commit-config.yaml') }}
98+
restore-keys: |
99+
pre-commit-security-
100+
101+
- name: Install hook environments (cache)
102+
run: pre-commit install-hooks
103+
104+
- name: Run security scans on changed files
105+
run: |
106+
# Get base ref for comparison
107+
if git show-ref --verify --quiet refs/heads/${{ github.base_ref }}; then
108+
BASE_REF="${{ github.base_ref }}"
109+
elif git show-ref --verify --quiet refs/remotes/origin/${{ github.base_ref }}; then
110+
BASE_REF="origin/${{ github.base_ref }}"
111+
else
112+
echo "Base branch reference not found, running security scans on all files"
113+
echo "⚠️ This may take longer than normal"
114+
pre-commit run --all-files detect-secrets bandit
115+
exit 0
116+
fi
117+
118+
echo "Running security scans on changed files against $BASE_REF"
119+
120+
# Run only security hooks on changed files
121+
SKIP=check-yaml,check-json,check-toml,check-merge-conflict,check-added-large-files,debug-statements,check-case-conflict,ruff,ruff-format,prettier,eslint,typescript-check-web,typescript-check-extension,commitizen \
122+
pre-commit run --from-ref $BASE_REF --to-ref HEAD || exit_code=$?
123+
124+
# Exit with the same code as pre-commit
125+
exit ${exit_code:-0}
126+
127+
python-backend:
128+
name: Python Backend Quality
129+
runs-on: ubuntu-latest
130+
if: github.event.pull_request.draft == false
131+
132+
steps:
133+
- name: Checkout code
134+
uses: actions/checkout@v4
135+
with:
136+
fetch-depth: 0
137+
138+
- name: Set up Python
139+
uses: actions/setup-python@v5
140+
with:
141+
python-version: '3.12'
142+
143+
- name: Install UV
144+
uses: astral-sh/setup-uv@v3
145+
146+
- name: Check if backend files changed
147+
id: backend-changes
148+
uses: dorny/paths-filter@v3
149+
with:
150+
filters: |
151+
backend:
152+
- 'surfsense_backend/**'
153+
154+
- name: Cache dependencies
155+
if: steps.backend-changes.outputs.backend == 'true'
156+
uses: actions/cache@v4
157+
with:
158+
path: |
159+
~/.cache/uv
160+
surfsense_backend/.venv
161+
key: python-deps-${{ hashFiles('surfsense_backend/uv.lock') }}
162+
163+
- name: Install dependencies
164+
if: steps.backend-changes.outputs.backend == 'true'
165+
working-directory: surfsense_backend
166+
run: uv sync
167+
168+
- name: Install pre-commit for backend checks
169+
if: steps.backend-changes.outputs.backend == 'true'
170+
run: pip install pre-commit
171+
172+
- name: Cache pre-commit hooks
173+
if: steps.backend-changes.outputs.backend == 'true'
174+
uses: actions/cache@v4
175+
with:
176+
path: ~/.cache/pre-commit
177+
key: pre-commit-backend-${{ hashFiles('.pre-commit-config.yaml') }}
178+
restore-keys: |
179+
pre-commit-backend-
180+
181+
- name: Install hook environments (cache)
182+
if: steps.backend-changes.outputs.backend == 'true'
183+
run: pre-commit install-hooks
184+
185+
- name: Run Python backend quality checks
186+
if: steps.backend-changes.outputs.backend == 'true'
187+
run: |
188+
# Get base ref for comparison
189+
if git show-ref --verify --quiet refs/heads/${{ github.base_ref }}; then
190+
BASE_REF="${{ github.base_ref }}"
191+
elif git show-ref --verify --quiet refs/remotes/origin/${{ github.base_ref }}; then
192+
BASE_REF="origin/${{ github.base_ref }}"
193+
else
194+
echo "Base branch reference not found, running Python backend checks on all files"
195+
pre-commit run --all-files ruff ruff-format
196+
exit 0
197+
fi
198+
199+
echo "Running Python backend checks on changed files against $BASE_REF"
200+
201+
# Run only ruff hooks on changed Python files
202+
SKIP=detect-secrets,bandit,check-yaml,check-json,check-toml,check-merge-conflict,check-added-large-files,debug-statements,check-case-conflict,prettier,eslint,typescript-check-web,typescript-check-extension,commitizen \
203+
pre-commit run --from-ref $BASE_REF --to-ref HEAD || exit_code=$?
204+
205+
# Exit with the same code as pre-commit
206+
exit ${exit_code:-0}
207+
208+
quality-gate:
209+
name: Quality Gate
210+
runs-on: ubuntu-latest
211+
needs: [file-quality, security-scan, python-backend]
212+
if: always()
213+
214+
steps:
215+
- name: Check all jobs status
216+
run: |
217+
if [[ "${{ needs.file-quality.result }}" == "failure" ||
218+
"${{ needs.security-scan.result }}" == "failure" ||
219+
"${{ needs.python-backend.result }}" == "failure" ]]; then
220+
echo "❌ Code quality checks failed"
221+
exit 1
222+
else
223+
echo "✅ All code quality checks passed"
224+
fi

.github/workflows/pre-commit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: pre-commit
33
on:
44
push:
55
pull_request:
6-
branches: [main]
6+
branches: [main, dev]
77

88
jobs:
99
pre-commit:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.flashrank_cache*
22
podcasts/
33
.env
4+
5+
.ruff_cache/

.pre-commit-config.yaml

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ repos:
66
- repo: https://github.com/pre-commit/pre-commit-hooks
77
rev: v5.0.0
88
hooks:
9-
- id: trailing-whitespace
10-
exclude: '\.md$'
119
- id: check-yaml
1210
args: [--multi, --unsafe]
1311
- id: check-json
@@ -31,52 +29,36 @@ repos:
3129
.*\.env\.template|
3230
.*/tests/.*|
3331
.*test.*\.py|
32+
test_.*\.py|
3433
.github/workflows/.*\.yml|
3534
.github/workflows/.*\.yaml|
3635
.*pnpm-lock\.yaml|
3736
.*alembic\.ini|
37+
.*alembic/versions/.*\.py|
3838
.*\.mdx$
3939
)$
4040
41-
# Python Backend Hooks (surfsense_backend)
42-
- repo: https://github.com/psf/black
43-
rev: 25.1.0
44-
hooks:
45-
- id: black
46-
files: ^surfsense_backend/
47-
language_version: python3
48-
49-
- repo: https://github.com/pycqa/isort
50-
rev: 6.0.1
51-
hooks:
52-
- id: isort
53-
files: ^surfsense_backend/
54-
args: ["--profile", "black", "--line-length", "88"]
55-
41+
# Python Backend Hooks (surfsense_backend) - Using Ruff for linting and formatting
5642
- repo: https://github.com/astral-sh/ruff-pre-commit
57-
rev: v0.12.4
43+
rev: v0.12.5
5844
hooks:
5945
- id: ruff
46+
name: ruff-check
6047
files: ^surfsense_backend/
61-
args: [--fix, --exit-non-zero-on-fix]
48+
exclude: ^surfsense_backend/(test_.*\.py|.*test.*\.py)
49+
args: [--fix]
6250
- id: ruff-format
51+
name: ruff-format
6352
files: ^surfsense_backend/
64-
65-
- repo: https://github.com/pre-commit/mirrors-mypy
66-
rev: v1.17.0
67-
hooks:
68-
- id: mypy
69-
files: ^surfsense_backend/
70-
additional_dependencies: []
71-
args: [--ignore-missing-imports, --disallow-untyped-defs]
53+
exclude: ^surfsense_backend/(test_.*\.py|.*test.*\.py)
7254

7355
- repo: https://github.com/PyCQA/bandit
7456
rev: 1.8.6
7557
hooks:
7658
- id: bandit
7759
files: ^surfsense_backend/
78-
args: ['-r', '.', '-f', 'json']
79-
exclude: ^surfsense_backend/(tests/|alembic/)
60+
args: ['-f', 'json', '--severity-level', 'high', '--confidence-level', 'high']
61+
exclude: ^surfsense_backend/(tests/|test_.*\.py|.*test.*\.py|alembic/)
8062

8163
# Frontend/Extension Hooks (TypeScript/JavaScript)
8264
- repo: https://github.com/pre-commit/mirrors-prettier

surfsense_backend/alembic/env.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import asyncio
2-
from logging.config import fileConfig
3-
42
import os
53
import sys
4+
from logging.config import fileConfig
5+
66
from sqlalchemy import pool
77
from sqlalchemy.engine import Connection
88
from sqlalchemy.ext.asyncio import async_engine_from_config
@@ -11,10 +11,10 @@
1111

1212
# Ensure the app directory is in the Python path
1313
# This allows Alembic to find your models
14-
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..')))
14+
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), "..")))
1515

1616
# Import your models base
17-
from app.db import Base # Assuming your Base is defined in app.db
17+
from app.db import Base # Assuming your Base is defined in app.db
1818

1919
# this is the Alembic Config object, which provides
2020
# access to the values within the .ini file in use.

0 commit comments

Comments
 (0)