Skip to content

Commit 05d5eb0

Browse files
horvathdoraDora Horvath
and
Dora Horvath
authored
PHOENIX-6836: Enable code coverage reporting to SonarQube in Phoenix-Queryserver (apache#112)
Co-authored-by: Dora Horvath <[email protected]>
1 parent b3af8ea commit 05d5eb0

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

dev/code-coverage/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
# Code analysis
20+
21+
The `run-coverage.sh` script runs maven with the **codecoverage** profile
22+
which generates the test coverage data for the java classes.
23+
If the required parameters are given it also runs the sonar code analysis
24+
and uploads the results to the given SonarQube Server.
25+
26+
## Running code analysis
27+
28+
After running the script the reports generated by the JaCoCo code coverage
29+
library can be found in the modules under the `/target/site/jacoco/` folder.
30+
31+
Here is how you can generate the code coverage report:
32+
33+
```./dev/code-coverage/run-coverage.sh```
34+
35+
## Publishing coverage results to SonarQube
36+
37+
The required parameters for publishing the results to SonarQube are:
38+
39+
- host URL,
40+
- login credentials,
41+
- project key
42+
43+
The project name is an optional parameter.
44+
45+
Here is an example command for running and publishing the coverage data:
46+
47+
`./dev/code-coverage/run-coverage.sh -l ProjectCredentials
48+
-u https://exampleserver.com -k Project_Key -n Project_Name`

dev/code-coverage/run-coverage.sh

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bash
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
usage() {
15+
echo
16+
echo "options:"
17+
echo " -h Display help"
18+
echo " -u SonarQube Host URL"
19+
echo " -l SonarQube Login Credentials"
20+
echo " -k SonarQube Project Key"
21+
echo " -n SonarQube Project Name"
22+
echo " -t Number of threads (example: 1 or 2C)."
23+
echo
24+
echo "Important:"
25+
echo " The required parameters for publishing the coverage results to SonarQube:"
26+
echo " - Host URL"
27+
echo " - Login Credentials"
28+
echo " - Project Key"
29+
echo
30+
}
31+
32+
execute() {
33+
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
34+
MAIN_POM="${SCRIPT_DIR}/../../pom.xml"
35+
# Check the syntax for the THREAD_COUNT variable
36+
if [[ "$THREAD_COUNT" =~ ^[0-9]+([.][0-9]+)?$ ]] || [[ "$THREAD_COUNT" =~ ^[0-9]+([.][0-9]+)+[C]?$ ]]; then
37+
THREADS="${THREAD_COUNT}"
38+
else
39+
THREADS=1
40+
fi
41+
42+
mvn -B -e -f "$MAIN_POM" clean verify -Pcodecoverage -fn -Dmaven.javadoc.skip=true -DskipShade -T "$THREADS"
43+
44+
# If the required parameters are given, the code coverage results are uploaded to the SonarQube Server
45+
if [ -n "$SONAR_LOGIN" ] && [ -n "$SONAR_PROJECT_KEY" ] && [ -n "$SONAR_URL" ]; then
46+
mvn -B -e -Pcodecoverage -f "$MAIN_POM" sonar:sonar -Dsonar.projectName="$SONAR_PROJECT_NAME" \
47+
-Dsonar.host.url="$SONAR_URL" -Dsonar.login="$SONAR_LOGIN" -Dsonar.projectKey="$SONAR_PROJECT_KEY" -T "$THREADS"
48+
fi
49+
}
50+
51+
while getopts ":u:l:k:n:t:h" option; do
52+
case $option in
53+
u) SONAR_URL=${OPTARG:-} ;;
54+
l) SONAR_LOGIN=${OPTARG:-} ;;
55+
k) SONAR_PROJECT_KEY=${OPTARG:-} ;;
56+
n) SONAR_PROJECT_NAME=${OPTARG:-} ;;
57+
t) THREAD_COUNT=${OPTARG:-} ;;
58+
h) # Display usage
59+
usage
60+
exit
61+
;;
62+
\?) # Invalid option
63+
echo "Error: Invalid option"
64+
exit
65+
;;
66+
esac
67+
done
68+
69+
# Start code analysis
70+
execute

pom.xml

+14-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@
101101
<spotbugs-maven-plugin.version>4.1.3</spotbugs-maven-plugin.version>
102102
<spotbugs.version>4.1.3</spotbugs.version>
103103
<maven-owasp-plugin.version>6.5.3</maven-owasp-plugin.version>
104-
<jacoco-maven-plugin.version>0.8.5</jacoco-maven-plugin.version>
104+
<jacoco-maven-plugin.version>0.8.8</jacoco-maven-plugin.version>
105+
<maven-sonar-plugin.version>3.9.1.2184</maven-sonar-plugin.version>
105106

106107
<!-- Plugin options -->
107108
<it.failIfNoSpecifiedTests>false</it.failIfNoSpecifiedTests>
@@ -701,6 +702,13 @@
701702
<name>!skip.code-coverage</name>
702703
</property>
703704
</activation>
705+
<properties>
706+
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
707+
<sonar.surefire.reportsPath>${project.build.directory}/surefire-reports</sonar.surefire.reportsPath>
708+
<sonar.coverage.jacoco.xmlReportPaths>
709+
${project.build.directory}/site/jacoco/jacoco.xml
710+
</sonar.coverage.jacoco.xmlReportPaths>
711+
</properties>
704712
<build>
705713
<plugins>
706714
<plugin>
@@ -752,6 +760,11 @@
752760
</execution>
753761
</executions>
754762
</plugin>
763+
<plugin>
764+
<groupId>org.sonarsource.scanner.maven</groupId>
765+
<artifactId>sonar-maven-plugin</artifactId>
766+
<version>${maven-sonar-plugin.version}</version>
767+
</plugin>
755768
</plugins>
756769
</build>
757770
</profile>

0 commit comments

Comments
 (0)