From 2d2fafef1b931da8ee2e8145f98d6ed81ebb1d04 Mon Sep 17 00:00:00 2001 From: Eric Nemchik Date: Thu, 30 Jul 2020 15:18:24 -0500 Subject: [PATCH] Format code --- lib/buildFileList.sh | 1016 ++++++++++---------- lib/linter.sh | 2109 +++++++++++++++++++++--------------------- lib/log.sh | 32 +- lib/termColors.sh | 32 +- lib/validation.sh | 320 +++---- lib/worker.sh | 1612 ++++++++++++++++---------------- 6 files changed, 2562 insertions(+), 2559 deletions(-) diff --git a/lib/buildFileList.sh b/lib/buildFileList.sh index 6c0ba76b..a98e885f 100755 --- a/lib/buildFileList.sh +++ b/lib/buildFileList.sh @@ -10,522 +10,522 @@ ################################################################################ #### Function BuildFileList #################################################### function BuildFileList() { - # Need to build a list of all files changed - # This can be pulled from the GITHUB_EVENT_PATH payload + # Need to build a list of all files changed + # This can be pulled from the GITHUB_EVENT_PATH payload - ################ - # print header # - ################ - debug "----------------------------------------------" - debug "Pulling in code history and branches..." + ################ + # print header # + ################ + debug "----------------------------------------------" + debug "Pulling in code history and branches..." - ################################################################################# - # Switch codebase back to the default branch to get a list of all files changed # - ################################################################################# - SWITCH_CMD=$( - git -C "${GITHUB_WORKSPACE}" pull --quiet - git -C "${GITHUB_WORKSPACE}" checkout "${DEFAULT_BRANCH}" 2>&1 - ) + ################################################################################# + # Switch codebase back to the default branch to get a list of all files changed # + ################################################################################# + SWITCH_CMD=$( + git -C "${GITHUB_WORKSPACE}" pull --quiet + git -C "${GITHUB_WORKSPACE}" checkout "${DEFAULT_BRANCH}" 2>&1 + ) - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - # Error - info "Failed to switch to ${DEFAULT_BRANCH} branch to get files changed!" - fatal "[${SWITCH_CMD}]" - fi - - ################ - # print header # - ################ - debug "----------------------------------------------" - debug "Generating Diff with:[git diff --name-only '${DEFAULT_BRANCH}..${GITHUB_SHA}' --diff-filter=d]" - - ################################################# - # Get the Array of files changed in the commits # - ################################################# - mapfile -t RAW_FILE_ARRAY < <(git -C "${GITHUB_WORKSPACE}" diff --name-only "${DEFAULT_BRANCH}..${GITHUB_SHA}" --diff-filter=d 2>&1) - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - # Error - error "Failed to gain a list of all files changed!" - fatal "[${RAW_FILE_ARRAY[*]}]" - fi - - ################################################ - # Iterate through the array of all files found # - ################################################ - info "----------------------------------------------" - info "Files that have been modified in the commit(s):" - for FILE in "${RAW_FILE_ARRAY[@]}"; do - ########################### - # Get the files extension # - ########################### - # Extract just the file extension - FILE_TYPE=${FILE##*.} - # To lowercase - FILE_TYPE=${FILE_TYPE,,} - - ############## - # Print file # - ############## - info "File:[${FILE}], File_type:[${FILE_TYPE}]" - - ######### - # DEBUG # - ######### - debug "FILE_TYPE:[${FILE_TYPE}]" - - ################################ - # Get the CLOUDFORMATION files # - ################################ - if [ "${FILE_TYPE}" == "yml" ] || [ "${FILE_TYPE}" == "yaml" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_YAML+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - - ##################################### - # Check if the file is CFN template # - ##################################### - if DetectCloudFormationFile "${FILE}"; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_CLOUDFORMATION+=("${FILE}") - - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - fi - ###################### - # Get the JSON files # - ###################### - elif [ "${FILE_TYPE}" == "json" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_JSON+=("${FILE}") - ############################ - # Check if file is OpenAPI # - ############################ - if DetectOpenAPIFile "${FILE}"; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_OPENAPI+=("${FILE}") - fi - ############################ - # Check if file is ARM # - ############################ - if DetectARMFile "${FILE}"; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_ARM+=("${FILE}") - fi - ##################################### - # Check if the file is CFN template # - ##################################### - if DetectCloudFormationFile "${FILE}"; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_CLOUDFORMATION+=("${FILE}") - fi - ############################################ - # Check if the file is AWS States Language # - ############################################ - if DetectAWSStatesFIle "${FILE}"; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_STATES+=("${FILE}") - fi - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ##################### - # Get the XML files # - ##################### - elif [ "${FILE_TYPE}" == "xml" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_XML+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ########################## - # Get the MARKDOWN files # - ########################## - elif [ "${FILE_TYPE}" == "md" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_MARKDOWN+=("${FILE}") - ###################### - # Get the BASH files # - ###################### - elif [ "${FILE_TYPE}" == "sh" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_BASH+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ###################### - # Get the PERL files # - ###################### - elif [ "${FILE_TYPE}" == "pl" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_PERL+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ###################### - # Get the RAKU files # - ###################### - elif [ "${FILE_TYPE}" == "raku" ] || [ "${FILE_TYPE}" == "rakumod" ] \ - || [ "${FILE_TYPE}" == "rakutest" ] || [ "${FILE_TYPE}" == "pm6" ] \ - || [ "${FILE_TYPE}" == "pl6" ] || [ "${FILE_TYPE}" == "p6" ] ; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_RAKU+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ###################### - # Get the PHP files # - ###################### - elif [ "${FILE_TYPE}" == "php" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_PHP+=("${FILE}") - FILE_ARRAY_PHP_PHPSTAN+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ###################### - # Get the RUBY files # - ###################### - elif [ "${FILE_TYPE}" == "rb" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_RUBY+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ######################## - # Get the PYTHON files # - ######################## - elif [ "${FILE_TYPE}" == "py" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_PYTHON_PYLINT+=("${FILE}") - FILE_ARRAY_PYTHON_FLAKE8+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ######################## - # Get the COFFEE files # - ######################## - elif [ "${FILE_TYPE}" == "coffee" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_COFFEESCRIPT+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ############################ - # Get the JavaScript files # - ############################ - elif [ "${FILE_TYPE}" == "js" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_JAVASCRIPT_ES+=("${FILE}") - FILE_ARRAY_JAVASCRIPT_STANDARD+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ############################ - # Get the JSX files # - ############################ - elif [ "${FILE_TYPE}" == "jsx" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_JSX+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ############################ - # Get the TSX files # - ############################ - elif [ "${FILE_TYPE}" == "tsx" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_TSX+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ######################## - # Get the Golang files # - ######################## - ############################ - # Get the TypeScript files # - ############################ - elif [ "${FILE_TYPE}" == "ts" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_TYPESCRIPT_ES+=("${FILE}") - FILE_ARRAY_TYPESCRIPT_STANDARD+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ######################## - # Get the Golang files # - ######################## - elif [ "${FILE_TYPE}" == "go" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_GO+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ########################### - # Get the Terraform files # - ########################### - elif [ "${FILE_TYPE}" == "tf" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_TERRAFORM+=("${FILE}") - FILE_ARRAY_TERRAFORM_TERRASCAN+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ########################### - # Get the Powershell files # - ########################### - elif [ "${FILE_TYPE}" == "ps1" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_POWERSHELL+=("${FILE}") - elif [ "${FILE_TYPE}" == "css" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_CSS+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - elif [ "${FILE_TYPE}" == "env" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_ENV+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - elif [ "${FILE_TYPE}" == "kt" ] || [ "${FILE_TYPE}" == "kts" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_KOTLIN+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - elif [ "$FILE_TYPE" == "lua" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_LUA+=("$FILE") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - ############################ - # Get the Protocol Buffers files # - ############################ - elif [ "${FILE_TYPE}" == "dart" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_DART+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - elif [ "${FILE_TYPE}" == "proto" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_PROTOBUF+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - elif [ "${FILE}" == "dockerfile" ] || [ "${FILE_TYPE}" == "dockerfile" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_DOCKER+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - elif [ "${FILE_TYPE}" == "clj" ] || [ "${FILE_TYPE}" == "cljs" ] || [ "${FILE_TYPE}" == "cljc" ] || [ "${FILE_TYPE}" == "edn" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_CLOJURE+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - elif [ "${FILE_TYPE}" == "html" ]; then - ################################ - # Append the file to the array # - ##############################p## - FILE_ARRAY_HTML+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - elif [ "$FILE_TYPE" == "groovy" ] || [ "$FILE_TYPE" == "jenkinsfile" ] || [ "$FILE_TYPE" == "gradle" ]; then - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_GROOVY+=("$FILE") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - else - ############################################## - # Use file to see if we can parse what it is # - ############################################## - GET_FILE_TYPE_CMD=$(file "${FILE}" 2>&1) - - ################# - # Check if bash # - ################# - if [[ ${GET_FILE_TYPE_CMD} == *"Bourne-Again shell script"* ]]; then - ####################### - # It is a bash script # - ####################### - warn "Found bash script without extension:[.sh]" - info "Please update file with proper extensions." - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_BASH+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - elif [[ ${GET_FILE_TYPE_CMD} == *"Ruby script"* ]]; then - ####################### - # It is a Ruby script # - ####################### - warn "Found ruby script without extension:[.rb]" - info "Please update file with proper extensions." - ################################ - # Append the file to the array # - ################################ - FILE_ARRAY_RUBY+=("${FILE}") - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - else - ############################ - # Extension was not found! # - ############################ - warn "Failed to get filetype for:[${FILE}]!" - ########################################################## - # Set the READ_ONLY_CHANGE_FLAG since this could be exec # - ########################################################## - READ_ONLY_CHANGE_FLAG=1 - fi + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + # Error + info "Failed to switch to ${DEFAULT_BRANCH} branch to get files changed!" + fatal "[${SWITCH_CMD}]" fi - done - export READ_ONLY_CHANGE_FLAG # Workaround SC2034 + ################ + # print header # + ################ + debug "----------------------------------------------" + debug "Generating Diff with:[git diff --name-only '${DEFAULT_BRANCH}..${GITHUB_SHA}' --diff-filter=d]" - ######################################### - # Need to switch back to branch of code # - ######################################### - SWITCH2_CMD=$(git -C "${GITHUB_WORKSPACE}" checkout --progress --force "${GITHUB_SHA}" 2>&1) + ################################################# + # Get the Array of files changed in the commits # + ################################################# + mapfile -t RAW_FILE_ARRAY < <(git -C "${GITHUB_WORKSPACE}" diff --name-only "${DEFAULT_BRANCH}..${GITHUB_SHA}" --diff-filter=d 2>&1) - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - # Error - error "Failed to switch back to branch!" - fatal "[${SWITCH2_CMD}]" - fi + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + # Error + error "Failed to gain a list of all files changed!" + fatal "[${RAW_FILE_ARRAY[*]}]" + fi - ################ - # Footer print # - ################ - info "----------------------------------------------" - info "Successfully gathered list of files..." + ################################################ + # Iterate through the array of all files found # + ################################################ + info "----------------------------------------------" + info "Files that have been modified in the commit(s):" + for FILE in "${RAW_FILE_ARRAY[@]}"; do + ########################### + # Get the files extension # + ########################### + # Extract just the file extension + FILE_TYPE=${FILE##*.} + # To lowercase + FILE_TYPE=${FILE_TYPE,,} + + ############## + # Print file # + ############## + info "File:[${FILE}], File_type:[${FILE_TYPE}]" + + ######### + # DEBUG # + ######### + debug "FILE_TYPE:[${FILE_TYPE}]" + + ################################ + # Get the CLOUDFORMATION files # + ################################ + if [ "${FILE_TYPE}" == "yml" ] || [ "${FILE_TYPE}" == "yaml" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_YAML+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + + ##################################### + # Check if the file is CFN template # + ##################################### + if DetectCloudFormationFile "${FILE}"; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_CLOUDFORMATION+=("${FILE}") + + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + fi + ###################### + # Get the JSON files # + ###################### + elif [ "${FILE_TYPE}" == "json" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_JSON+=("${FILE}") + ############################ + # Check if file is OpenAPI # + ############################ + if DetectOpenAPIFile "${FILE}"; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_OPENAPI+=("${FILE}") + fi + ############################ + # Check if file is ARM # + ############################ + if DetectARMFile "${FILE}"; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_ARM+=("${FILE}") + fi + ##################################### + # Check if the file is CFN template # + ##################################### + if DetectCloudFormationFile "${FILE}"; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_CLOUDFORMATION+=("${FILE}") + fi + ############################################ + # Check if the file is AWS States Language # + ############################################ + if DetectAWSStatesFIle "${FILE}"; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_STATES+=("${FILE}") + fi + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ##################### + # Get the XML files # + ##################### + elif [ "${FILE_TYPE}" == "xml" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_XML+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ########################## + # Get the MARKDOWN files # + ########################## + elif [ "${FILE_TYPE}" == "md" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_MARKDOWN+=("${FILE}") + ###################### + # Get the BASH files # + ###################### + elif [ "${FILE_TYPE}" == "sh" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_BASH+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ###################### + # Get the PERL files # + ###################### + elif [ "${FILE_TYPE}" == "pl" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_PERL+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ###################### + # Get the RAKU files # + ###################### + elif [ "${FILE_TYPE}" == "raku" ] || [ "${FILE_TYPE}" == "rakumod" ] || + [ "${FILE_TYPE}" == "rakutest" ] || [ "${FILE_TYPE}" == "pm6" ] || + [ "${FILE_TYPE}" == "pl6" ] || [ "${FILE_TYPE}" == "p6" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_RAKU+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ###################### + # Get the PHP files # + ###################### + elif [ "${FILE_TYPE}" == "php" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_PHP+=("${FILE}") + FILE_ARRAY_PHP_PHPSTAN+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ###################### + # Get the RUBY files # + ###################### + elif [ "${FILE_TYPE}" == "rb" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_RUBY+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ######################## + # Get the PYTHON files # + ######################## + elif [ "${FILE_TYPE}" == "py" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_PYTHON_PYLINT+=("${FILE}") + FILE_ARRAY_PYTHON_FLAKE8+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ######################## + # Get the COFFEE files # + ######################## + elif [ "${FILE_TYPE}" == "coffee" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_COFFEESCRIPT+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ############################ + # Get the JavaScript files # + ############################ + elif [ "${FILE_TYPE}" == "js" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_JAVASCRIPT_ES+=("${FILE}") + FILE_ARRAY_JAVASCRIPT_STANDARD+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ############################ + # Get the JSX files # + ############################ + elif [ "${FILE_TYPE}" == "jsx" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_JSX+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ############################ + # Get the TSX files # + ############################ + elif [ "${FILE_TYPE}" == "tsx" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_TSX+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ######################## + # Get the Golang files # + ######################## + ############################ + # Get the TypeScript files # + ############################ + elif [ "${FILE_TYPE}" == "ts" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_TYPESCRIPT_ES+=("${FILE}") + FILE_ARRAY_TYPESCRIPT_STANDARD+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ######################## + # Get the Golang files # + ######################## + elif [ "${FILE_TYPE}" == "go" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_GO+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ########################### + # Get the Terraform files # + ########################### + elif [ "${FILE_TYPE}" == "tf" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_TERRAFORM+=("${FILE}") + FILE_ARRAY_TERRAFORM_TERRASCAN+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ########################### + # Get the Powershell files # + ########################### + elif [ "${FILE_TYPE}" == "ps1" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_POWERSHELL+=("${FILE}") + elif [ "${FILE_TYPE}" == "css" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_CSS+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + elif [ "${FILE_TYPE}" == "env" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_ENV+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + elif [ "${FILE_TYPE}" == "kt" ] || [ "${FILE_TYPE}" == "kts" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_KOTLIN+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + elif [ "$FILE_TYPE" == "lua" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_LUA+=("$FILE") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ############################ + # Get the Protocol Buffers files # + ############################ + elif [ "${FILE_TYPE}" == "dart" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_DART+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + elif [ "${FILE_TYPE}" == "proto" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_PROTOBUF+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + elif [ "${FILE}" == "dockerfile" ] || [ "${FILE_TYPE}" == "dockerfile" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_DOCKER+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + elif [ "${FILE_TYPE}" == "clj" ] || [ "${FILE_TYPE}" == "cljs" ] || [ "${FILE_TYPE}" == "cljc" ] || [ "${FILE_TYPE}" == "edn" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_CLOJURE+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + elif [ "${FILE_TYPE}" == "html" ]; then + ################################ + # Append the file to the array # + ##############################p## + FILE_ARRAY_HTML+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + elif [ "$FILE_TYPE" == "groovy" ] || [ "$FILE_TYPE" == "jenkinsfile" ] || [ "$FILE_TYPE" == "gradle" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_GROOVY+=("$FILE") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + else + ############################################## + # Use file to see if we can parse what it is # + ############################################## + GET_FILE_TYPE_CMD=$(file "${FILE}" 2>&1) + + ################# + # Check if bash # + ################# + if [[ ${GET_FILE_TYPE_CMD} == *"Bourne-Again shell script"* ]]; then + ####################### + # It is a bash script # + ####################### + warn "Found bash script without extension:[.sh]" + info "Please update file with proper extensions." + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_BASH+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + elif [[ ${GET_FILE_TYPE_CMD} == *"Ruby script"* ]]; then + ####################### + # It is a Ruby script # + ####################### + warn "Found ruby script without extension:[.rb]" + info "Please update file with proper extensions." + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_RUBY+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + else + ############################ + # Extension was not found! # + ############################ + warn "Failed to get filetype for:[${FILE}]!" + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + fi + fi + done + + export READ_ONLY_CHANGE_FLAG # Workaround SC2034 + + ######################################### + # Need to switch back to branch of code # + ######################################### + SWITCH2_CMD=$(git -C "${GITHUB_WORKSPACE}" checkout --progress --force "${GITHUB_SHA}" 2>&1) + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + # Error + error "Failed to switch back to branch!" + fatal "[${SWITCH2_CMD}]" + fi + + ################ + # Footer print # + ################ + info "----------------------------------------------" + info "Successfully gathered list of files..." } diff --git a/lib/linter.sh b/lib/linter.sh index 062c2df8..e106dd0e 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -22,103 +22,103 @@ source /action/lib/worker.sh # Source the function script(s) # GLOBALS # ########### # Default Vars -DEFAULT_RULES_LOCATION='/action/lib/.automation' # Default rules files location +DEFAULT_RULES_LOCATION='/action/lib/.automation' # Default rules files location # Ansible Vars -ANSIBLE_FILE_NAME='.ansible-lint.yml' # Name of the file -ANSIBLE_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${ANSIBLE_FILE_NAME}" # Path to the Ansible lint rules +ANSIBLE_FILE_NAME='.ansible-lint.yml' # Name of the file +ANSIBLE_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${ANSIBLE_FILE_NAME}" # Path to the Ansible lint rules # Azure Resource Manager Vars -ARM_FILE_NAME='.arm-ttk.psd1' # Name of the file -ARM_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${ARM_FILE_NAME}" # Path to the ARM lint rules +ARM_FILE_NAME='.arm-ttk.psd1' # Name of the file +ARM_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${ARM_FILE_NAME}" # Path to the ARM lint rules # Cloudformation Vars -CLOUDFORMATION_FILE_NAME='.cfnlintrc.yml' # Name of the file -CLOUDFORMATION_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${CLOUDFORMATION_FILE_NAME}" # Path to the cloudformation lint rules +CLOUDFORMATION_FILE_NAME='.cfnlintrc.yml' # Name of the file +CLOUDFORMATION_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${CLOUDFORMATION_FILE_NAME}" # Path to the cloudformation lint rules # Clojure Vars -CLOJURE_FILE_NAME='.clj-kondo/config.edn' # Name of the file -CLOJURE_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${CLOJURE_FILE_NAME}" # Path to the Clojure lint rules +CLOJURE_FILE_NAME='.clj-kondo/config.edn' # Name of the file +CLOJURE_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${CLOJURE_FILE_NAME}" # Path to the Clojure lint rules # Coffee Vars -COFFEE_FILE_NAME='.coffee-lint.json' # Name of the file -COFFEESCRIPT_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${COFFEE_FILE_NAME}" # Path to the coffeescript lint rules +COFFEE_FILE_NAME='.coffee-lint.json' # Name of the file +COFFEESCRIPT_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${COFFEE_FILE_NAME}" # Path to the coffeescript lint rules # CSS Vars -CSS_FILE_NAME='.stylelintrc.json' # Name of the file -CSS_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${CSS_FILE_NAME}" # Path to the CSS lint rules +CSS_FILE_NAME='.stylelintrc.json' # Name of the file +CSS_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${CSS_FILE_NAME}" # Path to the CSS lint rules # Dart Vars -DART_FILE_NAME='analysis_options.yaml' # Name of the file -DART_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${DART_FILE_NAME}" # Path to the DART lint rules +DART_FILE_NAME='analysis_options.yaml' # Name of the file +DART_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${DART_FILE_NAME}" # Path to the DART lint rules # Docker Vars -DOCKER_FILE_NAME='.dockerfilelintrc' # Name of the file -DOCKER_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${DOCKER_FILE_NAME}" # Path to the Docker lint rules +DOCKER_FILE_NAME='.dockerfilelintrc' # Name of the file +DOCKER_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${DOCKER_FILE_NAME}" # Path to the Docker lint rules # Golang Vars -GO_FILE_NAME='.golangci.yml' # Name of the file -GO_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${GO_FILE_NAME}" # Path to the Go lint rules +GO_FILE_NAME='.golangci.yml' # Name of the file +GO_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${GO_FILE_NAME}" # Path to the Go lint rules # Groovy Vars -GROOVY_FILE_NAME='.groovylintrc.json' # Name of the file -GROOVY_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${GROOVY_FILE_NAME}" # Path to the Groovy lint rules +GROOVY_FILE_NAME='.groovylintrc.json' # Name of the file +GROOVY_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${GROOVY_FILE_NAME}" # Path to the Groovy lint rules # HTML Vars -HTML_FILE_NAME='.htmlhintrc' # Name of the file -HTML_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${HTML_FILE_NAME}" # Path to the CSS lint rules +HTML_FILE_NAME='.htmlhintrc' # Name of the file +HTML_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${HTML_FILE_NAME}" # Path to the CSS lint rules # Javascript Vars -JAVASCRIPT_FILE_NAME="${JAVASCRIPT_ES_CONFIG_FILE:-.eslintrc.yml}" # Name of the file -JAVASCRIPT_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${JAVASCRIPT_FILE_NAME}" # Path to the Javascript lint rules -JAVASCRIPT_STANDARD_LINTER_RULES='' # ENV string to pass when running js standard +JAVASCRIPT_FILE_NAME="${JAVASCRIPT_ES_CONFIG_FILE:-.eslintrc.yml}" # Name of the file +JAVASCRIPT_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${JAVASCRIPT_FILE_NAME}" # Path to the Javascript lint rules +JAVASCRIPT_STANDARD_LINTER_RULES='' # ENV string to pass when running js standard # Default linter path -LINTER_RULES_PATH="${LINTER_RULES_PATH:-.github/linters}" # Linter Path Directory +LINTER_RULES_PATH="${LINTER_RULES_PATH:-.github/linters}" # Linter Path Directory # Lua Vars -LUA_FILE_NAME='.luacheckrc' # Name of the file -LUA_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${LUA_FILE_NAME}" # Path to the Lua lint rules +LUA_FILE_NAME='.luacheckrc' # Name of the file +LUA_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${LUA_FILE_NAME}" # Path to the Lua lint rules # MD Vars -MARKDOWN_FILE_NAME='.markdown-lint.yml' # Name of the file -MARKDOWN_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${MARKDOWN_FILE_NAME}" # Path to the markdown lint rules +MARKDOWN_FILE_NAME='.markdown-lint.yml' # Name of the file +MARKDOWN_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${MARKDOWN_FILE_NAME}" # Path to the markdown lint rules # OpenAPI Vars -OPENAPI_FILE_NAME='.openapirc.yml' # Name of the file -OPENAPI_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${OPENAPI_FILE_NAME}" # Path to the OpenAPI lint rules +OPENAPI_FILE_NAME='.openapirc.yml' # Name of the file +OPENAPI_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${OPENAPI_FILE_NAME}" # Path to the OpenAPI lint rules # PHP Vars -PHPSTAN_FILE_NAME='phpstan.neon' # Name of the file -PHPSTAN_LINTER_RULES="${GITHUB_WORKSPACE}/${PHPSTAN_FILE_NAME}" # Path to the PHPStan lint rules in the repository +PHPSTAN_FILE_NAME='phpstan.neon' # Name of the file +PHPSTAN_LINTER_RULES="${GITHUB_WORKSPACE}/${PHPSTAN_FILE_NAME}" # Path to the PHPStan lint rules in the repository if [ ! -f "$PHPSTAN_LINTER_RULES" ]; then - PHPSTAN_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PHPSTAN_FILE_NAME}" # Path to the PHPStan lint rules + PHPSTAN_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PHPSTAN_FILE_NAME}" # Path to the PHPStan lint rules fi # Powershell Vars -POWERSHELL_FILE_NAME='.powershell-psscriptanalyzer.psd1' # Name of the file -POWERSHELL_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${POWERSHELL_FILE_NAME}" # Path to the Powershell lint rules +POWERSHELL_FILE_NAME='.powershell-psscriptanalyzer.psd1' # Name of the file +POWERSHELL_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${POWERSHELL_FILE_NAME}" # Path to the Powershell lint rules # Protocol Buffers Vars -PROTOBUF_FILE_NAME='.protolintrc.yml' # Name of the file -PROTOBUF_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PROTOBUF_FILE_NAME}" # Path to the Protocol Buffers lint rules +PROTOBUF_FILE_NAME='.protolintrc.yml' # Name of the file +PROTOBUF_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PROTOBUF_FILE_NAME}" # Path to the Protocol Buffers lint rules # Python Vars -PYTHON_PYLINT_FILE_NAME="${PYTHON_PYLINT_CONFIG_FILE:-.python-lint}" # Name of the file -PYTHON_PYLINT_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PYTHON_PYLINT_FILE_NAME}" # Path to the python lint rules -PYTHON_FLAKE8_FILE_NAME="${PYTHON_FLAKE8_CONFIG_FILE:-.flake8}" # Name of the file -PYTHON_FLAKE8_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PYTHON_FLAKE8_FILE_NAME}" # Path to the python lint rules +PYTHON_PYLINT_FILE_NAME="${PYTHON_PYLINT_CONFIG_FILE:-.python-lint}" # Name of the file +PYTHON_PYLINT_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PYTHON_PYLINT_FILE_NAME}" # Path to the python lint rules +PYTHON_FLAKE8_FILE_NAME="${PYTHON_FLAKE8_CONFIG_FILE:-.flake8}" # Name of the file +PYTHON_FLAKE8_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PYTHON_FLAKE8_FILE_NAME}" # Path to the python lint rules # Ruby Vars -RUBY_FILE_NAME="${RUBY_CONFIG_FILE:-.ruby-lint.yml}" # Name of the file -RUBY_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${RUBY_FILE_NAME}" # Path to the ruby lint rules +RUBY_FILE_NAME="${RUBY_CONFIG_FILE:-.ruby-lint.yml}" # Name of the file +RUBY_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${RUBY_FILE_NAME}" # Path to the ruby lint rules # Terraform Vars -TERRAFORM_FILE_NAME='.tflint.hcl' # Name of the file -TERRAFORM_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${TERRAFORM_FILE_NAME}" # Path to the Terraform lint rules +TERRAFORM_FILE_NAME='.tflint.hcl' # Name of the file +TERRAFORM_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${TERRAFORM_FILE_NAME}" # Path to the Terraform lint rules # Typescript Vars -TYPESCRIPT_FILE_NAME="${TYPESCRIPT_ES_CONFIG_FILE:-.eslintrc.yml}" # Name of the file -TYPESCRIPT_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${TYPESCRIPT_FILE_NAME}" # Path to the Typescript lint rules -TYPESCRIPT_STANDARD_LINTER_RULES='' # ENV string to pass when running js standard +TYPESCRIPT_FILE_NAME="${TYPESCRIPT_ES_CONFIG_FILE:-.eslintrc.yml}" # Name of the file +TYPESCRIPT_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${TYPESCRIPT_FILE_NAME}" # Path to the Typescript lint rules +TYPESCRIPT_STANDARD_LINTER_RULES='' # ENV string to pass when running js standard # YAML Vars -YAML_FILE_NAME="${YAML_CONFIG_FILE:-.yaml-lint.yml}" # Name of the file -YAML_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${YAML_FILE_NAME}" # Path to the yaml lint rules +YAML_FILE_NAME="${YAML_CONFIG_FILE:-.yaml-lint.yml}" # Name of the file +YAML_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${YAML_FILE_NAME}" # Path to the yaml lint rules ####################################### # Linter array for information prints # ####################################### LINTER_ARRAY=('ansible-lint' 'arm-ttk' 'asl-validator' 'cfn-lint' 'clj-kondo' 'coffeelint' - 'dart' 'dockerfilelint' 'dotenv-linter' 'eslint' 'flake8' 'golangci-lint' 'htmlhint' - 'jsonlint' 'ktlint' 'lua' 'markdownlint' 'npm-groovy-lint' 'perl' 'protolint' 'pwsh' - 'pylint' 'raku' 'rubocop' 'shellcheck' 'spectral' 'standard' 'stylelint' 'terrascan' - 'tflint' 'xmllint' 'yamllint') + 'dart' 'dockerfilelint' 'dotenv-linter' 'eslint' 'flake8' 'golangci-lint' 'htmlhint' + 'jsonlint' 'ktlint' 'lua' 'markdownlint' 'npm-groovy-lint' 'perl' 'protolint' 'pwsh' + 'pylint' 'raku' 'rubocop' 'shellcheck' 'spectral' 'standard' 'stylelint' 'terrascan' + 'tflint' 'xmllint' 'yamllint') ############################# # Language array for prints # ############################# LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'CLOUDFORMATION' 'CLOJURE' 'COFFEESCRIPT' 'CSS' - 'DART' 'DOCKER' 'ENV' 'GO' 'GROOVY' 'HTML' 'JAVASCRIPT_ES' 'JAVASCRIPT_STANDARD' - 'JSON' 'JSX' 'KOTLIN' 'LUA' 'MARKDOWN' 'OPENAPI' 'PERL' 'PHP' 'PHP_PHPSTAN' 'POWERSHELL' - 'PROTOBUF' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'RAKU' 'RUBY' 'STATES' 'TERRAFORM' - 'TERRAFORM_TERRASCAN' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YML') + 'DART' 'DOCKER' 'ENV' 'GO' 'GROOVY' 'HTML' 'JAVASCRIPT_ES' 'JAVASCRIPT_STANDARD' + 'JSON' 'JSX' 'KOTLIN' 'LUA' 'MARKDOWN' 'OPENAPI' 'PERL' 'PHP' 'PHP_PHPSTAN' 'POWERSHELL' + 'PROTOBUF' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'RAKU' 'RUBY' 'STATES' 'TERRAFORM' + 'TERRAFORM_TERRASCAN' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YML') ############################################ # Array for all languages that were linted # @@ -128,58 +128,58 @@ LINTED_LANGUAGES_ARRAY=() # Will be filled at run time with all languages that w ################### # GitHub ENV Vars # ################### -ANSIBLE_DIRECTORY="${ANSIBLE_DIRECTORY}" # Ansible Directory -DEFAULT_BRANCH="${DEFAULT_BRANCH:-master}" # Default Git Branch to use (master by default) -DISABLE_ERRORS="${DISABLE_ERRORS}" # Boolean to enable warning-only output without throwing errors -GITHUB_EVENT_PATH="${GITHUB_EVENT_PATH}" # Github Event Path -GITHUB_REPOSITORY="${GITHUB_REPOSITORY}" # GitHub Org/Repo passed from system -GITHUB_RUN_ID="${GITHUB_RUN_ID}" # GitHub RUn ID to point to logs -GITHUB_SHA="${GITHUB_SHA}" # GitHub sha from the commit -GITHUB_TOKEN="${GITHUB_TOKEN}" # GitHub Token passed from environment -GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # Github Workspace -LOG_FILE="${LOG_FILE:-super-linter.log}" # Default log file name (located in GITHUB_WORKSPACE folder) -LOG_LEVEL="${LOG_LEVEL:-VERBOSE}" # Default log level (VERBOSE, DEBUG, TRACE) -MULTI_STATUS="${MULTI_STATUS:-true}" # Multiple status are created for each check ran -TEST_CASE_RUN="${TEST_CASE_RUN}" # Boolean to validate only test cases -VALIDATE_ALL_CODEBASE="${VALIDATE_ALL_CODEBASE}" # Boolean to validate all files -VALIDATE_ANSIBLE="${VALIDATE_ANSIBLE}" # Boolean to validate language -VALIDATE_ARM="${VALIDATE_ARM}" # Boolean to validate language -VALIDATE_BASH="${VALIDATE_BASH}" # Boolean to validate language -VALIDATE_CLOUDFORMATION="${VALIDATE_CLOUDFORMATION}" # Boolean to validate language -VALIDATE_CLOJURE="${VALIDATE_CLOJURE}" # Boolean to validate language -VALIDATE_COFFEE="${VALIDATE_COFFEE}" # Boolean to validate language -VALIDATE_CSS="${VALIDATE_CSS}" # Boolean to validate language -VALIDATE_DART="${VALIDATE_DART}" # Boolean to validate language -VALIDATE_DOCKER="${VALIDATE_DOCKER}" # Boolean to validate language -VALIDATE_EDITORCONFIG="${VALIDATE_EDITORCONFIG}" # Boolean to validate files with editorconfig -VALIDATE_ENV="${VALIDATE_ENV}" # Boolean to validate language -VALIDATE_GO="${VALIDATE_GO}" # Boolean to validate language -VALIDATE_GROOVY="${VALIDATE_GROOVY}" # Boolean to validate language -VALIDATE_HTML="${VALIDATE_HTML}" # Boolean to validate language -VALIDATE_JAVASCRIPT_ES="${VALIDATE_JAVASCRIPT_ES}" # Boolean to validate language -VALIDATE_JAVASCRIPT_STANDARD="${VALIDATE_JAVASCRIPT_STANDARD}" # Boolean to validate language -VALIDATE_JSON="${VALIDATE_JSON}" # Boolean to validate language -VALIDATE_JSX="${VALIDATE_JSX}" # Boolean to validate language -VALIDATE_KOTLIN="${VALIDATE_KOTLIN}" # Boolean to validate language -VALIDATE_LUA="${VALIDATE_LUA}" # Boolean to validate language -VALIDATE_MARKDOWN="${VALIDATE_MD:-}" # Boolean to validate language -VALIDATE_OPENAPI="${VALIDATE_OPENAPI}" # Boolean to validate language -VALIDATE_PERL="${VALIDATE_PERL}" # Boolean to validate language -VALIDATE_PHP="${VALIDATE_PHP}" # Boolean to validate language -VALIDATE_PHP_PHPSTAN="${VALIDATE_PHP_PHPSTAN}" # Boolean to validate language -VALIDATE_POWERSHELL="${VALIDATE_POWERSHELL}" # Boolean to validate language -VALIDATE_PYTHON_PYLINT="${VALIDATE_PYTHON:-$VALIDATE_PYTHON_PYLINT}" # Boolean to validate language -VALIDATE_PYTHON_FLAKE8="${VALIDATE_PYTHON_FLAKE8}" # Boolean to validate language -VALIDATE_RAKU="${VALIDATE_RAKU}" # Boolean to validate language -VALIDATE_RUBY="${VALIDATE_RUBY}" # Boolean to validate language -VALIDATE_STATES="${VALIDATE_STATES}" # Boolean to validate language -VALIDATE_TERRAFORM="${VALIDATE_TERRAFORM}" # Boolean to validate language -VALIDATE_TERRAFORM_TERRASCAN="${VALIDATE_TERRAFORM_TERRASCAN}" # Boolean to validate language -VALIDATE_TSX="${VALIDATE_TSX}" # Boolean to validate language -VALIDATE_TYPESCRIPT_ES="${VALIDATE_TYPESCRIPT_ES}" # Boolean to validate language -VALIDATE_TYPESCRIPT_STANDARD="${VALIDATE_TYPESCRIPT_STANDARD}" # Boolean to validate language -VALIDATE_XML="${VALIDATE_XML}" # Boolean to validate language -VALIDATE_YAML="${VALIDATE_YAML}" # Boolean to validate language +ANSIBLE_DIRECTORY="${ANSIBLE_DIRECTORY}" # Ansible Directory +DEFAULT_BRANCH="${DEFAULT_BRANCH:-master}" # Default Git Branch to use (master by default) +DISABLE_ERRORS="${DISABLE_ERRORS}" # Boolean to enable warning-only output without throwing errors +GITHUB_EVENT_PATH="${GITHUB_EVENT_PATH}" # Github Event Path +GITHUB_REPOSITORY="${GITHUB_REPOSITORY}" # GitHub Org/Repo passed from system +GITHUB_RUN_ID="${GITHUB_RUN_ID}" # GitHub RUn ID to point to logs +GITHUB_SHA="${GITHUB_SHA}" # GitHub sha from the commit +GITHUB_TOKEN="${GITHUB_TOKEN}" # GitHub Token passed from environment +GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # Github Workspace +LOG_FILE="${LOG_FILE:-super-linter.log}" # Default log file name (located in GITHUB_WORKSPACE folder) +LOG_LEVEL="${LOG_LEVEL:-VERBOSE}" # Default log level (VERBOSE, DEBUG, TRACE) +MULTI_STATUS="${MULTI_STATUS:-true}" # Multiple status are created for each check ran +TEST_CASE_RUN="${TEST_CASE_RUN}" # Boolean to validate only test cases +VALIDATE_ALL_CODEBASE="${VALIDATE_ALL_CODEBASE}" # Boolean to validate all files +VALIDATE_ANSIBLE="${VALIDATE_ANSIBLE}" # Boolean to validate language +VALIDATE_ARM="${VALIDATE_ARM}" # Boolean to validate language +VALIDATE_BASH="${VALIDATE_BASH}" # Boolean to validate language +VALIDATE_CLOUDFORMATION="${VALIDATE_CLOUDFORMATION}" # Boolean to validate language +VALIDATE_CLOJURE="${VALIDATE_CLOJURE}" # Boolean to validate language +VALIDATE_COFFEE="${VALIDATE_COFFEE}" # Boolean to validate language +VALIDATE_CSS="${VALIDATE_CSS}" # Boolean to validate language +VALIDATE_DART="${VALIDATE_DART}" # Boolean to validate language +VALIDATE_DOCKER="${VALIDATE_DOCKER}" # Boolean to validate language +VALIDATE_EDITORCONFIG="${VALIDATE_EDITORCONFIG}" # Boolean to validate files with editorconfig +VALIDATE_ENV="${VALIDATE_ENV}" # Boolean to validate language +VALIDATE_GO="${VALIDATE_GO}" # Boolean to validate language +VALIDATE_GROOVY="${VALIDATE_GROOVY}" # Boolean to validate language +VALIDATE_HTML="${VALIDATE_HTML}" # Boolean to validate language +VALIDATE_JAVASCRIPT_ES="${VALIDATE_JAVASCRIPT_ES}" # Boolean to validate language +VALIDATE_JAVASCRIPT_STANDARD="${VALIDATE_JAVASCRIPT_STANDARD}" # Boolean to validate language +VALIDATE_JSON="${VALIDATE_JSON}" # Boolean to validate language +VALIDATE_JSX="${VALIDATE_JSX}" # Boolean to validate language +VALIDATE_KOTLIN="${VALIDATE_KOTLIN}" # Boolean to validate language +VALIDATE_LUA="${VALIDATE_LUA}" # Boolean to validate language +VALIDATE_MARKDOWN="${VALIDATE_MD:-}" # Boolean to validate language +VALIDATE_OPENAPI="${VALIDATE_OPENAPI}" # Boolean to validate language +VALIDATE_PERL="${VALIDATE_PERL}" # Boolean to validate language +VALIDATE_PHP="${VALIDATE_PHP}" # Boolean to validate language +VALIDATE_PHP_PHPSTAN="${VALIDATE_PHP_PHPSTAN}" # Boolean to validate language +VALIDATE_POWERSHELL="${VALIDATE_POWERSHELL}" # Boolean to validate language +VALIDATE_PYTHON_PYLINT="${VALIDATE_PYTHON:-$VALIDATE_PYTHON_PYLINT}" # Boolean to validate language +VALIDATE_PYTHON_FLAKE8="${VALIDATE_PYTHON_FLAKE8}" # Boolean to validate language +VALIDATE_RAKU="${VALIDATE_RAKU}" # Boolean to validate language +VALIDATE_RUBY="${VALIDATE_RUBY}" # Boolean to validate language +VALIDATE_STATES="${VALIDATE_STATES}" # Boolean to validate language +VALIDATE_TERRAFORM="${VALIDATE_TERRAFORM}" # Boolean to validate language +VALIDATE_TERRAFORM_TERRASCAN="${VALIDATE_TERRAFORM_TERRASCAN}" # Boolean to validate language +VALIDATE_TSX="${VALIDATE_TSX}" # Boolean to validate language +VALIDATE_TYPESCRIPT_ES="${VALIDATE_TYPESCRIPT_ES}" # Boolean to validate language +VALIDATE_TYPESCRIPT_STANDARD="${VALIDATE_TYPESCRIPT_STANDARD}" # Boolean to validate language +VALIDATE_XML="${VALIDATE_XML}" # Boolean to validate language +VALIDATE_YAML="${VALIDATE_YAML}" # Boolean to validate language ############## # Debug Vars # @@ -190,37 +190,41 @@ ACTIONS_RUNNER_DEBUG="${ACTIONS_RUNNER_DEBUG:-false}" # Boolean to see even more ############ # Log Vars # ############ -LOG_TRACE=$(if [[ ${LOG_LEVEL} == "TRACE" ]]; then echo "true";fi) # Boolean to see trace logs +if [[ ${ACTIONS_RUNNER_DEBUG} == true ]]; then LOG_LEVEL="DEBUG"; fi +# Boolean to see trace logs +LOG_TRACE=$(if [[ ${LOG_LEVEL} == "TRACE" ]]; then echo "true"; fi) export LOG_TRACE -LOG_DEBUG=$(if [[ ${LOG_LEVEL} == "DEBUG" || ${ACTIONS_RUNNER_DEBUG} == true ]]; then echo "true";fi) # Boolean to see debug logs +# Boolean to see debug logs +LOG_DEBUG=$(if [[ ${LOG_LEVEL} == "DEBUG" || ${LOG_LEVEL} == "TRACE" ]]; then echo "true"; fi) export LOG_DEBUG -LOG_VERBOSE=$(if [[ ${LOG_LEVEL} == "VERBOSE" ]]; then echo "true";fi) # Boolean to see verbose logs (info function) +# Boolean to see verbose logs (info function) +LOG_VERBOSE=$(if [[ ${LOG_LEVEL} == "VERBOSE" || ${LOG_LEVEL} == "DEBUG" || ${LOG_LEVEL} == "TRACE" ]]; then echo "true"; fi) export LOG_VERBOSE ################ # Default Vars # ################ -DEFAULT_VALIDATE_ALL_CODEBASE='true' # Default value for validate all files -DEFAULT_WORKSPACE="${DEFAULT_WORKSPACE:-/tmp/lint}" # Default workspace if running locally -DEFAULT_RUN_LOCAL='false' # Default value for debugging locally -DEFAULT_TEST_CASE_RUN='false' # Flag to tell code to run only test cases -DEFAULT_IFS="${IFS}" # Get the Default IFS for updating +DEFAULT_VALIDATE_ALL_CODEBASE='true' # Default value for validate all files +DEFAULT_WORKSPACE="${DEFAULT_WORKSPACE:-/tmp/lint}" # Default workspace if running locally +DEFAULT_RUN_LOCAL='false' # Default value for debugging locally +DEFAULT_TEST_CASE_RUN='false' # Flag to tell code to run only test cases +DEFAULT_IFS="${IFS}" # Get the Default IFS for updating ############################################################### # Default Vars that are called in Subs and need to be ignored # ############################################################### -DEFAULT_DISABLE_ERRORS='false' # Default to enabling errors -export DEFAULT_DISABLE_ERRORS # Workaround SC2034 -RAW_FILE_ARRAY=() # Array of all files that were changed -export RAW_FILE_ARRAY # Workaround SC2034 -READ_ONLY_CHANGE_FLAG=0 # Flag set to 1 if files changed are not txt or md -export READ_ONLY_CHANGE_FLAG # Workaround SC2034 -TEST_CASE_FOLDER='.automation/test' # Folder for test cases we should always ignore -export TEST_CASE_FOLDER # Workaround SC2034 -DEFAULT_ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}/ansible" # Default Ansible Directory -export DEFAULT_ANSIBLE_DIRECTORY # Workaround SC2034 -WARNING_ARRAY_TEST=() # Array of warning linters that did not have an expected test result. -export WARNING_ARRAY_TEST # Workaround SC2034 +DEFAULT_DISABLE_ERRORS='false' # Default to enabling errors +export DEFAULT_DISABLE_ERRORS # Workaround SC2034 +RAW_FILE_ARRAY=() # Array of all files that were changed +export RAW_FILE_ARRAY # Workaround SC2034 +READ_ONLY_CHANGE_FLAG=0 # Flag set to 1 if files changed are not txt or md +export READ_ONLY_CHANGE_FLAG # Workaround SC2034 +TEST_CASE_FOLDER='.automation/test' # Folder for test cases we should always ignore +export TEST_CASE_FOLDER # Workaround SC2034 +DEFAULT_ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}/ansible" # Default Ansible Directory +export DEFAULT_ANSIBLE_DIRECTORY # Workaround SC2034 +WARNING_ARRAY_TEST=() # Array of warning linters that did not have an expected test result. +export WARNING_ARRAY_TEST # Workaround SC2034 ############## # Format # @@ -356,561 +360,133 @@ export ERRORS_FOUND_YAML # Workaround SC2034 ################################################################################ #### Function Header ########################################################### Header() { - ############################### - # Give them the possum action # - ############################### - /bin/bash /action/lib/possum.sh + ############################### + # Give them the possum action # + ############################### + /bin/bash /action/lib/possum.sh - ########## - # Prints # - ########## - info "---------------------------------------------" - info "--- GitHub Actions Multi Language Linter ----" - info "---------------------------------------------" - info "---------------------------------------------" - info "The Super-Linter source code can be found at:" - info " - https://github.com/github/super-linter" - info "---------------------------------------------" + ########## + # Prints # + ########## + info "---------------------------------------------" + info "--- GitHub Actions Multi Language Linter ----" + info "---------------------------------------------" + info "---------------------------------------------" + info "The Super-Linter source code can be found at:" + info " - https://github.com/github/super-linter" + info "---------------------------------------------" } ################################################################################ #### Function GetLinterVersions ################################################ GetLinterVersions() { - ######################### - # Print version headers # - ######################### - debug "---------------------------------------------" - debug "Linter Version Info:" + ######################### + # Print version headers # + ######################### + debug "---------------------------------------------" + debug "Linter Version Info:" - ########################################################## - # Go through the array of linters and print version info # - ########################################################## - for LINTER in "${LINTER_ARRAY[@]}"; do - #################### - # Get the versions # - #################### - if [[ "${LINTER}" == "arm-ttk" ]]; then - # Need specific command for ARM - mapfile -t GET_VERSION_CMD < <(grep -iE 'version' "${ARM_TTK_PSD1}" | xargs 2>&1) - elif [[ "${LINTER}" == "protolint" ]]; then - # Need specific command for Protolint - mapfile -t GET_VERSION_CMD < <(echo "--version not supported") - else - # Standard version command - mapfile -t GET_VERSION_CMD < <("${LINTER}" --version 2>&1) - fi + ########################################################## + # Go through the array of linters and print version info # + ########################################################## + for LINTER in "${LINTER_ARRAY[@]}"; do + #################### + # Get the versions # + #################### + if [[ ${LINTER} == "arm-ttk" ]]; then + # Need specific command for ARM + mapfile -t GET_VERSION_CMD < <(grep -iE 'version' "${ARM_TTK_PSD1}" | xargs 2>&1) + elif [[ ${LINTER} == "protolint" ]]; then + # Need specific command for Protolint + mapfile -t GET_VERSION_CMD < <(echo "--version not supported") + else + # Standard version command + mapfile -t GET_VERSION_CMD < <("${LINTER}" --version 2>&1) + fi - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ] || [ -z "${GET_VERSION_CMD[*]}" ]; then - warn "[${LINTER}]: Failed to get version info for:" - else - ########################## - # Print the version info # - ########################## - debug "Successfully found version for ${F[W]}[${LINTER}]${F[B]}: ${F[W]}${GET_VERSION_CMD[*]}" - fi - done + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ] || [ -z "${GET_VERSION_CMD[*]}" ]; then + warn "[${LINTER}]: Failed to get version info for:" + else + ########################## + # Print the version info # + ########################## + debug "Successfully found version for ${F[W]}[${LINTER}]${F[B]}: ${F[W]}${GET_VERSION_CMD[*]}" + fi + done - ######################### - # Print version footers # - ######################### - debug "---------------------------------------------" + ######################### + # Print version footers # + ######################### + debug "---------------------------------------------" } ################################################################################ #### Function GetLinterRules ################################################### GetLinterRules() { - # Need to validate the rules files exist + # Need to validate the rules files exist - ################ - # Pull in vars # - ################ - LANGUAGE_NAME="${1}" # Name of the language were looking for + ################ + # Pull in vars # + ################ + LANGUAGE_NAME="${1}" # Name of the language were looking for - ####################################################### - # Need to create the variables for the real variables # - ####################################################### - LANGUAGE_FILE_NAME="${LANGUAGE_NAME}_FILE_NAME" - LANGUAGE_LINTER_RULES="${LANGUAGE_NAME}_LINTER_RULES" + ####################################################### + # Need to create the variables for the real variables # + ####################################################### + LANGUAGE_FILE_NAME="${LANGUAGE_NAME}_FILE_NAME" + LANGUAGE_LINTER_RULES="${LANGUAGE_NAME}_LINTER_RULES" - ##################################### - # Validate we have the linter rules # - ##################################### - if [ -f "${GITHUB_WORKSPACE}/${LINTER_RULES_PATH}/${!LANGUAGE_FILE_NAME}" ]; then - info "----------------------------------------------" - info "User provided file:[${!LANGUAGE_FILE_NAME}], setting rules file..." + ##################################### + # Validate we have the linter rules # + ##################################### + if [ -f "${GITHUB_WORKSPACE}/${LINTER_RULES_PATH}/${!LANGUAGE_FILE_NAME}" ]; then + info "----------------------------------------------" + info "User provided file:[${!LANGUAGE_FILE_NAME}], setting rules file..." - ######################################## - # Update the path to the file location # - ######################################## - eval "${LANGUAGE_LINTER_RULES}=${GITHUB_WORKSPACE}/${LINTER_RULES_PATH}/${!LANGUAGE_FILE_NAME}" - else - ######################################################## - # No user default provided, using the template default # - ######################################################## - debug " -> Codebase does NOT have file:[${LINTER_RULES_PATH}/${!LANGUAGE_FILE_NAME}], using Default rules at:[${!LANGUAGE_LINTER_RULES}]" - fi + ######################################## + # Update the path to the file location # + ######################################## + eval "${LANGUAGE_LINTER_RULES}=${GITHUB_WORKSPACE}/${LINTER_RULES_PATH}/${!LANGUAGE_FILE_NAME}" + else + ######################################################## + # No user default provided, using the template default # + ######################################################## + debug " -> Codebase does NOT have file:[${LINTER_RULES_PATH}/${!LANGUAGE_FILE_NAME}], using Default rules at:[${!LANGUAGE_LINTER_RULES}]" + fi } ################################################################################ #### Function GetStandardRules ################################################# GetStandardRules() { - ################ - # Pull In Vars # - ################ - LINTER="${1}" # Type: javascript | typescript + ################ + # Pull In Vars # + ################ + LINTER="${1}" # Type: javascript | typescript - ######################################################################### - # Need to get the ENV vars from the linter rules to run in command line # - ######################################################################### - # Copy orig IFS to var - ORIG_IFS="${IFS}" - # Set the IFS to newline - IFS=$'\n' + ######################################################################### + # Need to get the ENV vars from the linter rules to run in command line # + ######################################################################### + # Copy orig IFS to var + ORIG_IFS="${IFS}" + # Set the IFS to newline + IFS=$'\n' - ######################################### - # Get list of all environment variables # - ######################################### - # Only env vars that are marked as true - GET_ENV_ARRAY=() - if [[ ${LINTER} == "javascript" ]]; then - mapfile -t GET_ENV_ARRAY < <(yq .env "${JAVASCRIPT_LINTER_RULES}" | grep true) - elif [[ ${LINTER} == "typescript" ]]; then - mapfile -t GET_ENV_ARRAY < <(yq .env "${TYPESCRIPT_LINTER_RULES}" | grep true) - fi - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - # ERROR - error "Failed to gain list of ENV vars to load!" - fatal "[${GET_ENV_ARRAY[*]}]" - fi - - ########################## - # Set IFS back to normal # - ########################## - # Set IFS back to Orig - IFS="${ORIG_IFS}" - - ###################### - # Set the env string # - ###################### - ENV_STRING='' - - ############################# - # Pull out the envs to load # - ############################# - for ENV in "${GET_ENV_ARRAY[@]}"; do - ############################# - # remove spaces from return # - ############################# - ENV="$(echo -e "${ENV}" | tr -d '[:space:]')" - ################################ - # Get the env to add to string # - ################################ - ENV="$(echo "${ENV}" | cut -d'"' -f2)" - debug "ENV:[${ENV}]" - ENV_STRING+="--env ${ENV} " - done - - ######################################### - # Remove trailing and ending whitespace # - ######################################### - if [[ ${LINTER} == "javascript" ]]; then - JAVASCRIPT_STANDARD_LINTER_RULES="$(echo -e "${ENV_STRING}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" - elif [[ ${LINTER} == "typescript" ]]; then - TYPESCRIPT_STANDARD_LINTER_RULES="$(echo -e "${ENV_STRING}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" - fi -} -################################################################################ -#### Function DetectOpenAPIFile ################################################ -DetectOpenAPIFile() { - ################ - # Pull in vars # - ################ - FILE="${1}" - - ############################### - # Check the file for keywords # - ############################### - grep -E '"openapi":|"swagger":|^openapi:|^swagger:' "${FILE}" > /dev/null - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -eq 0 ]; then - ######################## - # Found string in file # - ######################## - return 0 - else - ################### - # No string match # - ################### - return 1 - fi -} -################################################################################ -#### Function DetectARMFile #################################################### -DetectARMFile() { - ################ - # Pull in vars # - ################ - FILE="${1}" # Name of the file/path we are validating - - ############################### - # Check the file for keywords # - ############################### - grep -E 'schema.management.azure.com' "${FILE}" > /dev/null - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -eq 0 ]; then - ######################## - # Found string in file # - ######################## - return 0 - else - ################### - # No string match # - ################### - return 1 - fi -} -################################################################################ -#### Function DetectCloudFormationFile ######################################### -DetectCloudFormationFile() { - ################ - # Pull in Vars # - ################ - FILE="${1}" # File that we need to validate - - # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-formats.html - # AWSTemplateFormatVersion is optional - ####################################### - # Check if file has AWS Template info # - ####################################### - if grep -q 'AWSTemplateFormatVersion' "${FILE}" > /dev/null; then - # Found it - return 0 - fi - - ##################################### - # See if it contains AWS References # - ##################################### - if grep -q -E '(AWS|Alexa|Custom)::' "${FILE}" > /dev/null; then - # Found it - return 0 - fi - - ##################################################### - # No identifiers of a CLOUDFORMATION template found # - ##################################################### - return 1 -} -################################################################################ -#### Function DetectAWSStatesFIle ############################################## -DetectAWSStatesFIle() { - ################ - # Pull in Vars # - ################ - FILE="${1}" # File that we need to validate - - # https://states-language.net/spec.html#example - ############################### - # check if file has resources # - ############################### - if grep '"Resource": *"arn"*' "${FILE}"; then - # Found it - return 0 - fi - - ################################################# - # No identifiers of a AWS States Language found # - ################################################# - return 1 -} -################################################################################ -#### Function GetGitHubVars #################################################### -GetGitHubVars() { - ########## - # Prints # - ########## - info "--------------------------------------------" - info "Gathering GitHub information..." - - ############################### - # Get the Run test cases flag # - ############################### - if [ -z "${TEST_CASE_RUN}" ]; then - ################################## - # No flag passed, set to default # - ################################## - TEST_CASE_RUN="${DEFAULT_TEST_CASE_RUN}" - fi - - ############################### - # Convert string to lowercase # - ############################### - TEST_CASE_RUN="${TEST_CASE_RUN,,}" - - ########################## - # Get the run local flag # - ########################## - if [ -z "${RUN_LOCAL}" ]; then - ################################## - # No flag passed, set to default # - ################################## - RUN_LOCAL="${DEFAULT_RUN_LOCAL}" - fi - - ############################### - # Convert string to lowercase # - ############################### - RUN_LOCAL="${RUN_LOCAL,,}" - - ################################# - # Check if were running locally # - ################################# - if [[ ${RUN_LOCAL} != "false" ]]; then - ########################################## - # We are running locally for a debug run # - ########################################## - info "NOTE: ENV VAR [RUN_LOCAL] has been set to:[true]" - info "bypassing GitHub Actions variables..." - - ############################ - # Set the GITHUB_WORKSPACE # - ############################ - if [ -z "${GITHUB_WORKSPACE}" ]; then - GITHUB_WORKSPACE="${DEFAULT_WORKSPACE}" + ######################################### + # Get list of all environment variables # + ######################################### + # Only env vars that are marked as true + GET_ENV_ARRAY=() + if [[ ${LINTER} == "javascript" ]]; then + mapfile -t GET_ENV_ARRAY < <(yq .env "${JAVASCRIPT_LINTER_RULES}" | grep true) + elif [[ ${LINTER} == "typescript" ]]; then + mapfile -t GET_ENV_ARRAY < <(yq .env "${TYPESCRIPT_LINTER_RULES}" | grep true) fi - if [ ! -d "${GITHUB_WORKSPACE}" ]; then - fatal "Provided volume is not a directory!" - fi - - info "Linting all files in mapped directory:[${DEFAULT_WORKSPACE}]" - - # No need to touch or set the GITHUB_SHA - # No need to touch or set the GITHUB_EVENT_PATH - # No need to touch or set the GITHUB_ORG - # No need to touch or set the GITHUB_REPO - - ################################# - # Set the VALIDATE_ALL_CODEBASE # - ################################# - VALIDATE_ALL_CODEBASE="${DEFAULT_VALIDATE_ALL_CODEBASE}" - else - ############################ - # Validate we have a value # - ############################ - if [ -z "${GITHUB_SHA}" ]; then - error "Failed to get [GITHUB_SHA]!" - fatal "[${GITHUB_SHA}]" - else - info "Successfully found:${F[W]}[GITHUB_SHA]${F[B]}, value:${F[W]}[${GITHUB_SHA}]" - fi - - ############################ - # Validate we have a value # - ############################ - if [ -z "${GITHUB_WORKSPACE}" ]; then - error "Failed to get [GITHUB_WORKSPACE]!" - fatal "[${GITHUB_WORKSPACE}]" - else - info "Successfully found:${F[W]}[GITHUB_WORKSPACE]${F[B]}, value:${F[W]}[${GITHUB_WORKSPACE}]" - fi - - ############################ - # Validate we have a value # - ############################ - if [ -z "${GITHUB_EVENT_PATH}" ]; then - error "Failed to get [GITHUB_EVENT_PATH]!" - fatal "[${GITHUB_EVENT_PATH}]" - else - info "Successfully found:${F[W]}[GITHUB_EVENT_PATH]${F[B]}, value:${F[W]}[${GITHUB_EVENT_PATH}]${F[B]}" - fi - - ################################################## - # Need to pull the GitHub Vars from the env file # - ################################################## - - ###################### - # Get the GitHub Org # - ###################### - GITHUB_ORG=$(jq -r '.repository.owner.login' < "${GITHUB_EVENT_PATH}") - - ############################ - # Validate we have a value # - ############################ - if [ -z "${GITHUB_ORG}" ]; then - error "Failed to get [GITHUB_ORG]!" - fatal "[${GITHUB_ORG}]" - else - info "Successfully found:${F[W]}[GITHUB_ORG]${F[B]}, value:${F[W]}[${GITHUB_ORG}]" - fi - - ####################### - # Get the GitHub Repo # - ####################### - GITHUB_REPO=$(jq -r '.repository.name' < "${GITHUB_EVENT_PATH}") - - ############################ - # Validate we have a value # - ############################ - if [ -z "${GITHUB_REPO}" ]; then - error "Failed to get [GITHUB_REPO]!" - fatal "[${GITHUB_REPO}]" - else - info "Successfully found:${F[W]}[GITHUB_REPO]${F[B]}, value:${F[W]}[${GITHUB_REPO}]" - fi - fi - - ############################ - # Validate we have a value # - ############################ - if [ -z "${GITHUB_TOKEN}" ] && [[ ${RUN_LOCAL} == "false" ]]; then - error "Failed to get [GITHUB_TOKEN]!" - error "[${GITHUB_TOKEN}]" - error "Please set a [GITHUB_TOKEN] from the main workflow environment to take advantage of multiple status reports!" - - ################################################################################ - # Need to set MULTI_STATUS to false as we cant hit API endpoints without token # - ################################################################################ - MULTI_STATUS='false' - else - info "Successfully found:${F[W]}[GITHUB_TOKEN]" - fi - - ############################### - # Convert string to lowercase # - ############################### - MULTI_STATUS="${MULTI_STATUS,,}" - - ####################################################################### - # Check to see if the multi status is set, and we have a token to use # - ####################################################################### - if [ "${MULTI_STATUS}" == "true" ] && [ -n "${GITHUB_TOKEN}" ]; then - ############################ - # Validate we have a value # - ############################ - if [ -z "${GITHUB_REPOSITORY}" ]; then - error "Failed to get [GITHUB_REPOSITORY]!" - fatal "[${GITHUB_REPOSITORY}]" - else - info "Successfully found:${F[W]}[GITHUB_REPOSITORY]${F[B]}, value:${F[W]}[${GITHUB_REPOSITORY}]" - fi - - ############################ - # Validate we have a value # - ############################ - if [ -z "${GITHUB_RUN_ID}" ]; then - error "Failed to get [GITHUB_RUN_ID]!" - fatal "[${GITHUB_RUN_ID}]" - else - info "Successfully found:${F[W]}[GITHUB_RUN_ID]${F[B]}, value:${F[W]}[${GITHUB_RUN_ID}]" - fi - fi -} -################################################################################ -#### Function ValidatePowershellModules ######################################## -function ValidatePowershellModules() { - VALIDATE_PSSA_MODULE=$(pwsh -c "(Get-Module -Name PSScriptAnalyzer -ListAvailable | Select-Object -First 1).Name" 2>&1) - # If module found, ensure Invoke-ScriptAnalyzer command is available - if [[ ${VALIDATE_PSSA_MODULE} == "PSScriptAnalyzer" ]]; then - VALIDATE_PSSA_CMD=$(pwsh -c "(Get-Command Invoke-ScriptAnalyzer | Select-Object -First 1).Name" 2>&1) - else - fatal "Failed to find module." - fi - - ######################################### - # validate we found the script analyzer # - ######################################### - if [[ ${VALIDATE_PSSA_CMD} != "Invoke-ScriptAnalyzer" ]]; then - fatal "Failed to find module." - fi - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - # Failed - error "Failed find module [PSScriptAnalyzer] for [${LINTER_NAME}] in system!" - fatal "[PSSA_MODULE ${VALIDATE_PSSA_MODULE}] [PSSA_CMD ${VALIDATE_PSSA_CMD}]" - else - # Success - debug "Successfully found module ${F[W]}[${VALIDATE_PSSA_MODULE}]${F[B]} in system" - debug "Successfully found command ${F[W]}[${VALIDATE_PSSA_CMD}]${F[B]} in system" - fi -} -################################################################################ -#### Function CallStatusAPI #################################################### -CallStatusAPI() { - #################### - # Pull in the vars # - #################### - LANGUAGE="${1}" # langauge that was validated - STATUS="${2}" # success | error - SUCCESS_MSG='No errors were found in the linting process' - FAIL_MSG='Errors were detected, please view logs' - MESSAGE='' # Message to send to status API - - ###################################### - # Check the status to create message # - ###################################### - if [ "${STATUS}" == "success" ]; then - # Success - MESSAGE="${SUCCESS_MSG}" - else - # Failure - MESSAGE="${FAIL_MSG}" - fi - - ########################################################## - # Check to see if were enabled for multi Status mesaages # - ########################################################## - if [ "${MULTI_STATUS}" == "true" ]; then - ############################################## - # Call the status API to create status check # - ############################################## - SEND_STATUS_CMD=$(curl -f -s -X POST \ - --url "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}" \ - -H 'accept: application/vnd.github.v3+json' \ - -H "authorization: Bearer ${GITHUB_TOKEN}" \ - -H 'content-type: application/json' \ - -d "{ \"state\": \"${STATUS}\", - \"target_url\": \"https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}\", - \"description\": \"${MESSAGE}\", \"context\": \"--> Linted: ${LANGUAGE}\" - }" 2>&1) - ####################### # Load the error code # ####################### @@ -919,116 +495,543 @@ CallStatusAPI() { ############################## # Check the shell for errors # ############################## - if [ "${ERROR_CODE}" -ne 0 ]; then - # ERROR - info "ERROR! Failed to call GitHub Status API!" - info "ERROR:[${SEND_STATUS_CMD}]" - # Not going to fail the script on this yet... + if [ ${ERROR_CODE} -ne 0 ]; then + # ERROR + error "Failed to gain list of ENV vars to load!" + fatal "[${GET_ENV_ARRAY[*]}]" + fi + + ########################## + # Set IFS back to normal # + ########################## + # Set IFS back to Orig + IFS="${ORIG_IFS}" + + ###################### + # Set the env string # + ###################### + ENV_STRING='' + + ############################# + # Pull out the envs to load # + ############################# + for ENV in "${GET_ENV_ARRAY[@]}"; do + ############################# + # remove spaces from return # + ############################# + ENV="$(echo -e "${ENV}" | tr -d '[:space:]')" + ################################ + # Get the env to add to string # + ################################ + ENV="$(echo "${ENV}" | cut -d'"' -f2)" + debug "ENV:[${ENV}]" + ENV_STRING+="--env ${ENV} " + done + + ######################################### + # Remove trailing and ending whitespace # + ######################################### + if [[ ${LINTER} == "javascript" ]]; then + JAVASCRIPT_STANDARD_LINTER_RULES="$(echo -e "${ENV_STRING}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" + elif [[ ${LINTER} == "typescript" ]]; then + TYPESCRIPT_STANDARD_LINTER_RULES="$(echo -e "${ENV_STRING}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" + fi +} +################################################################################ +#### Function DetectOpenAPIFile ################################################ +DetectOpenAPIFile() { + ################ + # Pull in vars # + ################ + FILE="${1}" + + ############################### + # Check the file for keywords # + ############################### + grep -E '"openapi":|"swagger":|^openapi:|^swagger:' "${FILE}" > /dev/null + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -eq 0 ]; then + ######################## + # Found string in file # + ######################## + return 0 + else + ################### + # No string match # + ################### + return 1 + fi +} +################################################################################ +#### Function DetectARMFile #################################################### +DetectARMFile() { + ################ + # Pull in vars # + ################ + FILE="${1}" # Name of the file/path we are validating + + ############################### + # Check the file for keywords # + ############################### + grep -E 'schema.management.azure.com' "${FILE}" > /dev/null + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -eq 0 ]; then + ######################## + # Found string in file # + ######################## + return 0 + else + ################### + # No string match # + ################### + return 1 + fi +} +################################################################################ +#### Function DetectCloudFormationFile ######################################### +DetectCloudFormationFile() { + ################ + # Pull in Vars # + ################ + FILE="${1}" # File that we need to validate + + # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-formats.html + # AWSTemplateFormatVersion is optional + ####################################### + # Check if file has AWS Template info # + ####################################### + if grep -q 'AWSTemplateFormatVersion' "${FILE}" > /dev/null; then + # Found it + return 0 + fi + + ##################################### + # See if it contains AWS References # + ##################################### + if grep -q -E '(AWS|Alexa|Custom)::' "${FILE}" > /dev/null; then + # Found it + return 0 + fi + + ##################################################### + # No identifiers of a CLOUDFORMATION template found # + ##################################################### + return 1 +} +################################################################################ +#### Function DetectAWSStatesFIle ############################################## +DetectAWSStatesFIle() { + ################ + # Pull in Vars # + ################ + FILE="${1}" # File that we need to validate + + # https://states-language.net/spec.html#example + ############################### + # check if file has resources # + ############################### + if grep '"Resource": *"arn"*' "${FILE}"; then + # Found it + return 0 + fi + + ################################################# + # No identifiers of a AWS States Language found # + ################################################# + return 1 +} +################################################################################ +#### Function GetGitHubVars #################################################### +GetGitHubVars() { + ########## + # Prints # + ########## + info "--------------------------------------------" + info "Gathering GitHub information..." + + ############################### + # Get the Run test cases flag # + ############################### + if [ -z "${TEST_CASE_RUN}" ]; then + ################################## + # No flag passed, set to default # + ################################## + TEST_CASE_RUN="${DEFAULT_TEST_CASE_RUN}" + fi + + ############################### + # Convert string to lowercase # + ############################### + TEST_CASE_RUN="${TEST_CASE_RUN,,}" + + ########################## + # Get the run local flag # + ########################## + if [ -z "${RUN_LOCAL}" ]; then + ################################## + # No flag passed, set to default # + ################################## + RUN_LOCAL="${DEFAULT_RUN_LOCAL}" + fi + + ############################### + # Convert string to lowercase # + ############################### + RUN_LOCAL="${RUN_LOCAL,,}" + + ################################# + # Check if were running locally # + ################################# + if [[ ${RUN_LOCAL} != "false" ]]; then + ########################################## + # We are running locally for a debug run # + ########################################## + info "NOTE: ENV VAR [RUN_LOCAL] has been set to:[true]" + info "bypassing GitHub Actions variables..." + + ############################ + # Set the GITHUB_WORKSPACE # + ############################ + if [ -z "${GITHUB_WORKSPACE}" ]; then + GITHUB_WORKSPACE="${DEFAULT_WORKSPACE}" + fi + + if [ ! -d "${GITHUB_WORKSPACE}" ]; then + fatal "Provided volume is not a directory!" + fi + + info "Linting all files in mapped directory:[${DEFAULT_WORKSPACE}]" + + # No need to touch or set the GITHUB_SHA + # No need to touch or set the GITHUB_EVENT_PATH + # No need to touch or set the GITHUB_ORG + # No need to touch or set the GITHUB_REPO + + ################################# + # Set the VALIDATE_ALL_CODEBASE # + ################################# + VALIDATE_ALL_CODEBASE="${DEFAULT_VALIDATE_ALL_CODEBASE}" + else + ############################ + # Validate we have a value # + ############################ + if [ -z "${GITHUB_SHA}" ]; then + error "Failed to get [GITHUB_SHA]!" + fatal "[${GITHUB_SHA}]" + else + info "Successfully found:${F[W]}[GITHUB_SHA]${F[B]}, value:${F[W]}[${GITHUB_SHA}]" + fi + + ############################ + # Validate we have a value # + ############################ + if [ -z "${GITHUB_WORKSPACE}" ]; then + error "Failed to get [GITHUB_WORKSPACE]!" + fatal "[${GITHUB_WORKSPACE}]" + else + info "Successfully found:${F[W]}[GITHUB_WORKSPACE]${F[B]}, value:${F[W]}[${GITHUB_WORKSPACE}]" + fi + + ############################ + # Validate we have a value # + ############################ + if [ -z "${GITHUB_EVENT_PATH}" ]; then + error "Failed to get [GITHUB_EVENT_PATH]!" + fatal "[${GITHUB_EVENT_PATH}]" + else + info "Successfully found:${F[W]}[GITHUB_EVENT_PATH]${F[B]}, value:${F[W]}[${GITHUB_EVENT_PATH}]${F[B]}" + fi + + ################################################## + # Need to pull the GitHub Vars from the env file # + ################################################## + + ###################### + # Get the GitHub Org # + ###################### + GITHUB_ORG=$(jq -r '.repository.owner.login' < "${GITHUB_EVENT_PATH}") + + ############################ + # Validate we have a value # + ############################ + if [ -z "${GITHUB_ORG}" ]; then + error "Failed to get [GITHUB_ORG]!" + fatal "[${GITHUB_ORG}]" + else + info "Successfully found:${F[W]}[GITHUB_ORG]${F[B]}, value:${F[W]}[${GITHUB_ORG}]" + fi + + ####################### + # Get the GitHub Repo # + ####################### + GITHUB_REPO=$(jq -r '.repository.name' < "${GITHUB_EVENT_PATH}") + + ############################ + # Validate we have a value # + ############################ + if [ -z "${GITHUB_REPO}" ]; then + error "Failed to get [GITHUB_REPO]!" + fatal "[${GITHUB_REPO}]" + else + info "Successfully found:${F[W]}[GITHUB_REPO]${F[B]}, value:${F[W]}[${GITHUB_REPO}]" + fi + fi + + ############################ + # Validate we have a value # + ############################ + if [ -z "${GITHUB_TOKEN}" ] && [[ ${RUN_LOCAL} == "false" ]]; then + error "Failed to get [GITHUB_TOKEN]!" + error "[${GITHUB_TOKEN}]" + error "Please set a [GITHUB_TOKEN] from the main workflow environment to take advantage of multiple status reports!" + + ################################################################################ + # Need to set MULTI_STATUS to false as we cant hit API endpoints without token # + ################################################################################ + MULTI_STATUS='false' + else + info "Successfully found:${F[W]}[GITHUB_TOKEN]" + fi + + ############################### + # Convert string to lowercase # + ############################### + MULTI_STATUS="${MULTI_STATUS,,}" + + ####################################################################### + # Check to see if the multi status is set, and we have a token to use # + ####################################################################### + if [ "${MULTI_STATUS}" == "true" ] && [ -n "${GITHUB_TOKEN}" ]; then + ############################ + # Validate we have a value # + ############################ + if [ -z "${GITHUB_REPOSITORY}" ]; then + error "Failed to get [GITHUB_REPOSITORY]!" + fatal "[${GITHUB_REPOSITORY}]" + else + info "Successfully found:${F[W]}[GITHUB_REPOSITORY]${F[B]}, value:${F[W]}[${GITHUB_REPOSITORY}]" + fi + + ############################ + # Validate we have a value # + ############################ + if [ -z "${GITHUB_RUN_ID}" ]; then + error "Failed to get [GITHUB_RUN_ID]!" + fatal "[${GITHUB_RUN_ID}]" + else + info "Successfully found:${F[W]}[GITHUB_RUN_ID]${F[B]}, value:${F[W]}[${GITHUB_RUN_ID}]" + fi + fi +} +################################################################################ +#### Function ValidatePowershellModules ######################################## +function ValidatePowershellModules() { + VALIDATE_PSSA_MODULE=$(pwsh -c "(Get-Module -Name PSScriptAnalyzer -ListAvailable | Select-Object -First 1).Name" 2>&1) + # If module found, ensure Invoke-ScriptAnalyzer command is available + if [[ ${VALIDATE_PSSA_MODULE} == "PSScriptAnalyzer" ]]; then + VALIDATE_PSSA_CMD=$(pwsh -c "(Get-Command Invoke-ScriptAnalyzer | Select-Object -First 1).Name" 2>&1) + else + fatal "Failed to find module." + fi + + ######################################### + # validate we found the script analyzer # + ######################################### + if [[ ${VALIDATE_PSSA_CMD} != "Invoke-ScriptAnalyzer" ]]; then + fatal "Failed to find module." + fi + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + # Failed + error "Failed find module [PSScriptAnalyzer] for [${LINTER_NAME}] in system!" + fatal "[PSSA_MODULE ${VALIDATE_PSSA_MODULE}] [PSSA_CMD ${VALIDATE_PSSA_CMD}]" + else + # Success + debug "Successfully found module ${F[W]}[${VALIDATE_PSSA_MODULE}]${F[B]} in system" + debug "Successfully found command ${F[W]}[${VALIDATE_PSSA_CMD}]${F[B]} in system" + fi +} +################################################################################ +#### Function CallStatusAPI #################################################### +CallStatusAPI() { + #################### + # Pull in the vars # + #################### + LANGUAGE="${1}" # langauge that was validated + STATUS="${2}" # success | error + SUCCESS_MSG='No errors were found in the linting process' + FAIL_MSG='Errors were detected, please view logs' + MESSAGE='' # Message to send to status API + + ###################################### + # Check the status to create message # + ###################################### + if [ "${STATUS}" == "success" ]; then + # Success + MESSAGE="${SUCCESS_MSG}" + else + # Failure + MESSAGE="${FAIL_MSG}" + fi + + ########################################################## + # Check to see if were enabled for multi Status mesaages # + ########################################################## + if [ "${MULTI_STATUS}" == "true" ]; then + ############################################## + # Call the status API to create status check # + ############################################## + SEND_STATUS_CMD=$(curl -f -s -X POST \ + --url "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}" \ + -H 'accept: application/vnd.github.v3+json' \ + -H "authorization: Bearer ${GITHUB_TOKEN}" \ + -H 'content-type: application/json' \ + -d "{ \"state\": \"${STATUS}\", + \"target_url\": \"https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}\", + \"description\": \"${MESSAGE}\", \"context\": \"--> Linted: ${LANGUAGE}\" + }" 2>&1) + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ "${ERROR_CODE}" -ne 0 ]; then + # ERROR + info "ERROR! Failed to call GitHub Status API!" + info "ERROR:[${SEND_STATUS_CMD}]" + # Not going to fail the script on this yet... + fi fi - fi } ################################################################################ #### Function Reports ########################################################## Reports() { - info "----------------------------------------------" - info "----------------------------------------------" - info "Generated reports:" - info "----------------------------------------------" - info "----------------------------------------------" + info "----------------------------------------------" + info "----------------------------------------------" + info "Generated reports:" + info "----------------------------------------------" + info "----------------------------------------------" - ################################### - # Prints output report if enabled # - ################################### - if [ -z "${FORMAT_REPORT}" ] ; then - info "Reports generated in folder ${REPORT_OUTPUT_FOLDER}" - fi + ################################### + # Prints output report if enabled # + ################################### + if [ -z "${FORMAT_REPORT}" ]; then + info "Reports generated in folder ${REPORT_OUTPUT_FOLDER}" + fi - ################################ - # Prints for warnings if found # - ################################ - for TEST in "${WARNING_ARRAY_TEST[@]}"; do - warn "Expected file to compare with was not found for ${TEST}" - done + ################################ + # Prints for warnings if found # + ################################ + for TEST in "${WARNING_ARRAY_TEST[@]}"; do + warn "Expected file to compare with was not found for ${TEST}" + done } ################################################################################ #### Function Footer ########################################################### Footer() { - info "----------------------------------------------" - info "----------------------------------------------" - info "The script has completed" - info "----------------------------------------------" - info "----------------------------------------------" + info "----------------------------------------------" + info "----------------------------------------------" + info "The script has completed" + info "----------------------------------------------" + info "----------------------------------------------" - #################################################### - # Need to clean up the lanuage array of duplicates # - #################################################### - mapfile -t UNIQUE_LINTED_ARRAY < <(echo "${LINTED_LANGUAGES_ARRAY[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ') + #################################################### + # Need to clean up the lanuage array of duplicates # + #################################################### + mapfile -t UNIQUE_LINTED_ARRAY < <(echo "${LINTED_LANGUAGES_ARRAY[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ') - ############################## - # Prints for errors if found # - ############################## - for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do - ########################### - # Build the error counter # - ########################### - ERROR_COUNTER="ERRORS_FOUND_${LANGUAGE}" + ############################## + # Prints for errors if found # + ############################## + for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do + ########################### + # Build the error counter # + ########################### + ERROR_COUNTER="ERRORS_FOUND_${LANGUAGE}" - ################## - # Print if not 0 # - ################## - if [[ "${!ERROR_COUNTER}" -ne 0 ]]; then - # We found errors in the language - ################### - # Print the goods # - ################### - error "ERRORS FOUND${NC} in ${LANGUAGE}:[${!ERROR_COUNTER}]" + ################## + # Print if not 0 # + ################## + if [[ ${!ERROR_COUNTER} -ne 0 ]]; then + # We found errors in the language + ################### + # Print the goods # + ################### + error "ERRORS FOUND${NC} in ${LANGUAGE}:[${!ERROR_COUNTER}]" - ######################################### - # Create status API for Failed language # - ######################################### - CallStatusAPI "${LANGUAGE}" "error" - ###################################### - # Check if we validated the langauge # - ###################################### - elif [[ "${!ERROR_COUNTER}" -eq 0 ]] && [[ "${UNIQUE_LINTED_ARRAY[*]}" =~ ${LANGUAGE} ]]; then - # No errors found when linting the language - CallStatusAPI "${LANGUAGE}" "success" + ######################################### + # Create status API for Failed language # + ######################################### + CallStatusAPI "${LANGUAGE}" "error" + ###################################### + # Check if we validated the langauge # + ###################################### + elif [[ ${!ERROR_COUNTER} -eq 0 ]] && [[ ${UNIQUE_LINTED_ARRAY[*]} =~ ${LANGUAGE} ]]; then + # No errors found when linting the language + CallStatusAPI "${LANGUAGE}" "success" + fi + done + + ################################## + # Exit with 0 if errors disabled # + ################################## + if [ "${DISABLE_ERRORS}" == "true" ]; then + warn "Exiting with exit code:[0] as:[DISABLE_ERRORS] was set to:[${DISABLE_ERRORS}]" + exit 0 fi - done - ################################## - # Exit with 0 if errors disabled # - ################################## - if [ "${DISABLE_ERRORS}" == "true" ]; then - warn "Exiting with exit code:[0] as:[DISABLE_ERRORS] was set to:[${DISABLE_ERRORS}]" + ############################### + # Exit with 1 if errors found # + ############################### + # Loop through all languages + for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do + # build the variable + ERRORS_FOUND_LANGUAGE="ERRORS_FOUND_${LANGUAGE}" + # Check if error was found + if [[ ${!ERRORS_FOUND_LANGUAGE} -ne 0 ]]; then + # Failed exit + fatal "Exiting with errors found!" + fi + done + + ######################## + # Footer prints Exit 0 # + ######################## + notice "All file(s) linted successfully with no errors detected" + info "----------------------------------------------" + # Successful exit exit 0 - fi - - ############################### - # Exit with 1 if errors found # - ############################### - # Loop through all languages - for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do - # build the variable - ERRORS_FOUND_LANGUAGE="ERRORS_FOUND_${LANGUAGE}" - # Check if error was found - if [[ "${!ERRORS_FOUND_LANGUAGE}" -ne 0 ]]; then - # Failed exit - fatal "Exiting with errors found!" - fi - done - - ######################## - # Footer prints Exit 0 # - ######################## - notice "All file(s) linted successfully with no errors detected" - info "----------------------------------------------" - # Successful exit - exit 0 } - ################################################################################ #### Function Cleanup ########################################################## cleanup() { @@ -1054,10 +1057,10 @@ Header # check flag for validating the report folder does not exist # ############################################################## if [ -n "${OUTPUT_FORMAT}" ]; then - if [ -d "${REPORT_OUTPUT_FOLDER}" ] ; then - error "ERROR! Found ${REPORT_OUTPUT_FOLDER}" - fatal "Please remove the folder and try again." - fi + if [ -d "${REPORT_OUTPUT_FOLDER}" ]; then + error "ERROR! Found ${REPORT_OUTPUT_FOLDER}" + fatal "Please remove the folder and try again." + fi fi ####################### @@ -1126,560 +1129,560 @@ GetLinterVersions ########################################### if [[ ${TEST_CASE_RUN} != "false" ]]; then - ############################################# - # Set the multi status to off for test runs # - ############################################# - MULTI_STATUS='false' + ############################################# + # Set the multi status to off for test runs # + ############################################# + MULTI_STATUS='false' - ########################### - # Run only the test cases # - ########################### - # Code will exit from inside this loop - RunTestCases + ########################### + # Run only the test cases # + ########################### + # Code will exit from inside this loop + RunTestCases fi ############################################# # check flag for validation of all codebase # ############################################# if [ "${VALIDATE_ALL_CODEBASE}" == "false" ]; then - ######################################## - # Get list of files changed if env set # - ######################################## - BuildFileList + ######################################## + # Get list of files changed if env set # + ######################################## + BuildFileList fi ################### # ANSIBLE LINTING # ################### if [ "${VALIDATE_ANSIBLE}" == "true" ]; then - ########################## - # Lint the Ansible files # - ########################## - # Due to the nature of how we want to validate Ansible, we cannot use the - # standard loop, since it looks for an ansible folder, excludes certain - # files, and looks for additional changes, it should be an outlier - LintAnsibleFiles "${ANSIBLE_LINTER_RULES}" # Passing rules but not needed, dont want to exclude unused var + ########################## + # Lint the Ansible files # + ########################## + # Due to the nature of how we want to validate Ansible, we cannot use the + # standard loop, since it looks for an ansible folder, excludes certain + # files, and looks for additional changes, it should be an outlier + LintAnsibleFiles "${ANSIBLE_LINTER_RULES}" # Passing rules but not needed, dont want to exclude unused var fi ######################## # ARM Template LINTING # ######################## if [ "${VALIDATE_ARM}" == "true" ]; then - # If we are validating all codebase we need to build file list because not every json file is an ARM file - if [ "${VALIDATE_ALL_CODEBASE}" == "true" ]; then - ############################################################################### - # Set the file seperator to newline to allow for grabbing objects with spaces # - ############################################################################### - IFS=$'\n' + # If we are validating all codebase we need to build file list because not every json file is an ARM file + if [ "${VALIDATE_ALL_CODEBASE}" == "true" ]; then + ############################################################################### + # Set the file seperator to newline to allow for grabbing objects with spaces # + ############################################################################### + IFS=$'\n' - mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex ".*\.\(json\)\$" 2>&1) - for FILE in "${LIST_FILES[@]}"; do - if DetectARMFile "${FILE}"; then - FILE_ARRAY_ARM+=("${FILE}") - fi - done + mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex ".*\.\(json\)\$" 2>&1) + for FILE in "${LIST_FILES[@]}"; do + if DetectARMFile "${FILE}"; then + FILE_ARRAY_ARM+=("${FILE}") + fi + done - ########################### - # Set IFS back to default # - ########################### - IFS="${DEFAULT_IFS}" - fi + ########################### + # Set IFS back to default # + ########################### + IFS="${DEFAULT_IFS}" + fi - ############################### - # Lint the ARM Template files # - ############################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "ARM" "arm-ttk" "Import-Module ${ARM_TTK_PSD1} ; \${config} = \$(Import-PowerShellDataFile -Path ${ARM_LINTER_RULES}) ; Test-AzTemplate @config -TemplatePath" "disabledfileext" "${FILE_ARRAY_ARM[@]}" + ############################### + # Lint the ARM Template files # + ############################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "ARM" "arm-ttk" "Import-Module ${ARM_TTK_PSD1} ; \${config} = \$(Import-PowerShellDataFile -Path ${ARM_LINTER_RULES}) ; Test-AzTemplate @config -TemplatePath" "disabledfileext" "${FILE_ARRAY_ARM[@]}" fi ################ # BASH LINTING # ################ if [ "${VALIDATE_BASH}" == "true" ]; then - ####################### - # Lint the bash files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "BASH" "shellcheck" "shellcheck --color" ".*\.\(sh\|bash\|dash\|ksh\)\$" "${FILE_ARRAY_BASH[@]}" + ####################### + # Lint the bash files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "BASH" "shellcheck" "shellcheck --color" ".*\.\(sh\|bash\|dash\|ksh\)\$" "${FILE_ARRAY_BASH[@]}" fi ########################## # CLOUDFORMATION LINTING # ########################## if [ "${VALIDATE_CLOUDFORMATION}" == "true" ]; then - # If we are validating all codebase we need to build file list because not every yml/json file is an CLOUDFORMATION file - if [ "${VALIDATE_ALL_CODEBASE}" == "true" ]; then - ############################################################################### - # Set the file seperator to newline to allow for grabbing objects with spaces # - ############################################################################### - IFS=$'\n' + # If we are validating all codebase we need to build file list because not every yml/json file is an CLOUDFORMATION file + if [ "${VALIDATE_ALL_CODEBASE}" == "true" ]; then + ############################################################################### + # Set the file seperator to newline to allow for grabbing objects with spaces # + ############################################################################### + IFS=$'\n' - mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex ".*\.\(yml\|yaml\|json\)\$" 2>&1) - for FILE in "${LIST_FILES[@]}"; do - if DetectCloudFormationFile "${FILE}"; then - FILE_ARRAY_CLOUDFORMATION+=("${FILE}") - fi - done + mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex ".*\.\(yml\|yaml\|json\)\$" 2>&1) + for FILE in "${LIST_FILES[@]}"; do + if DetectCloudFormationFile "${FILE}"; then + FILE_ARRAY_CLOUDFORMATION+=("${FILE}") + fi + done - ########################### - # Set IFS back to default # - ########################### - IFS="${DEFAULT_IFS}" - fi + ########################### + # Set IFS back to default # + ########################### + IFS="${DEFAULT_IFS}" + fi - ################################# - # Lint the CloudFormation files # - ################################# - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "CLOUDFORMATION" "cfn-lint" "cfn-lint --config-file ${CLOUDFORMATION_LINTER_RULES}" "disabledfileext" "${FILE_ARRAY_CLOUDFORMATION[@]}" + ################################# + # Lint the CloudFormation files # + ################################# + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "CLOUDFORMATION" "cfn-lint" "cfn-lint --config-file ${CLOUDFORMATION_LINTER_RULES}" "disabledfileext" "${FILE_ARRAY_CLOUDFORMATION[@]}" fi ################### # CLOJURE LINTING # ################### if [ "${VALIDATE_CLOJURE}" == "true" ]; then - ################################# - # Get Clojure standard rules # - ################################# - GetStandardRules "clj-kondo" - ######################### - # Lint the Clojure files # - ######################### - LintCodebase "CLOJURE" "clj-kondo" "clj-kondo --config ${CLOJURE_LINTER_RULES} --lint" ".*\.\(clj\|cljs\|cljc\|edn\)\$" "${FILE_ARRAY_CLOJURE[@]}" + ################################# + # Get Clojure standard rules # + ################################# + GetStandardRules "clj-kondo" + ######################### + # Lint the Clojure files # + ######################### + LintCodebase "CLOJURE" "clj-kondo" "clj-kondo --config ${CLOJURE_LINTER_RULES} --lint" ".*\.\(clj\|cljs\|cljc\|edn\)\$" "${FILE_ARRAY_CLOJURE[@]}" fi ######################## # COFFEESCRIPT LINTING # ######################## if [ "${VALIDATE_COFFEE}" == "true" ]; then - ######################### - # Lint the coffee files # - ######################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "COFFEESCRIPT" "coffeelint" "coffeelint -f ${COFFEESCRIPT_LINTER_RULES}" ".*\.\(coffee\)\$" "${FILE_ARRAY_COFFEESCRIPT[@]}" + ######################### + # Lint the coffee files # + ######################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "COFFEESCRIPT" "coffeelint" "coffeelint -f ${COFFEESCRIPT_LINTER_RULES}" ".*\.\(coffee\)\$" "${FILE_ARRAY_COFFEESCRIPT[@]}" fi ############### # CSS LINTING # ############### if [ "${VALIDATE_CSS}" == "true" ]; then - ################################# - # Get CSS standard rules # - ################################# - GetStandardRules "stylelint" - ############################# - # Lint the CSS files # - ############################# - LintCodebase "CSS" "stylelint" "stylelint --config ${CSS_LINTER_RULES}" ".*\.\(css\)\$" "${FILE_ARRAY_CSS[@]}" + ################################# + # Get CSS standard rules # + ################################# + GetStandardRules "stylelint" + ############################# + # Lint the CSS files # + ############################# + LintCodebase "CSS" "stylelint" "stylelint --config ${CSS_LINTER_RULES}" ".*\.\(css\)\$" "${FILE_ARRAY_CSS[@]}" fi ################ # DART LINTING # ################ if [ "${VALIDATE_DART}" == "true" ]; then - ####################### - # Lint the Dart files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "DART" "dart" "dartanalyzer --fatal-infos --fatal-warnings --options ${DART_LINTER_RULES}" ".*\.\(dart\)\$" "${FILE_ARRAY_DART[@]}" + ####################### + # Lint the Dart files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "DART" "dart" "dartanalyzer --fatal-infos --fatal-warnings --options ${DART_LINTER_RULES}" ".*\.\(dart\)\$" "${FILE_ARRAY_DART[@]}" fi ################## # DOCKER LINTING # ################## if [ "${VALIDATE_DOCKER}" == "true" ]; then - ######################### - # Lint the docker files # - ######################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - # NOTE: dockerfilelint's "-c" option expects the folder *containing* the DOCKER_LINTER_RULES file - LintCodebase "DOCKER" "dockerfilelint" "dockerfilelint -c $(dirname ${DOCKER_LINTER_RULES})" ".*\(Dockerfile\)\$" "${FILE_ARRAY_DOCKER[@]}" + ######################### + # Lint the docker files # + ######################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + # NOTE: dockerfilelint's "-c" option expects the folder *containing* the DOCKER_LINTER_RULES file + LintCodebase "DOCKER" "dockerfilelint" "dockerfilelint -c $(dirname ${DOCKER_LINTER_RULES})" ".*\(Dockerfile\)\$" "${FILE_ARRAY_DOCKER[@]}" fi ######################## # EDITORCONFIG LINTING # ######################## if [ "${VALIDATE_EDITORCONFIG}" == "true" ]; then - #################################### - # Lint the files with editorconfig # - #################################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "EDITORCONFIG" "editorconfig-checker" "editorconfig-checker" "^.*$" "${FILE_ARRAY_ENV[@]}" + #################################### + # Lint the files with editorconfig # + #################################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "EDITORCONFIG" "editorconfig-checker" "editorconfig-checker" "^.*$" "${FILE_ARRAY_ENV[@]}" fi ############### # ENV LINTING # ############### if [ "${VALIDATE_ENV}" == "true" ]; then - ####################### - # Lint the env files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "ENV" "dotenv-linter" "dotenv-linter" ".*\.\(env\).*\$" "${FILE_ARRAY_ENV[@]}" + ####################### + # Lint the env files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "ENV" "dotenv-linter" "dotenv-linter" ".*\.\(env\).*\$" "${FILE_ARRAY_ENV[@]}" fi ################## # GOLANG LINTING # ################## if [ "${VALIDATE_GO}" == "true" ]; then - ######################### - # Lint the golang files # - ######################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "GO" "golangci-lint" "golangci-lint run -c ${GO_LINTER_RULES}" ".*\.\(go\)\$" "${FILE_ARRAY_GO[@]}" + ######################### + # Lint the golang files # + ######################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "GO" "golangci-lint" "golangci-lint run -c ${GO_LINTER_RULES}" ".*\.\(go\)\$" "${FILE_ARRAY_GO[@]}" fi ################## # GROOVY LINTING # ################## if [ "$VALIDATE_GROOVY" == "true" ]; then - ######################### - # Lint the groovy files # - ######################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "GROOVY" "npm-groovy-lint" "npm-groovy-lint -c $GROOVY_LINTER_RULES --failon error" ".*\.\(groovy\|jenkinsfile\|gradle\)\$" "${FILE_ARRAY_GROOVY[@]}" + ######################### + # Lint the groovy files # + ######################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "GROOVY" "npm-groovy-lint" "npm-groovy-lint -c $GROOVY_LINTER_RULES --failon error" ".*\.\(groovy\|jenkinsfile\|gradle\)\$" "${FILE_ARRAY_GROOVY[@]}" fi ################ # HTML LINTING # ################ if [ "${VALIDATE_HTML}" == "true" ]; then - ########################### - # Get HTML standard rules # - ########################### - GetStandardRules "htmlhint" - ####################### - # Lint the HTML files # - ####################### - LintCodebase "HTML" "htmlhint" "htmlhint --config ${HTML_LINTER_RULES}" ".*\.\(html\)\$" "${FILE_ARRAY_HTML[@]}" + ########################### + # Get HTML standard rules # + ########################### + GetStandardRules "htmlhint" + ####################### + # Lint the HTML files # + ####################### + LintCodebase "HTML" "htmlhint" "htmlhint --config ${HTML_LINTER_RULES}" ".*\.\(html\)\$" "${FILE_ARRAY_HTML[@]}" fi ###################### # JAVASCRIPT LINTING # ###################### if [ "${VALIDATE_JAVASCRIPT_ES}" == "true" ]; then - ############################# - # Lint the Javascript files # - ############################# - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "JAVASCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${JAVASCRIPT_LINTER_RULES}" ".*\.\(js\)\$" "${FILE_ARRAY_JAVASCRIPT_ES[@]}" + ############################# + # Lint the Javascript files # + ############################# + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "JAVASCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${JAVASCRIPT_LINTER_RULES}" ".*\.\(js\)\$" "${FILE_ARRAY_JAVASCRIPT_ES[@]}" fi ###################### # JAVASCRIPT LINTING # ###################### if [ "${VALIDATE_JAVASCRIPT_STANDARD}" == "true" ]; then - ################################# - # Get Javascript standard rules # - ################################# - GetStandardRules "javascript" - ############################# - # Lint the Javascript files # - ############################# - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "JAVASCRIPT_STANDARD" "standard" "standard ${JAVASCRIPT_STANDARD_LINTER_RULES}" ".*\.\(js\)\$" "${FILE_ARRAY_JAVASCRIPT_STANDARD[@]}" + ################################# + # Get Javascript standard rules # + ################################# + GetStandardRules "javascript" + ############################# + # Lint the Javascript files # + ############################# + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "JAVASCRIPT_STANDARD" "standard" "standard ${JAVASCRIPT_STANDARD_LINTER_RULES}" ".*\.\(js\)\$" "${FILE_ARRAY_JAVASCRIPT_STANDARD[@]}" fi ################ # JSON LINTING # ################ if [ "${VALIDATE_JSON}" == "true" ]; then - ####################### - # Lint the json files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "JSON" "jsonlint" "jsonlint" ".*\.\(json\)\$" "${FILE_ARRAY_JSON[@]}" + ####################### + # Lint the json files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "JSON" "jsonlint" "jsonlint" ".*\.\(json\)\$" "${FILE_ARRAY_JSON[@]}" fi ############### # JSX LINTING # ############### if [ "${VALIDATE_JSX}" == "true" ]; then - ###################### - # Lint the JSX files # - ###################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "JSX" "eslint" "eslint --no-eslintrc -c ${JAVASCRIPT_LINTER_RULES}" ".*\.\(jsx\)\$" "${FILE_ARRAY_JSX[@]}" + ###################### + # Lint the JSX files # + ###################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "JSX" "eslint" "eslint --no-eslintrc -c ${JAVASCRIPT_LINTER_RULES}" ".*\.\(jsx\)\$" "${FILE_ARRAY_JSX[@]}" fi ################## # KOTLIN LINTING # ################## if [ "${VALIDATE_KOTLIN}" == "true" ]; then - ####################### - # Lint the Kotlin files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "KOTLIN" "ktlint" "ktlint" ".*\.\(kt\|kts\)\$" "${FILE_ARRAY_KOTLIN[@]}" + ####################### + # Lint the Kotlin files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "KOTLIN" "ktlint" "ktlint" ".*\.\(kt\|kts\)\$" "${FILE_ARRAY_KOTLIN[@]}" fi ############### # LUA LINTING # ############### if [ "${VALIDATE_LUA}" == "true" ]; then - ###################### - # Lint the Lua files # - ###################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "LUA" "lua" "luacheck --config ${LUA_LINTER_RULES}" ".*\.\(lua\)\$" "${FILE_ARRAY_LUA[@]}" + ###################### + # Lint the Lua files # + ###################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "LUA" "lua" "luacheck --config ${LUA_LINTER_RULES}" ".*\.\(lua\)\$" "${FILE_ARRAY_LUA[@]}" fi #################### # MARKDOWN LINTING # #################### if [ "${VALIDATE_MARKDOWN}" == "true" ]; then - ########################### - # Lint the Markdown Files # - ########################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "MARKDOWN" "markdownlint" "markdownlint -c ${MARKDOWN_LINTER_RULES}" ".*\.\(md\)\$" "${FILE_ARRAY_MARKDOWN[@]}" + ########################### + # Lint the Markdown Files # + ########################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "MARKDOWN" "markdownlint" "markdownlint -c ${MARKDOWN_LINTER_RULES}" ".*\.\(md\)\$" "${FILE_ARRAY_MARKDOWN[@]}" fi ################### # OPENAPI LINTING # ################### if [ "${VALIDATE_OPENAPI}" == "true" ]; then - # If we are validating all codebase we need to build file list because not every yml/json file is an OpenAPI file - if [ "${VALIDATE_ALL_CODEBASE}" == "true" ]; then - ############################################################################### - # Set the file seperator to newline to allow for grabbing objects with spaces # - ############################################################################### - IFS=$'\n' + # If we are validating all codebase we need to build file list because not every yml/json file is an OpenAPI file + if [ "${VALIDATE_ALL_CODEBASE}" == "true" ]; then + ############################################################################### + # Set the file seperator to newline to allow for grabbing objects with spaces # + ############################################################################### + IFS=$'\n' - mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex ".*\.\(yml\|yaml\|json\)\$" 2>&1) - for FILE in "${LIST_FILES[@]}"; do - if DetectOpenAPIFile "${FILE}"; then - FILE_ARRAY_OPENAPI+=("${FILE}") - fi - done + mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex ".*\.\(yml\|yaml\|json\)\$" 2>&1) + for FILE in "${LIST_FILES[@]}"; do + if DetectOpenAPIFile "${FILE}"; then + FILE_ARRAY_OPENAPI+=("${FILE}") + fi + done - ########################### - # Set IFS back to default # - ########################### - IFS="${DEFAULT_IFS}" - fi + ########################### + # Set IFS back to default # + ########################### + IFS="${DEFAULT_IFS}" + fi - ########################## - # Lint the OpenAPI files # - ########################## - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "OPENAPI" "spectral" "spectral lint -r ${OPENAPI_LINTER_RULES}" "disabledfileext" "${FILE_ARRAY_OPENAPI[@]}" + ########################## + # Lint the OpenAPI files # + ########################## + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "OPENAPI" "spectral" "spectral lint -r ${OPENAPI_LINTER_RULES}" "disabledfileext" "${FILE_ARRAY_OPENAPI[@]}" fi ################ # PERL LINTING # ################ if [ "${VALIDATE_PERL}" == "true" ]; then - ####################### - # Lint the perl files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "PERL" "perl" "perl -Mstrict -cw" ".*\.\(pl\)\$" "${FILE_ARRAY_PERL[@]}" + ####################### + # Lint the perl files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "PERL" "perl" "perl -Mstrict -cw" ".*\.\(pl\)\$" "${FILE_ARRAY_PERL[@]}" fi ################ # PHP LINTING # ################ if [ "${VALIDATE_PHP}" == "true" ]; then - ####################### - # Lint the PHP files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "PHP" "php" "php -l" ".*\.\(php\)\$" "${FILE_ARRAY_PHP[@]}" + ####################### + # Lint the PHP files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "PHP" "php" "php -l" ".*\.\(php\)\$" "${FILE_ARRAY_PHP[@]}" fi ################### # PHPStan LINTING # ################### if [ "${VALIDATE_PHP_PHPSTAN}" == "true" ]; then - ####################### - # Lint the PHP files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "PHP_PHPSTAN" "phpstan" "phpstan analyse --no-progress --no-ansi -c ${PHPSTAN_LINTER_RULES}" ".*\.\(php\)\$" "${FILE_ARRAY_PHP_PHPSTAN[@]}" + ####################### + # Lint the PHP files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "PHP_PHPSTAN" "phpstan" "phpstan analyse --no-progress --no-ansi -c ${PHPSTAN_LINTER_RULES}" ".*\.\(php\)\$" "${FILE_ARRAY_PHP_PHPSTAN[@]}" fi ###################### # POWERSHELL LINTING # ###################### if [ "${VALIDATE_POWERSHELL}" == "true" ]; then - ############################################################### - # For POWERSHELL, ensure PSScriptAnalyzer module is available # - ############################################################### - ValidatePowershellModules + ############################################################### + # For POWERSHELL, ensure PSScriptAnalyzer module is available # + ############################################################### + ValidatePowershellModules - ############################# - # Lint the powershell files # - ############################# - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "POWERSHELL" "pwsh" "Invoke-ScriptAnalyzer -EnableExit -Settings ${POWERSHELL_LINTER_RULES} -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "${FILE_ARRAY_POWERSHELL[@]}" + ############################# + # Lint the powershell files # + ############################# + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "POWERSHELL" "pwsh" "Invoke-ScriptAnalyzer -EnableExit -Settings ${POWERSHELL_LINTER_RULES} -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "${FILE_ARRAY_POWERSHELL[@]}" fi #################### # PROTOBUF LINTING # #################### if [ "${VALIDATE_PROTOBUF}" == "true" ]; then - ####################### - # Lint the Protocol Buffers files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "PROTOBUF" "protolint" "protolint lint --config_path ${PROTOBUF_LINTER_RULES}" ".*\.\(proto\)\$" "${FILE_ARRAY_PROTOBUF[@]}" + ####################### + # Lint the Protocol Buffers files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "PROTOBUF" "protolint" "protolint lint --config_path ${PROTOBUF_LINTER_RULES}" ".*\.\(proto\)\$" "${FILE_ARRAY_PROTOBUF[@]}" fi ################## # PYTHON LINTING # ################## if [ "${VALIDATE_PYTHON_PYLINT}" == "true" ]; then - ######################### - # Lint the python files # - ######################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "PYTHON_PYLINT" "pylint" "pylint --rcfile ${PYTHON_PYLINT_LINTER_RULES}" ".*\.\(py\)\$" "${FILE_ARRAY_PYTHON_PYLINT[@]}" + ######################### + # Lint the python files # + ######################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "PYTHON_PYLINT" "pylint" "pylint --rcfile ${PYTHON_PYLINT_LINTER_RULES}" ".*\.\(py\)\$" "${FILE_ARRAY_PYTHON_PYLINT[@]}" fi ################## # PYTHON LINTING # ################## if [ "${VALIDATE_PYTHON_FLAKE8}" == "true" ]; then - ######################### - # Lint the python files # - ######################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "PYTHON_FLAKE8" "flake8" "flake8 --config=${PYTHON_FLAKE8_LINTER_RULES}" ".*\.\(py\)\$" "${FILE_ARRAY_PYTHON_FLAKE8[@]}" + ######################### + # Lint the python files # + ######################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "PYTHON_FLAKE8" "flake8" "flake8 --config=${PYTHON_FLAKE8_LINTER_RULES}" ".*\.\(py\)\$" "${FILE_ARRAY_PYTHON_FLAKE8[@]}" fi ################ # RAKU LINTING # ################ if [ "${VALIDATE_RAKU}" == "true" ]; then - ####################### - # Lint the raku files # - ####################### + ####################### + # Lint the raku files # + ####################### info "${GITHUB_WORKSPACE}/META6.json" if [ -e "${GITHUB_WORKSPACE}/META6.json" ]; then - cd "${GITHUB_WORKSPACE}" && zef install --deps-only --/test . + cd "${GITHUB_WORKSPACE}" && zef install --deps-only --/test . fi - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "RAKU" "raku" "raku -I ${GITHUB_WORKSPACE}/lib -c" ".*\.\(raku\|rakumod\|rakutest\|pm6\|pl6\|p6\)\$" "${FILE_ARRAY_RAKU[@]}" + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "RAKU" "raku" "raku -I ${GITHUB_WORKSPACE}/lib -c" ".*\.\(raku\|rakumod\|rakutest\|pm6\|pl6\|p6\)\$" "${FILE_ARRAY_RAKU[@]}" fi ################ # RUBY LINTING # ################ if [ "${VALIDATE_RUBY}" == "true" ]; then - ####################### - # Lint the ruby files # - ####################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES} --force-exclusion" ".*\.\(rb\)\$" "${FILE_ARRAY_RUBY[@]}" + ####################### + # Lint the ruby files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES} --force-exclusion" ".*\.\(rb\)\$" "${FILE_ARRAY_RUBY[@]}" fi ###################### # AWS STATES LINTING # ###################### if [ "${VALIDATE_STATES}" == "true" ]; then - # If we are validating all codebase we need to build file list because not every json file is an aws states file - if [ "${VALIDATE_ALL_CODEBASE}" == "true" ]; then - ############################################################################### - # Set the file seperator to newline to allow for grabbing objects with spaces # - ############################################################################### - IFS=$'\n' + # If we are validating all codebase we need to build file list because not every json file is an aws states file + if [ "${VALIDATE_ALL_CODEBASE}" == "true" ]; then + ############################################################################### + # Set the file seperator to newline to allow for grabbing objects with spaces # + ############################################################################### + IFS=$'\n' - mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex ".*\.\(json\)\$" 2>&1) - for FILE in "${LIST_FILES[@]}"; do - if DetectAWSStatesFIle "${FILE}"; then - FILE_ARRAY_STATES+=("${FILE}") - fi - done + mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex ".*\.\(json\)\$" 2>&1) + for FILE in "${LIST_FILES[@]}"; do + if DetectAWSStatesFIle "${FILE}"; then + FILE_ARRAY_STATES+=("${FILE}") + fi + done - ########################### - # Set IFS back to default # - ########################### - IFS="${DEFAULT_IFS}" - fi + ########################### + # Set IFS back to default # + ########################### + IFS="${DEFAULT_IFS}" + fi - ######################### - # Lint the STATES files # - ######################### - LintCodebase "STATES" "asl-validator" "asl-validator --json-path" "disabledfileext" "${FILE_ARRAY_STATES[@]}" + ######################### + # Lint the STATES files # + ######################### + LintCodebase "STATES" "asl-validator" "asl-validator --json-path" "disabledfileext" "${FILE_ARRAY_STATES[@]}" fi ##################### # TERRAFORM LINTING # ##################### if [ "${VALIDATE_TERRAFORM}" == "true" ]; then - ############################ - # Lint the Terraform files # - ############################ - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "TERRAFORM" "tflint" "tflint -c ${TERRAFORM_LINTER_RULES}" ".*\.\(tf\)\$" "${FILE_ARRAY_TERRAFORM[@]}" + ############################ + # Lint the Terraform files # + ############################ + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "TERRAFORM" "tflint" "tflint -c ${TERRAFORM_LINTER_RULES}" ".*\.\(tf\)\$" "${FILE_ARRAY_TERRAFORM[@]}" fi ############################### # TERRAFORM TERRASCAN LINTING # ############################### if [ "${VALIDATE_TERRAFORM_TERRASCAN}" == "true" ]; then - ############################ - # Lint the Terraform files # - ############################ - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "TERRAFORM_TERRASCAN" "terrascan" "terrascan -f " ".*\.\(tf\)\$" "${FILE_ARRAY_TERRAFORM_TERRASCAN[@]}" + ############################ + # Lint the Terraform files # + ############################ + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "TERRAFORM_TERRASCAN" "terrascan" "terrascan -f " ".*\.\(tf\)\$" "${FILE_ARRAY_TERRAFORM_TERRASCAN[@]}" fi ############### # TSX LINTING # ############### if [ "${VALIDATE_TSX}" == "true" ]; then - ###################### - # Lint the TSX files # - ###################### - LintCodebase "TSX" "eslint" "eslint --no-eslintrc -c ${TYPESCRIPT_LINTER_RULES}" ".*\.\(tsx\)\$" "${FILE_ARRAY_TSX[@]}" + ###################### + # Lint the TSX files # + ###################### + LintCodebase "TSX" "eslint" "eslint --no-eslintrc -c ${TYPESCRIPT_LINTER_RULES}" ".*\.\(tsx\)\$" "${FILE_ARRAY_TSX[@]}" fi ###################### # TYPESCRIPT LINTING # ###################### if [ "${VALIDATE_TYPESCRIPT_ES}" == "true" ]; then - ############################# - # Lint the Typescript files # - ############################# - LintCodebase "TYPESCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${TYPESCRIPT_LINTER_RULES}" ".*\.\(ts\)\$" "${FILE_ARRAY_TYPESCRIPT_ES[@]}" + ############################# + # Lint the Typescript files # + ############################# + LintCodebase "TYPESCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${TYPESCRIPT_LINTER_RULES}" ".*\.\(ts\)\$" "${FILE_ARRAY_TYPESCRIPT_ES[@]}" fi ###################### # TYPESCRIPT LINTING # ###################### if [ "${VALIDATE_TYPESCRIPT_STANDARD}" == "true" ]; then - ################################# - # Get Typescript standard rules # - ################################# - GetStandardRules "typescript" - ############################# - # Lint the Typescript files # - ############################# - LintCodebase "TYPESCRIPT_STANDARD" "standard" "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin ${TYPESCRIPT_STANDARD_LINTER_RULES}" ".*\.\(ts\)\$" "${FILE_ARRAY_TYPESCRIPT_STANDARD[@]}" + ################################# + # Get Typescript standard rules # + ################################# + GetStandardRules "typescript" + ############################# + # Lint the Typescript files # + ############################# + LintCodebase "TYPESCRIPT_STANDARD" "standard" "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin ${TYPESCRIPT_STANDARD_LINTER_RULES}" ".*\.\(ts\)\$" "${FILE_ARRAY_TYPESCRIPT_STANDARD[@]}" fi ############### # XML LINTING # ############### if [ "${VALIDATE_XML}" == "true" ]; then - ###################### - # Lint the XML Files # - ###################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "XML" "xmllint" "xmllint" ".*\.\(xml\)\$" "${FILE_ARRAY_XML[@]}" + ###################### + # Lint the XML Files # + ###################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "XML" "xmllint" "xmllint" ".*\.\(xml\)\$" "${FILE_ARRAY_XML[@]}" fi ############### # YAML LINTING # ############### if [ "${VALIDATE_YAML}" == "true" ]; then - ###################### - # Lint the Yml Files # - ###################### - # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "YAML" "yamllint" "yamllint -c ${YAML_LINTER_RULES}" ".*\.\(yml\|yaml\)\$" "${FILE_ARRAY_YAML[@]}" + ###################### + # Lint the Yml Files # + ###################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "YAML" "yamllint" "yamllint -c ${YAML_LINTER_RULES}" ".*\.\(yml\|yaml\)\$" "${FILE_ARRAY_YAML[@]}" fi ########### diff --git a/lib/log.sh b/lib/log.sh index fce6b072..f6bd73a2 100644 --- a/lib/log.sh +++ b/lib/log.sh @@ -1,24 +1,24 @@ #!/usr/bin/env bash declare -Agr B=( - [B]=$(echo -e "\e[44m") - [C]=$(echo -e "\e[46m") - [G]=$(echo -e "\e[42m") - [K]=$(echo -e "\e[40m") - [M]=$(echo -e "\e[45m") - [R]=$(echo -e "\e[41m") - [W]=$(echo -e "\e[47m") - [Y]=$(echo -e "\e[43m") + [B]=$(echo -e "\e[44m") + [C]=$(echo -e "\e[46m") + [G]=$(echo -e "\e[42m") + [K]=$(echo -e "\e[40m") + [M]=$(echo -e "\e[45m") + [R]=$(echo -e "\e[41m") + [W]=$(echo -e "\e[47m") + [Y]=$(echo -e "\e[43m") ) declare -Agr F=( - [B]=$(echo -e "\e[0;34m") - [C]=$(echo -e "\e[0;36m") - [G]=$(echo -e "\e[0;32m") - [K]=$(echo -e "\e[0;30m") - [M]=$(echo -e "\e[0;35m") - [R]=$(echo -e "\e[0;31m") - [W]=$(echo -e "\e[0;37m") - [Y]=$(echo -e "\e[0;33m") + [B]=$(echo -e "\e[0;34m") + [C]=$(echo -e "\e[0;36m") + [G]=$(echo -e "\e[0;32m") + [K]=$(echo -e "\e[0;30m") + [M]=$(echo -e "\e[0;35m") + [R]=$(echo -e "\e[0;31m") + [W]=$(echo -e "\e[0;37m") + [Y]=$(echo -e "\e[0;33m") ) readonly NC=$(echo -e "\e[0m") diff --git a/lib/termColors.sh b/lib/termColors.sh index 8f22a26c..ce3e0e66 100755 --- a/lib/termColors.sh +++ b/lib/termColors.sh @@ -1,24 +1,24 @@ #!/usr/bin/env bash declare -Agr B=( - [B]=$(echo -e "\e[44m") - [C]=$(echo -e "\e[46m") - [G]=$(echo -e "\e[42m") - [K]=$(echo -e "\e[40m") - [M]=$(echo -e "\e[45m") - [R]=$(echo -e "\e[41m") - [W]=$(echo -e "\e[47m") - [Y]=$(echo -e "\e[43m") + [B]=$(echo -e "\e[44m") + [C]=$(echo -e "\e[46m") + [G]=$(echo -e "\e[42m") + [K]=$(echo -e "\e[40m") + [M]=$(echo -e "\e[45m") + [R]=$(echo -e "\e[41m") + [W]=$(echo -e "\e[47m") + [Y]=$(echo -e "\e[43m") ) declare -Agr F=( - [B]=$(echo -e "\e[0;34m") - [C]=$(echo -e "\e[0;36m") - [G]=$(echo -e "\e[0;32m") - [K]=$(echo -e "\e[0;30m") - [M]=$(echo -e "\e[0;35m") - [R]=$(echo -e "\e[0;31m") - [W]=$(echo -e "\e[0;37m") - [Y]=$(echo -e "\e[0;33m") + [B]=$(echo -e "\e[0;34m") + [C]=$(echo -e "\e[0;36m") + [G]=$(echo -e "\e[0;32m") + [K]=$(echo -e "\e[0;30m") + [M]=$(echo -e "\e[0;35m") + [R]=$(echo -e "\e[0;31m") + [W]=$(echo -e "\e[0;37m") + [Y]=$(echo -e "\e[0;33m") ) readonly NC=$(echo -e "\e[0m") diff --git a/lib/validation.sh b/lib/validation.sh index 6e4105b0..fcbcd4ac 100755 --- a/lib/validation.sh +++ b/lib/validation.sh @@ -10,175 +10,175 @@ ################################################################################ #### Function GetValidationInfo ################################################ function GetValidationInfo() { - ############################################ - # Print headers for user provided env vars # - ############################################ - info "--------------------------------------------" - info "Gathering user validation information..." + ############################################ + # Print headers for user provided env vars # + ############################################ + info "--------------------------------------------" + info "Gathering user validation information..." + + ########################################### + # Skip validation if were running locally # + ########################################### + if [[ ${RUN_LOCAL} != "true" ]]; then + ############################### + # Convert string to lowercase # + ############################### + VALIDATE_ALL_CODEBASE="${VALIDATE_ALL_CODEBASE,,}" + ###################################### + # Validate we should check all files # + ###################################### + if [[ ${VALIDATE_ALL_CODEBASE} != "false" ]]; then + # Set to true + VALIDATE_ALL_CODEBASE="${DEFAULT_VALIDATE_ALL_CODEBASE}" + info "- Validating ALL files in code base..." + else + # Its false + info "- Only validating [new], or [edited] files in code base..." + fi + fi + + ###################### + # Create Print Array # + ###################### + PRINT_ARRAY=() + + ################################ + # Convert strings to lowercase # + ################################ + # Loop through all languages + for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do + # build the variable + VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}" + # Set the value of the var to lowercase + eval "${VALIDATE_LANGUAGE}=${!VALIDATE_LANGUAGE,,}" + done + + ################################################ + # Determine if any linters were explicitly set # + ################################################ + ANY_SET="false" + # Loop through all languages + for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do + # build the variable + VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}" + # Check to see if the variable was set + if [ -n "${!VALIDATE_LANGUAGE}" ]; then + # It was set, need to set flag + ANY_SET="true" + fi + done + + ################################################### + # Validate if we should check individual lanuages # + ################################################### + # Loop through all languages + for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do + # build the variable + VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}" + # Check if ANY_SET was set + if [[ ${ANY_SET} == "true" ]]; then + # Check to see if the variable was set + if [ -z "${!VALIDATE_LANGUAGE}" ]; then + # Flag was not set, default to false + eval "${VALIDATE_LANGUAGE}='false'" + fi + else + # No linter flags were set - default all to true + eval "${VALIDATE_LANGUAGE}='true'" + fi + done + + ####################################### + # Print which linters we are enabling # + ####################################### + # Loop through all languages + for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do + # build the variable + VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}" + if [[ ${!VALIDATE_LANGUAGE} == "true" ]]; then + # We need to validate + PRINT_ARRAY+=("- Validating [$LANGUAGE] files in code base...") + else + # We are skipping the language + PRINT_ARRAY+=("- Excluding [$LANGUAGE] files in code base...") + fi + done + + ############################## + # Validate Ansible Directory # + ############################## + if [ -z "${ANSIBLE_DIRECTORY}" ]; then + # No Value, need to default + ANSIBLE_DIRECTORY="${DEFAULT_ANSIBLE_DIRECTORY}" + else + # Check if first char is '/' + if [[ ${ANSIBLE_DIRECTORY:0:1} == "/" ]]; then + # Remove first char + ANSIBLE_DIRECTORY="${ANSIBLE_DIRECTORY:1}" + fi + # Need to give it full path + TEMP_ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}/${ANSIBLE_DIRECTORY}" + # Set the value + ANSIBLE_DIRECTORY="${TEMP_ANSIBLE_DIRECTORY}" + fi + + ############################### + # Get the disable errors flag # + ############################### + if [ -z "${DISABLE_ERRORS}" ]; then + ################################## + # No flag passed, set to default # + ################################## + DISABLE_ERRORS="${DEFAULT_DISABLE_ERRORS}" + fi - ########################################### - # Skip validation if were running locally # - ########################################### - if [[ ${RUN_LOCAL} != "true" ]]; then ############################### # Convert string to lowercase # ############################### - VALIDATE_ALL_CODEBASE="${VALIDATE_ALL_CODEBASE,,}" - ###################################### - # Validate we should check all files # - ###################################### - if [[ ${VALIDATE_ALL_CODEBASE} != "false" ]]; then - # Set to true - VALIDATE_ALL_CODEBASE="${DEFAULT_VALIDATE_ALL_CODEBASE}" - info "- Validating ALL files in code base..." - else - # Its false - info "- Only validating [new], or [edited] files in code base..." + DISABLE_ERRORS="${DISABLE_ERRORS,,}" + + ############################ + # Set to false if not true # + ############################ + if [ "${DISABLE_ERRORS}" != "true" ]; then + DISABLE_ERRORS="false" fi - fi - ###################### - # Create Print Array # - ###################### - PRINT_ARRAY=() - - ################################ - # Convert strings to lowercase # - ################################ - # Loop through all languages - for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do - # build the variable - VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}" - # Set the value of the var to lowercase - eval "${VALIDATE_LANGUAGE}=${!VALIDATE_LANGUAGE,,}" - done - - ################################################ - # Determine if any linters were explicitly set # - ################################################ - ANY_SET="false" - # Loop through all languages - for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do - # build the variable - VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}" - # Check to see if the variable was set - if [ -n "${!VALIDATE_LANGUAGE}" ]; then - # It was set, need to set flag - ANY_SET="true" + ############################ + # Get the run verbose flag # + ############################ + if [ -z "${ACTIONS_RUNNER_DEBUG}" ]; then + ################################## + # No flag passed, set to default # + ################################## + ACTIONS_RUNNER_DEBUG="${DEFAULT_ACTIONS_RUNNER_DEBUG}" fi - done - ################################################### - # Validate if we should check individual lanuages # - ################################################### - # Loop through all languages - for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do - # build the variable - VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}" - # Check if ANY_SET was set - if [[ ${ANY_SET} == "true" ]]; then - # Check to see if the variable was set - if [ -z "${!VALIDATE_LANGUAGE}" ]; then - # Flag was not set, default to false - eval "${VALIDATE_LANGUAGE}='false'" - fi - else - # No linter flags were set - default all to true - eval "${VALIDATE_LANGUAGE}='true'" + ############################### + # Convert string to lowercase # + ############################### + ACTIONS_RUNNER_DEBUG="${ACTIONS_RUNNER_DEBUG,,}" + + ############################ + # Set to true if not false # + ############################ + if [ "${ACTIONS_RUNNER_DEBUG}" != "false" ]; then + ACTIONS_RUNNER_DEBUG="true" fi - done - ####################################### - # Print which linters we are enabling # - ####################################### - # Loop through all languages - for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do - # build the variable - VALIDATE_LANGUAGE="VALIDATE_${LANGUAGE}" - if [[ ${!VALIDATE_LANGUAGE} == "true" ]]; then - # We need to validate - PRINT_ARRAY+=("- Validating [$LANGUAGE] files in code base...") - else - # We are skipping the language - PRINT_ARRAY+=("- Excluding [$LANGUAGE] files in code base...") - fi - done + ########################### + # Print the validate info # + ########################### + for LINE in "${PRINT_ARRAY[@]}"; do + debug "${LINE}" + done - ############################## - # Validate Ansible Directory # - ############################## - if [ -z "${ANSIBLE_DIRECTORY}" ]; then - # No Value, need to default - ANSIBLE_DIRECTORY="${DEFAULT_ANSIBLE_DIRECTORY}" - else - # Check if first char is '/' - if [[ ${ANSIBLE_DIRECTORY:0:1} == "/" ]]; then - # Remove first char - ANSIBLE_DIRECTORY="${ANSIBLE_DIRECTORY:1}" - fi - # Need to give it full path - TEMP_ANSIBLE_DIRECTORY="${GITHUB_WORKSPACE}/${ANSIBLE_DIRECTORY}" - # Set the value - ANSIBLE_DIRECTORY="${TEMP_ANSIBLE_DIRECTORY}" - fi - - ############################### - # Get the disable errors flag # - ############################### - if [ -z "${DISABLE_ERRORS}" ]; then - ################################## - # No flag passed, set to default # - ################################## - DISABLE_ERRORS="${DEFAULT_DISABLE_ERRORS}" - fi - - ############################### - # Convert string to lowercase # - ############################### - DISABLE_ERRORS="${DISABLE_ERRORS,,}" - - ############################ - # Set to false if not true # - ############################ - if [ "${DISABLE_ERRORS}" != "true" ]; then - DISABLE_ERRORS="false" - fi - - ############################ - # Get the run verbose flag # - ############################ - if [ -z "${ACTIONS_RUNNER_DEBUG}" ]; then - ################################## - # No flag passed, set to default # - ################################## - ACTIONS_RUNNER_DEBUG="${DEFAULT_ACTIONS_RUNNER_DEBUG}" - fi - - ############################### - # Convert string to lowercase # - ############################### - ACTIONS_RUNNER_DEBUG="${ACTIONS_RUNNER_DEBUG,,}" - - ############################ - # Set to true if not false # - ############################ - if [ "${ACTIONS_RUNNER_DEBUG}" != "false" ]; then - ACTIONS_RUNNER_DEBUG="true" - fi - - ########################### - # Print the validate info # - ########################### - for LINE in "${PRINT_ARRAY[@]}"; do - debug "${LINE}" - done - - debug "--- DEBUG INFO ---" - debug "---------------------------------------------" - RUNNER=$(whoami) - debug "Runner:[${RUNNER}]" - PRINTENV=$(printenv) - debug "ENV:" - debug "${PRINTENV}" - debug "---------------------------------------------" + debug "--- DEBUG INFO ---" + debug "---------------------------------------------" + RUNNER=$(whoami) + debug "Runner:[${RUNNER}]" + PRINTENV=$(printenv) + debug "ENV:" + debug "${PRINTENV}" + debug "---------------------------------------------" } diff --git a/lib/worker.sh b/lib/worker.sh index b5d5988c..929b3ff9 100755 --- a/lib/worker.sh +++ b/lib/worker.sh @@ -10,883 +10,883 @@ ################################################################################ #### Function LintCodebase ##################################################### function LintCodebase() { - #################### - # Pull in the vars # - #################### - FILE_TYPE="${1}" && shift # Pull the variable and remove from array path (Example: JSON) - LINTER_NAME="${1}" && shift # Pull the variable and remove from array path (Example: jsonlint) - LINTER_COMMAND="${1}" && shift # Pull the variable and remove from array path (Example: jsonlint -c ConfigFile /path/to/file) - FILE_EXTENSIONS="${1}" && shift # Pull the variable and remove from array path (Example: *.json) - FILE_ARRAY=("$@") # Array of files to validate (Example: ${FILE_ARRAY_JSON}) + #################### + # Pull in the vars # + #################### + FILE_TYPE="${1}" && shift # Pull the variable and remove from array path (Example: JSON) + LINTER_NAME="${1}" && shift # Pull the variable and remove from array path (Example: jsonlint) + LINTER_COMMAND="${1}" && shift # Pull the variable and remove from array path (Example: jsonlint -c ConfigFile /path/to/file) + FILE_EXTENSIONS="${1}" && shift # Pull the variable and remove from array path (Example: *.json) + FILE_ARRAY=("$@") # Array of files to validate (Example: ${FILE_ARRAY_JSON}) - ###################### - # Create Print Array # - ###################### - PRINT_ARRAY=() - - ################ - # print header # - ################ - PRINT_ARRAY+=("") - PRINT_ARRAY+=("----------------------------------------------") - PRINT_ARRAY+=("----------------------------------------------") - PRINT_ARRAY+=("Linting [${FILE_TYPE}] files...") - PRINT_ARRAY+=("----------------------------------------------") - PRINT_ARRAY+=("----------------------------------------------") - - ##################################### - # Validate we have linter installed # - ##################################### - VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - # Failed - error "Failed to find [${LINTER_NAME}] in system!" - fatal "[${VALIDATE_INSTALL_CMD}]" - else - # Success - debug "Successfully found binary for ${F[W]}[${LINTER_NAME}]${F[B]} in system location: ${F[W]}[${VALIDATE_INSTALL_CMD}]" - fi - - ########################## - # Initialize empty Array # - ########################## - LIST_FILES=() - - ################ - # Set the flag # - ################ - SKIP_FLAG=0 - - ############################################################ - # Check to see if we need to go through array or all files # - ############################################################ - if [ ${#FILE_ARRAY[@]} -eq 0 ] && [ "${VALIDATE_ALL_CODEBASE}" == "false" ]; then - # No files found in commit and user has asked to not validate code base - SKIP_FLAG=1 - debug " - No files found in changeset to lint for language:[${FILE_TYPE}]" - elif [ ${#FILE_ARRAY[@]} -ne 0 ]; then - # We have files added to array of files to check - LIST_FILES=("${FILE_ARRAY[@]}") # Copy the array into list - else - ############################################################################### - # Set the file seperator to newline to allow for grabbing objects with spaces # - ############################################################################### - IFS=$'\n' - - ################################# - # Get list of all files to lint # - ################################# - mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex "${FILE_EXTENSIONS}" 2>&1) - - ########################### - # Set IFS back to default # - ########################### - IFS="${DEFAULT_IFS}" - - ############################################################ - # Set it back to empty if loaded with blanks from scanning # - ############################################################ - if [ ${#LIST_FILES[@]} -lt 1 ]; then - ###################### - # Set to empty array # - ###################### - LIST_FILES=() - ############################# - # Skip as we found no files # - ############################# - SKIP_FLAG=1 - fi - fi - - ############################### - # Check if any data was found # - ############################### - if [ ${SKIP_FLAG} -eq 0 ]; then ###################### - # Print Header array # + # Create Print Array # ###################### - for LINE in "${PRINT_ARRAY[@]}"; do - ######################### - # Print the header info # - ######################### - info "${LINE}" - done + PRINT_ARRAY=() - ######################################## - # Prepare context if TAP format output # - ######################################## - if IsTAP ; then - TMPFILE=$(mktemp -q "/tmp/super-linter-${FILE_TYPE}.XXXXXX") - INDEX=0 - mkdir -p "${REPORT_OUTPUT_FOLDER}" - REPORT_OUTPUT_FILE="${REPORT_OUTPUT_FOLDER}/super-linter-${FILE_TYPE}.${OUTPUT_FORMAT}" - fi + ################ + # print header # + ################ + PRINT_ARRAY+=("") + PRINT_ARRAY+=("----------------------------------------------") + PRINT_ARRAY+=("----------------------------------------------") + PRINT_ARRAY+=("Linting [${FILE_TYPE}] files...") + PRINT_ARRAY+=("----------------------------------------------") + PRINT_ARRAY+=("----------------------------------------------") - ################## - # Lint the files # - ################## - for FILE in "${LIST_FILES[@]}"; do - ################################### - # Get the file name and directory # - ################################### - FILE_NAME=$(basename "${FILE}" 2>&1) - DIR_NAME=$(dirname "${FILE}" 2>&1) - - ##################################################### - # Make sure we dont lint node modules or test cases # - ##################################################### - if [[ ${FILE} == *"node_modules"* ]]; then - # This is a node modules file - continue - elif [[ ${FILE} == *"${TEST_CASE_FOLDER}"* ]]; then - # This is the test cases, we should always skip - continue - elif [[ ${FILE} == *".git"* ]]; then - # This is likely the .git folder and shouldnt be parsed - continue - fi - - ################################## - # Increase the linted file index # - ################################## - (("INDEX++")) - - ############## - # File print # - ############## - info "---------------------------" - info "File:[${FILE}]" - - ################################# - # Add the language to the array # - ################################# - LINTED_LANGUAGES_ARRAY+=("${FILE_TYPE}") - - #################### - # Set the base Var # - #################### - LINT_CMD='' - - #################################### - # Corner case for pwsh subshell # - # - PowerShell (PSScriptAnalyzer) # - # - ARM (arm-ttk) # - #################################### - if [[ ${FILE_TYPE} == "POWERSHELL" ]] || [[ ${FILE_TYPE} == "ARM" ]]; then - ################################ - # Lint the file with the rules # - ################################ - # Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell - LINT_CMD=$( - cd "${GITHUB_WORKSPACE}" || exit - pwsh -NoProfile -NoLogo -Command "${LINTER_COMMAND} ${FILE}; if (\${Error}.Count) { exit 1 }" - exit $? 2>&1 - ) - ############################################################################### - # Corner case for groovy as we have to pass it as path and file in ant format # - ############################################################################### - elif [[ ${FILE_TYPE} == "GROOVY" ]]; then - ####################################### - # Lint the file with the updated path # - ####################################### - LINT_CMD=$( - cd "${GITHUB_WORKSPACE}" || exit - ${LINTER_COMMAND} --path "${DIR_NAME}" --files "$FILE_NAME" 2>&1 - ) - else - ################################ - # Lint the file with the rules # - ################################ - LINT_CMD=$( - cd "${GITHUB_WORKSPACE}" || exit - ${LINTER_COMMAND} "${FILE}" 2>&1 - ) - fi - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - ######### - # Error # - ######### - error "Found errors in [${LINTER_NAME}] linter!" - error "[${LINT_CMD}]" - error "Linter CMD:[${LINTER_COMMAND} ${FILE}]" - # Increment the error count - (("ERRORS_FOUND_${FILE_TYPE}++")) - - ####################################################### - # Store the linting as a temporary file in TAP format # - ####################################################### - if IsTAP ; then - NotOkTap "${INDEX}" "${FILE}" "${TMPFILE}" - AddDetailedMessageIfEnabled "${LINT_CMD}" "${TMPFILE}" - fi - else - ########### - # Success # - ########### - info " - File:${F[W]}[${FILE_NAME}]${F[B]} was linted with ${F[W]}[${LINTER_NAME}]${F[B]} successfully" - - ####################################################### - # Store the linting as a temporary file in TAP format # - ####################################################### - if IsTAP ; then - OkTap "${INDEX}" "${FILE}" "${TMPFILE}" - fi - fi - done - - ################################# - # Generate report in TAP format # - ################################# - if IsTAP && [ ${INDEX} -gt 0 ] ; then - HeaderTap "${INDEX}" "${REPORT_OUTPUT_FILE}" - cat "${TMPFILE}" >> "${REPORT_OUTPUT_FILE}" - fi - fi -} -################################################################################ -#### Function TestCodebase ##################################################### -function TestCodebase() { - #################### - # Pull in the vars # - #################### - FILE_TYPE="${1}" # Pull the variable and remove from array path (Example: JSON) - LINTER_NAME="${2}" # Pull the variable and remove from array path (Example: jsonlint) - LINTER_COMMAND="${3}" # Pull the variable and remove from array path (Example: jsonlint -c ConfigFile /path/to/file) - FILE_EXTENSIONS="${4}" # Pull the variable and remove from array path (Example: *.json) - INDVIDUAL_TEST_FOLDER="${5}" # Folder for specific tests - TESTS_RAN=0 # Incremented when tests are ran, this will help find failed finds - - ################ - # print header # - ################ - info "----------------------------------------------" - info "----------------------------------------------" - info "Testing Codebase [${FILE_TYPE}] files..." - info "----------------------------------------------" - info "----------------------------------------------" - - ##################################### - # Validate we have linter installed # - ##################################### - VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - # Failed - error "Failed to find [${LINTER_NAME}] in system!" - fatal "[${VALIDATE_INSTALL_CMD}]" - else - # Success - info "Successfully found binary for ${F[W]}[${LINTER_NAME}]${F[B]} in system location: ${F[W]}[${VALIDATE_INSTALL_CMD}]" - fi - - ########################## - # Initialize empty Array # - ########################## - LIST_FILES=() - - ################################# - # Get list of all files to lint # - ################################# - mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/${INDVIDUAL_TEST_FOLDER}" -type f -regex "${FILE_EXTENSIONS}" ! -path "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/ansible/ghe-initialize/*" | sort 2>&1) - - ######################################## - # Prepare context if TAP output format # - ######################################## - if IsTAP ; then - TMPFILE=$(mktemp -q "/tmp/super-linter-${FILE_TYPE}.XXXXXX") - mkdir -p "${REPORT_OUTPUT_FOLDER}" - REPORT_OUTPUT_FILE="${REPORT_OUTPUT_FOLDER}/super-linter-${FILE_TYPE}.${OUTPUT_FORMAT}" - fi - - ################## - # Lint the files # - ################## - for FILE in "${LIST_FILES[@]}"; do - ##################### - # Get the file name # - ##################### - FILE_NAME=$(basename "${FILE}" 2>&1) - DIR_NAME=$(dirname "${FILE}" 2>&1) - - ############################ - # Get the file pass status # - ############################ - # Example: markdown_good_1.md -> good - FILE_STATUS=$(echo "${FILE_NAME}" | cut -f2 -d'_') - - ######################################################### - # If not found, assume it should be linted successfully # - ######################################################### - if [ -z "${FILE_STATUS}" ] || [[ ${FILE} == *"README"* ]]; then - ################################## - # Set to good for proper linting # - ################################## - FILE_STATUS="good" - fi - - ############## - # File print # - ############## - info "---------------------------" - info "File:[${FILE}]" - - ######################## - # Set the lint command # - ######################## - LINT_CMD='' - - ####################################### - # Check if docker and get folder name # - ####################################### - if [[ ${FILE_TYPE} == "DOCKER" ]]; then - if [[ ${FILE} == *"good"* ]]; then - ############# - # Good file # - ############# - FILE_STATUS='good' - else - ############ - # Bad file # - ############ - FILE_STATUS='bad' - fi - fi - - ##################### - # Check for ansible # - ##################### - if [[ ${FILE_TYPE} == "ANSIBLE" ]]; then - ######################################## - # Make sure we dont lint certain files # - ######################################## - if [[ ${FILE} == *"vault.yml"* ]] || [[ ${FILE} == *"galaxy.yml"* ]]; then - # This is a file we dont look at - continue - fi - - ################################ - # Lint the file with the rules # - ################################ - LINT_CMD=$( - cd "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/${INDVIDUAL_TEST_FOLDER}" || exit - ${LINTER_COMMAND} "${FILE}" 2>&1 - ) - elif [[ ${FILE_TYPE} == "POWERSHELL" ]] || [[ ${FILE_TYPE} == "ARM" ]]; then - ################################ - # Lint the file with the rules # - ################################ - # Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell - LINT_CMD=$( - cd "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}" || exit - pwsh -NoProfile -NoLogo -Command "${LINTER_COMMAND} ${FILE}; if (\${Error}.Count) { exit 1 }" - exit $? 2>&1 - ) - ############################################################################### - # Corner case for groovy as we have to pass it as path and file in ant format # - ############################################################################### - elif [[ ${FILE_TYPE} == "GROOVY" ]]; then - ####################################### - # Lint the file with the updated path # - ####################################### - LINT_CMD=$( - cd "${GITHUB_WORKSPACE}" || exit - ${LINTER_COMMAND} --path "${DIR_NAME}" --files "$FILE_NAME" 2>&1 - ) - else - ################################ - # Lint the file with the rules # - ################################ - LINT_CMD=$( - cd "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}" || exit - ${LINTER_COMMAND} "${FILE}" 2>&1 - ) - fi + ##################################### + # Validate we have linter installed # + ##################################### + VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) ####################### # Load the error code # ####################### ERROR_CODE=$? - ######################################## - # Increment counter that check was ran # - ######################################## - (("TESTS_RAN++")) - - ######################################## - # Check for if it was supposed to pass # - ######################################## - if [[ ${FILE_STATUS} == "good" ]]; then - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - ######### - # Error # - ######### - error "Found errors in [${LINTER_NAME}] linter!" - error "[${LINT_CMD}]" - error "Linter CMD:[${LINTER_COMMAND} ${FILE}]" - # Increment the error count - (("ERRORS_FOUND_${FILE_TYPE}++")) - else - ########### - # Success # - ########### - info " - File:${F[W]}[${FILE_NAME}]${F[B]} was linted with ${F[W]}[${LINTER_NAME}]${F[B]} successfully" - fi - ####################################################### - # Store the linting as a temporary file in TAP format # - ####################################################### - if IsTAP ; then - OkTap "${TESTS_RAN}" "${FILE_NAME}" "${TMPFILE}" - fi + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + # Failed + error "Failed to find [${LINTER_NAME}] in system!" + fatal "[${VALIDATE_INSTALL_CMD}]" else - ####################################### - # File status = bad, this should fail # - ####################################### - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -eq 0 ]; then - ######### - # Error # - ######### - error "Found errors in [${LINTER_NAME}] linter!" - error "This file should have failed test case!" - error "Command run:${NC}[\$${LINT_CMD}]" - error "[${LINT_CMD}]" - error "Linter CMD:[${LINTER_COMMAND} ${FILE}]" - # Increment the error count - (("ERRORS_FOUND_${FILE_TYPE}++")) - else - ########### - # Success # - ########### - info " - File:${F[W]}[${FILE_NAME}]${F[B]} failed test case with ${F[W]}[${LINTER_NAME}]${F[B]} successfully" - fi - ####################################################### - # Store the linting as a temporary file in TAP format # - ####################################################### - if IsTAP ; then - NotOkTap "${TESTS_RAN}" "${FILE_NAME}" "${TMPFILE}" - AddDetailedMessageIfEnabled "${LINT_CMD}" "${TMPFILE}" - fi - fi - done - - ########################################################################### - # Generate report in TAP format and validate with the expected TAP output # - ########################################################################### - if IsTAP && [ ${TESTS_RAN} -gt 0 ] ; then - HeaderTap "${TESTS_RAN}" "${REPORT_OUTPUT_FILE}" - cat "${TMPFILE}" >> "${REPORT_OUTPUT_FILE}" - - ######################################################################## - # If expected TAP report exists then compare with the generated report # - ######################################################################## - EXPECTED_FILE="${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/${INDVIDUAL_TEST_FOLDER}/reports/expected-${FILE_TYPE}.tap" - if [ -e "${EXPECTED_FILE}" ] ; then - TMPFILE=$(mktemp -q "/tmp/diff-${FILE_TYPE}.XXXXXX") - ## Ignore white spaces, case sensitive - if ! diff -a -w -i "${EXPECTED_FILE}" "${REPORT_OUTPUT_FILE}" > "${TMPFILE}" 2>&1; then - ############################################# - # We failed to compare the reporting output # - ############################################# - error "Failed to assert TAP output:[${LINTER_NAME}]"! - info "Please validate the asserts!" - cat "${TMPFILE}" - exit 1 - else # Success - info "Successfully validation in the expected TAP format for ${F[W]}[${LINTER_NAME}]" - fi - else - warn "No TAP expected file found at:[${EXPECTED_FILE}]" - info "skipping report assertions" - ##################################### - # Append the file type to the array # - ##################################### - WARNING_ARRAY_TEST+=("${FILE_TYPE}") + debug "Successfully found binary for ${F[W]}[${LINTER_NAME}]${F[B]} in system location: ${F[W]}[${VALIDATE_INSTALL_CMD}]" fi - fi - ############################## - # Validate we ran some tests # - ############################## - if [ "${TESTS_RAN}" -eq 0 ]; then - ################################################# - # We failed to find files and no tests were ran # - ################################################# - error "Failed to find any tests ran for the Linter:[${LINTER_NAME}]"! - fatal "Please validate logic or that tests exist!" - fi + ########################## + # Initialize empty Array # + ########################## + LIST_FILES=() + + ################ + # Set the flag # + ################ + SKIP_FLAG=0 + + ############################################################ + # Check to see if we need to go through array or all files # + ############################################################ + if [ ${#FILE_ARRAY[@]} -eq 0 ] && [ "${VALIDATE_ALL_CODEBASE}" == "false" ]; then + # No files found in commit and user has asked to not validate code base + SKIP_FLAG=1 + debug " - No files found in changeset to lint for language:[${FILE_TYPE}]" + elif [ ${#FILE_ARRAY[@]} -ne 0 ]; then + # We have files added to array of files to check + LIST_FILES=("${FILE_ARRAY[@]}") # Copy the array into list + else + ############################################################################### + # Set the file seperator to newline to allow for grabbing objects with spaces # + ############################################################################### + IFS=$'\n' + + ################################# + # Get list of all files to lint # + ################################# + mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}" -type f -regex "${FILE_EXTENSIONS}" 2>&1) + + ########################### + # Set IFS back to default # + ########################### + IFS="${DEFAULT_IFS}" + + ############################################################ + # Set it back to empty if loaded with blanks from scanning # + ############################################################ + if [ ${#LIST_FILES[@]} -lt 1 ]; then + ###################### + # Set to empty array # + ###################### + LIST_FILES=() + ############################# + # Skip as we found no files # + ############################# + SKIP_FLAG=1 + fi + fi + + ############################### + # Check if any data was found # + ############################### + if [ ${SKIP_FLAG} -eq 0 ]; then + ###################### + # Print Header array # + ###################### + for LINE in "${PRINT_ARRAY[@]}"; do + ######################### + # Print the header info # + ######################### + info "${LINE}" + done + + ######################################## + # Prepare context if TAP format output # + ######################################## + if IsTAP; then + TMPFILE=$(mktemp -q "/tmp/super-linter-${FILE_TYPE}.XXXXXX") + INDEX=0 + mkdir -p "${REPORT_OUTPUT_FOLDER}" + REPORT_OUTPUT_FILE="${REPORT_OUTPUT_FOLDER}/super-linter-${FILE_TYPE}.${OUTPUT_FORMAT}" + fi + + ################## + # Lint the files # + ################## + for FILE in "${LIST_FILES[@]}"; do + ################################### + # Get the file name and directory # + ################################### + FILE_NAME=$(basename "${FILE}" 2>&1) + DIR_NAME=$(dirname "${FILE}" 2>&1) + + ##################################################### + # Make sure we dont lint node modules or test cases # + ##################################################### + if [[ ${FILE} == *"node_modules"* ]]; then + # This is a node modules file + continue + elif [[ ${FILE} == *"${TEST_CASE_FOLDER}"* ]]; then + # This is the test cases, we should always skip + continue + elif [[ ${FILE} == *".git"* ]]; then + # This is likely the .git folder and shouldnt be parsed + continue + fi + + ################################## + # Increase the linted file index # + ################################## + (("INDEX++")) + + ############## + # File print # + ############## + info "---------------------------" + info "File:[${FILE}]" + + ################################# + # Add the language to the array # + ################################# + LINTED_LANGUAGES_ARRAY+=("${FILE_TYPE}") + + #################### + # Set the base Var # + #################### + LINT_CMD='' + + #################################### + # Corner case for pwsh subshell # + # - PowerShell (PSScriptAnalyzer) # + # - ARM (arm-ttk) # + #################################### + if [[ ${FILE_TYPE} == "POWERSHELL" ]] || [[ ${FILE_TYPE} == "ARM" ]]; then + ################################ + # Lint the file with the rules # + ################################ + # Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell + LINT_CMD=$( + cd "${GITHUB_WORKSPACE}" || exit + pwsh -NoProfile -NoLogo -Command "${LINTER_COMMAND} ${FILE}; if (\${Error}.Count) { exit 1 }" + exit $? 2>&1 + ) + ############################################################################### + # Corner case for groovy as we have to pass it as path and file in ant format # + ############################################################################### + elif [[ ${FILE_TYPE} == "GROOVY" ]]; then + ####################################### + # Lint the file with the updated path # + ####################################### + LINT_CMD=$( + cd "${GITHUB_WORKSPACE}" || exit + ${LINTER_COMMAND} --path "${DIR_NAME}" --files "$FILE_NAME" 2>&1 + ) + else + ################################ + # Lint the file with the rules # + ################################ + LINT_CMD=$( + cd "${GITHUB_WORKSPACE}" || exit + ${LINTER_COMMAND} "${FILE}" 2>&1 + ) + fi + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + ######### + # Error # + ######### + error "Found errors in [${LINTER_NAME}] linter!" + error "[${LINT_CMD}]" + error "Linter CMD:[${LINTER_COMMAND} ${FILE}]" + # Increment the error count + (("ERRORS_FOUND_${FILE_TYPE}++")) + + ####################################################### + # Store the linting as a temporary file in TAP format # + ####################################################### + if IsTAP; then + NotOkTap "${INDEX}" "${FILE}" "${TMPFILE}" + AddDetailedMessageIfEnabled "${LINT_CMD}" "${TMPFILE}" + fi + else + ########### + # Success # + ########### + info " - File:${F[W]}[${FILE_NAME}]${F[B]} was linted with ${F[W]}[${LINTER_NAME}]${F[B]} successfully" + + ####################################################### + # Store the linting as a temporary file in TAP format # + ####################################################### + if IsTAP; then + OkTap "${INDEX}" "${FILE}" "${TMPFILE}" + fi + fi + done + + ################################# + # Generate report in TAP format # + ################################# + if IsTAP && [ ${INDEX} -gt 0 ]; then + HeaderTap "${INDEX}" "${REPORT_OUTPUT_FILE}" + cat "${TMPFILE}" >> "${REPORT_OUTPUT_FILE}" + fi + fi } ################################################################################ -#### Function RunTestCases ##################################################### -function RunTestCases() { - # This loop will run the test cases and exclude user code - # This is called from the automation process to validate new code - # When a PR is opened, the new code is validated with the default branch - # version of linter.sh, and a new container is built with the latest codebase - # for testing. That container is spun up, and ran, - # with the flag: TEST_CASE_RUN=true - # So that the new code can be validated against the test cases +#### Function TestCodebase ##################################################### +function TestCodebase() { + #################### + # Pull in the vars # + #################### + FILE_TYPE="${1}" # Pull the variable and remove from array path (Example: JSON) + LINTER_NAME="${2}" # Pull the variable and remove from array path (Example: jsonlint) + LINTER_COMMAND="${3}" # Pull the variable and remove from array path (Example: jsonlint -c ConfigFile /path/to/file) + FILE_EXTENSIONS="${4}" # Pull the variable and remove from array path (Example: *.json) + INDVIDUAL_TEST_FOLDER="${5}" # Folder for specific tests + TESTS_RAN=0 # Incremented when tests are ran, this will help find failed finds - ################# - # Header prints # - ################# - info "----------------------------------------------" - info "-------------- TEST CASE RUN -----------------" - info "----------------------------------------------" + ################ + # print header # + ################ + info "----------------------------------------------" + info "----------------------------------------------" + info "Testing Codebase [${FILE_TYPE}] files..." + info "----------------------------------------------" + info "----------------------------------------------" - ####################### - # Test case languages # - ####################### - # TestCodebase "Language" "Linter" "Linter-command" "Regex to find files" "Test Folder" - TestCodebase "ANSIBLE" "ansible-lint" "ansible-lint -v -c ${ANSIBLE_LINTER_RULES}" ".*\.\(yml\|yaml\)\$" "ansible" - TestCodebase "ARM" "arm-ttk" "Import-Module ${ARM_TTK_PSD1} ; \${config} = \$(Import-PowerShellDataFile -Path ${ARM_LINTER_RULES}) ; Test-AzTemplate @config -TemplatePath" ".*\.\(json\)\$" "arm" - TestCodebase "BASH" "shellcheck" "shellcheck --color" ".*\.\(sh\|bash\|dash\|ksh\)\$" "shell" - TestCodebase "CLOUDFORMATION" "cfn-lint" "cfn-lint --config-file ${CLOUDFORMATION_LINTER_RULES}" ".*\.\(json\|yml\|yaml\)\$" "cloudformation" - TestCodebase "CLOJURE" "clj-kondo" "clj-kondo --config ${CLOJURE_LINTER_RULES} --lint" ".*\.\(clj\|cljs\|cljc\|edn\)\$" "clojure" - TestCodebase "COFFEESCRIPT" "coffeelint" "coffeelint -f ${COFFEESCRIPT_LINTER_RULES}" ".*\.\(coffee\)\$" "coffeescript" - TestCodebase "CSS" "stylelint" "stylelint --config ${CSS_LINTER_RULES}" ".*\.\(css\)\$" "css" - TestCodebase "DART" "dart" "dartanalyzer --fatal-infos --fatal-warnings --options ${DART_LINTER_RULES}" ".*\.\(dart\)\$" "dart" - TestCodebase "DOCKER" "dockerfilelint" "dockerfilelint -c ${DOCKER_LINTER_RULES}" ".*\(Dockerfile\)\$" "docker" - TestCodebase "EDITORCONFIG" "editorconfig-checker" "editorconfig-checker" ".*\.ext$" "editorconfig-checker" - TestCodebase "ENV" "dotenv-linter" "dotenv-linter" ".*\.\(env\)\$" "env" - TestCodebase "GO" "golangci-lint" "golangci-lint run -c ${GO_LINTER_RULES}" ".*\.\(go\)\$" "golang" - TestCodebase "GROOVY" "npm-groovy-lint" "npm-groovy-lint -c $GROOVY_LINTER_RULES --failon error" ".*\.\(groovy\|jenkinsfile\|gradle\)\$" "groovy" - TestCodebase "HTML" "htmlhint" "htmlhint --config ${HTML_LINTER_RULES}" ".*\.\(html\)\$" "html" - TestCodebase "JAVASCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${JAVASCRIPT_LINTER_RULES}" ".*\.\(js\)\$" "javascript" - TestCodebase "JAVASCRIPT_STANDARD" "standard" "standard ${JAVASCRIPT_STANDARD_LINTER_RULES}" ".*\.\(js\)\$" "javascript" - TestCodebase "JSON" "jsonlint" "jsonlint" ".*\.\(json\)\$" "json" - TestCodebase "KOTLIN" "ktlint" "ktlint" ".*\.\(kt\|kts\)\$" "kotlin" - TestCodebase "LUA" "lua" "luacheck" ".*\.\(lua\)\$" "lua" - TestCodebase "MARKDOWN" "markdownlint" "markdownlint -c ${MARKDOWN_LINTER_RULES}" ".*\.\(md\)\$" "markdown" - TestCodebase "PERL" "perl" "perl -Mstrict -cw" ".*\.\(pl\)\$" "perl" - TestCodebase "PHP" "php" "php -l" ".*\.\(php\)\$" "php" - TestCodebase "PHP_PHPSTAN" "phpstan" "phpstan analyse --no-progress --no-ansi -c ${PHPSTAN_LINTER_RULES}" ".*\.\(php\)\$" "php" - TestCodebase "OPENAPI" "spectral" "spectral lint -r ${OPENAPI_LINTER_RULES}" ".*\.\(ymlopenapi\|jsonopenapi\)\$" "openapi" - TestCodebase "POWERSHELL" "pwsh" "Invoke-ScriptAnalyzer -EnableExit -Settings ${POWERSHELL_LINTER_RULES} -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "powershell" - TestCodebase "PROTOBUF" "protolint" "protolint lint --config_path ${PROTOBUF_LINTER_RULES}" ".*\.\(proto\)\$" "protobuf" - TestCodebase "PYTHON_PYLINT" "pylint" "pylint --rcfile ${PYTHON_PYLINT_LINTER_RULES}" ".*\.\(py\)\$" "python" - TestCodebase "PYTHON_FLAKE8" "flake8" "flake8 --config ${PYTHON_FLAKE8_LINTER_RULES}" ".*\.\(py\)\$" "python" - TestCodebase "RAKU" "raku" "raku -c" ".*\.\(raku\|rakumod\|rakutest\|pm6\|pl6\|p6\)\$" "raku" - TestCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES}" ".*\.\(rb\)\$" "ruby" - TestCodebase "STATES" "asl-validator" "asl-validator --json-path" ".*\.\(json\)\$" "states" - TestCodebase "TERRAFORM" "tflint" "tflint -c ${TERRAFORM_LINTER_RULES}" ".*\.\(tf\)\$" "terraform" - TestCodebase "TERRAFORM_TERRASCAN" "terrascan" "terrascan -f " ".*\.\(tf\)\$" "terraform_terrascan" - TestCodebase "TYPESCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${TYPESCRIPT_LINTER_RULES}" ".*\.\(ts\)\$" "typescript" - TestCodebase "TYPESCRIPT_STANDARD" "standard" "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin ${TYPESCRIPT_STANDARD_LINTER_RULES}" ".*\.\(ts\)\$" "typescript" - TestCodebase "XML" "xmllint" "xmllint" ".*\.\(xml\)\$" "xml" - TestCodebase "YAML" "yamllint" "yamllint -c ${YAML_LINTER_RULES}" ".*\.\(yml\|yaml\)\$" "yml" + ##################################### + # Validate we have linter installed # + ##################################### + VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) - ################# - # Footer prints # - ################# - # Call the footer to display run information - # and exit with error code - Footer -} -################################################################################ -#### Function LintAnsibleFiles ################################################# -function LintAnsibleFiles() { - ###################### - # Create Print Array # - ###################### - PRINT_ARRAY=() + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? - ################ - # print header # - ################ - PRINT_ARRAY+=("") - PRINT_ARRAY+=("----------------------------------------------") - PRINT_ARRAY+=("----------------------------------------------") - PRINT_ARRAY+=("Linting [Ansible] files...") - PRINT_ARRAY+=("----------------------------------------------") - PRINT_ARRAY+=("----------------------------------------------") + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + # Failed + error "Failed to find [${LINTER_NAME}] in system!" + fatal "[${VALIDATE_INSTALL_CMD}]" + else + # Success + info "Successfully found binary for ${F[W]}[${LINTER_NAME}]${F[B]} in system location: ${F[W]}[${VALIDATE_INSTALL_CMD}]" + fi - ###################### - # Name of the linter # - ###################### - LINTER_NAME="ansible-lint" - - ########################################### - # Validate we have ansible-lint installed # - ########################################### - VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - # Failed - error "Failed to find ${LINTER_NAME} in system!" - fatal "[${VALIDATE_INSTALL_CMD}]" - else - # Success - debug "Successfully found binary in system" - debug "Location:[${VALIDATE_INSTALL_CMD}]" - fi - - ########################## - # Initialize empty Array # - ########################## - LIST_FILES=() - - ####################### - # Create flag to skip # - ####################### - SKIP_FLAG=0 - - ###################################################### - # Only go into ansible linter if we have base folder # - ###################################################### - if [ -d "${ANSIBLE_DIRECTORY}" ]; then + ########################## + # Initialize empty Array # + ########################## + LIST_FILES=() ################################# # Get list of all files to lint # ################################# - mapfile -t LIST_FILES < <(ls "${ANSIBLE_DIRECTORY}"/*.{yaml,yml} 2>&1) - - ############################################################### - # Set the list to empty if only MD and TXT files were changed # - ############################################################### - # No need to run the full ansible checks on read only file changes - if [ "${READ_ONLY_CHANGE_FLAG}" -eq 0 ]; then - ########################## - # Set the array to empty # - ########################## - LIST_FILES=() - ################################### - # Send message that were skipping # - ################################### - debug "- Skipping Ansible lint run as file(s) that were modified were read only..." - ############################ - # Create flag to skip loop # - ############################ - SKIP_FLAG=1 - fi - - #################################### - # Check if we have data to look at # - #################################### - if [ ${SKIP_FLAG} -eq 0 ]; then - for LINE in "${PRINT_ARRAY[@]}"; do - ######################### - # Print the header line # - ######################### - info "${LINE}" - done - fi + mapfile -t LIST_FILES < <(find "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/${INDVIDUAL_TEST_FOLDER}" -type f -regex "${FILE_EXTENSIONS}" ! -path "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/ansible/ghe-initialize/*" | sort 2>&1) ######################################## # Prepare context if TAP output format # ######################################## - if IsTAP ; then - TMPFILE=$(mktemp -q "/tmp/super-linter-${FILE_TYPE}.XXXXXX") - INDEX=0 - mkdir -p "${REPORT_OUTPUT_FOLDER}" - REPORT_OUTPUT_FILE="${REPORT_OUTPUT_FOLDER}/super-linter-${FILE_TYPE}.${OUTPUT_FORMAT}" + if IsTAP; then + TMPFILE=$(mktemp -q "/tmp/super-linter-${FILE_TYPE}.XXXXXX") + mkdir -p "${REPORT_OUTPUT_FOLDER}" + REPORT_OUTPUT_FILE="${REPORT_OUTPUT_FOLDER}/super-linter-${FILE_TYPE}.${OUTPUT_FORMAT}" fi ################## # Lint the files # ################## for FILE in "${LIST_FILES[@]}"; do + ##################### + # Get the file name # + ##################### + FILE_NAME=$(basename "${FILE}" 2>&1) + DIR_NAME=$(dirname "${FILE}" 2>&1) - ######################################## - # Make sure we dont lint certain files # - ######################################## - if [[ ${FILE} == *"vault.yml"* ]] || [[ ${FILE} == *"galaxy.yml"* ]]; then - # This is a file we dont look at - continue - fi + ############################ + # Get the file pass status # + ############################ + # Example: markdown_good_1.md -> good + FILE_STATUS=$(echo "${FILE_NAME}" | cut -f2 -d'_') - ################################## - # Increase the linted file index # - ################################## - (("INDEX++")) - - #################### - # Get the filename # - #################### - FILE_NAME=$(basename "${ANSIBLE_DIRECTORY}/${FILE}" 2>&1) - - ############## - # File print # - ############## - info "---------------------------" - info "File:[${FILE}]" - - ################################ - # Lint the file with the rules # - ################################ - LINT_CMD=$("${LINTER_NAME}" -v -c "${ANSIBLE_LINTER_RULES}" "${ANSIBLE_DIRECTORY}/${FILE}" 2>&1) - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ ${ERROR_CODE} -ne 0 ]; then - ######### - # Error # - ######### - error "Found errors in [${LINTER_NAME}] linter!" - error "[${LINT_CMD}]" - # Increment error count - ((ERRORS_FOUND_ANSIBLE++)) - - ####################################################### - # Store the linting as a temporary file in TAP format # - ####################################################### - if IsTAP ; then - NotOkTap "${INDEX}" "${FILE}" "${TMPFILE}" - AddDetailedMessageIfEnabled "${LINT_CMD}" "${TMPFILE}" + ######################################################### + # If not found, assume it should be linted successfully # + ######################################################### + if [ -z "${FILE_STATUS}" ] || [[ ${FILE} == *"README"* ]]; then + ################################## + # Set to good for proper linting # + ################################## + FILE_STATUS="good" fi - else - ########### - # Success # - ########### - info " - File:${F[W]}[${FILE_NAME}]${F[B]} was linted with ${F[W]}[${LINTER_NAME}]${F[B]} successfully" + ############## + # File print # + ############## + info "---------------------------" + info "File:[${FILE}]" - ####################################################### - # Store the linting as a temporary file in TAP format # - ####################################################### - if IsTAP ; then - OkTap "${INDEX}" "${FILE}" "${TMPFILE}" + ######################## + # Set the lint command # + ######################## + LINT_CMD='' + + ####################################### + # Check if docker and get folder name # + ####################################### + if [[ ${FILE_TYPE} == "DOCKER" ]]; then + if [[ ${FILE} == *"good"* ]]; then + ############# + # Good file # + ############# + FILE_STATUS='good' + else + ############ + # Bad file # + ############ + FILE_STATUS='bad' + fi + fi + + ##################### + # Check for ansible # + ##################### + if [[ ${FILE_TYPE} == "ANSIBLE" ]]; then + ######################################## + # Make sure we dont lint certain files # + ######################################## + if [[ ${FILE} == *"vault.yml"* ]] || [[ ${FILE} == *"galaxy.yml"* ]]; then + # This is a file we dont look at + continue + fi + + ################################ + # Lint the file with the rules # + ################################ + LINT_CMD=$( + cd "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/${INDVIDUAL_TEST_FOLDER}" || exit + ${LINTER_COMMAND} "${FILE}" 2>&1 + ) + elif [[ ${FILE_TYPE} == "POWERSHELL" ]] || [[ ${FILE_TYPE} == "ARM" ]]; then + ################################ + # Lint the file with the rules # + ################################ + # Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell + LINT_CMD=$( + cd "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}" || exit + pwsh -NoProfile -NoLogo -Command "${LINTER_COMMAND} ${FILE}; if (\${Error}.Count) { exit 1 }" + exit $? 2>&1 + ) + ############################################################################### + # Corner case for groovy as we have to pass it as path and file in ant format # + ############################################################################### + elif [[ ${FILE_TYPE} == "GROOVY" ]]; then + ####################################### + # Lint the file with the updated path # + ####################################### + LINT_CMD=$( + cd "${GITHUB_WORKSPACE}" || exit + ${LINTER_COMMAND} --path "${DIR_NAME}" --files "$FILE_NAME" 2>&1 + ) + else + ################################ + # Lint the file with the rules # + ################################ + LINT_CMD=$( + cd "${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}" || exit + ${LINTER_COMMAND} "${FILE}" 2>&1 + ) + fi + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ######################################## + # Increment counter that check was ran # + ######################################## + (("TESTS_RAN++")) + + ######################################## + # Check for if it was supposed to pass # + ######################################## + if [[ ${FILE_STATUS} == "good" ]]; then + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + ######### + # Error # + ######### + error "Found errors in [${LINTER_NAME}] linter!" + error "[${LINT_CMD}]" + error "Linter CMD:[${LINTER_COMMAND} ${FILE}]" + # Increment the error count + (("ERRORS_FOUND_${FILE_TYPE}++")) + else + ########### + # Success # + ########### + info " - File:${F[W]}[${FILE_NAME}]${F[B]} was linted with ${F[W]}[${LINTER_NAME}]${F[B]} successfully" + fi + ####################################################### + # Store the linting as a temporary file in TAP format # + ####################################################### + if IsTAP; then + OkTap "${TESTS_RAN}" "${FILE_NAME}" "${TMPFILE}" + fi + else + ####################################### + # File status = bad, this should fail # + ####################################### + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -eq 0 ]; then + ######### + # Error # + ######### + error "Found errors in [${LINTER_NAME}] linter!" + error "This file should have failed test case!" + error "Command run:${NC}[\$${LINT_CMD}]" + error "[${LINT_CMD}]" + error "Linter CMD:[${LINTER_COMMAND} ${FILE}]" + # Increment the error count + (("ERRORS_FOUND_${FILE_TYPE}++")) + else + ########### + # Success # + ########### + info " - File:${F[W]}[${FILE_NAME}]${F[B]} failed test case with ${F[W]}[${LINTER_NAME}]${F[B]} successfully" + fi + ####################################################### + # Store the linting as a temporary file in TAP format # + ####################################################### + if IsTAP; then + NotOkTap "${TESTS_RAN}" "${FILE_NAME}" "${TMPFILE}" + AddDetailedMessageIfEnabled "${LINT_CMD}" "${TMPFILE}" + fi fi - fi done - ################################# - # Generate report in TAP format # - ################################# - if IsTAP && [ ${INDEX} -gt 0 ] ; then - HeaderTap "${INDEX}" "${REPORT_OUTPUT_FILE}" - cat "${TMPFILE}" >> "${REPORT_OUTPUT_FILE}" + ########################################################################### + # Generate report in TAP format and validate with the expected TAP output # + ########################################################################### + if IsTAP && [ ${TESTS_RAN} -gt 0 ]; then + HeaderTap "${TESTS_RAN}" "${REPORT_OUTPUT_FILE}" + cat "${TMPFILE}" >> "${REPORT_OUTPUT_FILE}" + + ######################################################################## + # If expected TAP report exists then compare with the generated report # + ######################################################################## + EXPECTED_FILE="${GITHUB_WORKSPACE}/${TEST_CASE_FOLDER}/${INDVIDUAL_TEST_FOLDER}/reports/expected-${FILE_TYPE}.tap" + if [ -e "${EXPECTED_FILE}" ]; then + TMPFILE=$(mktemp -q "/tmp/diff-${FILE_TYPE}.XXXXXX") + ## Ignore white spaces, case sensitive + if ! diff -a -w -i "${EXPECTED_FILE}" "${REPORT_OUTPUT_FILE}" > "${TMPFILE}" 2>&1; then + ############################################# + # We failed to compare the reporting output # + ############################################# + error "Failed to assert TAP output:[${LINTER_NAME}]"! + info "Please validate the asserts!" + cat "${TMPFILE}" + exit 1 + else + # Success + info "Successfully validation in the expected TAP format for ${F[W]}[${LINTER_NAME}]" + fi + else + warn "No TAP expected file found at:[${EXPECTED_FILE}]" + info "skipping report assertions" + ##################################### + # Append the file type to the array # + ##################################### + WARNING_ARRAY_TEST+=("${FILE_TYPE}") + fi + fi + + ############################## + # Validate we ran some tests # + ############################## + if [ "${TESTS_RAN}" -eq 0 ]; then + ################################################# + # We failed to find files and no tests were ran # + ################################################# + error "Failed to find any tests ran for the Linter:[${LINTER_NAME}]"! + fatal "Please validate logic or that tests exist!" + fi +} +################################################################################ +#### Function RunTestCases ##################################################### +function RunTestCases() { + # This loop will run the test cases and exclude user code + # This is called from the automation process to validate new code + # When a PR is opened, the new code is validated with the default branch + # version of linter.sh, and a new container is built with the latest codebase + # for testing. That container is spun up, and ran, + # with the flag: TEST_CASE_RUN=true + # So that the new code can be validated against the test cases + + ################# + # Header prints # + ################# + info "----------------------------------------------" + info "-------------- TEST CASE RUN -----------------" + info "----------------------------------------------" + + ####################### + # Test case languages # + ####################### + # TestCodebase "Language" "Linter" "Linter-command" "Regex to find files" "Test Folder" + TestCodebase "ANSIBLE" "ansible-lint" "ansible-lint -v -c ${ANSIBLE_LINTER_RULES}" ".*\.\(yml\|yaml\)\$" "ansible" + TestCodebase "ARM" "arm-ttk" "Import-Module ${ARM_TTK_PSD1} ; \${config} = \$(Import-PowerShellDataFile -Path ${ARM_LINTER_RULES}) ; Test-AzTemplate @config -TemplatePath" ".*\.\(json\)\$" "arm" + TestCodebase "BASH" "shellcheck" "shellcheck --color" ".*\.\(sh\|bash\|dash\|ksh\)\$" "shell" + TestCodebase "CLOUDFORMATION" "cfn-lint" "cfn-lint --config-file ${CLOUDFORMATION_LINTER_RULES}" ".*\.\(json\|yml\|yaml\)\$" "cloudformation" + TestCodebase "CLOJURE" "clj-kondo" "clj-kondo --config ${CLOJURE_LINTER_RULES} --lint" ".*\.\(clj\|cljs\|cljc\|edn\)\$" "clojure" + TestCodebase "COFFEESCRIPT" "coffeelint" "coffeelint -f ${COFFEESCRIPT_LINTER_RULES}" ".*\.\(coffee\)\$" "coffeescript" + TestCodebase "CSS" "stylelint" "stylelint --config ${CSS_LINTER_RULES}" ".*\.\(css\)\$" "css" + TestCodebase "DART" "dart" "dartanalyzer --fatal-infos --fatal-warnings --options ${DART_LINTER_RULES}" ".*\.\(dart\)\$" "dart" + TestCodebase "DOCKER" "dockerfilelint" "dockerfilelint -c ${DOCKER_LINTER_RULES}" ".*\(Dockerfile\)\$" "docker" + TestCodebase "EDITORCONFIG" "editorconfig-checker" "editorconfig-checker" ".*\.ext$" "editorconfig-checker" + TestCodebase "ENV" "dotenv-linter" "dotenv-linter" ".*\.\(env\)\$" "env" + TestCodebase "GO" "golangci-lint" "golangci-lint run -c ${GO_LINTER_RULES}" ".*\.\(go\)\$" "golang" + TestCodebase "GROOVY" "npm-groovy-lint" "npm-groovy-lint -c $GROOVY_LINTER_RULES --failon error" ".*\.\(groovy\|jenkinsfile\|gradle\)\$" "groovy" + TestCodebase "HTML" "htmlhint" "htmlhint --config ${HTML_LINTER_RULES}" ".*\.\(html\)\$" "html" + TestCodebase "JAVASCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${JAVASCRIPT_LINTER_RULES}" ".*\.\(js\)\$" "javascript" + TestCodebase "JAVASCRIPT_STANDARD" "standard" "standard ${JAVASCRIPT_STANDARD_LINTER_RULES}" ".*\.\(js\)\$" "javascript" + TestCodebase "JSON" "jsonlint" "jsonlint" ".*\.\(json\)\$" "json" + TestCodebase "KOTLIN" "ktlint" "ktlint" ".*\.\(kt\|kts\)\$" "kotlin" + TestCodebase "LUA" "lua" "luacheck" ".*\.\(lua\)\$" "lua" + TestCodebase "MARKDOWN" "markdownlint" "markdownlint -c ${MARKDOWN_LINTER_RULES}" ".*\.\(md\)\$" "markdown" + TestCodebase "PERL" "perl" "perl -Mstrict -cw" ".*\.\(pl\)\$" "perl" + TestCodebase "PHP" "php" "php -l" ".*\.\(php\)\$" "php" + TestCodebase "PHP_PHPSTAN" "phpstan" "phpstan analyse --no-progress --no-ansi -c ${PHPSTAN_LINTER_RULES}" ".*\.\(php\)\$" "php" + TestCodebase "OPENAPI" "spectral" "spectral lint -r ${OPENAPI_LINTER_RULES}" ".*\.\(ymlopenapi\|jsonopenapi\)\$" "openapi" + TestCodebase "POWERSHELL" "pwsh" "Invoke-ScriptAnalyzer -EnableExit -Settings ${POWERSHELL_LINTER_RULES} -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "powershell" + TestCodebase "PROTOBUF" "protolint" "protolint lint --config_path ${PROTOBUF_LINTER_RULES}" ".*\.\(proto\)\$" "protobuf" + TestCodebase "PYTHON_PYLINT" "pylint" "pylint --rcfile ${PYTHON_PYLINT_LINTER_RULES}" ".*\.\(py\)\$" "python" + TestCodebase "PYTHON_FLAKE8" "flake8" "flake8 --config ${PYTHON_FLAKE8_LINTER_RULES}" ".*\.\(py\)\$" "python" + TestCodebase "RAKU" "raku" "raku -c" ".*\.\(raku\|rakumod\|rakutest\|pm6\|pl6\|p6\)\$" "raku" + TestCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES}" ".*\.\(rb\)\$" "ruby" + TestCodebase "STATES" "asl-validator" "asl-validator --json-path" ".*\.\(json\)\$" "states" + TestCodebase "TERRAFORM" "tflint" "tflint -c ${TERRAFORM_LINTER_RULES}" ".*\.\(tf\)\$" "terraform" + TestCodebase "TERRAFORM_TERRASCAN" "terrascan" "terrascan -f " ".*\.\(tf\)\$" "terraform_terrascan" + TestCodebase "TYPESCRIPT_ES" "eslint" "eslint --no-eslintrc -c ${TYPESCRIPT_LINTER_RULES}" ".*\.\(ts\)\$" "typescript" + TestCodebase "TYPESCRIPT_STANDARD" "standard" "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin ${TYPESCRIPT_STANDARD_LINTER_RULES}" ".*\.\(ts\)\$" "typescript" + TestCodebase "XML" "xmllint" "xmllint" ".*\.\(xml\)\$" "xml" + TestCodebase "YAML" "yamllint" "yamllint -c ${YAML_LINTER_RULES}" ".*\.\(yml\|yaml\)\$" "yml" + + ################# + # Footer prints # + ################# + # Call the footer to display run information + # and exit with error code + Footer +} +################################################################################ +#### Function LintAnsibleFiles ################################################# +function LintAnsibleFiles() { + ###################### + # Create Print Array # + ###################### + PRINT_ARRAY=() + + ################ + # print header # + ################ + PRINT_ARRAY+=("") + PRINT_ARRAY+=("----------------------------------------------") + PRINT_ARRAY+=("----------------------------------------------") + PRINT_ARRAY+=("Linting [Ansible] files...") + PRINT_ARRAY+=("----------------------------------------------") + PRINT_ARRAY+=("----------------------------------------------") + + ###################### + # Name of the linter # + ###################### + LINTER_NAME="ansible-lint" + + ########################################### + # Validate we have ansible-lint installed # + ########################################### + VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + # Failed + error "Failed to find ${LINTER_NAME} in system!" + fatal "[${VALIDATE_INSTALL_CMD}]" + else + # Success + debug "Successfully found binary in system" + debug "Location:[${VALIDATE_INSTALL_CMD}]" + fi + + ########################## + # Initialize empty Array # + ########################## + LIST_FILES=() + + ####################### + # Create flag to skip # + ####################### + SKIP_FLAG=0 + + ###################################################### + # Only go into ansible linter if we have base folder # + ###################################################### + if [ -d "${ANSIBLE_DIRECTORY}" ]; then + + ################################# + # Get list of all files to lint # + ################################# + mapfile -t LIST_FILES < <(ls "${ANSIBLE_DIRECTORY}"/*.{yaml,yml} 2>&1) + + ############################################################### + # Set the list to empty if only MD and TXT files were changed # + ############################################################### + # No need to run the full ansible checks on read only file changes + if [ "${READ_ONLY_CHANGE_FLAG}" -eq 0 ]; then + ########################## + # Set the array to empty # + ########################## + LIST_FILES=() + ################################### + # Send message that were skipping # + ################################### + debug "- Skipping Ansible lint run as file(s) that were modified were read only..." + ############################ + # Create flag to skip loop # + ############################ + SKIP_FLAG=1 + fi + + #################################### + # Check if we have data to look at # + #################################### + if [ ${SKIP_FLAG} -eq 0 ]; then + for LINE in "${PRINT_ARRAY[@]}"; do + ######################### + # Print the header line # + ######################### + info "${LINE}" + done + fi + + ######################################## + # Prepare context if TAP output format # + ######################################## + if IsTAP; then + TMPFILE=$(mktemp -q "/tmp/super-linter-${FILE_TYPE}.XXXXXX") + INDEX=0 + mkdir -p "${REPORT_OUTPUT_FOLDER}" + REPORT_OUTPUT_FILE="${REPORT_OUTPUT_FOLDER}/super-linter-${FILE_TYPE}.${OUTPUT_FORMAT}" + fi + + ################## + # Lint the files # + ################## + for FILE in "${LIST_FILES[@]}"; do + + ######################################## + # Make sure we dont lint certain files # + ######################################## + if [[ ${FILE} == *"vault.yml"* ]] || [[ ${FILE} == *"galaxy.yml"* ]]; then + # This is a file we dont look at + continue + fi + + ################################## + # Increase the linted file index # + ################################## + (("INDEX++")) + + #################### + # Get the filename # + #################### + FILE_NAME=$(basename "${ANSIBLE_DIRECTORY}/${FILE}" 2>&1) + + ############## + # File print # + ############## + info "---------------------------" + info "File:[${FILE}]" + + ################################ + # Lint the file with the rules # + ################################ + LINT_CMD=$("${LINTER_NAME}" -v -c "${ANSIBLE_LINTER_RULES}" "${ANSIBLE_DIRECTORY}/${FILE}" 2>&1) + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ ${ERROR_CODE} -ne 0 ]; then + ######### + # Error # + ######### + error "Found errors in [${LINTER_NAME}] linter!" + error "[${LINT_CMD}]" + # Increment error count + ((ERRORS_FOUND_ANSIBLE++)) + + ####################################################### + # Store the linting as a temporary file in TAP format # + ####################################################### + if IsTAP; then + NotOkTap "${INDEX}" "${FILE}" "${TMPFILE}" + AddDetailedMessageIfEnabled "${LINT_CMD}" "${TMPFILE}" + fi + + else + ########### + # Success # + ########### + info " - File:${F[W]}[${FILE_NAME}]${F[B]} was linted with ${F[W]}[${LINTER_NAME}]${F[B]} successfully" + + ####################################################### + # Store the linting as a temporary file in TAP format # + ####################################################### + if IsTAP; then + OkTap "${INDEX}" "${FILE}" "${TMPFILE}" + fi + fi + done + + ################################# + # Generate report in TAP format # + ################################# + if IsTAP && [ ${INDEX} -gt 0 ]; then + HeaderTap "${INDEX}" "${REPORT_OUTPUT_FILE}" + cat "${TMPFILE}" >> "${REPORT_OUTPUT_FILE}" + fi + else + ######################## + # No Ansible dir found # + ######################## + warn "No Ansible base directory found at:[${ANSIBLE_DIRECTORY}]" + debug "skipping ansible lint" fi - else - ######################## - # No Ansible dir found # - ######################## - warn "No Ansible base directory found at:[${ANSIBLE_DIRECTORY}]" - debug "skipping ansible lint" - fi } ################################################################################ #### Function IsTap ############################################################ function IsTAP() { - if [ "${OUTPUT_FORMAT}" == "tap" ] ; then - return 0 - else - return 1 - fi + if [ "${OUTPUT_FORMAT}" == "tap" ]; then + return 0 + else + return 1 + fi } ################################################################################ #### Function TransformTAPDetails ############################################## function TransformTAPDetails() { - DATA=${1} - if [ -n "${DATA}" ] && [ "${OUTPUT_DETAILS}" == "detailed" ] ; then - ######################################################### - # Transform new lines to \\n, remove colours and colons # - ######################################################### - echo "${DATA}" | awk 'BEGIN{RS="\n";ORS="\\n"}1' | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g" | tr ':' ' ' - fi + DATA=${1} + if [ -n "${DATA}" ] && [ "${OUTPUT_DETAILS}" == "detailed" ]; then + ######################################################### + # Transform new lines to \\n, remove colours and colons # + ######################################################### + echo "${DATA}" | awk 'BEGIN{RS="\n";ORS="\\n"}1' | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g" | tr ':' ' ' + fi } ################################################################################ #### Function HeaderTap ######################################################## function HeaderTap() { - ################ - # Pull in Vars # - ################ - INDEX="${1}" # File being validated - OUTPUT_FILE="${2}" # Output location + ################ + # Pull in Vars # + ################ + INDEX="${1}" # File being validated + OUTPUT_FILE="${2}" # Output location - ################### - # Print the goods # - ################### - printf "TAP version 13\n1..%s\n" "${INDEX}" > "${OUTPUT_FILE}" + ################### + # Print the goods # + ################### + printf "TAP version 13\n1..%s\n" "${INDEX}" > "${OUTPUT_FILE}" } ################################################################################ #### Function OkTap ############################################################ function OkTap() { - ################ - # Pull in Vars # - ################ - INDEX="${1}" # Location - FILE="${2}" # File being validated - TEMP_FILE="${3}" # Temp file location + ################ + # Pull in Vars # + ################ + INDEX="${1}" # Location + FILE="${2}" # File being validated + TEMP_FILE="${3}" # Temp file location - ################### - # Print the goods # - ################### - echo "ok ${INDEX} - ${FILE}" >> "${TEMP_FILE}" + ################### + # Print the goods # + ################### + echo "ok ${INDEX} - ${FILE}" >> "${TEMP_FILE}" } ################################################################################ #### Function NotOkTap ######################################################### function NotOkTap() { - ################ - # Pull in Vars # - ################ - INDEX="${1}" # Location - FILE="${2}" # File being validated - TEMP_FILE="${3}" # Temp file location + ################ + # Pull in Vars # + ################ + INDEX="${1}" # Location + FILE="${2}" # File being validated + TEMP_FILE="${3}" # Temp file location - ################### - # Print the goods # - ################### - echo "not ok ${INDEX} - ${FILE}" >> "${TEMP_FILE}" + ################### + # Print the goods # + ################### + echo "not ok ${INDEX} - ${FILE}" >> "${TEMP_FILE}" } ################################################################################ #### Function AddDetailedMessageIfEnabled ###################################### function AddDetailedMessageIfEnabled() { - ################ - # Pull in Vars # - ################ - LINT_CMD="${1}" # Linter command - TEMP_FILE="${2}" # Temp file + ################ + # Pull in Vars # + ################ + LINT_CMD="${1}" # Linter command + TEMP_FILE="${2}" # Temp file - #################### - # Check the return # - #################### - DETAILED_MSG=$(TransformTAPDetails "${LINT_CMD}") - if [ -n "${DETAILED_MSG}" ] ; then - printf " ---\n message: %s\n ...\n" "${DETAILED_MSG}" >> "${TEMP_FILE}" - fi + #################### + # Check the return # + #################### + DETAILED_MSG=$(TransformTAPDetails "${LINT_CMD}") + if [ -n "${DETAILED_MSG}" ]; then + printf " ---\n message: %s\n ...\n" "${DETAILED_MSG}" >> "${TEMP_FILE}" + fi }