From 438007e7026c89b101c61458a7c1c9dfd9ad3344 Mon Sep 17 00:00:00 2001 From: JohT Date: Sat, 24 Feb 2024 16:56:51 +0100 Subject: [PATCH 1/2] Create jQAssistant configuration only if missing --- scripts/resetAndScan.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/scripts/resetAndScan.sh b/scripts/resetAndScan.sh index 88af30d0d..016b649fa 100755 --- a/scripts/resetAndScan.sh +++ b/scripts/resetAndScan.sh @@ -32,8 +32,7 @@ echo "resetAndScan: SCRIPTS_DIR=${SCRIPTS_DIR}" # Internal constants JQASSISTANT_DIRECTORY="${TOOLS_DIRECTORY}/${JQASSISTANT_CLI_ARTIFACT}-${JQASSISTANT_CLI_VERSION}" JQASSISTANT_BIN="${JQASSISTANT_DIRECTORY}/bin" -#JQASSISTANT_NEO4J_OPTIONS="-D jqassistant.store.uri=${NEO4J_BOLT_URI} -D jqassistant.store.remote.username=${NEO4J_USER} -D jqassistant.store.remote.password=${NEO4J_INITIAL_PASSWORD}" -#JQASSISTANT_NEO4J_OPTIONS=-configurationLocations "${JQASSISTANT_CONFIG}/.jqassistant.yaml" +JQASSISTANT_CONFIG_TEMPLATE_PATH="${SCRIPTS_DIR}/configuration/${JQASSISTANT_CONFIG_TEMPLATE}" # Check if environment variable is set if [ -z "${NEO4J_INITIAL_PASSWORD}" ]; then @@ -55,9 +54,16 @@ else echo "resetAndScan: Using jQAssistant binary directory ${JQASSISTANT_BIN}" fi -# Create jQAssistant configuration YAML file by copying a template for it +# Create jQAssistant configuration YAML file by copying it from a corresponding template mkdir -p "./.jqassistant" -cp "${SCRIPTS_DIR}/configuration/${JQASSISTANT_CONFIG_TEMPLATE}" "./.jqassistant/" + +echo "resetAndScan: Check if ./.jqassistant/${JQASSISTANT_CONFIG_TEMPLATE} needs to be copied." +if [ ! -f "./.jqassistant/${JQASSISTANT_CONFIG_TEMPLATE}" ]; then + cp "${JQASSISTANT_CONFIG_TEMPLATE_PATH}" "./.jqassistant/" + echo "resetAndScan: jQAssistant configuration copied from configuration template" +else + echo "resetAndScan: jQAssistant configuration won't be changed since it already exists." +fi # Use jQAssistant to scan the downloaded artifacts and write the results into the separate, local Neo4j Graph Database echo "resetAndScan: Scanning ${ARTIFACTS_DIRECTORY} with jQAssistant CLI version ${JQASSISTANT_CLI_VERSION}" From eb2e9b8e487f0e5c2f142ef1ae1cd75cccd2f383 Mon Sep 17 00:00:00 2001 From: JohT Date: Sat, 24 Feb 2024 17:45:37 +0100 Subject: [PATCH 2/2] Write change detection file AFTER successful scan --- scripts/detectChangedArtifacts.sh | 52 +++++++++++++++++++++++++++---- scripts/resetAndScanChanged.sh | 5 +-- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/scripts/detectChangedArtifacts.sh b/scripts/detectChangedArtifacts.sh index 7d48c4bbb..b564170c6 100755 --- a/scripts/detectChangedArtifacts.sh +++ b/scripts/detectChangedArtifacts.sh @@ -2,11 +2,45 @@ # Detect changed files in the artifacts directory with a text file containing the last hash code of the contents. # The hash value is generated based on all files (their names and properties) within the artifacts directory. -# A change is detected when the current hash and the stored one differ. +# A change is detected when the current hash and the stored differ. +# +# Command line options: +# --readonly Detect changes without creating or updating the change detection file (stateless). +# A second call without this option will be needed for the change detection to work. +# This is helpful to decide if an operation should be done based on changes while waiting for its success to finally save the change state. # Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands) set -o errexit -o pipefail +# Function to display script usage +usage() { + echo "Usage: $0 [--readonly]" + exit 1 +} + +# Default values +readonlyMode=false + +# Parse command line arguments +while [[ $# -gt 0 ]]; do + key="$1" + case $key in + --readonly) + readonlyMode=true + shift + ;; + *) + echo "detectChangedArtifacts: Error: Unknown option: ${key}" + usage + ;; + esac + shift || true # ignore error when there are no more arguments +done + +if ${readonlyMode}; then + echo "detectChangedArtifacts: Readonly mode activated. Change detection file won't be created." >&2 +fi + ARTIFACTS_DIRECTORY=${ARTIFACTS_DIRECTORY:-"artifacts"} ARTIFACTS_CHANGE_DETECTION_HASH_FILE=${ARTIFACTS_CHANGE_DETECTION_HASH_FILE:-"artifactsChangeDetectionHash.txt"} # Name of the file that contains the hash code of the file list for change detection ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH="./${ARTIFACTS_DIRECTORY}/$ARTIFACTS_CHANGE_DETECTION_HASH_FILE" @@ -25,8 +59,11 @@ CURRENT_ARTIFACTS_HASH="$( find "./$ARTIFACTS_DIRECTORY" -type f -not -name "${A # Assume that the files where changed if the file containing the hash of the file list does not exist yet. if [ ! -f "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}" ] ; then - # Create the file containing the hash of the files list to a new file for the next call - echo "${CURRENT_ARTIFACTS_HASH}" > "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}" + if ! ${readonlyMode}; then + # Create the file containing the hash of the files list to a new file for the next call + echo "${CURRENT_ARTIFACTS_HASH}" > "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}" + echo "detectChangedArtifacts: Change detection file created" >&2 + fi echo 1 exit 0 fi @@ -36,7 +73,10 @@ fi if [[ $(< "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}") == "$CURRENT_ARTIFACTS_HASH" ]] ; then echo 0 else - # Write the updated hash into the file containing the hash of the files list for the next call - echo "$CURRENT_ARTIFACTS_HASH" > "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}" + if ! ${readonlyMode}; then + # Write the updated hash into the file containing the hash of the files list for the next call + echo "$CURRENT_ARTIFACTS_HASH" > "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}" + echo "detectChangedArtifacts: Change detection file updated" >&2 + fi echo 2 -fi +fi \ No newline at end of file diff --git a/scripts/resetAndScanChanged.sh b/scripts/resetAndScanChanged.sh index 8911f39f6..61577197e 100755 --- a/scripts/resetAndScanChanged.sh +++ b/scripts/resetAndScanChanged.sh @@ -14,13 +14,14 @@ set -o errexit -o pipefail # CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes. # This way non-standard tools like readlink aren't needed. SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts -echo "resetAndScanChanged SCRIPTS_DIR=$SCRIPTS_DIR" +echo "resetAndScanChanged SCRIPTS_DIR=${SCRIPTS_DIR}" # Scan and analyze Artifacts when they were changed -changeDetectionReturnCode=$( source "$SCRIPTS_DIR/detectChangedArtifacts.sh" ) +changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedArtifacts.sh" --readonly) if [[ "${changeDetectionReturnCode}" == "0" ]] ; then echo "resetAndScanChanged: Artifacts unchanged. Scan skipped." else echo "resetAndScanChanged: Detected change (${changeDetectionReturnCode}). Resetting database and scanning artifacts." source "${SCRIPTS_DIR}/resetAndScan.sh" + changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedArtifacts.sh") fi \ No newline at end of file