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

Add shell linter Github action for pull requests #1007

Merged
Merged
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
31 changes: 31 additions & 0 deletions .github/workflows/linters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
name: linters
on:
pull_request:

permissions:
contents: read

defaults:
run:
shell: bash -o pipefail {0}

jobs:
lint:
runs-on: ubuntu-latest

permissions:
security-events: write
pull-requests: write

steps:
- name: Checkout code
uses: actions/checkout@v1
with:
fetch-depth: 0

- name: Lint shell scripts
uses: redhat-plumbers-in-action/differential-shellcheck@latest
with:
token: ${{ secrets.GITHUB_TOKEN }}
shell-scripts: ush/preamble.sh
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately, this will not work as you expect. shell-scripts option takes the path to a text file that contains a list of file paths that will be ignored. I know that it is a bit clumsy, and it would deserve to rework on the linter side.

But I think you could be fine by running a linter with default settings. It performs differential scans, so only new changes introduced via PR will be reported.

I'm glad you are using my action. If you have any issues or requests, feel free to reach out to me or create issue on differential-shellcheck.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seemed to work when I tested it, but thanks for the information. It was probably just accidental that it did what I expected. I don't know that using a separate file is really all that clumsy at all.

Actually really like your action, thank you for making it available. My only request would be to make a new one that does the same for python linter ;). I'm having trouble finding one quite as well integrated into GitHub actions, to the point I am considering adapting yours for a different linter.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm glad you like it. 👍

We are planning to create also other linters, including differential-cppcheck and differential-pylint. I can't give you any exact release date, but they are coming. They will be available at: @redhat-plumbers-in-action

2 changes: 2 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Global settings for Spellcheck (https://github.com/koalaman/shellcheck)
enable=all
31 changes: 18 additions & 13 deletions ush/preamble.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,26 @@ fi
start_time=$(date +%s)

# Get the base name of the calling script
_calling_script=$(basename ${BASH_SOURCE[1]})
_calling_script=$(basename "${BASH_SOURCE[1]}")

# Announce the script has begun
echo "Begin ${_calling_script} at $(date -u)"
start_time_human=$(date -d"@${start_time}" -u)
echo "Begin ${_calling_script} at ${start_time_human}"

# Stage our variables
export STRICT=${STRICT:-"YES"}
export TRACE=${TRACE:-"YES"}
export ERR_EXIT_ON=""
export TRACE_ON=""

if [[ $STRICT == "YES" ]]; then
if [[ ${STRICT} == "YES" ]]; then
# Exit on error and undefined variable
export ERR_EXIT_ON="set -eu"
fi
if [[ $TRACE == "YES" ]]; then
if [[ ${TRACE} == "YES" ]]; then
export TRACE_ON="set -x"
# Print the script name and line number of each command as it is executed
export PS4='+ $(basename $BASH_SOURCE)[$LINENO]'"$id: "
export PS4='+ $(basename ${BASH_SOURCE})[${LINENO}]'"${id}: "
fi

postamble() {
Expand All @@ -64,23 +65,27 @@ postamble() {
#

set +x
script=${1}
start_time=${2}
rc=${3}
script="${1}"
start_time="${2}"
rc="${3}"

# Calculate the elapsed time
end_time=$(date +%s)
end_time_human=$(date -d@"${end_time}" -u +%H:%M:%S)
elapsed_sec=$((end_time - start_time))
elapsed=$(date -d@${elapsed_sec} -u +%H:%M:%S)
elapsed=$(date -d@"${elapsed_sec}" -u +%H:%M:%S)

# Announce the script has ended, then pass the error code up
echo "End ${script} at $(date -u) with error code ${rc:-0} (time elapsed: ${elapsed})"
exit ${rc}
echo "End ${script} at ${end_time_human} with error code ${rc:-0} (time elapsed: ${elapsed})"
exit "${rc}"
}

# Place the postamble in a trap so it is always called no matter how the script exits
# Shellcheck: Turn off warning about substitions at runtime instead of signal time
# shellcheck disable=SC2064
trap "postamble ${_calling_script} ${start_time} \$?" EXIT
# shellcheck disable=

# Turn on our settings
$ERR_EXIT_ON
$TRACE_ON
${ERR_EXIT_ON}
${TRACE_ON}