Add support for SonarCloud 78/14478/3
authorThanh Ha <thanh.ha@linuxfoundation.org>
Thu, 7 Feb 2019 00:32:00 +0000 (19:32 -0500)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 8 Feb 2019 18:38:10 +0000 (13:38 -0500)
If SonarCloud is used the old method is automatically disabled so that
Sonar is not run twice.

Example Usage:

- project:
    name: yangtools-sonar
    jobs:
      - gerrit-maven-sonar

    project: 'yangtools'
    project-name: 'yangtools'
    branch: 'master'
    mvn-settings: 'yangtools-settings'
    mvn-opts: '-Xmx1024m'
    sonarcloud: true
    sonarcloud-project-key: KEY
    sonarcloud-project-organization: ORGANIZATION
    sonarcloud-api-token: TOKEN

Issue: RELENG-1721
Change-Id: Ica1cd29bfc9246f2452370dd6ceb8b7e5b8e5329
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
.jjb-test/lf-maven-jobs/maven-sonarcloud.yaml [new file with mode: 0644]
docs/jjb/lf-maven-jobs.rst
jjb/lf-maven-jobs.yaml
releasenotes/notes/sonarcloud-support-7f0d1dcacff44ba4.yaml [new file with mode: 0644]
shell/maven-sonar.sh

diff --git a/.jjb-test/lf-maven-jobs/maven-sonarcloud.yaml b/.jjb-test/lf-maven-jobs/maven-sonarcloud.yaml
new file mode 100644 (file)
index 0000000..e367729
--- /dev/null
@@ -0,0 +1,15 @@
+---
+- project:
+    name: example-sonarcloud
+    jobs:
+      - gerrit-maven-sonar
+
+    project: 'sonarcloud'
+    project-name: 'sonarcloud'
+    branch: 'master'
+    mvn-settings: 'sonarcloud-settings'
+    mvn-opts: '-Xmx1024m'
+    sonarcloud: true
+    sonarcloud-project-key: KEY
+    sonarcloud-project-organization: ORGANIZATION
+    sonarcloud-api-token: TOKEN
index 25258ae..aca0f5f 100644 (file)
@@ -27,6 +27,20 @@ Runs Sonar against a Maven project.
     :mvn-version: Version of Maven to execute Sonar with.
     :mvn-settings: Maven settings.xml file containing credentials to use.
 
+lf-infra-maven-sonarcloud
+-------------------------
+
+Runs Sonar against a Maven project and pushes results to SonarCloud.
+
+:Required Parameters:
+
+    :java-version: Version of Java to execute Sonar with.
+    :mvn-version: Version of Maven to execute Sonar with.
+    :mvn-settings: Maven settings.xml file containing credentials to use.
+    :sonarcloud-project-key: SonarCloud project key.
+    :sonarcloud-project-organization: SonarCloud project organization.
+    :sonarcloud-api-token: SonarCloud API Token.
+
 lf-maven-build
 --------------
 
@@ -324,6 +338,8 @@ directory is then used later to deploy to Nexus.
 
     :gerrit_release_triggers: Override Gerrit Triggers.
 
+.. _maven-sonar:
+
 Maven Sonar
 -----------
 
@@ -363,6 +379,14 @@ interest in that kind of support.
     :mvn-opts: Sets MAVEN_OPTS. (default: '')
     :mvn-params: Additional mvn parameters to pass to the cli. (default: '')
     :mvn-version: Version of maven to use. (default: mvn35)
+    :sonar-mvn-goals: Maven goals to run for sonar analysis.
+        (default: sonar:sonar)
+    :sonarcloud: Whether or not to use SonarCloud ``true|false``.
+        (default: false)
+    :sonarcloud-project-key: SonarCloud project key. (default: '')
+    :sonarcloud-project-organization: SonarCloud project organization.
+        (default: '')
+    :sonarcloud-api-token: SonarCloud API Token. (default: '')
     :stream: Keyword that can be used to represent a release code-name.
         Often the same as the branch. (default: master)
     :submodule-recursive: Whether to checkout submodules recursively.
@@ -372,6 +396,12 @@ interest in that kind of support.
 
     :gerrit_sonar_triggers: Override Gerrit Triggers.
 
+
+SonarCloud Example:
+
+.. literalinclude:: ../../.jjb-test/lf-maven-jobs/maven-sonarcloud.yaml
+   :language: yaml
+
 Maven Verify
 ------------
 
index 9c8b953..46a078a 100644 (file)
     mvn-params: ''
     mvn-version: mvn35
     sonar-mvn-goal: 'sonar:sonar'
