chore: simplify error code checks (#5131)

Instead of loading the error code and checking it, shorten checks by
running the command directly.
This commit is contained in:
Marco Ferrari 2024-01-30 08:48:24 +01:00 committed by GitHub
parent d0ec3f0ddf
commit 05009f2816
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: B5690EEEBB952194
5 changed files with 23 additions and 74 deletions

View file

@ -23,30 +23,20 @@ function GenerateFileDiff() {
} }
function RunFileDiffCommand() { function RunFileDiffCommand() {
local CMD
CMD="${1}" CMD="${1}"
debug "Generating Diff with:[$CMD]" debug "Generating Diff with:[$CMD]"
################################################# #################################################
# Get the Array of files changed in the commits # # Get the Array of files changed in the commits #
################################################# #################################################
CMD_OUTPUT=$(eval "set -eo pipefail; $CMD; set +eo pipefail") if ! CMD_OUTPUT=$(eval "set -eo pipefail; $CMD; set +eo pipefail"); then
ERROR_CODE=$?
debug "Diff command return code: ${ERROR_CODE}"
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
error "Failed to get Diff with:[$CMD]" error "Failed to get Diff with:[$CMD]"
IssueHintForFullGitHistory IssueHintForFullGitHistory
fatal "[Diff command output: ${CMD_OUTPUT}]" fatal "Diff command output: ${CMD_OUTPUT}"
fi fi
###################################################
# Map command output to an array to proper handle #
###################################################
mapfile -t RAW_FILE_ARRAY < <(echo -n "$CMD_OUTPUT") mapfile -t RAW_FILE_ARRAY < <(echo -n "$CMD_OUTPUT")
debug "RAW_FILE_ARRAY contents: ${RAW_FILE_ARRAY[*]}"
} }
function BuildFileList() { function BuildFileList() {
@ -72,7 +62,7 @@ function BuildFileList() {
if [ "${USE_FIND_ALGORITHM}" == 'true' ]; then if [ "${USE_FIND_ALGORITHM}" == 'true' ]; then
debug "----------------------------------------------" debug "----------------------------------------------"
debug "Populating the file list with all the files in the ${WORKSPACE_PATH} workspace using FIND algorithm" debug "Populating the file list with all the files in the ${WORKSPACE_PATH} workspace using FIND algorithm"
mapfile -t RAW_FILE_ARRAY < <(find "${WORKSPACE_PATH}" \ if ! mapfile -t RAW_FILE_ARRAY < <(find "${WORKSPACE_PATH}" \
-not \( -path '*/\.git' -prune \) \ -not \( -path '*/\.git' -prune \) \
-not \( -path '*/\.pytest_cache' -prune \) \ -not \( -path '*/\.pytest_cache' -prune \) \
-not \( -path '*/\.rbenv' -prune \) \ -not \( -path '*/\.rbenv' -prune \) \
@ -92,20 +82,21 @@ function BuildFileList() {
-not -name "*.woff2" \ -not -name "*.woff2" \
-not -name "*.zip" \ -not -name "*.zip" \
-type f \ -type f \
2>&1 | sort) 2>&1 | sort); then
fatal "Failed to get a list of changed files. USE_FIND_ALGORITHM: ${USE_FIND_ALGORITHM}"
fi
else else
debug "----------------------------------------------" debug "----------------------------------------------"
DIFF_GIT_VALIDATE_ALL_CODEBASE="git -C \"${WORKSPACE_PATH}\" ls-tree -r --name-only HEAD | xargs -I % sh -c \"echo ${WORKSPACE_PATH}/%\" 2>&1" DIFF_GIT_VALIDATE_ALL_CODEBASE="git -C \"${WORKSPACE_PATH}\" ls-tree -r --name-only HEAD | xargs -I % sh -c \"echo ${WORKSPACE_PATH}/%\" 2>&1"
debug "Populating the file list with: ${DIFF_GIT_VALIDATE_ALL_CODEBASE}" debug "Populating the file list with: ${DIFF_GIT_VALIDATE_ALL_CODEBASE}"
mapfile -t RAW_FILE_ARRAY < <(eval "set -eo pipefail; ${DIFF_GIT_VALIDATE_ALL_CODEBASE}; set +eo pipefail") if ! mapfile -t RAW_FILE_ARRAY < <(eval "set -eo pipefail; ${DIFF_GIT_VALIDATE_ALL_CODEBASE}; set +eo pipefail"); then
debug "RAW_FILE_ARRAY contents: ${RAW_FILE_ARRAY[*]}" fatal "Failed to get a list of changed files. USE_FIND_ALGORITHM: ${USE_FIND_ALGORITHM}"
fi
fi fi
fi fi
ERROR_CODE=$? debug "RAW_FILE_ARRAY contents: ${RAW_FILE_ARRAY[*]}"
if [ ${ERROR_CODE} -ne 0 ]; then
fatal "Failed to gain a list of all files changed! Error code: ${ERROR_CODE}"
fi
if [ ${#RAW_FILE_ARRAY[@]} -eq 0 ]; then if [ ${#RAW_FILE_ARRAY[@]} -eq 0 ]; then
warn "No files were found in the GITHUB_WORKSPACE:[${GITHUB_WORKSPACE}] to lint!" warn "No files were found in the GITHUB_WORKSPACE:[${GITHUB_WORKSPACE}] to lint!"

View file

@ -4,10 +4,9 @@ function GetGithubPushEventCommitCount() {
local GITHUB_EVENT_FILE_PATH local GITHUB_EVENT_FILE_PATH
GITHUB_EVENT_FILE_PATH="${1}" GITHUB_EVENT_FILE_PATH="${1}"
local GITHUB_PUSH_COMMIT_COUNT local GITHUB_PUSH_COMMIT_COUNT
GITHUB_PUSH_COMMIT_COUNT=$(jq -r '.commits | length' <"${GITHUB_EVENT_FILE_PATH}")
ERROR_CODE=$? if ! GITHUB_PUSH_COMMIT_COUNT=$(jq -r '.commits | length' <"${GITHUB_EVENT_FILE_PATH}"); then
if [ ${ERROR_CODE} -ne 0 ]; then fatal "Failed to initialize GITHUB_PUSH_COMMIT_COUNT for a push event. Output: ${GITHUB_PUSH_COMMIT_COUNT}"
fatal "Failed to initialize GITHUB_PUSH_COMMIT_COUNT for a push event. Error code: ${ERROR_CODE}. Output: ${GITHUB_PUSH_COMMIT_COUNT}"
fi fi
if IsUnsignedInteger "${GITHUB_PUSH_COMMIT_COUNT}" && [ -n "${GITHUB_PUSH_COMMIT_COUNT}" ]; then if IsUnsignedInteger "${GITHUB_PUSH_COMMIT_COUNT}" && [ -n "${GITHUB_PUSH_COMMIT_COUNT}" ]; then

View file

@ -194,22 +194,8 @@ GetStandardRules() {
######################################### #########################################
# Only env vars that are marked as true # Only env vars that are marked as true
GET_ENV_ARRAY=() GET_ENV_ARRAY=()
if [[ ${LINTER} == "javascript" ]]; then if [[ ${LINTER} == "javascript" ]] && ! mapfile -t GET_ENV_ARRAY < <(yq .env "${JAVASCRIPT_STANDARD_LINTER_RULES}" | grep true); then
mapfile -t GET_ENV_ARRAY < <(yq .env "${JAVASCRIPT_STANDARD_LINTER_RULES}" | grep true) fatal "Failed to gain list of ENV vars to load: [${GET_ENV_ARRAY[*]}]"
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 fi
########################## ##########################
@ -218,14 +204,8 @@ GetStandardRules() {
# Set IFS back to Orig # Set IFS back to Orig
IFS="${ORIG_IFS}" IFS="${ORIG_IFS}"
######################
# Set the env string #
######################
ENV_STRING='' ENV_STRING=''
#############################
# Pull out the envs to load #
#############################
for ENV in "${GET_ENV_ARRAY[@]}"; do for ENV in "${GET_ENV_ARRAY[@]}"; do
############################# #############################
# remove spaces from return # # remove spaces from return #

View file

@ -16,7 +16,7 @@ GetLinterVersions() {
fi fi
if ! cat "${VERSION_FILE}"; then if ! cat "${VERSION_FILE}"; then
fatal "Failed to view version file: ${VERSION_FILE}." fatal "Failed to view version file: ${VERSION_FILE}"
fi fi
} }
################################################################################ ################################################################################

View file

@ -490,10 +490,7 @@ GetGitHubVars() {
if [[ "${USE_FIND_ALGORITHM}" == "false" ]]; then if [[ "${USE_FIND_ALGORITHM}" == "false" ]]; then
ConfigureGitSafeDirectories ConfigureGitSafeDirectories
debug "Initializing GITHUB_SHA considering ${GITHUB_WORKSPACE}" debug "Initializing GITHUB_SHA considering ${GITHUB_WORKSPACE}"
GITHUB_SHA=$(git -C "${GITHUB_WORKSPACE}" rev-parse HEAD) if ! GITHUB_SHA=$(git -C "${GITHUB_WORKSPACE}" rev-parse HEAD); then
ERROR_CODE=$?
debug "GITHUB_SHA initalization return code: ${ERROR_CODE}"
if [ ${ERROR_CODE} -ne 0 ]; then
fatal "Failed to initialize GITHUB_SHA. Output: ${GITHUB_SHA}" fatal "Failed to initialize GITHUB_SHA. Output: ${GITHUB_SHA}"
fi fi
debug "GITHUB_SHA: ${GITHUB_SHA}" debug "GITHUB_SHA: ${GITHUB_SHA}"
@ -532,11 +529,8 @@ GetGitHubVars() {
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request # https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
debug "This is a GitHub pull request. Updating the current GITHUB_SHA (${GITHUB_SHA}) to the pull request HEAD SHA" debug "This is a GitHub pull request. Updating the current GITHUB_SHA (${GITHUB_SHA}) to the pull request HEAD SHA"
GITHUB_SHA=$(jq -r .pull_request.head.sha <"$GITHUB_EVENT_PATH")
ERROR_CODE=$?
debug "GITHUB_SHA update error code: ${ERROR_CODE}"
if [ ${ERROR_CODE} -ne 0 ]; then if ! GITHUB_SHA=$(jq -r .pull_request.head.sha <"$GITHUB_EVENT_PATH"); then
fatal "Failed to update GITHUB_SHA for pull request event: ${GITHUB_SHA}" fatal "Failed to update GITHUB_SHA for pull request event: ${GITHUB_SHA}"
fi fi
debug "Updated GITHUB_SHA: ${GITHUB_SHA}" debug "Updated GITHUB_SHA: ${GITHUB_SHA}"
@ -552,10 +546,7 @@ GetGitHubVars() {
# Ref: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context # Ref: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
debug "Get the hash of the commit to start the diff from from Git because the GitHub push event payload may not contain references to base_ref or previous commit." debug "Get the hash of the commit to start the diff from from Git because the GitHub push event payload may not contain references to base_ref or previous commit."
# shellcheck disable=SC2086 # We checked that GITHUB_PUSH_COMMIT_COUNT is an integer # shellcheck disable=SC2086 # We checked that GITHUB_PUSH_COMMIT_COUNT is an integer
GITHUB_BEFORE_SHA=$(git -C "${GITHUB_WORKSPACE}" rev-parse HEAD~${GITHUB_PUSH_COMMIT_COUNT}) if ! GITHUB_BEFORE_SHA=$(git -C "${GITHUB_WORKSPACE}" rev-parse HEAD~${GITHUB_PUSH_COMMIT_COUNT}); then
ERROR_CODE=$?
debug "GITHUB_BEFORE_SHA initialization error code: ${ERROR_CODE}"
if [ ${ERROR_CODE} -ne 0 ]; then
fatal "Failed to initialize GITHUB_BEFORE_SHA for a push event. Output: ${GITHUB_BEFORE_SHA}" fatal "Failed to initialize GITHUB_BEFORE_SHA for a push event. Output: ${GITHUB_BEFORE_SHA}"
fi fi
@ -660,7 +651,7 @@ CallStatusAPI() {
############################################## ##############################################
# Call the status API to create status check # # Call the status API to create status check #
############################################## ##############################################
SEND_STATUS_CMD=$( if ! SEND_STATUS_CMD=$(
curl -f -s --show-error -X POST \ curl -f -s --show-error -X POST \
--url "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}" \ --url "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}" \
-H 'accept: application/vnd.github.v3+json' \ -H 'accept: application/vnd.github.v3+json' \
@ -670,19 +661,7 @@ CallStatusAPI() {
\"target_url\": \"https://${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}\", \"target_url\": \"https://${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}\",
\"description\": \"${MESSAGE}\", \"context\": \"--> Linted: ${LANGUAGE}\" \"description\": \"${MESSAGE}\", \"context\": \"--> Linted: ${LANGUAGE}\"
}" 2>&1 }" 2>&1
) ); then
#######################
# Load the error code #
#######################
ERROR_CODE=$?
debug "Send status comd output: [$SEND_STATUS_CMD]"
##############################
# Check the shell for errors #
##############################
if [ "${ERROR_CODE}" -ne 0 ]; then
info "Failed to call GitHub Status API: ${SEND_STATUS_CMD}" info "Failed to call GitHub Status API: ${SEND_STATUS_CMD}"
fi fi
fi fi