You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by za...@apache.org on 2023/01/04 16:48:24 UTC

[calcite] 01/01: [CALCITE-5427] Provide code quality/coverage metrics with Jacoco and Sonarcloud

This is an automated email from the ASF dual-hosted git repository.

zabetak pushed a commit to branch dev-sonarcloud
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit ad2cddcac35239d16003e4bef0a0c5de67dc8aa9
Author: Stamatis Zampetakis <za...@gmail.com>
AuthorDate: Thu Jul 30 13:21:04 2020 +0300

    [CALCITE-5427] Provide code quality/coverage metrics with Jacoco and Sonarcloud
---
 Jenkinsfile         | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 build.gradle.kts    | 20 +++++++++++++++++++
 gradle.properties   |  7 +++++++
 settings.gradle.kts |  2 ++
 4 files changed, 84 insertions(+)

diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000000..2f2aee558f
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+node('ubuntu') {
+  def JAVA_JDK_17=tool name: 'jdk_17_latest', type: 'hudson.model.JDK'
+  stage('Checkout') {
+    if(env.CHANGE_ID) {
+      // By default checkout on PRs will fetch only that branch and nothing else. However, in order for the Sonar plugin
+      // to function properly we need to fetch also the target branch ${CHANGE_TARGET} so we need to customize the
+      // refspec. If the target branch of the PR is not present warnings like the following may appear:
+      // Could not find ref 'main' in refs/heads, refs/remotes/upstream or refs/remotes/origin
+      checkout([
+        $class: 'GitSCM',
+        branches: scm.branches,
+        doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
+        extensions: scm.extensions,
+        userRemoteConfigs: scm.userRemoteConfigs + [[
+          name: 'origin',
+          refspec: scm.userRemoteConfigs[0].refspec+ " +refs/heads/${CHANGE_TARGET}:refs/remotes/origin/${CHANGE_TARGET}",
+          url: scm.userRemoteConfigs[0].url,
+          credentialsId: scm.userRemoteConfigs[0].credentialsId
+        ]],
+      ])
+    } else {
+      checkout scm
+    }
+  }
+  stage('Code Quality') {
+    withEnv(["Path+JDK=$JAVA_JDK_17/bin","JAVA_HOME=$JAVA_JDK_17"]) {
+      withCredentials([string(credentialsId: 'SONARCLOUD_TOKEN', variable: 'SONAR_TOKEN')]) {
+        if ( env.BRANCH_NAME.startsWith("PR-") ) {
+          sh './gradlew --no-parallel --no-daemon build jacocoTestReport sonar -PenableJacoco -Dsonar.pullrequest.branch=${CHANGE_BRANCH} -Dsonar.pullrequest.base=${CHANGE_TARGET} -Dsonar.pullrequest.key=${CHANGE_ID} -Dsonar.login=${SONAR_TOKEN}'
+        } else {
+          sh './gradlew --no-parallel --no-daemon build jacocoTestReport sonar -PenableJacoco -Dsonar.branch.name=${BRANCH_NAME} -Dsonar.login=${SONAR_TOKEN}'
+        }
+      }
+    }
+  }
+  cleanWs()
+}
+
diff --git a/build.gradle.kts b/build.gradle.kts
index 89c384d9d6..ec1e8f9468 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -38,6 +38,7 @@ plugins {
     // Verification
     checkstyle
     calcite.buildext
+    jacoco
     id("org.checkerframework") apply false
     id("com.github.autostyle")
     id("org.nosphere.apache.rat")
@@ -47,6 +48,7 @@ plugins {
     id("com.github.vlsi.jandex") apply false
     id("org.owasp.dependencycheck")
     id("com.github.johnrengelman.shadow") apply false
+    id("org.sonarqube")
     // IDE configuration
     id("org.jetbrains.gradle.plugin.idea-ext")
     id("com.github.vlsi.ide")
@@ -81,6 +83,7 @@ val enableSpotBugs = props.bool("spotbugs")
 val enableCheckerframework by props()
 val enableErrorprone by props()
 val enableDependencyAnalysis by props()
+val enableJacoco by props()
 val skipJandex by props()
 val skipCheckstyle by props()
 val skipAutostyle by props()
@@ -294,6 +297,12 @@ fun com.github.autostyle.gradle.BaseFormatExtension.license() {
     endWithNewline()
 }
 
+sonarqube {
+    properties {
+        property("sonar.test.inclusions", "**/*Test*/**")
+    }
+}
+
 allprojects {
     group = "org.apache.calcite"
     version = buildVersion
@@ -333,6 +342,9 @@ allprojects {
                 testRuntimeOnly("org.junit.vintage:junit-vintage-engine")
             }
         }
+        if (enableJacoco) {
+            apply(plugin = "jacoco")
+        }
     }
 
     if (!skipAutostyle) {
@@ -763,6 +775,14 @@ allprojects {
                 }
                 enabled = enableSpotBugs
             }
+            configureEach<JacocoReport> {
+                reports {
+                    // The reports are mainly consumed by Sonar, which only uses the XML format
+                    xml.required.set(true)
+                    html.required.set(false)
+                    csv.required.set(false)
+                }
+            }
 
             afterEvaluate {
                 // Add default license/notice when missing
diff --git a/gradle.properties b/gradle.properties
index e5b2863be0..53463b71a7 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -51,12 +51,14 @@ com.github.spotbugs.version=2.0.0
 com.github.vlsi.vlsi-release-plugins.version=1.84
 com.google.protobuf.version=0.8.10
 de.thetaphi.forbiddenapis.version=3.4
+jacoco.version=0.8.8
 kotlin.version=1.5.31
 net.ltgt.errorprone.version=1.3.0
 me.champeau.gradle.jmh.version=0.5.3
 org.jetbrains.gradle.plugin.idea-ext.version=0.5
 org.nosphere.apache.rat.version=0.7.0
 org.owasp.dependencycheck.version=6.1.6
+org.sonarqube.version=3.5.0.2730
 
 # For now, we use Kotlin for tests only, so we don't want to include kotlin-stdlib dependency to the runtimeClasspath
 # See https://kotlinlang.org/docs/gradle.html#dependency-on-the-standard-library
@@ -158,3 +160,8 @@ tpch.version=1.0
 uzaygezen.version=0.2
 xalan.version=2.7.1
 xercesImpl.version=2.9.1
+
+# sonar properties
+systemProp.sonar.organization=apache
+systemProp.sonar.projectKey=apache_calcite
+systemProp.sonar.host.url=https://sonarcloud.io
diff --git a/settings.gradle.kts b/settings.gradle.kts
index ad956b868b..d5a9c5fe88 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -33,11 +33,13 @@ pluginManagement {
         idv("com.github.vlsi.stage-vote-release", "com.github.vlsi.vlsi-release-plugins")
         idv("com.google.protobuf")
         idv("de.thetaphi.forbiddenapis")
+        idv("jacoco")
         idv("me.champeau.gradle.jmh")
         idv("net.ltgt.errorprone")
         idv("org.jetbrains.gradle.plugin.idea-ext")
         idv("org.nosphere.apache.rat")
         idv("org.owasp.dependencycheck")
+        idv("org.sonarqube")
         kotlin("jvm") version "kotlin".v()
     }
     if (extra.has("enableMavenLocal") && extra["enableMavenLocal"].toString().ifBlank { "true" }.toBoolean()) {