Skip to content

Commit feee78f

Browse files
feat: Add SonarQube for code coverage
1 parent c9ad255 commit feee78f

File tree

6 files changed

+82
-7
lines changed

6 files changed

+82
-7
lines changed

.pre-commit-config.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ repos:
4646
exclude: server/internal/pbs
4747
- id: go-imports
4848
exclude: server/internal/pbs
49+
- id: go-vet
50+
language: system
51+
entry: bash -c "cd server && go vet ./..."
4952
- id: golangci-lint
5053
language: system
5154
entry: bash -c "cd server && golangci-lint run"

Makefile

+20-3
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,23 @@ define grafana_port
2424
$(shell kubectl --context ${PROFILE} get --namespace grafana -o jsonpath="{.spec.ports[0].nodePort}" services grafana)
2525
endef
2626

27+
define sonar_ip
28+
$(shell kubectl --context ${PROFILE} get nodes --namespace sonarqube -o jsonpath="{.items[0].status.addresses[0].address}")
29+
endef
30+
31+
define sonar_port
32+
$(shell kubectl --context ${PROFILE} get --namespace sonarqube -o jsonpath="{.spec.ports[0].nodePort}" services sonarqube-sonarqube)
33+
endef
34+
2735
image-client:
2836
@docker build --build-arg VERSION=${VERSION} -t ${IMAGE_NAME}:${VERSION} --file client.Dockerfile .
2937

3038
image-server:
3139
@docker build --build-arg VERSION=${VERSION} -t ${SERVER_IMAGE_NAME}:${VERSION} --file server.Dockerfile .
3240

3341
image-test-server:
34-
@docker build --build-arg VERSION=${VERSION} -t ${SERVER_IMAGE_NAME}-test:${VERSION} --file server.Dockerfile --target go-test .
35-
@docker run --rm ${SERVER_IMAGE_NAME}-test:${VERSION}
42+
@docker build --build-arg VERSION=${VERSION} --build-arg SONAR_ADDRESS=http://$(call sonar_ip):$(call sonar_port) -t ${SERVER_IMAGE_NAME}-test:${VERSION} --file server.Dockerfile --target go-test .
43+
@docker run --rm --net=host ${SERVER_IMAGE_NAME}-test:${VERSION}
3644

3745
image-client-debug:
3846
@docker build --build-arg VERSION=${VERSION} -t ${IMAGE_NAME}-debug:${VERSION} --file client.Dockerfile --target mvn-build .
@@ -70,7 +78,8 @@ observability-minikube:
7078
@helm --kube-context ${PROFILE} -n fluent install --create-namespace fluent-bit fluent/fluent-bit --version=0.29.0 -f observability/fluentbit_values.yaml --wait || true
7179
@helm --kube-context ${PROFILE} -n tempo install --create-namespace tempo grafana/tempo --version=1.0.0 -f observability/tempo_values.yaml --wait || true
7280
@helm --kube-context ${PROFILE} -n monitoring install --create-namespace kube-prometheus-stack prometheus/kube-prometheus-stack --version=45.23.0 -f observability/prometheus_values.yaml --wait || true
73-
@helm --kube-context ${PROFILE} -n grafana install --create-namespace grafana grafana/grafana --version=6.50.7 -f observability/grafana_values.yaml --wait
81+
@helm --kube-context ${PROFILE} -n grafana install --create-namespace grafana grafana/grafana --version=6.50.7 -f observability/grafana_values.yaml --wait || true
82+
@helm --kube-context ${PROFILE} -n sonarqube install --create-namespace sonarqube sonarqube/sonarqube --version=10.0.0 -f observability/sonarqube_values.yaml --wait || true
7483

7584
clean-observability-minikube:
7685
@helm --kube-context ${PROFILE} -n elasticsearch delete elasticsearch --wait
@@ -85,10 +94,17 @@ clean-fluent:
8594
clean-grafana:
8695
@helm --kube-context ${PROFILE} -n grafana delete grafana --wait
8796

97+
clean-sonar:
98+
@helm --kube-context ${PROFILE} -n sonarqube delete sonarqube --wait
99+
88100
grafana:
89101
@echo http://$(call grafana_ip):$(call grafana_port)
90102
@python -mwebbrowser http://$(call grafana_ip):$(call grafana_port)
91103

104+
sonar:
105+
@echo http://$(call sonar_ip):$(call sonar_port)
106+
@python -mwebbrowser http://$(call sonar_ip):$(call sonar_port)
107+
92108
load:
93109
@minikube -p ${PROFILE} image load ${SERVER_IMAGE_NAME}:${VERSION}
94110

@@ -122,6 +138,7 @@ dependencies-helm:
122138
@helm repo add fluent https://fluent.github.io/helm-charts
123139
@helm repo add grafana https://grafana.github.io/helm-charts
124140
@helm repo add prometheus https://prometheus-community.github.io/helm-charts
141+
@helm repo add sonarqube https://SonarSource.github.io/helm-chart-sonarqube
125142
@cd charts/librarygames; \
126143
helm dependencies update; \
127144
helm dependencies build; \

