code-quality.yml 8.3 KB

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