Feat: Allow scans in short lived branches 51/68151/16
authorJessica Wagantall <jwagantall@linuxfoundation.org>
Fri, 25 Jun 2021 19:32:25 +0000 (12:32 -0700)
committerJessica Wagantall <jwagantall@linuxfoundation.org>
Thu, 9 Sep 2021 18:58:51 +0000 (11:58 -0700)
Add functionality to allow scans of short lived
branches in SonarCloud.
Developers will be able to turn this feature on
by setting "scan-dev-branch: true"

Issue: RELENG-3937
Signed-off-by: Jessica Wagantall <jwagantall@linuxfoundation.org>
Change-Id: I6b32771ba00e86927ba179596c467b37c831bd2d

.jjb-test/lf-maven-jobs/maven-sonarcloud.yaml
docs/jjb/lf-maven-jobs.rst
jjb/lf-maven-jobs.yaml
jjb/lf-python-jobs.yaml
releasenotes/notes/sonar-short-lived-branch-support-835d15e843160095.yaml [new file with mode: 0644]
shell/maven-sonar.sh

index c3239a2..5d73a16 100644 (file)
@@ -13,6 +13,7 @@
     sonarcloud-project-key: KEY
     sonarcloud-project-organization: ORGANIZATION
     sonarcloud-api-token: TOKEN
+    scan-dev-branch: false
 
 - project:
     name: example-sonarcloud-with-prescan-script
@@ -30,3 +31,4 @@
     sonarcloud-api-token: TOKEN
     sonar-prescan-script: |
       echo "Run script at start of job."
+    scan-dev-branch: false
index d312cec..ffbbedc 100644 (file)
@@ -515,6 +515,8 @@ multi-branch configuration.
         (default: 10)
     :submodule-disable: Disable submodule checkout operation.
         (default: false)
+    :scan-dev-branch: Run the scan on a developer branch.
+        (default: false)
 
     :gerrit_sonar_triggers: Override Gerrit Triggers.
 
index 2b5c794..c63fe81 100644 (file)
     submodule-recursive: true
     submodule-timeout: 10
     submodule-disable: false
+    scan-dev-branch: false
 
     gerrit_sonar_triggers:
       - comment-added-contains-event:
                 sonarcloud-project-organization: "{sonarcloud-project-organization}"
                 sonarcloud-api-token: "{sonarcloud-api-token}"
                 sonarcloud-java-version: "{sonarcloud-java-version}"
+                scan-dev-branch: "{scan-dev-branch}"
       # With SonarQube
       - conditional-step:
           condition-kind: not
                 sonarcloud-project-organization: "{sonarcloud-project-organization}"
                 sonarcloud-api-token: "{sonarcloud-api-token}"
                 sonarcloud-java-version: "{sonarcloud-java-version}"
+                scan-dev-branch: "{scan-dev-branch}"
       # With SonarQube
       - conditional-step:
           condition-kind: not
             MAVEN_GOALS={mvn-goals}
             API_TOKEN={sonarcloud-api-token}
             SONARCLOUD_JAVA_VERSION={sonarcloud-java-version}
+            SCAN_DEV_BRANCH={scan-dev-branch}
       - lf-provide-maven-settings:
           global-settings-file: global-settings
           settings-file: "{mvn-settings}"
index 8d3c885..6fbd226 100644 (file)
     submodule-disable: false
     tox-dir: "."
     tox-envs: ""
+    scan-dev-branch: false
 
     gerrit_trigger_file_paths:
       - compare-type: REG_EXP
                 sonarcloud-project-organization: "{sonarcloud-project-organization}"
                 sonarcloud-api-token: "{sonarcloud-api-token}"
                 sonarcloud-java-version: "{sonarcloud-java-version}"
+                scan-dev-branch: "{scan-dev-branch}"
       # With SonarQube
       - conditional-step:
           condition-kind: not
diff --git a/releasenotes/notes/sonar-short-lived-branch-support-835d15e843160095.yaml b/releasenotes/notes/sonar-short-lived-branch-support-835d15e843160095.yaml
new file mode 100644 (file)
index 0000000..d35fdd4
--- /dev/null
@@ -0,0 +1,4 @@
+---
+features:
+  - |
+    Add support in lf-maven-sonar scan jobs to process short lived dev branches.
index 9e00d21..9af6e42 100644 (file)
@@ -34,6 +34,16 @@ if [ "$SONAR_HOST_URL" = "https://sonarcloud.io" ]; then
     params+=("-Dsonar.projectKey=$PROJECT_KEY")
     params+=("-Dsonar.organization=$PROJECT_ORGANIZATION")
     params+=("-Dsonar.login=$API_TOKEN")
+    if $SCAN_DEV_BRANCH; then
+        echo "Will scan short lived branch ..."
+        # shellcheck disable=SC2236
+        if [ ! -z ${GERRIT_CHANGE_NUMBER+x} ]; then
+            GERRIT_SHORT_LIVED_BRANCH=${GERRIT_CHANGE_NUMBER}-${GERRIT_PATCHSET_NUMBER}
+            params+=("-Dsonar.analysis.gerritProjectName=$PROJECT")
+            params+=("-Dsonar.branch.target=$GERRIT_BRANCH")
+            params+=("-Dsonar.branch.name=$GERRIT_SHORT_LIVED_BRANCH")
+        fi
+    fi
 fi
 
 if [ "$SET_JDK_VERSION" != "$SONARCLOUD_JAVA_VERSION" ]; then