Skip to content

Commit

Permalink
pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoberezhniy committed Apr 29, 2024
1 parent 8f0e0a6 commit 5f80422
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- id: differential-shellcheck
name: Differential ShellCheck
description: Static analysis tool for changed shell scripts
language: docker
entry: /action/pre-commit.sh
types: [shell]
verbose: true
7 changes: 5 additions & 2 deletions src/index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ WORK_DIR="${WORK_DIR-../}"
declare \
GITHUB_STEP_SUMMARY

export GROUP="::group::"
export ENDGROUP="::endgroup::"

# Make directory $GITHUB_WORKSPACE (/github/workspace) git-save
git config --global --add safe.directory "${GITHUB_WORKSPACE:-}"

Expand Down Expand Up @@ -50,9 +53,9 @@ show_versions

echo -e "${MAIN_HEADING}"

echo -e "::group::📜 ${WHITE}List of shell scripts for scanning${NOCOLOR}"
echo -e "${GROUP}📜 ${WHITE}List of shell scripts for scanning${NOCOLOR}"
echo "${all_scripts[@]:-${only_changed_scripts[@]}}"
echo "::endgroup::"
echo "${ENDGROUP}"
echo

# ------------ #
Expand Down
79 changes: 79 additions & 0 deletions src/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later

INPUT_SEVERITY="style"

for arg
do
case "${arg}" in
-S=*|--severity=*)
INPUT_SEVERITY="${arg#*=}"
shift
;;
-S|--severity)
# shellcheck disable=SC2034
INPUT_SEVERITY="$2"
# shellcheck disable=SC2016
shift 2 || { echo 'option `--severity` requires an argument SEVERITY' >&2; exit 1; }
;;
-x|--external-sources)
# shellcheck disable=SC2034
INPUT_EXTERNAL_SOURCES=y
shift
;;
*)
break
;;
esac
done

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")/"

Check warning

Code scanning / shellcheck

Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore). Warning

Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore).
export SCRIPT_DIR

# shellcheck source=functions.sh
. "${SCRIPT_DIR-}functions.sh"

Check warning

Code scanning / shellcheck

Not following: functions.sh: openBinaryFile: does not exist (No such file or directory) Warning

Not following: functions.sh: openBinaryFile: does not exist (No such file or directory)

WORK_DIR="$(mktemp -d)/"
export WORK_DIR

export GROUP=""
export ENDGROUP=""

only_changed_scripts=("$@")

echo -e "${VERSIONS_HEADING}"

Check warning

Code scanning / shellcheck

VERSIONS_HEADING is referenced but not assigned. Warning

VERSIONS_HEADING is referenced but not assigned.
show_versions

echo -e "${MAIN_HEADING}"

Check warning

Code scanning / shellcheck

MAIN_HEADING is referenced but not assigned. Warning

MAIN_HEADING is referenced but not assigned.

echo -e "📜 ${WHITE}List of shell scripts for scanning${NOCOLOR}"

Check warning

Code scanning / shellcheck

WHITE is referenced but not assigned. Warning

WHITE is referenced but not assigned.

Check warning

Code scanning / shellcheck

NOCOLOR is referenced but not assigned. Warning

NOCOLOR is referenced but not assigned.
echo "${only_changed_scripts[@]}"
echo

# ------------ #
# SHELLCHECK #
# ------------ #

exit_status=0

execute_shellcheck "${only_changed_scripts[@]}" > "${WORK_DIR}head-shellcheck.err"

git stash >/dev/null

execute_shellcheck "${only_changed_scripts[@]}" > "${WORK_DIR}base-shellcheck.err"

git stash apply --index >/dev/null

get_fixes "${WORK_DIR}base-shellcheck.err" "${WORK_DIR}head-shellcheck.err"
evaluate_and_print_fixes

get_defects "${WORK_DIR}head-shellcheck.err" "${WORK_DIR}base-shellcheck.err"

echo

evaluate_and_print_defects
exit_status=$?

summary

exit "${exit_status}"
6 changes: 4 additions & 2 deletions src/validation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
. "${SCRIPT_DIR=}summary.sh"

WORK_DIR="${WORK_DIR-../}"
GROUP=${GROUP-"::group::"}
ENDGROUP=${ENDGROUP-"::endgroup::"}

# Get file containing fixes based on two input files
# $1 - <string> absolute path to a file containing results from BASE scan
Expand Down Expand Up @@ -64,11 +66,11 @@ evaluate_and_print_defects () {
# Function to print statistics of defects
# it requires gather_statistics to be called first
print_statistics () {
echo -e "::group::📊 ${WHITE}Statistics of defects${NOCOLOR}"
echo -e "${GROUP}📊 ${WHITE}Statistics of defects${NOCOLOR}"
[[ -n ${stat_error} ]] && echo -e "Error: ${stat_error}"
[[ -n ${stat_warning} ]] && echo -e "Warning: ${stat_warning}"
[[ -n ${stat_info} ]] && echo -e "Style or Note: ${stat_info}"
echo "::endgroup::"
echo "${ENDGROUP}"
echo
}

Expand Down

0 comments on commit 5f80422

Please sign in to comment.