code-quality.yml 8.1 KB

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