observability/sonarqube_values.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
# Default values for sonarqube deployed on minikube.
3+
# This is a YAML-formatted file.
4+
# Declare variables to be passed into your templates.
5+
6+
account:
7+
currentAdminPassword: admin
8+
adminPassword: hi
9+
persistence:
10+
enabled: true
11+
service:
12+
type: NodePort

server.Dockerfile

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Dependency image
22
FROM --platform=linux/amd64 golang:1.20.3 AS go-build
33

4-
ARG VERSION
5-
64
WORKDIR /tmp/librarygames/server
75

86
COPY server/go.mod server/go.sum /tmp/librarygames/server
@@ -11,13 +9,41 @@ RUN go mod download
119

1210
COPY server/ /tmp/librarygames/server
1311

12+
ARG VERSION
1413
ENV VERSION=${VERSION}
1514
RUN go build -mod=readonly -ldflags "-X main.version=$VERSION" -race -o /tmp/librarygames-server ./main
1615

1716
# Test image
1817
FROM go-build AS go-test
1918

20-
CMD ["go", "test", "-v", "./..."]
19+
RUN apt-get update && \
20+
apt-get install -y \
21+
unzip \
22+
&& apt-get clean && rm -rf /var/lib/apt/lists/*
23+
24+
ENV SONAR_SCANNER_VERSION=4.8.0.2856
25+
RUN mkdir -p /tmp/sonar \
26+
&& curl -sSLo /tmp/sonar/scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip \
27+
&& unzip -o /tmp/sonar/scanner.zip -d /usr/local/ \
28+
&& rm -rf /tmp/sonar
29+
RUN for exe in /usr/local/sonar-scanner-${SONAR_SCANNER_VERSION}-linux/bin/*; do ln -s ${exe} /usr/local/bin/$(basename "${exe}"); done
30+
31+
COPY sonar-project.properties /tmp/librarygames/sonar-project.properties
32+
33+
RUN go test -short -race -coverprofile=/tmp/cov.out `go list ./... | grep -v vendor/`
34+
RUN go tool cover -func=/tmp/cov.out
35+
RUN sed -i 's/github.com\/patrickkenney9801\/librarygames/\/tmp\/librarygames\/server/g' /tmp/cov.out
36+
37+
ARG VERSION
38+
ARG SONAR_ADDRESS
39+
ENV SONAR_ADDRESS=${SONAR_ADDRESS}
40+
ENV VERSION=${VERSION}
41+
42+
WORKDIR /tmp/librarygames
43+
44+
RUN echo "sonar-scanner -Dsonar.host.url=$SONAR_ADDRESS -Dsonar.projectVersion=$VERSION -Dsonar.go.coverage.reportPaths=/tmp/cov.out" > sonar.sh
45+
46+
CMD ["bash", "sonar.sh"]
2147

2248
# Main image
2349
FROM --platform=linux/amd64 gcr.io/distroless/base-debian11 AS go-release

sonar-project.properties

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sonar.projectName=librarygames
2+
sonar.projectKey=service_sonar
3+
4+
sonar.login=admin
5+
sonar.password=hi
6+
7+
sonar.sources=server
8+
sonar.exclusions=**/*_test.go,**/vendor/**,**/testdata/*,**/pbs/**
9+
10+
sonar.tests=server
11+
sonar.test.inclusions=**/*_test.go
12+
sonar.test.exclusions=**/vendor/**

src/main/java/com/nfehs/librarygames/net/GameClient.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public void run() {
7979
if (t == null) {
8080
return;
8181
}
82-
System.out.println("Res: " + t);
8382

8483
// verify that user is still on the login screen, if not exit
8584
if (Game.gameState != Game.LOGIN) return;
@@ -105,6 +104,9 @@ public String loginSync(final String username, final String password) {
105104
resp = loginStub.login(req);
106105
} catch (StatusRuntimeException e) {
107106
System.out.println("login RPC failed: " + e.getStatus());
107+
if (Game.gameState == Game.LOGIN) {
108+
Game.setErrorLoginScreen(e.getStatus().getDescription());
109+
}
108110
return null;
109111
}
110112
return resp.getUserToken();
@@ -125,6 +127,9 @@ public void run() {
125127
resp = createAccountStub.createAccount(req);
126128
} catch (StatusRuntimeException e) {
127129
System.out.println("create account RPC failed: " + e.getStatus());
130+
if (Game.gameState == Game.CREATE_ACCOUNT) {
131+
Game.setErrorCreateAccountScreen(e.getStatus().getDescription());
132+
}
128133
return;
129134
}
130135

0 commit comments

Comments
 (0)