Merge pull request #329 from github/ShellCheck

Shell check
This commit is contained in:
Lukas Gravley 2020-06-30 16:48:30 -05:00 committed by GitHub
commit dbc4dc9425
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 75 deletions

View file

@ -87,8 +87,7 @@ CheckGHEProcess()
####################################################
# Check to see if the process is alive and running #
####################################################
# shellcheck disable=SC2009
CHECK_PROCESS_CMD=$(ps -aef |grep "$GHE_APPLY_COMMAND" |grep -v grep 2>&1)
CHECK_PROCESS_CMD=$(pgrep -f "$GHE_APPLY_COMMAND" 2>&1)
#######################
# Load the error code #

View file

@ -166,7 +166,7 @@ ValidateInput()
##############################
# Get the name of the branch #
##############################
BRANCH_NAME=$(git branch --contains "$GITHUB_SHA" |awk '{print $2}' 2>&1)
BRANCH_NAME=$(git -C "$GITHUB_WORKSPACE" branch --contains "$GITHUB_SHA" |awk '{print $2}' 2>&1)
#######################
# Load the error code #
@ -407,8 +407,7 @@ UploadImage()
REPO=$(echo "$GET_INFO_CMD" | awk '{print $1}')
TAG=$(echo "$GET_INFO_CMD" | awk '{print $2}')
IMAGE_ID=$(echo "$GET_INFO_CMD" | awk '{print $3}')
# shellcheck disable=SC2116
SIZE=$(echo "${GET_INFO_CMD##* }")
SIZE="${GET_INFO_CMD##* }"
###################
# Print the goods #

View file

@ -22,5 +22,5 @@ fi
#########################
export RUN_LOCAL=true
# shellcheck disable=SC1091,SC1090
# shellcheck source=/dev/null
source "$PWD"/lib/linter.sh

View file

@ -1,5 +1,4 @@
#!/usr/bin/env bash
# shellcheck disable=SC2034
################################################################################
################################################################################
@ -56,8 +55,7 @@ function BuildFileList()
#################################################
# Get the Array of files changed in the commits #
#################################################
# shellcheck disable=SC2207
RAW_FILE_ARRAY=($(cd "$GITHUB_WORKSPACE" || exit; git diff --name-only "$DEFAULT_BRANCH..$GITHUB_SHA" --diff-filter=d 2>&1))
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 #
@ -413,6 +411,8 @@ function BuildFileList()
fi
done
echo ${READ_ONLY_CHANGE_FLAG} > /dev/null 2>&1 || true # Workaround SC2034
#########################################
# Need to switch back to branch of code #
#########################################

View file

