Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-commit handling #394

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

@mpoberezhniy mpoberezhniy Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is temporary, docker_image should be used when ready.

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
Fixed Show fixed Hide fixed
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=src/functions.sh
. "${SCRIPT_DIR-}functions.sh"
Fixed Show fixed Hide fixed

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

export GROUP=""
export ENDGROUP=""

only_changed_scripts=("$@")

echo -e "${VERSIONS_HEADING}"
Fixed Show fixed Hide fixed
show_versions

echo -e "${MAIN_HEADING}"
Fixed Show fixed Hide fixed

echo -e "πŸ“œ ${WHITE}List of shell scripts for scanning${NOCOLOR}"
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
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