From e48325e02f7d1bd3b9430696517a423cbdc7f725 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Thu, 4 Apr 2024 12:01:21 -0600 Subject: [PATCH 1/6] Per #366, add SonarQube workflow for METcalcpy --- .github/jobs/configure_sonarqube.sh | 65 ++++++++++++++++ .github/pull_request_template.md | 3 + .github/workflows/sonarqube.yml | 76 +++++++++++++++++++ .../installation/modulefiles/2.1.0_hera | 13 ---- internal/scripts/sonarqube/run_sonarqube.sh | 42 ++++++---- .../sonarqube/sonar-project.properties | 22 ++---- 6 files changed, 181 insertions(+), 40 deletions(-) create mode 100755 .github/jobs/configure_sonarqube.sh create mode 100644 .github/workflows/sonarqube.yml delete mode 100644 internal/scripts/scripts/installation/modulefiles/2.1.0_hera diff --git a/.github/jobs/configure_sonarqube.sh b/.github/jobs/configure_sonarqube.sh new file mode 100755 index 00000000..929b03fa --- /dev/null +++ b/.github/jobs/configure_sonarqube.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +# Constants +SONAR_PROPERTIES_DIR=internal/scripts/sonarqube +SONAR_PROPERTIES=sonar-project.properties + +# Check that this is being run from the top-level METdataio directory +if [ ! -e $SONAR_PROPERTIES_DIR/$SONAR_PROPERTIES ]; then + echo "ERROR: ${0} -> must be run from the top-level METdataio directory" + exit 1 +fi + +# Check required environment variables +if [ -z ${SOURCE_BRANCH+x} ]; then + echo "ERROR: ${0} -> \$SOURCE_BRANCH not defined!" + exit 1 +fi +if [ -z ${WD_REFERENCE_BRANCH+x} ]; then + echo "ERROR: ${0} -> \$WD_REFERENCE_BRANCH not defined!" + exit 1 +fi +if [ -z ${SONAR_HOST_URL+x} ]; then + echo "ERROR: ${0} -> \$SONAR_HOST_URL not defined!" + exit 1 +fi +if [ -z ${SONAR_TOKEN+x} ]; then + echo "ERROR: ${0} -> \$SONAR_TOKEN not defined!" + exit 1 +fi + +# Define the version string +SONAR_PROJECT_VERSION=$(cat docs/version | cut -d'=' -f2 | tr -d '" ') + +# +# Define the $SONAR_REFERENCE_BRANCH as the +# - Target of any requests +# - Manual setting for workflow dispatch +# - Source branch for any pushes (e.g. develop) +# +if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then + export SONAR_REFERENCE_BRANCH=$GITHUB_BASE_REF +elif [ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]; then + export SONAR_REFERENCE_BRANCH=$WD_REFERENCE_BRANCH +else + export SONAR_REFERENCE_BRANCH=$SOURCE_BRANCH +fi + +# Configure the sonar-project.properties +[ -e $SONAR_PROPERTIES ] && rm $SONAR_PROPERTIES +sed -e "s|SONAR_PROJECT_KEY|METdataio-GHA|" \ + -e "s|SONAR_PROJECT_NAME|METdataio GHA|" \ + -e "s|SONAR_PROJECT_VERSION|$SONAR_PROJECT_VERSION|" \ + -e "s|SONAR_HOST_URL|$SONAR_HOST_URL|" \ + -e "s|SONAR_TOKEN|$SONAR_TOKEN|" \ + -e "s|SONAR_BRANCH_NAME|$SOURCE_BRANCH|" \ + $SONAR_PROPERTIES_DIR/$SONAR_PROPERTIES > $SONAR_PROPERTIES + +# Define new code when the source and reference branches differ +if [ "$SOURCE_BRANCH" != "$SONAR_REFERENCE_BRANCH" ]; then + echo "sonar.newCode.referenceBranch=${SONAR_REFERENCE_BRANCH}" >> $SONAR_PROPERTIES +fi + +echo "Contents of the $SONAR_PROPERTIES file:" +cat $SONAR_PROPERTIES + diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index dda54835..342abeb9 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -11,6 +11,9 @@ - [ ] Will this PR result in changes to the test suite? **[Yes or No]**
If **yes**, describe the new output and/or changes to the existing output:
+- [ ] Do these changes introduce new SonarQube findings? **[Yes or No]**
+If **yes**, please describe: + - [ ] Please complete this pull request review by **[Fill in date]**.
## Pull Request Checklist ## diff --git a/.github/workflows/sonarqube.yml b/.github/workflows/sonarqube.yml new file mode 100644 index 00000000..d48e1237 --- /dev/null +++ b/.github/workflows/sonarqube.yml @@ -0,0 +1,76 @@ +name: SonarQube Scan + +# Run SonarQube for Pull Requests and changes to the develop and main_vX.Y branches + +on: + + # Trigger analysis for pushes to develop and main_vX.Y branches + push: + branches: + - develop + - 'main_v**' + paths-ignore: + - 'docs/**' + - '.github/pull_request_template.md' + - '.github/ISSUE_TEMPLATE/**' + - '**/README.md' + - '**/LICENSE.md' + + # Trigger analysis for pull requests to develop and main_vX.Y branches + pull_request: + types: [opened, synchronize, reopened] + branches: + - develop + - 'main_v**' + paths-ignore: + - 'docs/**' + - '.github/pull_request_template.md' + - '.github/ISSUE_TEMPLATE/**' + - '**/README.md' + - '**/LICENSE.md' + + workflow_dispatch: + inputs: + reference_branch: + description: 'Reference Branch' + default: develop + type: string + +jobs: + sonarqube: + name: SonarQube Scan + runs-on: ubuntu-latest + + steps: + + - uses: actions/checkout@v4 + with: + # Disable shallow clones for better analysis + fetch-depth: 0 + + - name: Get branch name + id: get_branch_name + run: echo branch_name=${GITHUB_REF#refs/heads/} >> $GITHUB_OUTPUT + + - name: Configure SonarQube + run: .github/jobs/configure_sonarqube.sh + env: + SOURCE_BRANCH: ${{ steps.get_branch_name.outputs.branch_name }} + WD_REFERENCE_BRANCH: ${{ github.event.inputs.reference_branch }} + SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + + - name: SonarQube Scan + uses: sonarsource/sonarqube-scan-action@master + env: + SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + + - name: SonarQube Quality Gate check + id: sonarqube-quality-gate-check + uses: sonarsource/sonarqube-quality-gate-action@master + # Force to fail step after specific time. + timeout-minutes: 5 + env: + SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/internal/scripts/scripts/installation/modulefiles/2.1.0_hera b/internal/scripts/scripts/installation/modulefiles/2.1.0_hera deleted file mode 100644 index f0863ba0..00000000 --- a/internal/scripts/scripts/installation/modulefiles/2.1.0_hera +++ /dev/null @@ -1,13 +0,0 @@ -#%Module###################################################################### -## -## METcalcpy -## -proc ModulesHelp { } { - puts stderr "Sets up the paths and environment variables to use the METcalcpy-2.1.0. - *** For help see the official MET webpage at http://www.dtcenter.org/met/users ***" -} - -prereq intel - -prepend-path PATH /scratch1/BMC/dtc/miniconda/miniconda3/envs/metplus_v5.1_py3.10/bin -prepend-path PYTHONPATH /contrib/METcalcpy/METcalcpy-2.1.0 diff --git a/internal/scripts/sonarqube/run_sonarqube.sh b/internal/scripts/sonarqube/run_sonarqube.sh index b3d57817..d43df3ec 100755 --- a/internal/scripts/sonarqube/run_sonarqube.sh +++ b/internal/scripts/sonarqube/run_sonarqube.sh @@ -1,10 +1,10 @@ #!/bin/bash # -# Run SonarQube Source Code Analyzer on a specified revision of MET +# Run SonarQube Source Code Analyzer for METcalcpy #======================================================================= # # This run_sonarqube.sh script will check out the specified version -# of MET and run the SonarQube Source Code Analyzer on it. First, +# of METcalcpy and run the SonarQube Source Code Analyzer on it. First, # go to the directory where you would like the SCA output written and # then run: # @@ -12,9 +12,9 @@ # METcalcpy/sonarqube/run_sonarqube.sh name # # Usage: run_sonarqube.sh name -# Test the specified branched version of MET: +# Test the specified branched version of METcalcpy: # run_sonarqube.sh {branch name} -# Test the specified tagged version of MET: +# Test the specified tagged version of METcalcpy: # run_sonarqube.sh {tag name} # #======================================================================= @@ -24,15 +24,25 @@ GIT_REPO_NAME=METcalcpy GIT_REPO="https://github.com/dtcenter/${GIT_REPO_NAME}" function usage { - echo - echo "USAGE: $(basename $0) name" - echo " where \"name\" specifies a branch, tag, or hash." - echo + echo + echo "USAGE: $(basename $0) name" + echo " where \"name\" specifies a branch, tag, or hash." + echo } # Check for arguments if [[ $# -lt 1 ]]; then usage; exit; fi +# Check that SONAR_TOKEN and SONAR_HOST_URL are defined +if [ -z ${SONAR_TOKEN} ]; then + echo "ERROR: SONAR_TOKEN must be set" + exit 1 +fi +if [ -z ${SONAR_HOST_URL} ]; then + echo "ERROR: SONAR_HOST_URL must be set" + exit 1 +fi + # Check that SONARQUBE_WRAPPER_BIN is defined if [ -z ${SONARQUBE_WRAPPER_BIN} ]; then which build-wrapper-linux-x86-64 2> /dev/null @@ -87,7 +97,6 @@ function run_command() { return ${STATUS} } - # Store the full path to the scripts directory SCRIPT_DIR=`dirname $0` if [[ ${0:0:1} != "/" ]]; then SCRIPT_DIR=$(pwd)/${SCRIPT_DIR}; fi @@ -102,14 +111,21 @@ run_command "git clone ${GIT_REPO} ${REPO_DIR}" run_command "cd ${REPO_DIR}" run_command "git checkout ${1}" +# Define the version string +SONAR_PROJECT_VERSION=$(cat docs/version | cut -d'=' -f2 | tr -d '" ') + SONAR_PROPERTIES=sonar-project.properties -# Copy sonar-project.properties for Python code +# Configure the sonar-project.properties [ -e $SONAR_PROPERTIES ] && rm $SONAR_PROPERTIES -cp -p $SCRIPT_DIR/sonar-project.properties $SONAR_PROPERTIES +sed -e "s|SONAR_PROJECT_KEY|METcalcpy_NB|" \ + -e "s|SONAR_PROJECT_NAME|METcalcpy Nightly Build|" \ + -e "s|SONAR_PROJECT_VERSION|$SONAR_PROJECT_VERSION|" \ + -e "s|SONAR_HOST_URL|$SONAR_HOST_URL|" \ + -e "s|SONAR_TOKEN|$SONAR_TOKEN|" \ + -e "s|SONAR_BRANCH_NAME|${1}|" \ + $SCRIPT_DIR/$SONAR_PROPERTIES > $SONAR_PROPERTIES # Run SonarQube scan for Python code run_command "${SONARQUBE_SCANNER_BIN}/sonar-scanner" -# Run SonarQube report generator to make a PDF file -#TODAY=`date +%Y%m%d` diff --git a/internal/scripts/sonarqube/sonar-project.properties b/internal/scripts/sonarqube/sonar-project.properties index 35335020..37755f03 100644 --- a/internal/scripts/sonarqube/sonar-project.properties +++ b/internal/scripts/sonarqube/sonar-project.properties @@ -1,17 +1,11 @@ -sonar.projectKey=org.sonarqube:METcalcpy_NB -sonar.projectName=METcalcpy Nightly Build -sonar.projectVersion=1.0 - +# Project and source code settings +sonar.projectKey=SONAR_PROJECT_KEY +sonar.projectName=SONAR_PROJECT_NAME +sonar.projectVersion=SONAR_PROJECT_VERSION +sonar.branch.name=SONAR_BRANCH_NAME sonar.sources=metcalcpy,scratch,test - -# The build-wrapper output dir - -# Encoding of the source files sonar.sourceEncoding=UTF-8 -#----- Default SonarQube server -#sonar.host.url=http://localhost:9000 -sonar.host.url=http://mandan:9000 - -sonar.login=met -sonar.password=met@sonar.ucar +# SonarQube server +sonar.host.url=SONAR_HOST_URL +sonar.token=SONAR_TOKEN From 5d9b1923db785faeb0313728c65625b1ea644f7e Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Thu, 4 Apr 2024 12:24:49 -0600 Subject: [PATCH 2/6] Per #366, update nightly build email list. --- internal/scripts/sonarqube/run_nightly.sh | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/internal/scripts/sonarqube/run_nightly.sh b/internal/scripts/sonarqube/run_nightly.sh index ad1935ea..fe257b6c 100755 --- a/internal/scripts/sonarqube/run_nightly.sh +++ b/internal/scripts/sonarqube/run_nightly.sh @@ -12,7 +12,7 @@ # METcalcpy/sonarqube/run_nightly.sh name # # Usage: run_nightly.sh name -# where "name" speci1fies a branch, tag, or hash +# where "name" specifies a branch, tag, or hash # # For example, scan the develop branch: # run_nightly.sh develop @@ -20,8 +20,7 @@ #======================================================================= # Constants -EMAIL_LIST="johnhg@ucar.edu hsoh@ucar.edu mccabe@ucar.edu minnawin@ucar.edu" -TEST_EMAIL_LIST="hsoh@ucar.edu" +EMAIL_LIST="johnhg@ucar.edu hsoh@ucar.edu jpresto@ucar.edu minnawin@ucar.edu" KEEP_DAYS=5 GIT_REPO_NAME=METcalcpy @@ -68,12 +67,9 @@ LOGFILE=${RUN_DIR}/run_sonarqube_${TODAY}.log # Run scan and check for bad return status ${SCRIPT_DIR}/run_sonarqube.sh ${1} >& ${LOGFILE} if [[ $? -ne 0 ]]; then - [ ! -z $2 ] && EMAIL_LIST=$TEST_EMAIL_LIST echo "$0: The nightly SonarQube scanning for $GIT_REPO_NAME FAILED in `basename ${RUN_DIR}`." >> ${LOGFILE} cat ${LOGFILE} | mail -s "$GIT_REPO_NAME SonarQube scanning failed for ${1} in `basename ${RUN_DIR}` (autogen msg)" ${EMAIL_LIST} exit 1 fi -# Convert SonarQube report from pdf to html - exit 0 From 46ba354b83b7d055a846094b5fb8384901ba653a Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Thu, 4 Apr 2024 21:07:07 +0000 Subject: [PATCH 3/6] Per #366, fix cut/paste error in the configure_sonarqube.sh script. --- .github/jobs/configure_sonarqube.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/jobs/configure_sonarqube.sh b/.github/jobs/configure_sonarqube.sh index 929b03fa..aedcb9ae 100755 --- a/.github/jobs/configure_sonarqube.sh +++ b/.github/jobs/configure_sonarqube.sh @@ -4,9 +4,9 @@ SONAR_PROPERTIES_DIR=internal/scripts/sonarqube SONAR_PROPERTIES=sonar-project.properties -# Check that this is being run from the top-level METdataio directory +# Check that this is being run from the top-level METcalcpy directory if [ ! -e $SONAR_PROPERTIES_DIR/$SONAR_PROPERTIES ]; then - echo "ERROR: ${0} -> must be run from the top-level METdataio directory" + echo "ERROR: ${0} -> must be run from the top-level METcalcpy directory" exit 1 fi @@ -47,8 +47,8 @@ fi # Configure the sonar-project.properties [ -e $SONAR_PROPERTIES ] && rm $SONAR_PROPERTIES -sed -e "s|SONAR_PROJECT_KEY|METdataio-GHA|" \ - -e "s|SONAR_PROJECT_NAME|METdataio GHA|" \ +sed -e "s|SONAR_PROJECT_KEY|METcalcpy-GHA|" \ + -e "s|SONAR_PROJECT_NAME|METcalcpy GHA|" \ -e "s|SONAR_PROJECT_VERSION|$SONAR_PROJECT_VERSION|" \ -e "s|SONAR_HOST_URL|$SONAR_HOST_URL|" \ -e "s|SONAR_TOKEN|$SONAR_TOKEN|" \ From 8ce98e87c90e85bf8fefcf03d0b3b83deb749511 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Thu, 4 Apr 2024 18:35:23 -0600 Subject: [PATCH 4/6] Per #366, just whitespace change to trigger another GHA run for the PR --- internal/scripts/sonarqube/sonar-project.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/scripts/sonarqube/sonar-project.properties b/internal/scripts/sonarqube/sonar-project.properties index 37755f03..8b24cea0 100644 --- a/internal/scripts/sonarqube/sonar-project.properties +++ b/internal/scripts/sonarqube/sonar-project.properties @@ -9,3 +9,4 @@ sonar.sourceEncoding=UTF-8 # SonarQube server sonar.host.url=SONAR_HOST_URL sonar.token=SONAR_TOKEN + From ceac1820d09ce281b4a5f67b6301a1da727aa26b Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Thu, 4 Apr 2024 18:47:33 -0600 Subject: [PATCH 5/6] Per #366, whitespace --- internal/scripts/sonarqube/run_nightly.sh | 1 - internal/scripts/sonarqube/sonar-project.properties | 1 - 2 files changed, 2 deletions(-) diff --git a/internal/scripts/sonarqube/run_nightly.sh b/internal/scripts/sonarqube/run_nightly.sh index fe257b6c..50645f11 100755 --- a/internal/scripts/sonarqube/run_nightly.sh +++ b/internal/scripts/sonarqube/run_nightly.sh @@ -34,7 +34,6 @@ function usage { # Check for arguments if [ $# -lt 1 ]; then usage; exit 1; fi - # Store the full path to the scripts directory SCRIPT_DIR=`dirname $0` if [[ ${0:0:1} != "/" ]]; then SCRIPT_DIR=$(pwd)/${SCRIPT_DIR}; fi diff --git a/internal/scripts/sonarqube/sonar-project.properties b/internal/scripts/sonarqube/sonar-project.properties index 8b24cea0..37755f03 100644 --- a/internal/scripts/sonarqube/sonar-project.properties +++ b/internal/scripts/sonarqube/sonar-project.properties @@ -9,4 +9,3 @@ sonar.sourceEncoding=UTF-8 # SonarQube server sonar.host.url=SONAR_HOST_URL sonar.token=SONAR_TOKEN - From 2084dd5e3b1e2b31de4edd587feaaf37848e0d84 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Thu, 4 Apr 2024 18:47:33 -0600 Subject: [PATCH 6/6] Per #366, exclude test code from code coverage statistics. --- internal/scripts/sonarqube/sonar-project.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/scripts/sonarqube/sonar-project.properties b/internal/scripts/sonarqube/sonar-project.properties index 37755f03..c3534080 100644 --- a/internal/scripts/sonarqube/sonar-project.properties +++ b/internal/scripts/sonarqube/sonar-project.properties @@ -4,6 +4,7 @@ sonar.projectName=SONAR_PROJECT_NAME sonar.projectVersion=SONAR_PROJECT_VERSION sonar.branch.name=SONAR_BRANCH_NAME sonar.sources=metcalcpy,scratch,test +sonar.coverage.exclusions=test/** sonar.sourceEncoding=UTF-8 # SonarQube server