-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpre-commit
More file actions
99 lines (86 loc) · 2.5 KB
/
Copy pathpre-commit
File metadata and controls
99 lines (86 loc) · 2.5 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
96
97
98
99
#!/bin/bash
# Check if there are unstaged changes
if git diff --quiet; then
echo -e "No unstaged changes to stash...\n"
STASH_CREATED=false
else
# Unstaged changes detected
git stash --keep-index -u
echo -e "Stashing unstaged changes...\n"
STASH_CREATED=true
fi
# Find all changed files for this commit
# Compute the diff only once to save a small amount of time.
CHANGED_FILES=$(git diff --name-only --cached --diff-filter=ACMR)
# Get only changed files that match our file suffix pattern
get_pattern_files() {
pattern=$(echo "$*" | sed "s/ /\$\\\|/g")
echo "$CHANGED_FILES" | { grep "$pattern$" || true; }
}
# Get all changed python files
PY_FILES=$(get_pattern_files .py)
JS_FILES=$(get_pattern_files .js)
# Tracks if any checks fail
FAIL=0
# Git Secrets
echo "Running Git Secrets..."
./nhsd-git-secrets/pre-commit-mac.sh
if [ $? -ne 0 ]; then
echo -e "Git Secrets failed. Commit aborted. \n"
FAIL=1
else
echo -e "Git Secrets check passed.\n"
fi
# Lint JavaScript files in the sandbox folder
echo "Linting JavaScript files..."
if [[ -n "$JS_FILES" ]]; then
node_modules/.bin/eslint 'sandbox/**/*.{js,jsx}'
if [ $? -ne 0 ]; then
echo -e "JavaScript linting failed. Commit aborted. \n"
FAIL=1
else
echo -e "JavaScript linting passed.\n"
fi
else
echo -e "No JavaScript files to lint. Skipping JavaScript linting. \n"
fi
# Lint Python files in the project
echo "Linting Python files..."
if [[ -n "$PY_FILES" ]]; then
./scripts/lint_python.sh
if [ $? -ne 0 ]; then
echo -e "Python linting failed. Commit aborted. \n"
FAIL=1
else
echo -e "Python linting passed.\n"
fi
else
echo -e "No Python files to lint. Skipping Python linting. \n"
fi
# Ensure Test Documentation Validity
echo "Ensuring test documentation validity..."
if ./scripts/ensure_docs_validity.sh > /dev/null 2>&1; then
echo -e "Documentation check passed. \n"
else
echo -e "Test documentation validation failed. Commit aborted. \n"
FAIL=1
fi
# Check Licences
echo "Checking licenses..."
if ./scripts/check_licences.sh > /dev/null 2>&1; then
echo -e "License check passed. \n"
else
echo -e "License check failed. Commit aborted. \n"
FAIL=1
fi
# Pop the stash only if a stash was created
if [ "$STASH_CREATED" = true ]; then
echo "Restoring unstaged changes..."
git stash pop -q
fi
# Exit with failure if any checks failed
if [ $FAIL -ne 0 ]; then
exit 1
fi
echo -e "All checks passed. Proceeding with commit. \n"
exit 0