[SEBA-8]

Add the ability to run a set of commands in preparation for sonarqube,
and to specify a path to the output java bytecode

Change-Id: Iaf5fcc17fc25a93df1cfd973505d9a3f30a3105b
diff --git a/jjb/cord-macros.yaml b/jjb/cord-macros.yaml
index 8b3e659..3a3b2ef 100644
--- a/jjb/cord-macros.yaml
+++ b/jjb/cord-macros.yaml
@@ -39,7 +39,10 @@
 - builder:
     name: cord-infra-sonarqube
     builders:
-      - shell: 'pylint --version'
+      - inject:
+          properties-content: |
+            SONAR_PREP_COMMANDS="{sonar-prep-commands}"
+      - shell: !include-raw-escape: shell/sonarprep.sh
       - sonar:
           sonar-name: 'sonarqube.opencord.org'
           java-opts: '-Xmx1280m'
@@ -47,6 +50,7 @@
             sonar.sources=.
             sonar.projectKey={project}_$GERRIT_BRANCH
             sonar.python.pylint=/usr/local/bin/pylint
+            sonar.java.binaries="{sonar-java-binaries}"
 
 # trigger on gerrit patchsets and actions
 # docs: https://docs.openstack.org/infra/jenkins-job-builder/triggers.html#triggers.gerrit
diff --git a/jjb/defaults.yaml b/jjb/defaults.yaml
index b6b6248..9a3e455 100644
--- a/jjb/defaults.yaml
+++ b/jjb/defaults.yaml
@@ -63,3 +63,7 @@
     docs-ssh-host: 'guide.opencord.org'
     docs-ssh-host-key: 'guide.opencord.org,52.9.82.207 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFHwOY3/8GucdKzBngH/FC08nHac/RJ/OheZp2+5EpOPXZG9zQW2YUbXH5A9pO76lI5CG3z3+huG62xEGo99UQU='
 
+    # For running Sonarqube configuration - see sonar.yaml for more info
+    sonar-prep-commands: ''
+    sonar-java-binaries: ''
+
diff --git a/jjb/shell/sonarprep.sh b/jjb/shell/sonarprep.sh
new file mode 100755
index 0000000..6f2e23f
--- /dev/null
+++ b/jjb/shell/sonarprep.sh
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+
+# Copyright 2018-present Open Networking Foundation
+#
+# Licensed 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.
+
+# sonarprep.sh - prep project for running sonarqube
+set -eu -o pipefail
+
+# run build commands if not blank
+if [ ! -z "$SONAR_PREP_COMMANDS" ]
+then
+  echo "# Running SONAR_PREP_COMMANDS: '$SONAR_PREP_COMMANDS'"
+  $SONAR_PREP_COMMANDS
+else
+  echo "# No preparation commands set in SONAR_PREP_COMMANDS"
+fi
+
+echo "# pylint version"
+pylint --version
+
diff --git a/jjb/sonar.yaml b/jjb/sonar.yaml
index 344a1c2..205079e 100644
--- a/jjb/sonar.yaml
+++ b/jjb/sonar.yaml
@@ -12,6 +12,13 @@
 # Sonarqube docs:
 #  https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins
 #  https://docs.sonarqube.org/display/SCAN/Advanced+SonarQube+Scanner+Usages
+#
+# Variables:
+#  sonar-java-binaries - paths to Java bytecodefiles for SonarQube to analyze. Defaults to "".
+#                        See: https://docs.sonarqube.org/display/PLUG/Java+Plugin+and+Bytecode
+#  sonar-prep-commands - shell commands to run before SonarScanner is run, frequently used to
+#                        build java bytecode. Defaults to "" (nothing run)
+#
 
 - project:
     name: 'sonarqube-ongoing-coverage'
@@ -67,11 +74,11 @@
     project-type: freestyle
     concurrent: true
 
-# run `pylint --version` before sonarqube, to expose version in logs and to
-# avoid a timeout when sonarqube runs it during analysis.
     builders:
         - 'cord-infra-sonarqube':
             project: '{project}'
+            sonar-prep-commands: '{sonar-prep-commands}'
+            sonar-java-binaries: '{sonar-java-binaries}'
 
 # run Sonarqube as a verification jobs on individual patchsets
 - job-template:
@@ -111,8 +118,8 @@
     project-type: freestyle
     concurrent: true
 
-# run `pylint --version` before sonarqube, to expose version in logs and to
-# avoid a timeout when sonarqube runs it during analysis.
     builders:
         - 'cord-infra-sonarqube':
             project: '{project}'
+            sonar-prep-commands: '{sonar-prep-commands}'
+            sonar-java-binaries: '{sonar-java-binaries}'
diff --git a/jjb/verify/ves-agent.yaml b/jjb/verify/ves-agent.yaml
index 49daf05..a9c4abb 100644
--- a/jjb/verify/ves-agent.yaml
+++ b/jjb/verify/ves-agent.yaml
@@ -15,3 +15,5 @@
       - 'verify-licensed'
       - 'verify-sonarqube':
           dependency-jobs: 'verify_ves-agent_tag-collision'
+          sonar-prep-commands: 'build.sh'
+          sonar-java-binaries: 'target'