Skip to content

ci: run the sonar scanner so coverage and analysis actually work - #145

Merged
svaningelgem merged 6 commits into
masterfrom
ci/sonar-cpd-exclusion
Aug 6, 2026
Merged

ci: run the sonar scanner so coverage and analysis actually work#145
svaningelgem merged 6 commits into
masterfrom
ci/sonar-cpd-exclusion

Conversation

@svaningelgem

@svaningelgem svaningelgem commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

SonarCloud was running Automatic Analysis, which scans source directly and never invokes Gradle. Every setting in build.gradle's sonar block was therefore inert. This runs the scanner instead, which turns that existing config on and fixes three separate symptoms at once.

+22/-2 lines, two files, no production code touched.

What was broken

gradle-build.yml cached ~/.sonar/cache, then ran only clean jar and four test invocations — never ./gradlew sonar. Consequences:

Symptom Cause
v1/ and v2/YamlWrapperImpl reported 66.7% duplicated sonar.cpd.exclusions was declared but never read
No coverage displayed sonar.coverage.jacoco.xmlReportPaths never read — and Automatic Analysis cannot report coverage at all, since it doesn't build or test

The block was already correct. It just needed to run.

Changes

gradle-build.yml — scanner step after all four test runs, so every report-*.xml exists when it reads them.

build.gradle

  • Removed sonar.scm.disabled = true. fetch-depth: 0 is set specifically so Sonar can read git blame, and this was discarding it. Every gate condition is a new_* metric, and "new code" is derived from SCM data — including the coverage being enabled here. It dated from Implement automated switching between snakeyaml 1.14 & 2.2 #30, where it was boilerplate in a bulk commit and never mattered because nothing ran.
  • Added sonar.java.libraries / sonar.java.test.libraries, fixing "Missing 'sonar.java.libraries' property. You might end up with less precise analysis results."main.java.srcDirs = [], so the plugin infers an empty classpath and the Java analyser cannot resolve SnakeYAML or annotation types. Only the SnakeYAML matching the current build is added: putting 1.x and 2.x on one classpath would make the two ToStringRepresenter variants resolve against the wrong API. This mirrors how jacoco already selects one version's source set via isV1.
  • A comment recording why the YamlWrapperImpl duplication is deliberate.

Verified on the PR analysis

before after
coverage (none) 100.0% (491 lines to cover, 0 uncovered)
duplicated_lines 18 + 18 0
quality gate OK OK

All CI steps green, including the new scanner step.

Why the duplication itself stays

The two files are byte-for-byte identical apart from package. The entire v1/v2 API divergence is two lines, both in ToStringRepresentersuper() vs super(new DumperOptions()), and ScalarStyle.PLAIN.getChar() vs ScalarStyle.PLAIN.

YamlWrapperImpl could move to common taking a Representer, since every API it touches is signature-identical across 1.14 and 2.5. But common deliberately has no SnakeYAML dependency — that is what makes dual-version support trustworthy. Sharing it would pin common to one major version and run it against the other, trading a compile-time guarantee for a runtime assumption to save 18 lines.

Prerequisites (already done)

Automatic Analysis disabled in the SonarCloud UI, and a SONAR_TOKEN repo secret added. Both were required — the two analysis methods are mutually exclusive.

The sonar { } block in build.gradle is never executed -- CI runs jar and test, never
the Gradle sonar task -- so its cpd.exclusions setting has had no effect and the two
per-version YamlWrapperImpl copies were reported as 66.7% duplicated. Automatic
Analysis reads sonar-project.properties, so the exclusion goes there instead.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Nothing in CI invokes the Gradle sonar task, so the sonar { } block and the
org.sonarqube plugin never ran. Analysis comes from SonarCloud Automatic Analysis,
which is configured by sonar-project.properties instead. Removing the plugin also
stops renovate bumping a dependency nothing uses.
@svaningelgem svaningelgem changed the title ci: make the CPD exclusion actually reach SonarCloud ci: configure SonarCloud where it actually reads, drop the dead Gradle config Aug 6, 2026
Keeps sonar-project.properties as the single source of truth for settings both
analysers understand, and restores the sonar { } block to read it -- the Gradle
scanner does not read that file on its own. Scanner-only settings (project identity,
build outputs, jacoco report paths) stay in build.gradle, since Automatic Analysis
never builds the project and cannot act on them.
Nothing invoked the Gradle sonar task, so build.gradle's sonar block never ran --
which is why the CPD exclusion was ignored and coverage never appeared. Automatic
Analysis cannot report coverage at all, since it never builds or tests the project.

Adds the scanner step after the four test runs so all four jacoco reports exist, and
records why the YamlWrapperImpl duplication is deliberate. Requires disabling
Automatic Analysis and adding a SONAR_TOKEN secret.
@svaningelgem svaningelgem changed the title ci: configure SonarCloud where it actually reads, drop the dead Gradle config ci: run the sonar scanner so coverage and exclusions take effect Aug 6, 2026
fetch-depth: 0 is already set so blame data is available, but sonar.scm.disabled
threw it away. Every quality gate condition is a new_* metric, and 'new code' is
derived from SCM data -- including the coverage now being enabled.
main has no sources of its own, so the plugin infers an empty compile classpath and
warns about a missing sonar.java.libraries -- leaving the Java analyser unable to
resolve SnakeYAML or annotation types. Adds common plus the SnakeYAML matching the
current build, the same way jacoco already selects one version's source set.
@sonarqubecloud

sonarqubecloud Bot commented Aug 6, 2026

Copy link
Copy Markdown

@svaningelgem svaningelgem changed the title ci: run the sonar scanner so coverage and exclusions take effect ci: run the sonar scanner so coverage and analysis actually work Aug 6, 2026
@svaningelgem
svaningelgem merged commit 129dbce into master Aug 6, 2026
3 checks passed
@svaningelgem
svaningelgem deleted the ci/sonar-cpd-exclusion branch August 6, 2026 04:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants