From 1946192be975eeea75432ec39b27d19952742b15 Mon Sep 17 00:00:00 2001 From: Sergey Krylov Date: Wed, 16 Sep 2026 21:15:21 +0300 Subject: [PATCH] ci: add backend SonarQube analysis #23 --- .gitea/workflows/verify.yml | 10 ++++++++++ .gitignore | 1 + README.md | 38 ++++++++++++++++++++++++++++++++++--- package.json | 2 +- sonar-project.properties | 12 ++++++++++++ tsconfig.sonar.json | 15 +++++++++++++++ 6 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 sonar-project.properties create mode 100644 tsconfig.sonar.json diff --git a/.gitea/workflows/verify.yml b/.gitea/workflows/verify.yml index 7878842..204159a 100644 --- a/.gitea/workflows/verify.yml +++ b/.gitea/workflows/verify.yml @@ -10,6 +10,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: https://github.com/actions/checkout@v4 + with: + fetch-depth: 0 - name: Record checked revision run: git rev-parse HEAD - uses: https://github.com/actions/setup-node@v4 @@ -22,3 +24,11 @@ jobs: run: npm run verify - name: Run isolated SQLite and runtime OpenAPI suites run: npm run test:e2e + - name: Analyze develop with SonarQube + if: gitea.event_name == 'push' && gitea.ref == 'refs/heads/develop' + # v2.3.0: same Docker-based scanner used for SonarQube 9.9. + uses: https://github.com/SonarSource/sonarqube-scan-action@aecaf43ae57e412bd97d70ef9ce6076e672fe0a9 + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + with: + args: -Dsonar.qualitygate.wait=true diff --git a/.gitignore b/.gitignore index c01f41d..a87206a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ lerna-debug.log* # Tests /coverage +/.scannerwork /.nyc_output # IDEs and editors diff --git a/README.md b/README.md index dc94750..10d9e4c 100644 --- a/README.md +++ b/README.md @@ -58,9 +58,9 @@ npm run verify ``` Он запускает zero-warning lint, -application/test typechecks, Jest и build. `npm run lint:fix` — отдельная -записывающая команда, `npm run test:cov` — диагностика без общего числового -threshold. `npm run test:e2e` отдельно проверяет все HTTP suites на новых базах +application/test typechecks, Jest с покрытием и build. `npm run lint:fix` — отдельная +записывающая команда. `npm run test:cov` также доступен отдельно; общего числового +threshold нет. `npm run test:e2e` отдельно проверяет все HTTP suites на новых базах и соответствие генерируемого OpenAPI настроенному приложению. Для полной backend-проверки выполните обе команды. @@ -102,6 +102,38 @@ Required checks настраиваются отдельно в Gitea: налич runtime-валидацию. В обычном коде нужны инициализация или сужение типа после проверки значения. +## SonarQube + +Проект `wealthledger-backend` на `https://sonarqube.ksv741.keenetic.pro` +использует `sonar-project.properties`. Анализируются исходники `src`; unit specs +и HTTP specs учитываются как тесты. Внешние T-Bank proto contracts исключены, +generated Prisma client находится за пределами `sonar.sources`. + +`npm run verify` запускает unit tests с покрытием один раз. Jest штатно создаёт +`coverage/lcov.info`; scanner импортирует этот отчёт. Покрытие HTTP/e2e tests +в него не входит, сами HTTP suites остаются обязательным отдельным шагом CI. + +Создайте в SonarQube проект с ключом `wealthledger-backend`, а в Gitea → backend → +Settings → Actions → Secrets добавьте `SONAR_TOKEN` с правом анализа этого проекта. +Токен не сохраняется в файлах. Community Edition 9.9.8 получает анализ только +при push в `develop`, после успешных `verify` и SQLite/e2e checks. CI ожидает +Quality Gate; отрицательный результат или ошибка scanner завершает job с ошибкой. +PR и другие ветки проходят штатные проверки без отправки анализа. +Переименование основной ветки SonarQube не требуется; `sonar.branch.name` не задаётся. + +Runner должен поддерживать Docker actions и иметь доступ к GitHub, Docker Hub +и HTTPS-адресу SonarQube с действующим сертификатом для этого имени. +Официальный Docker action закреплён на commit версии 2.3.0, как в frontend. +`tsconfig.sonar.json` содержит совместимые со старым анализатором настройки +модулей и decorators с target ES2022, без новых compiler options приложения. +Штатная компиляция приложения остаётся на ES2023. +После обновления сервера пересмотрите scanner и необходимость отдельного tsconfig. + +Локально после `nvm use --silent`, `npm ci` и генерации Prisma client выполните +`npm run verify`, затем `sonar-scanner` с совместимым SonarScanner CLI и +`SONAR_TOKEN` в окружении. Первый запуск CI после merge должен подтвердить +загрузку отчёта и покрытия: локальные проверки не доказывают доступность сервера. + ## Документация - [AGENTS: правила и источники](AGENTS.md) diff --git a/package.json b/package.json index 9c5aed9..8ead7c8 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "test:container": "node test/container-smoke.cjs", "test:durability": "node test/bank-cards-durability.cjs", "test:openapi-contract": "npm run build && node test/sqlite.cjs openapi-contract.e2e-spec.ts", - "verify": "npm run lint && npm run typecheck:app && npm run typecheck:test && npm test -- --runInBand && npm run build", + "verify": "npm run lint && npm run typecheck:app && npm run typecheck:test && npm run test:cov -- --runInBand && npm run build", "release:preview": "release-it --dry-run --ci", "release:prepare": "release-it --ci", "commitlint": "commitlint" diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..7c8078f --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,12 @@ +sonar.projectKey=wealthledger-backend +sonar.projectName=WealthLedger backend +sonar.host.url=https://sonarqube.ksv741.keenetic.pro +sonar.sourceEncoding=UTF-8 + +sonar.sources=src +sonar.tests=src,test +sonar.test.inclusions=src/**/*.spec.ts,test/**/*.e2e-spec.ts +sonar.exclusions=src/**/*.spec.ts,src/integrations/tbank/proto/contracts/** +sonar.javascript.lcov.reportPaths=coverage/lcov.info +# SonarQube 9.9 embeds an older TypeScript analyzer than the application compiler. +sonar.typescript.tsconfigPaths=tsconfig.sonar.json diff --git a/tsconfig.sonar.json b/tsconfig.sonar.json new file mode 100644 index 0000000..763f85e --- /dev/null +++ b/tsconfig.sonar.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "nodenext", + "moduleResolution": "nodenext", + "target": "ES2022", + "esModuleInterop": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "strict": true, + "skipLibCheck": true, + "noEmit": true + }, + "include": ["src/**/*.ts", "test/**/*.ts"], + "exclude": ["node_modules", "dist", "generated"] +} -- 2.54.0