Merge pull request #204 from poli0981/ci/sonarcloud-ci-analysis
ci: analyse with SonarCloud in CI instead of automatic analysis
This commit is contained in:
8 files changed
+143
-27
No files matched your search
@@ -0,0 +1,81 @@
|
||||
name: SonarCloud
|
||||
|
||||
# CI-based analysis, which replaced SonarCloud's automatic analysis: the two
|
||||
# cannot both be on, and the scan fails while automatic analysis is enabled in
|
||||
# the project's Administration > Analysis Method. What is code, what is test
|
||||
# and what is not analysed at all is sonar-project.properties at the root.
|
||||
# SonarCloud's GitHub App still posts the "SonarCloud Code Analysis" check on
|
||||
# the pull request; this job only uploads the analysis.
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
# What the data jobs commit, all of it outside the analysis scope: a
|
||||
# commit that touches nothing else has nothing new to analyse.
|
||||
paths-ignore: ['data/**', 'games/**', 'scripts/*.jsonl', 'scripts/*.log']
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened]
|
||||
paths-ignore: ['data/**', 'games/**', 'scripts/*.jsonl', 'scripts/*.log']
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
# SonarCloud refuses an analysis older than the last one it has for a
|
||||
# branch, so main's runs queue rather than overlap.
|
||||
group: sonarcloud-${{ github.ref }}
|
||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||
|
||||
jobs:
|
||||
scan:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
steps:
|
||||
# Pull requests from forks and from Dependabot get no repository
|
||||
# secrets. Without the token the scan cannot upload anything, so the job
|
||||
# says so and passes rather than failing every such pull request.
|
||||
- name: Check for the token
|
||||
id: token
|
||||
env:
|
||||
HAS_TOKEN: ${{ secrets.SONAR_TOKEN != '' }}
|
||||
run: |
|
||||
echo "present=$HAS_TOKEN" >> "$GITHUB_OUTPUT"
|
||||
if [ "$HAS_TOKEN" != "true" ]; then
|
||||
echo "::notice::SONAR_TOKEN is not available to this run; skipping the SonarCloud scan."
|
||||
fi
|
||||
|
||||
# Full history: SonarCloud blames every line to tell new code from old.
|
||||
- uses: actions/checkout@v6
|
||||
if: steps.token.outputs.present == 'true'
|
||||
with:
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
|
||||
# The TypeScript analyser builds a program from each tsconfig named in
|
||||
# sonar-project.properties. web/tsconfig.json extends the one
|
||||
# `svelte-kit sync` generates, and the types come from node_modules;
|
||||
# without them it logs "Failed to parse tsconfig" and analyses web/src
|
||||
# with no types. Both are gitignored, so neither is analysed itself.
|
||||
- uses: actions/setup-node@v7
|
||||
if: steps.token.outputs.present == 'true'
|
||||
with:
|
||||
node-version: '26'
|
||||
cache: 'npm'
|
||||
cache-dependency-path: web/package-lock.json
|
||||
|
||||
# --ignore-scripts, as everywhere this repo runs `npm ci` (web-ci.yml says
|
||||
# why). The bin by path, not `npx`, which may fetch a package the
|
||||
# lockfile never named.
|
||||
- name: Prepare the TypeScript projects
|
||||
if: steps.token.outputs.present == 'true'
|
||||
working-directory: web
|
||||
run: |
|
||||
npm ci --ignore-scripts
|
||||
node_modules/.bin/svelte-kit sync
|
||||
|
||||
- name: SonarCloud scan
|
||||
if: steps.token.outputs.present == 'true'
|
||||
uses: SonarSource/sonarqube-scan-action@d209202bc7d53ff1cc128f7f907dac145c9d6ae9 # v8.3.0
|
||||
env:
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
@@ -7,14 +7,14 @@ name: Web CI
|
||||
|
||||
# Workflow and Sonar changes run it too: web/shared/pipeline.test.ts and
|
||||
# web/shared/toolchain.test.ts read .github/workflows/ (the data-write group, the
|
||||
# Node and Python pins) and .sonarcloud.properties, and a pull request that
|
||||
# Node and Python pins) and sonar-project.properties, and a pull request that
|
||||
# touches only those files is exactly the one that can break them.
|
||||
on:
|
||||
pull_request:
|
||||
paths: ['web/**', '.github/workflows/**', '.sonarcloud.properties']
|
||||
paths: ['web/**', '.github/workflows/**', 'sonar-project.properties']
|
||||
push:
|
||||
branches: [main]
|
||||
paths: ['web/**', '.github/workflows/**', '.sonarcloud.properties']
|
||||
paths: ['web/**', '.github/workflows/**', 'sonar-project.properties']
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
|
||||
+5
-1
@@ -13,7 +13,11 @@ All notable changes to this awesome noob repo will be documented here.
|
||||
release's own downloads, checking every signature on the way.
|
||||
- **SonarCloud reads tests as tests**, and no longer analyses the dataset,
|
||||
the Android resources the Tauri CLI generated, or the SQLite migrations its
|
||||
PL/SQL analyser misread (`.sonarcloud.properties`).
|
||||
PL/SQL analyser misread.
|
||||
- **SonarCloud analyses in CI.** A workflow runs the scan on pull requests and
|
||||
on `main` in place of SonarCloud's automatic analysis, with its settings in
|
||||
`sonar-project.properties` (formerly `.sonarcloud.properties`). Commits that
|
||||
only carry data are not scanned.
|
||||
- **TypeScript 7, Node 26 and Python 3.15.** The type-check runs on
|
||||
TypeScript 7, the native compiler: `tsc` for the Worker, the tests and the
|
||||
admin's Node code, and `svelte-check --tsgo` for the app. TypeScript 6 stays
|
||||
|
||||
@@ -436,7 +436,7 @@ scraping pipeline (`scripts/`) driven by GitHub Actions.
|
||||
`uv pip compile --universal --generate-hashes --python-version 3.15`.
|
||||
- **One version of each runtime, and a test that holds them together**
|
||||
(`web/shared/toolchain.test.ts`; web-ci also runs on `.github/workflows/**`
|
||||
and `.sonarcloud.properties` so a pull request that only moves a pin meets
|
||||
and `sonar-project.properties` so a pull request that only moves a pin meets
|
||||
it). `docs/plan/16-toolchain-2026.md` has the upgrade notes.
|
||||
- Node is the major in `web/.node-version` (26). Every `setup-node` repeats
|
||||
it as a quoted literal, never `node-version-file`: `release-android.yml`
|
||||
@@ -452,13 +452,23 @@ scraping pipeline (`scripts/`) driven by GitHub Actions.
|
||||
text open in `scripts/` still names `encoding="utf-8"`: on Windows an
|
||||
older Python writes the locale's code page, and cp1252 cannot encode the
|
||||
leaderboards' emoji.
|
||||
- **SonarCloud's scope is `.sonarcloud.properties`** (automatic analysis; only
|
||||
scope keys are read, from the default branch). Code and tests both start at
|
||||
the root and are split by patterns: a new test-helper folder goes into
|
||||
`sonar.test.inclusions` AND `sonar.exclusions`, or it is analysed as code.
|
||||
It leaves out the dataset, generated Android resources and the SQLite
|
||||
migrations (the PL/SQL analyser reads them as Oracle). Suppressing a rule
|
||||
for a file is done in the SonarCloud UI, not in this file or with NOSONAR.
|
||||
- **SonarCloud analyses in CI (`sonarcloud.yml`), and its scope is
|
||||
`sonar-project.properties`.** Automatic analysis is OFF in the project's
|
||||
Administration > Analysis Method and must stay off: while it is on, every CI
|
||||
scan fails. The scanner reads the file from the commit it analyses. Code and
|
||||
tests both start at the root and are split by patterns: a new test-helper
|
||||
folder goes into `sonar.test.inclusions` AND `sonar.exclusions`, or it is
|
||||
analysed as code (and a file both include fails the scan). It leaves out the
|
||||
dataset, generated Android resources and the SQLite migrations (the PL/SQL
|
||||
analyser reads them as Oracle). `sonar.typescript.tsconfigPaths` names all
|
||||
six tsconfigs in `web/` (auto-detection finds only files named exactly
|
||||
`tsconfig.json`), and the job runs `npm ci --ignore-scripts` and
|
||||
`svelte-kit sync` first: `web/tsconfig.json` extends the generated one, and
|
||||
without it the analyser logged "Failed to parse tsconfig" and read `web/src`
|
||||
untyped. A new tsconfig joins that list. The job skips commits that touch only what
|
||||
the data jobs write, and passes without scanning when `SONAR_TOKEN` is not
|
||||
available (forks, Dependabot). Suppressing a rule for a file is done in the
|
||||
SonarCloud UI, not in this file or with NOSONAR.
|
||||
|
||||
## Frontend gotchas
|
||||
|
||||
|
||||
+15
-5
@@ -179,7 +179,7 @@ Stop `wrangler dev` before running `npm ci`. On Windows the install deletes
|
||||
## Checks
|
||||
|
||||
`.github/workflows/web-ci.yml` runs on pull requests and on pushes to `main`
|
||||
touching `web/**`, `.github/workflows/**` or `.sonarcloud.properties`:
|
||||
touching `web/**`, `.github/workflows/**` or `sonar-project.properties`:
|
||||
`npm ci --ignore-scripts`, the typechecks (app, Worker and tests, on
|
||||
TypeScript 7; the admin's svelte-check on TypeScript 6), `npm test` (which
|
||||
includes the toolchain-pin and data-write-group checks over the workflows),
|
||||
@@ -196,13 +196,23 @@ none of the three packages that have one needs it (the comment in
|
||||
`web-ci.yml` says why), and no workflow runs `npx`, which can fetch a package
|
||||
the lockfile never named.
|
||||
|
||||
CodeQL (`codeql.yml`) and SonarCloud's automatic analysis both look at pull
|
||||
CodeQL (`codeql.yml`) and SonarCloud (`sonarcloud.yml`) both look at pull
|
||||
requests and `main`. Each posts a check on the pull request (`CodeQL`,
|
||||
`SonarCloud Code Analysis`) about the issues the change brings in; the full list
|
||||
for `main` lives in the repository's Security tab and on the SonarCloud
|
||||
dashboard. `.sonarcloud.properties` at the root decides what SonarCloud reads as
|
||||
code, what as tests, and what not at all (the dataset, generated Android
|
||||
resources, the SQLite migrations); it takes effect from `main`.
|
||||
dashboard. `sonar-project.properties` at the root names the project and decides
|
||||
what SonarCloud reads as code, what as tests, and what not at all (the dataset,
|
||||
generated Android resources, the SQLite migrations); the scanner reads it from
|
||||
the commit it analyses.
|
||||
|
||||
SonarCloud runs in CI, with the repository secret `SONAR_TOKEN` (a SonarCloud
|
||||
token that may analyse the project). Its automatic analysis must stay off
|
||||
(project Administration > Analysis Method): with both on, every CI scan fails.
|
||||
Commits that touch only what the data jobs write (`data/`, `games/`, the
|
||||
`.jsonl` queues and logs under `scripts/`) are not scanned. A pull request from
|
||||
a fork or from Dependabot gets no repository secrets, so its scan job passes
|
||||
without scanning; giving Dependabot its own `SONAR_TOKEN` (Settings > Secrets >
|
||||
Dependabot) would have its pull requests analysed too.
|
||||
|
||||
### A red "Workers Builds" check on a PR branch is usually not yours
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ replace TypeScript 6, which stays installed beside it.
|
||||
| Python (CodeQL) | 3.14 | `codeql.yml` | — | named in the test's `PYTHON_EXCEPTIONS` |
|
||||
| TypeScript | 7.0.x and 6.0.x | `web/package.json`: `@typescript/native` (`npm:typescript@~7.0.2`) and `typescript` (`^6.0.3`) | `.github/dependabot.yml` ignores | `web/shared/toolchain.test.ts` |
|
||||
|
||||
Web CI now also runs on `.github/workflows/**` and `.sonarcloud.properties`, so
|
||||
Web CI now also runs on `.github/workflows/**` and `sonar-project.properties`, so
|
||||
a pull request that moves only a pin still meets the test. The test was proven
|
||||
by breaking one pin at a time - one workflow's Python, `web/.node-version`
|
||||
alone, a `setup-node` switched to `node-version-file` - and each failed it with
|
||||
@@ -222,7 +222,9 @@ CI: sixteen `setup-python` steps moved from `'3.12'` to `'3.15'`; the
|
||||
now sets up 3.15 like the rest. `codeql.yml` alone uses 3.14: CodeQL's
|
||||
supported-languages page lists Python up to 3.14 and its extractor runs on
|
||||
that interpreter. The pipeline uses no 3.15-only syntax, so nothing goes
|
||||
unparsed. `sonar.python.version` is 3.15; sonar-python knows 3.15.
|
||||
unparsed. `sonar.python.version` is 3.15. SonarCloud's sonar-python does not
|
||||
list 3.15 yet: the CI scan (2026-10-22) warns and analyses as 3.14, which
|
||||
changes nothing for this code.
|
||||
|
||||
## Next time
|
||||
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
# SonarCloud automatic analysis: what is code, what is test code, and what is
|
||||
# not analysed at all. Only scope settings are read from this file; it acts
|
||||
# once it is on the default branch, and it overrides the same settings made in
|
||||
# the SonarCloud UI.
|
||||
# https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/automatic-analysis/
|
||||
# SonarCloud's CI-based analysis (.github/workflows/sonarcloud.yml): which
|
||||
# project, what is code, what is test code, and what is not analysed at all.
|
||||
# The scanner reads this file from the commit it analyses, so a pull request
|
||||
# that changes it is analysed with the change. Settings here override the same
|
||||
# settings made in the SonarCloud UI.
|
||||
# https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/analysis-parameters/
|
||||
#
|
||||
# sonar.sources and sonar.tests take directories. The other keys take path
|
||||
# patterns relative to the repository root, comma-separated: ** is any number
|
||||
# of directories, * any part of a name.
|
||||
|
||||
sonar.organization=poli0981
|
||||
sonar.projectKey=poli0981_free-steam-games-list
|
||||
|
||||
# Tests sit beside the code they test, so both start at the root and the
|
||||
# patterns below split them: a file is a test if it matches
|
||||
# sonar.test.inclusions, and code if it is not excluded.
|
||||
@@ -33,6 +37,11 @@ sonar.test.inclusions=scripts/tests/**,**/*.test.ts,web/src/lib/testing/**,web/w
|
||||
sonar.exclusions=scripts/tests/**,**/*.test.ts,web/src/lib/testing/**,web/worker/testing/**,web/admin/mock/**,data/**,games/**,**/*.jsonl,**/*.sql,web/src/app.html,web/src-tauri/gen/android/app/src/main/res/**,web/src-tauri/gen/android/buildSrc/**,web/src-tauri/icons/**
|
||||
|
||||
sonar.sourceEncoding=UTF-8
|
||||
# Every TypeScript project in web/. Left to itself the analyser finds only
|
||||
# files named exactly tsconfig.json, and analyses the Worker, shared/ and the
|
||||
# tests without their own compiler options. web/tsconfig.json extends the one
|
||||
# `svelte-kit sync` generates, which the workflow runs first.
|
||||
sonar.typescript.tsconfigPaths=web/tsconfig.json,web/tsconfig.worker.json,web/tsconfig.test.json,web/admin/tsconfig.json,web/admin/tsconfig.node.json,web/admin/tsconfig.test.json
|
||||
# The version every workflow sets up (actions/setup-python), except CodeQL's,
|
||||
# which stays on the newest Python CodeQL supports. web/shared/toolchain.test.ts
|
||||
# holds them equal.
|
||||
@@ -18,7 +18,7 @@
|
||||
* call a bare one. docs/plan/17-typescript-7.1.md says when TypeScript 6 goes
|
||||
* and how this block changes then.
|
||||
*
|
||||
* web-ci.yml runs on changes to .github/workflows/ and .sonarcloud.properties
|
||||
* web-ci.yml runs on changes to .github/workflows/ and sonar-project.properties
|
||||
* as well as web/, so a pull request that edits only a pin still meets this.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
@@ -98,9 +98,9 @@ describe("Node", () => {
|
||||
});
|
||||
|
||||
describe("Python", () => {
|
||||
const python = /^sonar\.python\.version=(\S+)$/m.exec(read(join(REPO, ".sonarcloud.properties")))?.[1] ?? "";
|
||||
const python = /^sonar\.python\.version=(\S+)$/m.exec(read(join(REPO, "sonar-project.properties")))?.[1] ?? "";
|
||||
|
||||
it("is one X.Y in .sonarcloud.properties", () => {
|
||||
it("is one X.Y in sonar-project.properties", () => {
|
||||
expect(python).toMatch(/^3\.\d+$/);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user