From 40f2b5183eac30bd7af6e88eb684a8698686f3dd Mon Sep 17 00:00:00 2001 From: Romain Vanhee Date: Thu, 26 Feb 2026 16:01:17 +0100 Subject: [PATCH 1/4] add workflow to activate sonarqube on push --- .github/workflows/sonar.yaml | 42 ++++++++++++++++++++++++++++++++++++ src/sonar-project.properties | 1 + 2 files changed, 43 insertions(+) create mode 100644 .github/workflows/sonar.yaml create mode 100644 src/sonar-project.properties diff --git a/.github/workflows/sonar.yaml b/.github/workflows/sonar.yaml new file mode 100644 index 0000000..a68b277 --- /dev/null +++ b/.github/workflows/sonar.yaml @@ -0,0 +1,42 @@ +on: + push: + branches: + - '**' + pull_request: + types: [opened, synchronize, reopened] + +name: Sonar +jobs: + sonarqube: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + - name: Set up JDK 25 + uses: actions/setup-java@v4 + with: + distribution: "temurin" + java-version: 25 + overwrite-settings: false + - name: Cache SonarQube packages + uses: actions/cache@v4 + with: + path: ~/.sonar/cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar + - name: Cache Maven packages + uses: actions/cache@v4 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-maven + - name: Build and analyze + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} + run: | + mkdir -p src/test/resources + echo ${{ secrets.APPLICATION_TEST_PROPERTIES }} | base64 -d > src/test/resources/application-test.properties + mvn clean verify sonar:sonar \ No newline at end of file diff --git a/src/sonar-project.properties b/src/sonar-project.properties new file mode 100644 index 0000000..e6f0e50 --- /dev/null +++ b/src/sonar-project.properties @@ -0,0 +1 @@ +sonar.projectKey=spring_boot_java_random_user \ No newline at end of file From 48b8377f982cd01beff3e06bcf63b85bfa866521 Mon Sep 17 00:00:00 2001 From: Romain Vanhee Date: Thu, 26 Feb 2026 16:43:31 +0100 Subject: [PATCH 2/4] chore(init_sonarqube) exclude main class from tests --- README.md | 27 ++++++++++++++++++++++++++- pom.xml | 1 + src/sonar-project.properties | 1 - 3 files changed, 27 insertions(+), 2 deletions(-) delete mode 100644 src/sonar-project.properties diff --git a/README.md b/README.md index a901edd..ab9d46b 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,31 @@ Tests use **Testcontainers** to spin up ephemeral Docker containers for external > **Prerequisite for tests**: Docker must be installed and running. +--- + +## 🔎 Code Quality (SonarQube) + +A GitHub Actions workflow is configured in: + +```bash +.github/workflows/sonar.yaml +``` + +### Workflow triggers + +- `push` on all branches +- `pull_request` + +### What it runs + +- Java 25 setup (Temurin) +- Maven build + tests + SonarQube analysis: + +```bash +mvn clean verify sonar:sonar +``` + + --- ## 📁 Project structure @@ -156,7 +181,7 @@ This project consumes the public **Random User Generator** API: ## ✅ Todo -- [ ] [Add Sonarqube in the project](https://github.com/XPEHO/spring_boot_java_random_user/issues/2) +- [x] [Add Sonarqube in the project](https://github.com/XPEHO/spring_boot_java_random_user/issues/2) - [ ] [Add PostgreSQL database with docker](https://github.com/XPEHO/spring_boot_java_random_user/issues/6) - [ ] [Add this endpoint get /user/random](https://github.com/XPEHO/spring_boot_java_random_user/issues/5) - [ ] [Add this endpoint get /user/{id}](https://github.com/XPEHO/spring_boot_java_random_user/issues/8) diff --git a/pom.xml b/pom.xml index fb16c48..ea748ba 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ 25 + **/*Application.java diff --git a/src/sonar-project.properties b/src/sonar-project.properties deleted file mode 100644 index e6f0e50..0000000 --- a/src/sonar-project.properties +++ /dev/null @@ -1 +0,0 @@ -sonar.projectKey=spring_boot_java_random_user \ No newline at end of file From f07bc20a30df58213ef6bddf44ec42bf1db3fdb6 Mon Sep 17 00:00:00 2001 From: Romain Vanhee Date: Thu, 26 Feb 2026 16:50:50 +0100 Subject: [PATCH 3/4] chore(init_sonarqube): add jacoco to add coverage requirement on a pull request --- .github/workflows/sonar.yaml | 2 +- pom.xml | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/.github/workflows/sonar.yaml b/.github/workflows/sonar.yaml index a68b277..7b02547 100644 --- a/.github/workflows/sonar.yaml +++ b/.github/workflows/sonar.yaml @@ -39,4 +39,4 @@ jobs: run: | mkdir -p src/test/resources echo ${{ secrets.APPLICATION_TEST_PROPERTIES }} | base64 -d > src/test/resources/application-test.properties - mvn clean verify sonar:sonar \ No newline at end of file + mvn clean verify sonar:sonar -Dsonar.qualitygate.wait=true diff --git a/pom.xml b/pom.xml index ea748ba..d2d5210 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,7 @@ 25 **/*Application.java + ${project.build.directory}/site/jacoco/jacoco.xml @@ -93,6 +94,25 @@ org.springframework.boot spring-boot-maven-plugin + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + + prepare-agent + + + + report + verify + + report + + + + From cdb04c8047146fff3e35b6672b86d79433b0acfa Mon Sep 17 00:00:00 2001 From: Romain Vanhee Date: Thu, 26 Feb 2026 17:25:04 +0100 Subject: [PATCH 4/4] chore(init_sonarqube): update the jacoco version to be compatible with JAVA25 --- README.md | 13 +++++++++++++ pom.xml | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ab9d46b..02b7578 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,19 @@ A GitHub Actions workflow is configured in: mvn clean verify sonar:sonar ``` +### Generate the JaCoCo coverage report locally + +Run: + +```bash +./mvnw clean verify +``` + +Generated reports: + +- HTML report: `target/site/jacoco/index.html` +- XML report (used by SonarQube): `target/site/jacoco/jacoco.xml` + --- diff --git a/pom.xml b/pom.xml index d2d5210..bac59da 100644 --- a/pom.xml +++ b/pom.xml @@ -97,7 +97,7 @@ org.jacoco jacoco-maven-plugin - 0.8.12 + 0.8.14