Skip to content

Commit

Permalink
chore: simplify error code checks (#5131)
Browse files Browse the repository at this point in the history
Instead of loading the error code and checking it, shorten checks by
running the command directly.
  • Loading branch information
ferrarimarco committed Jan 30, 2024
1 parent d0ec3f0 commit 05009f2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 74 deletions.
33 changes: 12 additions & 21 deletions lib/functions/buildFileList.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,20 @@ function GenerateFileDiff() {
}

function RunFileDiffCommand() {
local CMD
CMD="${1}"
debug "Generating Diff with:[$CMD]"

#################################################
# Get the Array of files changed in the commits #
#################################################
CMD_OUTPUT=$(eval "set -eo pipefail; $CMD; set +eo pipefail")
ERROR_CODE=$?
debug "Diff command return code: ${ERROR_CODE}"

##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
if ! CMD_OUTPUT=$(eval "set -eo pipefail; $CMD; set +eo pipefail"); then
error "Failed to get Diff with:[$CMD]"
IssueHintForFullGitHistory
fatal "[Diff command output: ${CMD_OUTPUT}]"
fatal "Diff command output: ${CMD_OUTPUT}"
fi

###################################################
# Map command output to an array to proper handle #
###################################################
mapfile -t RAW_FILE_ARRAY < <(echo -n "$CMD_OUTPUT")
debug "RAW_FILE_ARRAY contents: ${RAW_FILE_ARRAY[*]}"
}

function BuildFileList() {
Expand All @@ -72,7 +62,7 @@ function BuildFileList() {
if [ "${USE_FIND_ALGORITHM}" == 'true' ]; then
debug "----------------------------------------------"
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 '*/\.pytest_cache' -prune \) \
-not \( -path '*/\.rbenv' -prune \) \
Expand All @@ -92,20 +82,21 @@ function BuildFileList() {
-not -name "*.woff2" \
-not -name "*.zip" \
-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
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"
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")
debug "RAW_FILE_ARRAY contents: ${RAW_FILE_ARRAY[*]}"
if ! mapfile -t RAW_FILE_ARRAY < <(eval "set -eo pipefail; ${DIFF_GIT_VALIDATE_ALL_CODEBASE}; set +eo pipefail"); then
fatal "Failed to get a list of changed files. USE_FIND_ALGORITHM: ${USE_FIND_ALGORITHM}"
fi
fi
fi

ERROR_CODE=$?
if [ ${ERROR_CODE} -ne 0 ]; then
fatal "Failed to gain a list of all files changed! Error code: ${ERROR_CODE}"
fi
debug "RAW_FILE_ARRAY contents: ${RAW_FILE_ARRAY[*]}"

if [ ${#RAW_FILE_ARRAY[@]} -eq 0 ]; then
warn "No files were found in the GITHUB_WORKSPACE:[${GITHUB_WORKSPACE}] to lint!"
Expand Down
7 changes: 3 additions & 4 deletions lib/functions/githubEvent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ function GetGithubPushEventCommitCount() {
local GITHUB_EVENT_FILE_PATH
GITHUB_EVENT_FILE_PATH="${1}"
local GITHUB_PUSH_COMMIT_COUNT
GITHUB_PUSH_COMMIT_COUNT=$(jq -r '.commits | length' <"${GITHUB_EVENT_FILE_PATH}")
ERROR_CODE=$?
if [ ${ERROR_CODE} -ne 0 ]; then
fatal "Failed to initialize GITHUB_PUSH_COMMIT_COUNT for a push event. Error code: ${ERROR_CODE}. Output: ${GITHUB_PUSH_COMMIT_COUNT}"

if ! GITHUB_PUSH_COMMIT_COUNT=$(jq -r '.commits | length' <"${GITHUB_EVENT_FILE_PATH}"); then
fatal "Failed to initialize GITHUB_PUSH_COMMIT_COUNT for a push event. Output: ${GITHUB_PUSH_COMMIT_COUNT}"
fi

if IsUnsignedInteger "${GITHUB_PUSH_COMMIT_COUNT}" && [ -n "${GITHUB_PUSH_COMMIT_COUNT}" ]; then
Expand Down
24 changes: 2 additions & 22 deletions lib/functions/linterRules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,8 @@ GetStandardRules() {
#########################################
# Only env vars that are marked as true
GET_ENV_ARRAY=()
if [[ ${LINTER} == "javascript" ]]; then
mapfile -t GET_ENV_ARRAY < <(yq .env "${JAVASCRIPT_STANDARD_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[*]}]"
if [[ ${LINTER} == "javascript" ]] && ! mapfile -t GET_ENV_ARRAY < <(yq .env "${JAVASCRIPT_STANDARD_LINTER_RULES}" | grep true); then
fatal "Failed to gain list of ENV vars to load: [${GET_ENV_ARRAY[*]}]"
fi

##########################
Expand All @@ -218,14 +204,8 @@ GetStandardRules() {
# 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 #
Expand Down
2 changes: 1 addition & 1 deletion lib/functions/linterVersions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ GetLinterVersions() {
fi

if ! cat "${VERSION_FILE}"; then
fatal "Failed to view version file: ${VERSION_FILE}."
fatal "Failed to view version file: ${VERSION_FILE}"
fi
}
################################################################################
Expand Down
31 changes: 5 additions & 26 deletions lib/linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,7 @@ GetGitHubVars() {
if [[ "${USE_FIND_ALGORITHM}" == "false" ]]; then
ConfigureGitSafeDirectories
debug "Initializing GITHUB_SHA considering ${GITHUB_WORKSPACE}"
GITHUB_SHA=$(git -C "${GITHUB_WORKSPACE}" rev-parse HEAD)
ERROR_CODE=$?
debug "GITHUB_SHA initalization return code: ${ERROR_CODE}"
if [ ${ERROR_CODE} -ne 0 ]; then
if ! GITHUB_SHA=$(git -C "${GITHUB_WORKSPACE}" rev-parse HEAD); then
fatal "Failed to initialize GITHUB_SHA. Output: ${GITHUB_SHA}"
fi
debug "GITHUB_SHA: ${GITHUB_SHA}"
Expand Down Expand Up @@ -532,11 +529,8 @@ GetGitHubVars() {
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request
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"
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}"
fi
debug "Updated GITHUB_SHA: ${GITHUB_SHA}"
Expand All @@ -552,10 +546,7 @@ GetGitHubVars() {
# 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."
# 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})
ERROR_CODE=$?
debug "GITHUB_BEFORE_SHA initialization error code: ${ERROR_CODE}"
if [ ${ERROR_CODE} -ne 0 ]; then
if ! GITHUB_BEFORE_SHA=$(git -C "${GITHUB_WORKSPACE}" rev-parse HEAD~${GITHUB_PUSH_COMMIT_COUNT}); then
fatal "Failed to initialize GITHUB_BEFORE_SHA for a push event. Output: ${GITHUB_BEFORE_SHA}"
fi

Expand Down Expand Up @@ -660,7 +651,7 @@ CallStatusAPI() {
##############################################
# Call the status API to create status check #
##############################################
SEND_STATUS_CMD=$(
if ! SEND_STATUS_CMD=$(
curl -f -s --show-error -X POST \
--url "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}" \
-H 'accept: application/vnd.github.v3+json' \
Expand All @@ -670,19 +661,7 @@ CallStatusAPI() {
\"target_url\": \"https://${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}\",
\"description\": \"${MESSAGE}\", \"context\": \"--> Linted: ${LANGUAGE}\"
}" 2>&1
)

#######################
# 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
); then
info "Failed to call GitHub Status API: ${SEND_STATUS_CMD}"
fi
fi
Expand Down

0 comments on commit 05009f2

Please sign in to comment.