@ -1,5 +1,4 @@
#!/usr/bin/env bash
# shellcheck disable=SC1003,SC2016,SC1091
################################################################################
################################################################################
@ -10,8 +9,11 @@
#########################
# Source Function Files #
#########################
# shellcheck source=/dev/null
source /action/lib/buildFileList.sh # Source the function script(s)
# shellcheck source=/dev/null
source /action/lib/validation.sh # Source the function script(s)
# shellcheck source=/dev/null
source /action/lib/worker.sh # Source the function script(s)
###########
@ -147,16 +149,16 @@ DEFAULT_IFS="$IFS" # Get the Default IFS for updating
###############################################################
# Default Vars that are called in Subs and need to be ignored #
###############################################################
# shellcheck disable=SC2034
DEFAULT_DISABLE_ERRORS='false' # Default to enabling errors
# shellcheck disable=SC2034
echo "${DEFAULT_DISABLE_ERRORS}" > /dev/null 2>&1 || true # Workaround SC2034
RAW_FILE_ARRAY=() # Array of all files that were changed
# shellcheck disable=SC2034
echo "${RAW_FILE_ARRAY[*]}" > /dev/null 2>&1 || true # Workaround SC2034
READ_ONLY_CHANGE_FLAG=0 # Flag set to 1 if files changed are not txt or md
# shellcheck disable=SC2034
echo "${READ_ONLY_CHANGE_FLAG}" > /dev/null 2>&1 || true # Workaround SC2034
TEST_CASE_FOLDER='.automation/test' # Folder for test cases we should always ignore
# shellcheck disable=SC2034
echo "${TEST_CASE_FOLDER}" > /dev/null 2>&1 || true # Workaround SC2034
DEFAULT_ANSIBLE_DIRECTORY="$GITHUB_WORKSPACE/ansible" # Default Ansible Directory
echo "${DEFAULT_ANSIBLE_DIRECTORY}" > /dev/null 2>&1 || true # Workaround SC2034
##########################
# Array of changed files #
@ -265,8 +267,7 @@ GetLinterVersions()
###################
# Get the version #
###################
# shellcheck disable=SC2207
GET_VERSION_CMD=($("$LINTER" --version 2>&1))
mapfile -t GET_VERSION_CMD < <("$LINTER" --version 2>&1)
#######################
# Load the error code #
@ -357,11 +358,9 @@ GetStandardRules()
# Only env vars that are marked as true
GET_ENV_ARRAY=()
if [[ "$LINTER" == "javascript" ]]; then
# shellcheck disable=SC2207
GET_ENV_ARRAY=($(yq .env "$JAVASCRIPT_LINTER_RULES" |grep true))
mapfile -t GET_ENV_ARRAY < <(yq .env "$JAVASCRIPT_LINTER_RULES" | grep true)
elif [[ "$LINTER" == "typescript" ]]; then
# shellcheck disable=SC2207
GET_ENV_ARRAY=($(yq .env "$TYPESCRIPT_LINTER_RULES" |grep true))
mapfile -t GET_ENV_ARRAY < <(yq .env "$TYPESCRIPT_LINTER_RULES" | grep true)
fi
#######################
@ -612,8 +611,7 @@ GetGitHubVars()
######################
# Get the GitHub Org #
######################
# shellcheck disable=SC2002
GITHUB_ORG=$(cat "$GITHUB_EVENT_PATH" | jq -r '.repository.owner.login' )
GITHUB_ORG=$(jq -r '.repository.owner.login' < "$GITHUB_EVENT_PATH" )
############################
# Validate we have a value #
@ -629,8 +627,7 @@ GetGitHubVars()
#######################
# Get the GitHub Repo #
#######################
# shellcheck disable=SC2002
GITHUB_REPO=$(cat "$GITHUB_EVENT_PATH"| jq -r '.repository.name' )
GITHUB_REPO=$(jq -r '.repository.name' < "$GITHUB_EVENT_PATH" )
############################
# Validate we have a value #
@ -1109,8 +1106,7 @@ if [ "$VALIDATE_OPENAPI" == "true" ]; then
###############################################################################
IFS=$'\n'
# shellcheck disable=SC2207
LIST_FILES=($(cd "$GITHUB_WORKSPACE" || exit; find . -type f -regex ".*\.\(yml\|yaml\|json\)\$" 2>&1))
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

View file

