Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added sonarQube for code quality #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,6 @@ Temporary Items
dist
local/
documentation

# SonarQube
.scannerwork
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ To view sample implementations based on this starter-kit, please visit the [nest
| Logging | nestjs-pino | Done |
| Request Validation | class-validator | Done |
| Docker Ready | Dockerfile | Done |
| Continuous Code Quality | SonarQube | Done |
| Auto-generated OpenAPI | - | Done |
| Auto-generated ChangeLog | - | WIP |

Expand Down Expand Up @@ -97,8 +98,26 @@ $ npm run migration:run

# revert migration
$ npm run migration:revert

```

## SonarQube locally analyse

To locally analyse your code it is enough to run

```bash
# build image
$ docker-compose up

# run container from image
$ docker-compose exec sonarscanner sonar-scanner

```

The results can be viewed in local instance of sonarqube [http://localhost:9000/sonarqube/dashboard?id=nest-typescript-starter](http://localhost:9000/sonarqube/dashboard?id=nest-typescript-starter).

Local instance of sonarqube uses `sonar-scanner.properties` file.

## Docker

```bash
Expand Down
28 changes: 27 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.8"
version: '3.8'

services:
app:
Expand Down Expand Up @@ -44,6 +44,32 @@ services:
depends_on:
- mysqldb

sonarqube:
container_name: sonarqube
Copy link
Contributor

@war1oc war1oc Oct 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A docker-compose generated container name would be more preferable in my opinion than a fixed name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was set by mean to link with sonarscanner. Now, we can expose one more port for TCP connection. And omit this container_name.

image: sonarqube:8-community
command: '-Dsonar.web.context=/sonarqube'
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
- sonarqube_temp:/opt/sonarqube/temp
ports:
- 9000:9000
sonarscanner:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we pass the sonar.properties to sonarqube container itself in volume /opt/sonarqube/conf rather than another container?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, sonar.properties most use case when we scan our code. Which handle by sonarscanner. So, I thought that users can change properties and make an easy scan.

container_name: sonar-scanner
build:
context: .
dockerfile: ./docker/sonar-scanner/Dockerfile
volumes:
- .:/usr/src/
- ./sonar-scanner.properties:/usr/lib/sonar-scanner/conf/sonar-scanner.properties
network_mode: host
command: tail -f /dev/null

volumes:
node_modules:
mysqldata:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
sonarqube_temp:
28 changes: 28 additions & 0 deletions docker/sonar-scanner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM openjdk:8-alpine

ARG RELEASE=4.0.0.1744

RUN apk add --no-cache curl grep sed unzip bash nodejs nodejs-npm

RUN npm install -g typescript
ENV NODE_PATH "/usr/lib/node_modules/"

# Set timezone to CST
ENV TZ=Europe/Warsaw
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

WORKDIR /usr/src

RUN curl --insecure -o ./sonarscanner.zip -L https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$RELEASE-linux.zip && \
unzip sonarscanner.zip && \
rm sonarscanner.zip && \
mv sonar-scanner-$RELEASE-linux /usr/lib/sonar-scanner && \
ln -s /usr/lib/sonar-scanner/bin/sonar-scanner /usr/local/bin/sonar-scanner

ENV SONAR_RUNNER_HOME=/usr/lib/sonar-scanner

# ensure Sonar uses the provided Java for musl instead of a borked glibc one
RUN sed -i 's/use_embedded_jre=true/use_embedded_jre=false/g' /usr/lib/sonar-scanner/bin/sonar-scanner

ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
28 changes: 28 additions & 0 deletions sonar-scanner.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://localhost:9000/sonarqube

#----- Default source code encoding
# sonar.sourceEncoding=UTF-8

#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

# H2 database from Docker Sonar container
sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
sonar.projectKey=nest-typescript-starter
sonar.projectName=nest-typescript-starter
sonar.projectVersion=1
sonar.projectBaseDir=/usr/src
sonar.sources=src
sonar.exclusions=**/*.spec.ts
sonar.tests.inclusions=**/*.spec.ts
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.typescript.lcov.reportPaths=coverage/lcov.info

# Exclude node_modules for JS/TS-based scanning - this is provided in default configuration from
# the server, but in case you want to provide it locally and/or override it here:
sonar.exclusions=**/node_modules/**