-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathshellcheck_validate.sh
More file actions
executable file
·95 lines (78 loc) · 2.54 KB
/
shellcheck_validate.sh
File metadata and controls
executable file
·95 lines (78 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env bash
set -euo pipefail
# Auto-detect which shell scripts to validate based on changed files from paths-filter
# Uses paths-filter outputs from GitHub Actions:
# ALL_CHANGED_FILES - all files changed in the PR (for logging)
# SHARED_CHANGED - boolean indicating if shared infrastructure changed
# SHELL_CHANGED_FILES - only .sh files (for processing)
# Validates all shell scripts if shared infrastructure changes
#
# This script validates all shell scripts across the repository
validate_shell_script() {
local file="$1"
echo "Validating $file"
# Run shellcheck with warning severity level
# Using gcc format for better IDE/editor integration
if ! shellcheck --severity=warning --format=gcc "$file"; then
return 1
fi
return 0
}
main() {
echo "==> Detecting changed files..."
if [[ -n "${ALL_CHANGED_FILES:-}" ]]; then
echo "Changed files in PR:"
echo "$ALL_CHANGED_FILES" | tr ' ' '\n' | sed 's/^/ - /'
echo ""
fi
# Determine which files to check
local files_to_check=()
if [[ "${SHARED_CHANGED:-false}" == "true" ]]; then
echo "==> Shared infrastructure changed"
echo "==> Validating all shell scripts for safety"
# Find all .sh files in the repository, excluding node_modules, .git, and .terraform
mapfile -t files_to_check < <(find . -type f -name "*.sh" ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/.terraform/*" | sort)
elif [[ -z "${SHELL_CHANGED_FILES:-}" ]]; then
echo "✓ No shell script files changed, skipping validation"
exit 0
else
# Process only changed shell scripts
CHANGED_FILES=$(echo "$SHELL_CHANGED_FILES" | tr ' ' '\n')
while IFS= read -r file; do
if [[ -f "$file" && "$file" == *.sh ]]; then
files_to_check+=("$file")
fi
done <<< "$CHANGED_FILES"
fi
if [[ ${#files_to_check[@]} -eq 0 ]]; then
echo "✓ No shell scripts to validate"
exit 0
fi
echo "==> Validating ${#files_to_check[@]} shell script(s):"
for file in "${files_to_check[@]}"; do
echo " - $file"
done
echo ""
# Validate each file
local status=0
local failed_files=()
for file in "${files_to_check[@]}"; do
if ! validate_shell_script "$file"; then
status=1
failed_files+=("$file")
fi
done
# Report results
if [[ $status -eq 0 ]]; then
echo ""
echo "✓ All shell scripts passed validation"
else
echo ""
echo "❌ ShellCheck validation failed for ${#failed_files[@]} file(s):"
for file in "${failed_files[@]}"; do
echo " - $file"
done
fi
exit $status
}
main