|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause |
| 4 | +# Copyright 2024 Intel Corporation |
| 5 | + |
| 6 | +script_dir=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")") |
| 7 | +repository_root=$(readlink -f "${script_dir}/../..") |
| 8 | + |
| 9 | +# shellcheck source="../../scripts/common.sh" |
| 10 | +. "${repository_root}/scripts/common.sh" |
| 11 | + |
| 12 | +allow_non_ascii_filenames="false" |
| 13 | + |
| 14 | +function get_main_sha() |
| 15 | +{ |
| 16 | + log_info "Getting origin/main commit SHA" |
| 17 | + git_origin_main=$(git rev-parse --verify origin/main) |
| 18 | + log_info "running against origin/master=${git_origin_main}" |
| 19 | + echo $git_origin_main |
| 20 | +} |
| 21 | + |
| 22 | +function get_head_sha() |
| 23 | +{ |
| 24 | + log_info "Getting HEAD commit SHA" |
| 25 | + if $(git rev-parse --verify HEAD >/dev/null 2>&1) |
| 26 | + then |
| 27 | + git_current_hash=$(git rev-parse --verify HEAD) |
| 28 | + else |
| 29 | + echo "This is first commit, nothing to check, exiting" |
| 30 | + exit 0 |
| 31 | + fi |
| 32 | + log_info "running against HEAD=${git_current_hash}" |
| 33 | + echo $git_current_hash |
| 34 | +} |
| 35 | + |
| 36 | +function check_nonascii_files() |
| 37 | +{ |
| 38 | + local github_origin_main="$1" |
| 39 | + local github_current_hash="$2" |
| 40 | + local allow_non_ascii=${3:-false} |
| 41 | + |
| 42 | + if [ "$allow_non_ascii" == "false" ] |
| 43 | + then |
| 44 | + if test $(git diff --diff-filter=AR --relative --name-only -z $github_origin_main $github_current_hash | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 |
| 45 | + then |
| 46 | + cat <<EOF |
| 47 | +Error: You attempted to add a non-ASCII file name. |
| 48 | + This is not allowed in Cloud Native Data Services repository. |
| 49 | + To be portable it is mandatory for you to rename all the file. |
| 50 | +EOF |
| 51 | + fi |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +function check_file_subdir_type() |
| 56 | +{ |
| 57 | + local modified_file="$1" |
| 58 | + local fields_range=${2:-'1'} |
| 59 | + |
| 60 | + if [ -z "$modified_file" ] |
| 61 | + then |
| 62 | + printf "Function check_file_subdir_type requires exacly 1 valid argument." 1>&2 |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + printf "${modified_file}" | cut -d'/' "-f${fields_range}" |
| 66 | +} |
| 67 | + |
| 68 | +function files_subdir_types() |
| 69 | +{ |
| 70 | + local github_origin_main="$1" |
| 71 | + local github_current_hash="$2" |
| 72 | + |
| 73 | +# diff-filter params, uppercase include, lowercase exclude: |
| 74 | +# Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), changed (T), Unmerged (U), Unknown (X), pairing Broken (B) |
| 75 | + modified_file_list="$(git diff --diff-filter=dxb --relative --name-only -z $github_origin_main $github_current_hash | xargs -0)" |
| 76 | + |
| 77 | + for pt in $modified_file_list |
| 78 | + do |
| 79 | + modified_file="$(readlink -f "${pt}")" |
| 80 | + |
| 81 | + [ -d "$modified_file" ] && modified_dir="$modified_file" || modified_dir="$(dirname "$modified_file")"; |
| 82 | + case $(check_file_subdir_type "${modified_file}") in |
| 83 | + deployment) |
| 84 | + deployment_subdir_check "${modified_file}" "${modified_dir}" |
| 85 | + ;; |
| 86 | + |
| 87 | + config) |
| 88 | + configuration_subdir_check "${modified_file}" |
| 89 | + ;; |
| 90 | + |
| 91 | + docs) |
| 92 | + documentation_subdir_check "${modified_file}" |
| 93 | + ;; |
| 94 | + |
| 95 | + .github) |
| 96 | + github_workflow_subdir_check "${modified_file}" |
| 97 | + ;; |
| 98 | + |
| 99 | + tests|scripts) |
| 100 | + ansible_subdir_check "${modified_file}" |
| 101 | + ;; |
| 102 | + |
| 103 | + *) |
| 104 | + if echo "${modified_file}" | grep --silent ".*\.sh\$"; then |
| 105 | + shell_script_file_check "${modified_file}" |
| 106 | + elif echo "${modified_file}" | grep --silent ".*\.py\$"; then |
| 107 | + python_script_file_check "${modified_file}" |
| 108 | + elif echo "${modified_file}" | grep --silent "\(\.yaml\$\)\|\(\.yml\$\)"; then |
| 109 | + ansible_subdir_check "${modified_file}" |
| 110 | + else |
| 111 | + other_file_check "${modified_file}" |
| 112 | + fi |
| 113 | + ;; |
| 114 | + esac |
| 115 | + done |
| 116 | +} |
| 117 | + |
| 118 | +function images_subdir_check { |
| 119 | + local filepath="$1" |
| 120 | + shift |
| 121 | + log_info "Dockerfiles images subdirectory. ${filepath}" |
| 122 | +} |
| 123 | + |
| 124 | +function deployment_subdir_check() { |
| 125 | + local filepath="$1" |
| 126 | + local dirpath="$2" |
| 127 | + shift; shift; |
| 128 | + log_info "Helm Charts deployment subdirectory. ${filepath}" |
| 129 | + helm lint "$dirpath" 1>&2 || true |
| 130 | +} |
| 131 | + |
| 132 | +function ansible_subdir_check() { |
| 133 | + local filepath="$1" |
| 134 | + local dirpath="$2" |
| 135 | + shift; shift; |
| 136 | + echo "ansible roles and playbooks subdirectory. ${filepath}" |
| 137 | + ansible-lint "$filepath" 1>&2 || true |
| 138 | +} |
| 139 | + |
| 140 | +function inventories_subdir_check() { |
| 141 | + local filepath="$1" |
| 142 | + shift |
| 143 | + log_info "inventories files subdirectory. ${filepath}" |
| 144 | +} |
| 145 | + |
| 146 | +function configuration_subdir_check() { |
| 147 | + local filepath="$1" |
| 148 | + shift |
| 149 | + log_info "configuration files subdirectory. ${filepath}" |
| 150 | +} |
| 151 | + |
| 152 | +function documentation_subdir_check() { |
| 153 | + local filepath="$1" |
| 154 | + shift |
| 155 | + log_info "documentation files and styles subdirectory. ${filepath}" |
| 156 | +} |
| 157 | + |
| 158 | +function github_workflow_subdir_check() { |
| 159 | + local filepath="$1" |
| 160 | + shift |
| 161 | + log_info "GitHub workflows subdirectory. ${filepath}" |
| 162 | +} |
| 163 | + |
| 164 | +function shell_script_file_check() { |
| 165 | + local filepath="$1" |
| 166 | + shift |
| 167 | + log_info "Shell script file path. ${filepath}" |
| 168 | + shellcheck -f tty "${repository_root}/$filepath" 1>&2 |
| 169 | +} |
| 170 | + |
| 171 | +function python_script_file_check() { |
| 172 | + local filepath="$1" |
| 173 | + shift |
| 174 | + log_info "Python script file path. ${filepath}" |
| 175 | +} |
| 176 | + |
| 177 | +function other_file_check() { |
| 178 | + local filepath="$1" |
| 179 | + shift |
| 180 | + log_info "Other file path, not categorized. ${filepath}" |
| 181 | +} |
| 182 | + |
| 183 | +function start_git_head_parsing() { |
| 184 | + cd "${repository_root}" |
| 185 | + git_current_hash="$(get_head_sha)" |
| 186 | + git_origin_main="$(get_main_sha)" |
| 187 | + check_nonascii_files "$git_origin_main" "$git_current_hash" allow_non_ascii_filenames |
| 188 | + files_subdir_types "$git_origin_main" "$git_current_hash" || true |
| 189 | +} |
| 190 | + |
| 191 | +start_git_head_parsing |
0 commit comments