+    sonarcloud: false
+    sonarcloud-project-key: ''
+    sonarcloud-project-organization: ''
+    sonarcloud-api-token: ''
     stream: master
     submodule-recursive: true
     submodule-timeout: 10
 
     builders:
       - lf-infra-pre-build
-      - lf-infra-maven-sonar:
-          java-version: '{java-version}'
-          mvn-settings: '{mvn-settings}'
-          mvn-version: '{mvn-version}'
+      # With SonarCloud
+      - conditional-step:
+          condition-kind: boolean-expression
+          condition-expression: '{sonarcloud}'
+          steps:
+            - shell: echo 'Using SonarCloud'
+            - lf-infra-maven-sonarcloud:
+                java-version: '{java-version}'
+                mvn-settings: '{mvn-settings}'
+                mvn-version: '{mvn-version}'
+                sonarcloud-project-key: '{sonarcloud-project-key}'
+                sonarcloud-project-organization: '{sonarcloud-project-organization}'
+                sonarcloud-api-token: '{sonarcloud-api-token}'
+      # With SonarQube
+      - conditional-step:
+          condition-kind: not
+          condition-operand:
+            condition-kind: boolean-expression
+            condition-expression: '{sonarcloud}'
+          steps:
+            - shell: echo 'Using SonarQube'
+            - lf-infra-maven-sonar:
+                java-version: '{java-version}'
+                mvn-settings: '{mvn-settings}'
+                mvn-version: '{mvn-version}'
 
     publishers:
       - lf-jacoco-report
           - ../shell/maven-sonar.sh
       - lf-provide-maven-settings-cleanup
 
+- builder:
+    name: lf-infra-maven-sonarcloud
+    # Run a Sonar build with Maven
+    builders:
+      - lf-maven-install:
+          mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
+      - inject:
+          # Switch this to the sonar wrapper when JJB 2.0 is available
+          properties-content: |
+            SONAR_HOST_URL=https://sonarcloud.io
+            PROJECT_KEY={sonarcloud-project-key}
+            PROJECT_ORGANIZATION={sonarcloud-project-organization}
+            API_TOKEN={sonarcloud-api-token}
+      - lf-provide-maven-settings:
+          global-settings-file: global-settings
+          settings-file: '{mvn-settings}'
+      - shell: !include-raw-escape:
+          - ../shell/common-variables.sh
+          - ../shell/maven-sonar.sh
+      - lf-provide-maven-settings-cleanup
+
 - job-template:
     name: '{project-name}-sonar'
     id: gerrit-maven-sonar
diff --git a/releasenotes/notes/sonarcloud-support-7f0d1dcacff44ba4.yaml b/releasenotes/notes/sonarcloud-support-7f0d1dcacff44ba4.yaml
new file mode 100644 (file)
index 0000000..ba8b467
--- /dev/null
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    Add support for pushing Sonar results to SonarCloud. Refer to
+    :ref:`Maven Sonar docs <maven-sonar>` for details.
index 5857234..ae280b2 100644 (file)
@@ -19,18 +19,26 @@ set +u
 
 export MAVEN_OPTS
 
+declare -a params
+params+=("--global-settings $GLOBAL_SETTINGS_FILE")
+params+=("--settings $SETTINGS_FILE")
+
 # Disable SC2086 because we want to allow word splitting for $MAVEN_* parameters.
 # shellcheck disable=SC2086
 $MVN clean install \
     -e -Dsonar \
-    --global-settings "$GLOBAL_SETTINGS_FILE" \
-    --settings "$SETTINGS_FILE" \
+    ${params[*]} \
     $MAVEN_OPTIONS $MAVEN_PARAMS
 
+if [ "$SONAR_HOST_URL" = "https://sonarcloud.io" ]; then
+    params+=("-Dsonar.projectKey=$PROJECT_KEY")
+    params+=("-Dsonar.organization=$PROJECT_ORGANIZATION")
+    params+=("-Dsonar.login=$API_TOKEN")
+fi
+
 # Disable SC2086 because we want to allow word splitting for $MAVEN_* parameters.
 # shellcheck disable=SC2086
-$MVN $SONAR_MAVEN_GOAL \
+"$MVN" $SONAR_MAVEN_GOAL \
     -e -Dsonar -Dsonar.host.url="$SONAR_HOST_URL" \
-    --global-settings "$GLOBAL_SETTINGS_FILE" \
-    --settings "$SETTINGS_FILE" \
+    ${params[*]} \
     $MAVEN_OPTIONS $MAVEN_PARAMS