| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- name: Code Quality & Security
- on:
- pull_request:
- branches: ["main", "dev"]
- push:
- branches: ["main", "dev"]
- schedule:
- - cron: "0 2 * * 1" # Weekly Monday 2 AM
- permissions:
- contents: read
- security-events: write
- pull-requests: write
- concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
- env:
- PYTHONUNBUFFERED: 1
- jobs:
- # =========================
- # 🔍 QUALITY CHECK
- # =========================
- quality-check:
- name: Quality Analysis
- runs-on: ubuntu-latest
- timeout-minutes: 15
- strategy:
- fail-fast: false
- matrix:
- python-version: ["3.11", "3.12", "3.13", "3.14"]
- steps:
- - uses: actions/checkout@v7
- with:
- fetch-depth: 0
- - uses: actions/setup-python@v6.3.0
- with:
- python-version: ${{ matrix.python-version }}
- cache: pip
- - name: Install system dependencies
- run: |
- sudo apt-get update
- sudo apt-get install -y \
- build-essential \
- libssl-dev \
- libicapapi-dev \
- python3-dev \
- libpq-dev
- - name: Install dependencies
- run: |
- python -m pip install --upgrade pip
- pip install ruff mypy
- if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- - name: Run Ruff Linter
- run: ruff check . --output-format=github
- - name: Run Ruff Formatter Check
- run: ruff format . --check
- - name: Run MyPy
- continue-on-error: true
- run: mypy . --ignore-missing-imports --show-error-codes
- - name: Validate Critical Imports
- run: |
- python << 'EOF'
- import sys, os
- sys.path.insert(0, os.getcwd())
- try:
- from database.database import get_session, SystemMetrics
- from database.models.models import BlacklistDomain
- from services.system.system_info import get_cpu_info, get_ram_info, get_network_stats
- from services.system.metrics_service import MetricsService
- from parsers.log import parse_log_line, process_logs
- print("✅ Core imports OK")
- except Exception as e:
- print(f"❌ Core import error: {e}")
- sys.exit(1)
- print("✔ Import validation completed")
- EOF
- # =========================
- # 🧪 TEST SUITE
- # =========================
- test-suite:
- name: Test Suite
- runs-on: ubuntu-latest
- timeout-minutes: 20
- needs: quality-check
- strategy:
- fail-fast: false
- matrix:
- python-version: ["3.11", "3.12", "3.13", "3.14"]
- steps:
- - uses: actions/checkout@v7
- - uses: actions/setup-python@v6.3.0
- with:
- python-version: ${{ matrix.python-version }}
- cache: pip
- - name: Install system dependencies
- run: |
-
- sudo apt-get update
- sudo apt-get install -y \
- build-essential \
- libssl-dev \
- libicapapi-dev \
- python3-dev \
- libpq-dev
- - name: Install dependencies
- run: |
- python -m pip install --upgrade pip
- pip install pytest pytest-cov pytest-xdist pytest-asyncio
- if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- - name: Run Tests
- run: |
- if [ -d "tests" ]; then
- pytest tests/ -v \
- --cov=. \
- --cov-report=xml \
- --cov-report=html \
- --tb=short
- else
- echo "No tests directory found, skipping."
- fi
- # - name: Upload Coverage (only 3.11)
- # if: matrix.python-version == '3.11'
- # uses: codecov/codecov-action@v4
- # with:
- # file: ./coverage.xml
- # fail_ci_if_error: false
- # - name: Upload Coverage Artifact
- # if: matrix.python-version == '3.11'
- # uses: actions/upload-artifact@v7.0.1
- # with:
- # name: coverage-report
- # path: htmlcov/
- # retention-days: 30
- # =========================
- # 🗄 DATABASE VALIDATION
- # =========================
- database-check:
- name: Database Validation
- runs-on: ubuntu-latest
- timeout-minutes: 10
- needs: quality-check
- steps:
- - uses: actions/checkout@v7
- - uses: actions/setup-python@v6.3.0
- with:
- python-version: "3.11"
- cache: pip
- - name: Install dependencies
- run: |
- python -m pip install --upgrade pip
- if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- - name: Validate Database Creation
- run: |
- python << 'EOF'
- import sys, os
- sys.path.insert(0, os.getcwd())
- try:
- from database.database import get_engine, create_dynamic_tables
- engine = get_engine()
- create_dynamic_tables(engine)
- print("✅ Database validation successful")
- except Exception as e:
- print(f"❌ Database error: {e}")
- sys.exit(1)
- EOF
- # =========================
- # ⚙ CONFIG VALIDATION
- # =========================
- config-validation:
- name: Configuration Validation
- runs-on: ubuntu-latest
- timeout-minutes: 5
- needs: quality-check
- steps:
- - uses: actions/checkout@v7
- - uses: actions/setup-python@v6.3.0
- with:
- python-version: "3.11"
- - name: Install validation deps
- run: pip install pyyaml
- - name: Validate YAML & JSON
- run: |
- python << 'EOF'
- import yaml, json, os, sys
- for root, _, files in os.walk('.'):
- for file in files:
- path = os.path.join(root, file)
- if file.endswith(('.yml', '.yaml')):
- try:
- with open(path) as f:
- yaml.safe_load(f)
- except Exception as e:
- print(f"❌ Invalid YAML: {path} -> {e}")
- sys.exit(1)
- if file.endswith('.json'):
- try:
- with open(path) as f:
- json.load(f)
- except Exception as e:
- print(f"❌ Invalid JSON: {path} -> {e}")
- sys.exit(1)
- print("✅ Configuration files valid")
- EOF
- # =========================
- # 🔐 SECURITY SCAN
- # =========================
- security-check:
- name: Security Scan
- runs-on: ubuntu-latest
- timeout-minutes: 15
- needs: quality-check
- steps:
- - uses: actions/checkout@v7
- - uses: actions/setup-python@v6.3.0
- with:
- python-version: "3.11"
- - name: Install security tools
- run: |
- python -m pip install --upgrade pip
- pip install bandit safety
- - name: Run Bandit
- run: |
- bandit -r . -x tests/ -f json -o bandit-report.json || true
- bandit -r . -x tests/ -f txt || true
- - name: Run Safety
- run: |
- if [ -f requirements.txt ]; then
- safety check -r requirements.txt \
- --json --output safety-report.json || true
- fi
- - name: Upload Security Reports
- if: always()
- uses: actions/upload-artifact@v7.0.1
- with:
- name: security-reports
- path: |
- bandit-report.json
- safety-report.json
- retention-days: 30
|