code-quality.yml 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 on Monday at 2 AM
  9. # Set permissions for security scanning
  10. permissions:
  11. contents: read
  12. security-events: write
  13. pull-requests: write
  14. # Cancel previous runs if a new one is triggered
  15. concurrency:
  16. group: ${{ github.workflow }}-${{ github.ref }}
  17. cancel-in-progress: true
  18. jobs:
  19. quality-check:
  20. name: Quality Analysis
  21. runs-on: ubuntu-latest
  22. timeout-minutes: 15
  23. strategy:
  24. fail-fast: false
  25. matrix:
  26. python-version: ["3.11", "3.12"]
  27. steps:
  28. - name: Checkout repository
  29. uses: actions/checkout@v4
  30. with:
  31. fetch-depth: 0
  32. - name: Set up Python ${{ matrix.python-version }}
  33. uses: actions/setup-python@v5
  34. with:
  35. python-version: ${{ matrix.python-version }}
  36. cache: 'pip'
  37. - name: Install system dependencies
  38. run: |
  39. sudo apt-get update
  40. sudo apt-get install -y build-essential libssl-dev libicapapi-dev python3-dev
  41. - name: Install dependencies
  42. run: |
  43. python -m pip install --upgrade pip
  44. pip install ruff mypy
  45. if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
  46. - name: Run Ruff linter
  47. run: ruff check . --output-format=github
  48. - name: Run Ruff formatter check
  49. run: ruff format . --check
  50. - name: Run MyPy type checker
  51. run: mypy . --ignore-missing-imports || true
  52. - name: Test import structure
  53. run: |
  54. python -c "
  55. import sys
  56. import os
  57. sys.path.insert(0, os.getcwd())
  58. # Test core imports
  59. try:
  60. from database.database import get_session, SystemMetrics
  61. from services.system_info import get_cpu_info, get_ram_info, get_network_stats
  62. from parsers.log import parse_log_line, process_logs
  63. from services.metrics_service import MetricsService
  64. print('✅ Core imports work correctly')
  65. except Exception as e:
  66. print(f'❌ Core import error: {e}')
  67. sys.exit(1)
  68. # Test optional imports
  69. try:
  70. from services.auditoria_service import get_all_usernames
  71. from services.blacklist_users import find_blacklisted_sites
  72. print('✅ Optional imports work correctly')
  73. except Exception as e:
  74. print(f'⚠️ Optional import warning: {e}')
  75. # No exit here, just warning
  76. print('✅ All critical imports successful')
  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. - name: Checkout repository
  89. uses: actions/checkout@v4
  90. with:
  91. fetch-depth: 0
  92. - name: Set up Python ${{ matrix.python-version }}
  93. uses: actions/setup-python@v5
  94. with:
  95. python-version: ${{ matrix.python-version }}
  96. cache: 'pip'
  97. - name: Install system dependencies
  98. run: |
  99. sudo apt-get update
  100. sudo apt-get install -y build-essential libssl-dev libicapapi-dev python3-dev
  101. - name: Install dependencies
  102. run: |
  103. python -m pip install --upgrade pip
  104. pip install pytest pytest-cov pytest-xdist
  105. if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
  106. - name: Run tests
  107. run: |
  108. if [ -d "tests" ]; then
  109. pytest tests/ -v --cov=. --cov-report=xml --cov-report=html --tb=short || true
  110. else
  111. echo "No tests directory found, skipping tests"
  112. fi
  113. - name: Upload coverage to Codecov
  114. uses: codecov/codecov-action@v4
  115. if: matrix.python-version == '3.11'
  116. with:
  117. file: ./coverage.xml
  118. fail_ci_if_error: false
  119. verbose: true
  120. - name: Upload coverage report
  121. uses: actions/upload-artifact@v4
  122. if: matrix.python-version == '3.11'
  123. with:
  124. name: coverage-report
  125. path: htmlcov/
  126. retention-days: 30
  127. database-check:
  128. name: Database Validation
  129. runs-on: ubuntu-latest
  130. timeout-minutes: 10
  131. needs: quality-check
  132. steps:
  133. - name: Checkout repository
  134. uses: actions/checkout@v4
  135. - name: Set up Python
  136. uses: actions/setup-python@v5
  137. with:
  138. python-version: "3.11"
  139. cache: 'pip'
  140. - name: Install system dependencies
  141. run: |
  142. sudo apt-get update
  143. sudo apt-get install -y build-essential libssl-dev libicapapi-dev python3-dev
  144. - name: Install dependencies
  145. run: |
  146. python -m pip install --upgrade pip
  147. if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
  148. - name: Test database creation
  149. run: |
  150. python -c "
  151. import sys
  152. import 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 creation test passed')
  159. except Exception as e:
  160. print(f'❌ Database test error: {e}')
  161. sys.exit(1)
  162. "
  163. config-validation:
  164. name: Configuration Validation
  165. runs-on: ubuntu-latest
  166. timeout-minutes: 5
  167. needs: quality-check
  168. steps:
  169. - name: Checkout repository
  170. uses: actions/checkout@v4
  171. - name: Set up Python
  172. uses: actions/setup-python@v5
  173. with:
  174. python-version: "3.11"
  175. cache: 'pip'
  176. - name: Install system dependencies
  177. run: |
  178. sudo apt-get update
  179. sudo apt-get install -y build-essential libssl-dev libicapapi-dev python3-dev
  180. - name: Install dependencies
  181. run: |
  182. python -m pip install --upgrade pip
  183. pip install pyyaml
  184. - name: Validate YAML files
  185. run: |
  186. python -c "
  187. import yaml
  188. import os
  189. yaml_files = []
  190. for root, dirs, files in os.walk('.'):
  191. for file in files:
  192. if file.endswith('.yml') or file.endswith('.yaml'):
  193. yaml_files.append(os.path.join(root, file))
  194. for yaml_file in yaml_files:
  195. try:
  196. with open(yaml_file, 'r') as f:
  197. yaml.safe_load(f)
  198. print(f'✅ {yaml_file} is valid')
  199. except yaml.YAMLError as e:
  200. print(f'❌ {yaml_file} is invalid: {e}')
  201. exit(1)
  202. "
  203. - name: Validate JSON files
  204. run: |
  205. python -c "
  206. import json
  207. import os
  208. json_files = []
  209. for root, dirs, files in os.walk('.'):
  210. for file in files:
  211. if file.endswith('.json'):
  212. json_files.append(os.path.join(root, file))
  213. for json_file in json_files:
  214. try:
  215. with open(json_file, 'r') as f:
  216. json.load(f)
  217. print(f'✅ {json_file} is valid')
  218. except json.JSONDecodeError as e:
  219. print(f'❌ {json_file} is invalid: {e}')
  220. exit(1)
  221. "
  222. security-check:
  223. name: Security Scan
  224. runs-on: ubuntu-latest
  225. timeout-minutes: 15
  226. needs: quality-check
  227. steps:
  228. - name: Checkout repository
  229. uses: actions/checkout@v4
  230. - name: Set up Python
  231. uses: actions/setup-python@v5
  232. with:
  233. python-version: "3.11"
  234. cache: 'pip'
  235. - name: Install security tools
  236. run: |
  237. python -m pip install --upgrade pip
  238. pip install bandit safety
  239. - name: Run Bandit security scan
  240. run: |
  241. bandit -r . -x tests/ -f json -o bandit-report.json || true
  242. bandit -r . -x tests/ -f txt || true
  243. continue-on-error: true
  244. - name: Check for known vulnerabilities
  245. run: |
  246. if [ -f requirements.txt ]; then
  247. safety check -r requirements.txt --json --output safety-report.json || true
  248. safety check -r requirements.txt || true
  249. else
  250. echo "No requirements.txt found, skipping safety check"
  251. fi
  252. continue-on-error: true
  253. - name: Upload security reports
  254. uses: actions/upload-artifact@v4
  255. if: always()
  256. with:
  257. name: security-reports
  258. path: |
  259. bandit-report.json
  260. safety-report.json
  261. retention-days: 30