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