|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) 2022-2023 MobileCoin Inc. |
| 3 | + |
| 4 | +# Set fancy pants colors |
| 5 | +no_color='\033[0m' |
| 6 | +bold_white='\033[1;37m' |
| 7 | +red='\033[0;31m' |
| 8 | +green='\033[0;32m' |
| 9 | + |
| 10 | +# Set default headers |
| 11 | +header="${bold_white}[pre-commit]${no_color}" |
| 12 | +ok="${green}[ OK ]${no_color}" |
| 13 | +failed="${red}[ FAILED ]${no_color}" |
| 14 | + |
| 15 | +# Check the results and print output if there are errors. |
| 16 | +check_results() |
| 17 | +{ |
| 18 | + if [[ "${error}" == "true" ]] |
| 19 | + then |
| 20 | + echo -e "${failed}" |
| 21 | + echo -e "${out}" |
| 22 | + exit 1 |
| 23 | + else |
| 24 | + echo -e "${ok}" |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +# error flag starts off as false |
| 29 | +error="false" |
| 30 | + |
| 31 | +# Get list of files included in the commit. We don't need to necessarily check the whole project. |
| 32 | +files=$(git diff --cached --name-only --diff-filter=ACM) |
| 33 | + |
| 34 | +# Run shellcheck if installed |
| 35 | +if which shellcheck >/dev/null 2>&1 |
| 36 | +then |
| 37 | + echo -e -n "${header} Run shellcheck on files included in the commit " |
| 38 | + |
| 39 | + out="" |
| 40 | + for f in ${files} |
| 41 | + do |
| 42 | + # check only files that have proper shebang headers |
| 43 | + if grep -E "^#!.*(sh|bash|ksh)$" "${f}" >/dev/null 2>&1 |
| 44 | + then |
| 45 | + if ! out+=$(shellcheck -Calways -x "${f}" 2>&1) |
| 46 | + then |
| 47 | + error="true" |
| 48 | + fi |
| 49 | + fi |
| 50 | + done |
| 51 | + |
| 52 | + # check for error status and print results |
| 53 | + check_results |
| 54 | +fi |
| 55 | + |
| 56 | +# Run actionlint to check GHA workflow syntax |
| 57 | +if which actionlint >/dev/null 2>&1 |
| 58 | +then |
| 59 | + echo -e -n "${header} Run actionlint on GHA workflow files " |
| 60 | + |
| 61 | + out="" |
| 62 | + if ! out+=$(actionlint -color 2>&1) |
| 63 | + then |
| 64 | + error="true" |
| 65 | + fi |
| 66 | + |
| 67 | + # check for error status and print results |
| 68 | + check_results |
| 69 | +fi |
| 70 | + |
| 71 | +# Run hadolint on Dockerfiles included in .internal-ci/docker |
| 72 | +if which hadolint >/dev/null 2>&1 |
| 73 | +then |
| 74 | + echo -e -n "${header} Run hadolint on Dockerfiles " |
| 75 | + |
| 76 | + out="" |
| 77 | + |
| 78 | + # Find Dockerfile files |
| 79 | + docker_files=$(find . -name "Dockerfile*" -type f | grep -v "dockerignore") |
| 80 | + |
| 81 | + # helm lint on directories where there are chart.yaml files |
| 82 | + for f in ${docker_files} |
| 83 | + do |
| 84 | + if ! out+=$(hadolint "${f}" 2>&1) |
| 85 | + then |
| 86 | + error="true" |
| 87 | + fi |
| 88 | + done |
| 89 | + |
| 90 | + # check for error status and print results |
| 91 | + check_results |
| 92 | +fi |
0 commit comments