code-quality.yml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. name: Code Quality & Security
  2. on:
  3. pull_request:
  4. branches: ["main", "develop"]
  5. push:
  6. branches: ["main", "develop"]
  7. schedule:
  8. - cron: "0 2 * * 1" # Weekly Monday 2 AM
  9. permissions:
  10. contents: read
  11. security-events: write
  12. pull-requests: write
  13. concurrency:
  14. group: ${{ github.workflow }}-${{ github.ref }}
  15. cancel-in-progress: true
  16. env:
  17. PYTHONUNBUFFERED: 1
  18. jobs:
  19. # =========================
  20. # 🔍 QUALITY CHECK
  21. # =========================
  22. quality-check:
  23. name: Quality Analysis
  24. runs-on: ubuntu-latest
  25. timeout-minutes: 15
  26. strategy:
  27. fail-fast: false
  28. matrix:
  29. python-version: ["3.11", "3.12"]
  30. steps:
  31. - uses: actions/checkout@v4
  32. with:
  33. fetch-depth: 0
  34. - uses: actions/setup-python@v5
  35. with:
  36. python-version: ${{ matrix.python-version }}
  37. cache: pip
  38. - name: Install system dependencies
  39. run: |
  40. sudo apt-get update
  41. sudo apt-get install -y \
  42. build-essential \
  43. libssl-dev \
  44. libicapapi-dev \
  45. python3-dev \
  46. libpq-dev
  47. - name: Install dependencies
  48. run: |
  49. python -m pip install --upgrade pip
  50. pip install ruff mypy
  51. if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
  52. - name: Run Ruff Linter
  53. run: ruff check . --output-format=github
  54. - name: Run Ruff Formatter Check
  55. run: ruff format . --check
  56. - name: Run MyPy
  57. run: mypy . --ignore-missing-imports
  58. - name: Validate Critical Imports
  59. run: |
  60. python << 'EOF'
  61. import sys, os
  62. sys.path.insert(0, os.getcwd())
  63. try:
  64. from database.database import get_session, SystemMetrics
  65. from database.models.models import BlacklistDomain
  66. from services.system.system_info import get_cpu_info, get_ram_info, get_network_stats
  67. from services.system.metrics_service import MetricsService
  68. from parsers.log import parse_log_line, process_logs
  69. print("✅ Core imports OK")
  70. except Exception as e:
  71. print(f"❌ Core import error: {e}")
  72. sys.exit(1)
  73. print("✔ Import validation completed")
  74. EOF
  75. # =========================
  76. # 🧪 TEST SUITE
  77. # =========================
  78. test-suite:
  79. name: Test Suite
  80. runs-on: ubuntu-latest
  81. timeout-minutes: 20
  82. needs: quality-check
  83. strategy:
  84. fail-fast: false
  85. matrix:
  86. python-version: ["3.11", "3.12"]
  87. steps:
  88. - uses: actions/checkout@v4
  89. - uses: actions/setup-python@v5
  90. with:
  91. python-version: ${{ matrix.python-version }}
  92. cache: pip
  93. - name: Install system dependencies
  94. run: |
  95. sudo apt-get update
  96. sudo apt-get install -y \
  97. build-essential \
  98. libssl-dev \
  99. libicapapi-dev \
  100. python3-dev \
  101. libpq-dev
  102. - name: Install dependencies
  103. run: |
  104. python -m pip install --upgrade pip
  105. pip install pytest pytest-cov pytest-xdist
  106. if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
  107. - name: Run Tests
  108. run: |
  109. if [ -d "tests" ]; then
  110. pytest tests/ -v \
  111. --cov=. \
  112. --cov-report=xml \
  113. --cov-report=html \
  114. --tb=short
  115. else
  116. echo "No tests directory found, skipping."
  117. fi
  118. - name: Upload Coverage (only 3.11)
  119. if: matrix.python-version == '3.11'
  120. uses: codecov/codecov-action@v4
  121. with:
  122. file: ./coverage.xml
  123. fail_ci_if_error: false
  124. - name: Upload Coverage Artifact
  125. if: matrix.python-version == '3.11'
  126. uses: actions/upload-artifact@v4
  127. with:
  128. name: coverage-report
  129. path: htmlcov/
  130. retention-days: 30
  131. # =========================
  132. # 🗄 DATABASE VALIDATION
  133. # =========================
  134. database-check:
  135. name: Database Validation
  136. runs-on: ubuntu-latest
  137. timeout-minutes: 10
  138. needs: quality-check
  139. steps:
  140. - uses: actions/checkout@v4
  141. - uses: actions/setup-python@v5
  142. with:
  143. python-version: "3.11"
  144. cache: pip
  145. - name: Install dependencies
  146. run: |
  147. python -m pip install --upgrade pip
  148. if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
  149. - name: Validate Database Creation
  150. run: |
  151. python << 'EOF'
  152. import sys, os
  153. sys.path.insert(0, os.getcwd())
  154. try:
  155. from database.database import get_engine, create_dynamic_tables
  156. engine = get_engine()
  157. create_dynamic_tables(engine)
  158. print("✅ Database validation successful")
  159. except Exception as e:
  160. print(f"❌ Database error: {e}")
  161. sys.exit(1)
  162. EOF
  163. # =========================
  164. # ⚙ CONFIG VALIDATION
  165. # =========================
  166. config-validation:
  167. name: Configuration Validation
  168. runs-on: ubuntu-latest
  169. timeout-minutes: 5
  170. needs: quality-check
  171. steps:
  172. - uses: actions/checkout@v4
  173. - uses: actions/setup-python@v5
  174. with:
  175. python-version: "3.11"
  176. - name: Install validation deps
  177. run: pip install pyyaml
  178. - name: Validate YAML & JSON
  179. run: |
  180. python << 'EOF'
  181. import yaml, json, os, sys
  182. for root, _, files in os.walk('.'):
  183. for file in files:
  184. path = os.path.join(root, file)
  185. if file.endswith(('.yml', '.yaml')):
  186. try:
  187. with open(path) as f:
  188. yaml.safe_load(f)
  189. except Exception as e:
  190. print(f"❌ Invalid YAML: {path} -> {e}")
  191. sys.exit(1)
  192. if file.endswith('.json'):
  193. try:
  194. with open(path) as f:
  195. json.load(f)
  196. except Exception as e:
  197. print(f"❌ Invalid JSON: {path} -> {e}")
  198. sys.exit(1)
  199. print("✅ Configuration files valid")
  200. EOF
  201. # =========================
  202. # 🔐 SECURITY SCAN
  203. # =========================
  204. security-check:
  205. name: Security Scan
  206. runs-on: ubuntu-latest
  207. timeout-minutes: 15
  208. needs: quality-check
  209. steps:
  210. - uses: actions/checkout@v4
  211. - uses: actions/setup-python@v5
  212. with:
  213. python-version: "3.11"
  214. - name: Install security tools
  215. run: |
  216. python -m pip install --upgrade pip
  217. pip install bandit safety
  218. - name: Run Bandit
  219. run: |
  220. bandit -r . -x tests/ -f json -o bandit-report.json || true
  221. bandit -r . -x tests/ -f txt || true
  222. - name: Run Safety
  223. run: |
  224. if [ -f requirements.txt ]; then
  225. safety check -r requirements.txt \
  226. --json --output safety-report.json || true
  227. fi
  228. - name: Upload Security Reports
  229. if: always()
  230. uses: actions/upload-artifact@v4
  231. with:
  232. name: security-reports
  233. path: |
  234. bandit-report.json
  235. safety-report.json
  236. retention-days: 30