Merge remote-tracking branch 'upstream/master' into github-issue-318

This commit is contained in:
Kevin Rowlandson 2020-07-01 08:36:12 +01:00
commit 6b60348fd5
No known key found for this signature in database
GPG key ID: F33348ACAE26BE84
9 changed files with 62 additions and 80 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

@ -8,7 +8,6 @@ The end goal of this tool:
- Build guidelines for code layout and format
- Automate the process to help streamline code reviews
## Table of Contents
- [How it works](#how-it-works)
@ -60,10 +59,15 @@ Developers on **GitHub** can call the **GitHub Action** to lint their code base
| **YAML** | [YamlLint](https://github.com/adrienverge/yamllint) |
## How to use
<img height="512" src="https://github.com/github/super-linter/blob/quickstart/docs/how-to.gif?raw=true" alt="How to gif">
To use this **GitHub** Action you will need to complete the following:
- Add the **GitHub** Action: **Super-Linter** to your current **GitHub** Actions workflow
- Enjoy your more *stable*, and *cleaner* code base
- Check out the [Wiki](https://github.com/github/super-linter/wiki) for customization options
1. Create a new file in your repository called `.github/workflows/linter.yml`
2. Copy the example workflow from below into that new file, no extra configuration required
3. Commit that file to a new branch
4. Open up a pull request and observe the action working
5. Enjoy your more *stable*, and *cleaner* code base
6. Check out the [Wiki](https://github.com/github/super-linter/wiki) for customization options
### Example connecting GitHub Action Workflow
In your repository you should have a `.github/workflows` folder with **GitHub** Action similar to below:

BIN
docs/how-to.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 MiB

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

@ -68,7 +68,7 @@ function GetValidationInfo()
VALIDATE_POWERSHELL=$(echo "$VALIDATE_POWERSHELL" | awk '{print tolower($0)}')
VALIDATE_CSS=$(echo "$VALIDATE_CSS" | awk '{print tolower($0)}')
VALIDATE_ENV=$(echo "$VALIDATE_ENV" | awk '{print tolower($0)}')
VALIDATE_CLOJURE=$(echo "$VALIDATE_CLOJURE" | awk '{print tolower($0)')
VALIDATE_CLOJURE=$(echo "$VALIDATE_CLOJURE" | awk '{print tolower($0)}')
VALIDATE_KOTLIN=$(echo "$VALIDATE_KOTLIN" | awk '{print tolower($0)}')
VALIDATE_PROTOBUF=$(echo "$VALIDATE_PROTOBUF" | awk '{print tolower($0)}')
VALIDATE_OPENAPI=$(echo "$VALIDATE_OPENAPI" | awk '{print tolower($0)}')

View file

@ -38,7 +38,6 @@ function LintCodebase()
#####################################
# Validate we have linter installed #
#####################################
# shellcheck disable=SC2230
VALIDATE_INSTALL_CMD=$(command -v "$LINTER_NAME" 2>&1)
#######################
@ -123,8 +122,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 #
@ -246,6 +244,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 #
@ -261,7 +260,6 @@ function TestCodebase()
#####################################
# Validate we have linter installed #
#####################################
# shellcheck disable=SC2230
VALIDATE_INSTALL_CMD=$(command -v "$LINTER_NAME" 2>&1)
#######################
@ -288,32 +286,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
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 #
@ -384,7 +360,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 #
@ -419,11 +395,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
#######################################
@ -442,14 +422,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 #####################################################
@ -492,7 +488,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"
@ -537,7 +533,6 @@ function LintAnsibleFiles()
###########################################
# Validate we have ansible-lint installed #
###########################################
# shellcheck disable=SC2230
VALIDATE_INSTALL_CMD=$(command -v "$LINTER_NAME" 2>&1)
#######################
@ -577,21 +572,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
mapfile -t LIST_FILES < <(ls "$ANSIBLE_DIRECTORY/*.yml" 2>&1)
###############################################################
# Set the list to empty if only MD and TXT files were changed #