code-quality.yml 7.3 KB

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