Skip to content

Commit

Permalink
Sonar setup (#51)
Browse files Browse the repository at this point in the history
* provide compose.yaml

that (pulls) and runs sonarQube in persistent mode with pg

* setup of sonar server and gradle build

for scanning and test coverage woth jacoco.

* remove java plugin from root Build_gradle

* fix bootstrap issue on other OS with elastic
  • Loading branch information
wisskirchenj authored Jan 21, 2024
1 parent 83ca801 commit 337bc2f
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 9 deletions.
File renamed without changes
File renamed without changes.
24 changes: 24 additions & 0 deletions .dev/sonar-first-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

echo -e "\n*** setting up SonarQube locally for flashcards project ***\n"
docker-compose -f sonar/docker-compose.yaml up -d

# Wait for SonarQube to be up and running
echo -e "\n*** waiting for SonarQube to start ***\n"
until curl --silent http://localhost:9000/api/system/status | grep "UP" ; do
printf '.'
sleep 5 # wait 5 seconds before check again
done

echo -e "\n*** SonarQube is up. Creating flashcards-server project via WebAPI ***\n"
echo "Enter SonarQube Admin password: " && read password
curl -X POST "http://localhost:9000/api/projects/create?name=Flashcards%20Server&project=flashcards-server" -u "admin:$password"
echo
response=$(curl -s -X POST -u "admin:$password" "http://localhost:9000/api/user_tokens/generate?name=flashcardsToken&projectKey=flashcards-server&type=PROJECT_ANALYSIS_TOKEN")
token=$(echo $response | grep -o '"token":"[^"]*' | cut -d'"' -f4)
echo -e "\nGenerated project token:\nsonarToken=$token"
echo "**** copy above line (sonarToken=...) to the gradle.properties file in the root of the project"
echo "it will be used by the sonarqube gradle plugin to upload the results of the analysis to the server"
echo -e "It is on .gitignore so it will not be committed to the repository.\n"

echo -e "Now go to http://localhost:9000/projects/flashcards-server and have fun!\n"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ out/
.idea/modules/
.idea/*.xml
compose.yaml
gradle.properties

### Mac OS ###
.DS_Store
Expand Down
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The authserver docker image is now provided for AMD64 and ARM64 architecture. Th
by copying the compose.yaml file for the respective architecture from the docker folder into root directory.

#### Behaviour:
To access the app see the video in .dev folder: [Accessing the app](.dev/run-flashcards.mp4)
To access the app see the video in .dev folder: [Accessing the app](.dev/assets/run-flashcards.mp4)

You need to **access the flashcards-website now via http://127.0.0.1:8080 context path**, as oauth2 demands a
different host URLs for the login request (which is localhost of course).
Expand All @@ -40,7 +40,7 @@ It's located in .dev folder. But it seems Ultimate Edition only as of now.
To use the client, you have to
- select an environment - I provided dev (and empty prod for future)
- run with double-arrow on top to chain token and get/post call. (see picture)
![img.png](.dev/img.png)
![http-client.png](.dev/assets/http-client.png)
NOTE: The token generated by the http-client currently has no subject.

#### Alternatively using postman:
Expand All @@ -65,6 +65,30 @@ Take care, running this script deletes all persistent data of the mongo contain
.dev/remove-mongo-volume.sh
```

### Setup and usage of SonarQube

#### One-time setup
To start working with sonarQube, run the following script - just with the double-arrow at the gutter.
It pulls necessary containers (if not present - so expect some time), starts up a SonarQube server in a network
together with a Postgres database to persist sonar projects.
Then it creates a sonar project (presently only flashcards-server) and a token for the project.
**The script instructs you to copy the token to the gradle.properties file in the root directory of the project.**
At the end you are presented with the URL of the new sonar project.

**NOTE: The script will ask for an admin password for the sonar server. First time after container pull,
this is the default password `admin`. You should change it afterwards.**

```shell
.dev/sonar-first-setup.sh
```

#### Usage
Afterwards you can run the sonar scan and analyze the server project with
> ./gradlew :sonar
respectively by clicking the task under verification in the gradle tool window.


## Technology / External Libraries

- Java 21
Expand Down
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ tasks.register<Copy>("select-compose-file") {
rename(composeFile, "compose.yaml")
}

tasks.register("sonar") {
group = "verification"
description = "Run sonarqube analysis - presently only for flashcards-server"
dependsOn(":flashcards-server:sonar")
}

tasks.register<BootRun>("bootRunFlashcards") {
group = "build"
description = "Run flashcards app and start containers for mongodb and auth server"
Expand Down
7 changes: 0 additions & 7 deletions frontend/src/feature/category/components/CategoryIterator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
>
<v-card>
<v-card-title class="d-flex align-center">
<v-icon
:color="item.raw.color"
:icon="item.raw.icon"
start
size="18"
></v-icon>

<h4>{{ item.raw.name }}</h4>
</v-card-title>

Expand Down
Empty file removed gradle.properties
Empty file.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
spring-boot = "3.2.1"
spring-dependency-management = "1.1.4"
node-gradle = "7.0.1"
sonar-gradle = "4.4.1.3373"
node-js = "21.5.0"
graalvm-buildtools = "0.9.28"
26 changes: 26 additions & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ plugins {
application
id("org.springframework.boot") version libs.versions.spring.boot
id("io.spring.dependency-management") version libs.versions.spring.dependency.management
id("org.sonarqube") version libs.versions.sonar.gradle
jacoco
}

group = "org.hyperskill.community"
Expand All @@ -13,6 +15,30 @@ java {
sourceCompatibility = JavaVersion.VERSION_21
}

tasks.jacocoTestReport {
dependsOn(tasks.test)
reports {
xml.required.set(true)
}
}

val sonarToken: String by project
sonar {
properties {
property("sonar.token", sonarToken)
property("sonar.projectKey", "flashcards-server")
property("sonar.projectName", "Flashcards Server")
property("sonar.jacoco.reportPaths", "build/reports/jacoco")
property("sonar.junit.reportPaths", "build/test-results/test")
property("sonar.host.url", "http://localhost:9000")
}
}

tasks.sonar {
dependsOn(tasks.jacocoTestReport)
}


configurations {
compileOnly {
extendsFrom(configurations.annotationProcessor.get())
Expand Down
37 changes: 37 additions & 0 deletions sonar/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: "3"
services:
sonarqube:
image: sonarqube:community
hostname: sonarqube
container_name: sonarqube
depends_on:
- db
environment:
SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
SONAR_JDBC_USERNAME: sonar
SONAR_JDBC_PASSWORD: sonar
SONAR_ES_BOOTSTRAP_CHECKS_DISABLE: "true"
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
ports:
- "9000:9000"
db:
image: postgres:alpine
hostname: postgresql
container_name: postgres-sonar
environment:
POSTGRES_USER: sonar
POSTGRES_PASSWORD: sonar
POSTGRES_DB: sonar
volumes:
- postgresql:/var/lib/postgresql
- postgresql_data:/var/lib/postgresql/data

volumes:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
postgresql:
postgresql_data:

0 comments on commit 337bc2f

Please sign in to comment.