|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +RED='\033[0;31m' |
| 4 | +GREEN='\033[0;32m' |
| 5 | +YELLOW='\033[1;33m' |
| 6 | +NC='\033[0m' # No Color |
| 7 | + |
| 8 | +# global errors counter |
| 9 | +errors=0 |
| 10 | + |
| 11 | +function info() { |
| 12 | + echo -e "${GREEN}INFO${NC}: $*" |
| 13 | +} |
| 14 | + |
| 15 | +function warning() { |
| 16 | + echo -e "${YELLOW}WARNING${NC}: $*" |
| 17 | +} |
| 18 | + |
| 19 | +function error() { |
| 20 | + echo -e "${RED}ERROR${NC}: $*" |
| 21 | + : $((errors++)) |
| 22 | +} |
| 23 | + |
| 24 | +function fatal() { |
| 25 | + echo -e "${RED}FATAL${NC}: $*" |
| 26 | + exit 1 |
| 27 | +} |
| 28 | + |
| 29 | +function check_tabs() { |
| 30 | + local file="$1" |
| 31 | + info "--> Checking BASH indentation" |
| 32 | + grep -P '^\t' "$file" >/dev/null 2>&1 && warning "--> File $file contains leading tab character (suspected bad indentation)." |
| 33 | +} |
| 34 | + |
| 35 | +jq=$(type -p jq) || fatal "Could not locate jq command" |
| 36 | +# Determining the robotidy command |
| 37 | +robotidy_path=$(type -p robocop) || robotidy_path=$(type -p robotidy) || fatal "Could not locate either robocop nor robotidy. Cannot check robot lint" |
| 38 | +robotidy_exe="${robotidy_path##*/}" |
| 39 | +info "Robot lint tool is $robotidy_exe" |
| 40 | +# Options depend on the use binary |
| 41 | +declare -A robotidy_opts=([robotidy]="-->-check --skip-keyword-call Examples:" [robocop]="check" ) |
| 42 | +# Get list of committed files |
| 43 | +mapfile -t committed_files < <(git diff --cached --name-only --diff-filter=ACMR) |
| 44 | +info "Starting plugins pre-commit hooks for ${#committed_files[@]} files" |
| 45 | +for file in "${committed_files[@]}"; do |
| 46 | + info "--> $file:" |
| 47 | + file_extension="${file##*.}" |
| 48 | + case "$file_extension" in |
| 49 | + pm|pl) |
| 50 | + # check that the perl file compiles |
| 51 | + info "--> Checking that file compiles" |
| 52 | + PERL5LIB="$PERL5LIB:./src/" perl -c "$file" >/dev/null 2>&1 || error "File $file does not compile with perl -c" |
| 53 | + # check the copyright year |
| 54 | + info "--> Checking that file copyright is OK" |
| 55 | + grep "Copyright 20..-Present Centreon" "$file" >/dev/null || error "Copyright in $file does not contain \"Copyright $(date +%Y)-Present Centreon\"" |
| 56 | + # check that no help is written as --warning-* --critical-* |
| 57 | + info "--> Checking there's no unsplitted '--warning-*' / '--critical-*'" |
| 58 | + grep -- '--warning-\*\|--critical-\*' "$file" >/dev/null && error "File $file contains help that is written as --warning-* or --critical-*" |
| 59 | + # check spelling |
| 60 | + info "--> Checking that spelling in file is OK" |
| 61 | + perl .github/scripts/pod_spell_check.t "$file" ./tests/resources/spellcheck/stopwords.txt >/dev/null 2>&1 || error "Spellcheck error on file $file" |
| 62 | + check_tabs "$file" |
| 63 | + ;; |
| 64 | + txt) |
| 65 | + if [[ "${file##*/}" == "stopwords.txt" ]]; then |
| 66 | + # sort file and check if it makes a difference |
| 67 | + info "--> Checking that stopwords.txt is sorted " |
| 68 | + sort -ui "$file" >/tmp/sorted_stopwords |
| 69 | + diff "$file" /tmp/sorted_stopwords >/dev/null || error "stopwords.txt not sorted properly" |
| 70 | + fi |
| 71 | + ;; |
| 72 | + robot) |
| 73 | + info "--> Checking robot lint" |
| 74 | + $robotidy_path ${robotidy_opts[$robotidy_exe]} "$file" >/dev/null 2>&1 || warning "--> Robot lint errors found in $file" |
| 75 | + check_tabs "$file" |
| 76 | + ;; |
| 77 | + sh) |
| 78 | + check_tabs "$file" |
| 79 | + ;; |
| 80 | + json) |
| 81 | + info "--> Checking JSON validity" |
| 82 | + jq '' "$file" >/dev/null 2>&1 || error "JSON file $file is not valid" |
| 83 | + check_tabs "$file" |
| 84 | + ;; |
| 85 | + *) |
| 86 | + info "File extension '.${file_extension}' has no checks" |
| 87 | + ;; |
| 88 | + esac |
| 89 | +done |
| 90 | +(( errors > 0 )) && fatal "$errors errors found in pre-commit checks" |
| 91 | +info "All plugins pre-commit checks passed" |
0 commit comments