Skip to content

Commit b061ba3

Browse files
Chore/init sonarqube (#14)
* chore(init_sonarqube): add workflow to activate sonarqube on push * chore(init_sonarqube): exclude main class from tests * chore(init_sonarqube): add jacoco to add coverage requirement on a pull request * chore(init_sonarqube): update the jacoco version to be compatible with JAVA25 --------- Co-authored-by: Romain Vanhee <romain.vanhee@yrycom.com>
1 parent bb65eac commit b061ba3

3 files changed

Lines changed: 102 additions & 1 deletion

File tree

.github/workflows/sonar.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
on:
2+
push:
3+
branches:
4+
- '**'
5+
pull_request:
6+
types: [opened, synchronize, reopened]
7+
8+
name: Sonar
9+
jobs:
10+
sonarqube:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
16+
- name: Set up JDK 25
17+
uses: actions/setup-java@v4
18+
with:
19+
distribution: "temurin"
20+
java-version: 25
21+
overwrite-settings: false
22+
- name: Cache SonarQube packages
23+
uses: actions/cache@v4
24+
with:
25+
path: ~/.sonar/cache
26+
key: ${{ runner.os }}-sonar
27+
restore-keys: ${{ runner.os }}-sonar
28+
- name: Cache Maven packages
29+
uses: actions/cache@v4
30+
with:
31+
path: ~/.m2/repository
32+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
33+
restore-keys: ${{ runner.os }}-maven
34+
- name: Build and analyze
35+
env:
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
37+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
38+
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
39+
run: |
40+
mkdir -p src/test/resources
41+
echo ${{ secrets.APPLICATION_TEST_PROPERTIES }} | base64 -d > src/test/resources/application-test.properties
42+
mvn clean verify sonar:sonar -Dsonar.qualitygate.wait=true

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,44 @@ Tests use **Testcontainers** to spin up ephemeral Docker containers for external
127127

128128
> **Prerequisite for tests**: Docker must be installed and running.
129129
130+
---
131+
132+
## 🔎 Code Quality (SonarQube)
133+
134+
A GitHub Actions workflow is configured in:
135+
136+
```bash
137+
.github/workflows/sonar.yaml
138+
```
139+
140+
### Workflow triggers
141+
142+
- `push` on all branches
143+
- `pull_request`
144+
145+
### What it runs
146+
147+
- Java 25 setup (Temurin)
148+
- Maven build + tests + SonarQube analysis:
149+
150+
```bash
151+
mvn clean verify sonar:sonar
152+
```
153+
154+
### Generate the JaCoCo coverage report locally
155+
156+
Run:
157+
158+
```bash
159+
./mvnw clean verify
160+
```
161+
162+
Generated reports:
163+
164+
- HTML report: `target/site/jacoco/index.html`
165+
- XML report (used by SonarQube): `target/site/jacoco/jacoco.xml`
166+
167+
130168
---
131169

132170
## 📁 Project structure
@@ -156,7 +194,7 @@ This project consumes the public **Random User Generator** API:
156194

157195
## ✅ Todo
158196

159-
- [ ] [Add Sonarqube in the project](https://github.com/XPEHO/spring_boot_java_random_user/issues/2)
197+
- [x] [Add Sonarqube in the project](https://github.com/XPEHO/spring_boot_java_random_user/issues/2)
160198
- [ ] [Add PostgreSQL database with docker](https://github.com/XPEHO/spring_boot_java_random_user/issues/6)
161199
- [ ] [Add this endpoint get /user/random](https://github.com/XPEHO/spring_boot_java_random_user/issues/5)
162200
- [ ] [Add this endpoint get /user/{id}](https://github.com/XPEHO/spring_boot_java_random_user/issues/8)

pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
</scm>
2929
<properties>
3030
<java.version>25</java.version>
31+
<sonar.coverage.exclusions>**/*Application.java</sonar.coverage.exclusions>
32+
<sonar.coverage.jacoco.xmlReportPaths>${project.build.directory}/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
3133
</properties>
3234
<dependencies>
3335
<dependency>
@@ -92,6 +94,25 @@
9294
<groupId>org.springframework.boot</groupId>
9395
<artifactId>spring-boot-maven-plugin</artifactId>
9496
</plugin>
97+
<plugin>
98+
<groupId>org.jacoco</groupId>
99+
<artifactId>jacoco-maven-plugin</artifactId>
100+
<version>0.8.14</version>
101+
<executions>
102+
<execution>
103+
<goals>
104+
<goal>prepare-agent</goal>
105+
</goals>
106+
</execution>
107+
<execution>
108+
<id>report</id>
109+
<phase>verify</phase>
110+
<goals>
111+
<goal>report</goal>
112+
</goals>
113+
</execution>
114+
</executions>
115+
</plugin>
95116
</plugins>
96117
</build>
97118

0 commit comments

Comments
 (0)