| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- name: Code Quality & Security
- on:
- pull_request:
- branches: [ "main", "develop" ]
- push:
- branches: [ "main", "develop" ]
- schedule:
- - cron: '0 2 * * 1' # Weekly on Monday at 2 AM
- # Set permissions for security scanning
- permissions:
- contents: read
- security-events: write
- pull-requests: write
- # Cancel previous runs if a new one is triggered
- concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
- jobs:
- quality-check:
- name: Quality Analysis
- runs-on: ubuntu-latest
- timeout-minutes: 15
- strategy:
- fail-fast: false
- matrix:
- python-version: ["3.11", "3.12"]
-
- steps:
- - name: Checkout repository
- uses: actions/checkout@v4
- with:
- fetch-depth: 0
- - name: Set up Python ${{ matrix.python-version }}
- uses: actions/setup-python@v5
- with:
- python-version: ${{ matrix.python-version }}
- cache: 'pip'
- - 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 type checker
- run: mypy . --ignore-missing-imports || true
- - name: Test import structure
- run: |
- python -c "
- import sys
- import os
- sys.path.insert(0, os.getcwd())
-
- # Test core imports
- try:
- from database.database import get_session, SystemMetrics
- from services.system_info import get_cpu_info, get_ram_info, get_network_stats
- from parsers.log import parse_log_line, process_logs
- from services.metrics_service import MetricsService
- print('✅ Core imports work correctly')
- except Exception as e:
- print(f'❌ Core import error: {e}')
- sys.exit(1)
-
- # Test optional imports
- try:
- from services.auditoria_service import get_all_usernames
- from services.blacklist_users import find_blacklisted_sites
- print('✅ Optional imports work correctly')
- except Exception as e:
- print(f'⚠️ Optional import warning: {e}')
- # No exit here, just warning
-
- print('✅ All critical imports successful')
- "
- 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"]
-
- steps:
- - name: Checkout repository
- uses: actions/checkout@v4
- with:
- fetch-depth: 0
- - name: Set up Python ${{ matrix.python-version }}
- uses: actions/setup-python@v5
- with:
- python-version: ${{ matrix.python-version }}
- cache: 'pip'
- - name: Install dependencies
- run: |
- python -m pip install --upgrade pip
- pip install pytest pytest-cov pytest-xdist
- 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 || true
- else
- echo "No tests directory found, skipping tests"
- fi
- - name: Upload coverage to Codecov
- uses: codecov/codecov-action@v4
- if: matrix.python-version == '3.11'
- with:
- file: ./coverage.xml
- fail_ci_if_error: false
- verbose: true
- - name: Upload coverage report
- uses: actions/upload-artifact@v4
- if: matrix.python-version == '3.11'
- with:
- name: coverage-report
- path: htmlcov/
- retention-days: 30
- database-check:
- name: Database Validation
- runs-on: ubuntu-latest
- timeout-minutes: 10
- needs: quality-check
-
- steps:
- - name: Checkout repository
- uses: actions/checkout@v4
- - name: Set up Python
- uses: actions/setup-python@v5
- 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: Test database creation
- run: |
- python -c "
- import sys
- import 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 creation test passed')
- except Exception as e:
- print(f'❌ Database test error: {e}')
- sys.exit(1)
- "
- config-validation:
- name: Configuration Validation
- runs-on: ubuntu-latest
- timeout-minutes: 5
- needs: quality-check
-
- steps:
- - name: Checkout repository
- uses: actions/checkout@v4
- - name: Set up Python
- uses: actions/setup-python@v5
- with:
- python-version: "3.11"
- cache: 'pip'
- - name: Install dependencies
- run: |
- python -m pip install --upgrade pip
- pip install pyyaml
- - name: Validate YAML files
- run: |
- python -c "
- import yaml
- import os
-
- yaml_files = []
- for root, dirs, files in os.walk('.'):
- for file in files:
- if file.endswith('.yml') or file.endswith('.yaml'):
- yaml_files.append(os.path.join(root, file))
-
- for yaml_file in yaml_files:
- try:
- with open(yaml_file, 'r') as f:
- yaml.safe_load(f)
- print(f'✅ {yaml_file} is valid')
- except yaml.YAMLError as e:
- print(f'❌ {yaml_file} is invalid: {e}')
- exit(1)
- "
- - name: Validate JSON files
- run: |
- python -c "
- import json
- import os
-
- json_files = []
- for root, dirs, files in os.walk('.'):
- for file in files:
- if file.endswith('.json'):
- json_files.append(os.path.join(root, file))
-
- for json_file in json_files:
- try:
- with open(json_file, 'r') as f:
- json.load(f)
- print(f'✅ {json_file} is valid')
- except json.JSONDecodeError as e:
- print(f'❌ {json_file} is invalid: {e}')
- exit(1)
- "
- security-check:
- name: Security Scan
- runs-on: ubuntu-latest
- timeout-minutes: 15
- needs: quality-check
-
- steps:
- - name: Checkout repository
- uses: actions/checkout@v4
- - name: Set up Python
- uses: actions/setup-python@v5
- with:
- python-version: "3.11"
- cache: 'pip'
- - name: Install security tools
- run: |
- python -m pip install --upgrade pip
- pip install bandit safety
- - name: Run Bandit security scan
- run: |
- bandit -r . -x tests/ -f json -o bandit-report.json || true
- bandit -r . -x tests/ -f txt || true
- continue-on-error: true
- - name: Check for known vulnerabilities
- run: |
- if [ -f requirements.txt ]; then
- safety check -r requirements.txt --json --output safety-report.json || true
- safety check -r requirements.txt || true
- else
- echo "No requirements.txt found, skipping safety check"
- fi
- continue-on-error: true
- - name: Upload security reports
- uses: actions/upload-artifact@v4
- if: always()
- with:
- name: security-reports
- path: |
- bandit-report.json
- safety-report.json
- retention-days: 30
|