@ -38,7 +38,6 @@ function LintCodebase()
#######################################
# Validate we have jsonlint installed #
#######################################
# shellcheck disable=SC2230
VALIDATE_INSTALL_CMD=$(command -v "$LINTER_NAME" 2>&1)
#######################
@ -91,8 +90,7 @@ function LintCodebase()
#################################
# Get list of all files to lint #
#################################
# shellcheck disable=SC2207,SC2086
LIST_FILES=($(cd "$GITHUB_WORKSPACE" || exit; find . -type f -regex "$FILE_EXTENSIONS" 2>&1))
mapfile -t LIST_FILES < <(find "$GITHUB_WORKSPACE" -type f -regex "$FILE_EXTENSIONS" 2>&1)
###########################
# Set IFS back to default #
@ -214,6 +212,7 @@ function TestCodebase()
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 #
@ -229,7 +228,6 @@ function TestCodebase()
#####################################
# Validate we have linter installed #
#####################################
# shellcheck disable=SC2230
VALIDATE_INSTALL_CMD=$(command -v "$LINTER_NAME" 2>&1)
#######################
@ -256,32 +254,10 @@ function TestCodebase()
##########################
LIST_FILES=()
############################################
# Check if its ansible, as its the outlier #
############################################
if [[ "$FILE_TYPE" == "ANSIBLE" ]]; then
#################################
# Get list of all files to lint #
#################################
# shellcheck disable=SC2207,SC2086,SC2010
LIST_FILES=($(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER" || exit; ls ansible/ | grep ".yml" 2>&1))
else
###############################################################################
# Set the file seperator to newline to allow for grabbing objects with spaces #
###############################################################################
IFS=$'\n'
#################################
# Get list of all files to lint #
#################################
# shellcheck disable=SC2207,SC2086
LIST_FILES=($(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER" || exit; find "$INDVIDUAL_TEST_FOLDER" -type f -regex "$FILE_EXTENSIONS" ! -path "*./ansible*" 2>&1))
###########################
# Set IFS back to default #
###########################
IFS="$DEFAULT_IFS"
fi
#################################
# 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/*" 2>&1)
##################
# Lint the files #
@ -352,7 +328,7 @@ function TestCodebase()
################################
# Lint the file with the rules #
################################
LINT_CMD=$(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER/ansible" || exit; $LINTER_COMMAND "$FILE" 2>&1)
LINT_CMD=$(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER/$INDVIDUAL_TEST_FOLDER" || exit; $LINTER_COMMAND "$FILE" 2>&1)
elif [[ "$FILE_TYPE" == "POWERSHELL" ]]; then
################################
# Lint the file with the rules #
@ -387,11 +363,15 @@ function TestCodebase()
echo "ERROR: Linter CMD:[$LINTER_COMMAND $FILE]"
# Increment the error count
(("ERRORS_FOUND_$FILE_TYPE++"))
# Increment counter that check was ran
((TESTS_RAN++))
else
###########
# Success #
###########
echo " - File:[$FILE_NAME] was linted with [$LINTER_NAME] successfully"
# Increment counter that check was ran
((TESTS_RAN++))
fi
else
#######################################
@ -410,14 +390,30 @@ function TestCodebase()
echo "ERROR: Linter CMD:[$LINTER_COMMAND $FILE]"
# Increment the error count
(("ERRORS_FOUND_$FILE_TYPE++"))
# Increment counter that check was ran
((TESTS_RAN++))
else
###########
# Success #
###########
echo " - File:[$FILE_NAME] failed test case with [$LINTER_NAME] successfully"
# Increment counter that check was ran
((TESTS_RAN++))
fi
fi
done
##############################
# Validate we ran some tests #
##############################
if [ "$TESTS_RAN" -eq 0 ]; then
#################################################
# We failed to find files and no tests were ran #
#################################################
echo "ERROR! Failed to find any tests ran for the Linter:[$LINTER_NAME]"!
echo "Please validate logic or that tests exist!"
exit 1
fi
}
################################################################################
#### Function RunTestCases #####################################################
@ -460,7 +456,7 @@ function RunTestCases()
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 "DOCKER" "/dockerfilelint/bin/dockerfilelint" "/dockerfilelint/bin/dockerfilelint -c $DOCKER_LINTER_RULES" ".*\(Dockerfile\)\$" "docker"
TestCodebase "ANSIBLE" "ansible-lint" "ansible-lint -v -c $ANSIBLE_LINTER_RULES" "ansible-lint" "ansible"
TestCodebase "ANSIBLE" "ansible-lint" "ansible-lint -v -c $ANSIBLE_LINTER_RULES" ".*\.\(yml\|yaml\)\$" "ansible"
TestCodebase "TERRAFORM" "tflint" "tflint -c $TERRAFORM_LINTER_RULES" ".*\.\(tf\)\$" "terraform"
TestCodebase "CFN" "cfn-lint" "cfn-lint --config-file $CFN_LINTER_RULES" ".*\.\(json\|yml\|yaml\)\$" "cfn"
TestCodebase "POWERSHELL" "pwsh" "pwsh -c Invoke-ScriptAnalyzer -EnableExit -Settings $POWERSHELL_LINTER_RULES -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "powershell"
@ -505,7 +501,6 @@ function LintAnsibleFiles()
###########################################
# Validate we have ansible-lint installed #
###########################################
# shellcheck disable=SC2230
VALIDATE_INSTALL_CMD=$(command -v "$LINTER_NAME" 2>&1)
#######################
@ -545,21 +540,10 @@ function LintAnsibleFiles()
######################################################
if [ -d "$ANSIBLE_DIRECTORY" ]; then
############################################################
# Check to see if we need to go through array or all files #
############################################################
if [ "$VALIDATE_ALL_CODEBASE" == "false" ]; then
# We need to only check the ansible playbooks that have updates
#LIST_FILES=("${ANSIBLE_ARRAY[@]}")
# shellcheck disable=SC2164,SC2010,SC2207
LIST_FILES=($(cd "$ANSIBLE_DIRECTORY"; ls | grep ".yml" 2>&1))
else
#################################
# Get list of all files to lint #
#################################
# shellcheck disable=SC2164,SC2010,SC2207
LIST_FILES=($(cd "$ANSIBLE_DIRECTORY"; ls | grep ".yml" 2>&1))
fi
#################################
# Get list of all files to lint #
#################################
mapfile -t LIST_FILES < <(ls "$ANSIBLE_DIRECTORY/*.yml" 2>&1)
###############################################################
# Set the list to empty if only MD and TXT files were changed #