-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit-quality-checks.sh
More file actions
executable file
·330 lines (295 loc) · 10.4 KB
/
pre-commit-quality-checks.sh
File metadata and controls
executable file
·330 lines (295 loc) · 10.4 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env bash
# Pre-commit checks for specfact-cli-modules.
#
# Pre-commit buffers each hook's output until that hook finishes; one long hook looks
# "silent" until the end. This script is split into subcommands (see .pre-commit-config.yaml)
# so each stage completes and prints before the next hook starts.
#
# Subcommands: block1-format | block1-yaml | block1-bundle | block1-lint | block2 | all
# Run with no args or `all` for manual/CI full pipeline.
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
info() { echo -e "${BLUE}$*${NC}" >&2; }
success() { echo -e "${GREEN}$*${NC}" >&2; }
warn() { echo -e "${YELLOW}$*${NC}" >&2; }
error() { echo -e "${RED}$*${NC}" >&2; }
print_block1_overview() {
echo "" >&2
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" >&2
echo " modules pre-commit — Block 1: quality checks (4 stages)" >&2
echo " 1/4 format (hatch run format; tree must not change)" >&2
echo " 2/4 YAML manifests (hatch run yaml-lint) if staged *.yaml/*.yml" >&2
echo " 3/4 bundle import boundaries (hatch run check-bundle-imports)" >&2
echo " 4/4 lint (hatch run lint) if staged *.py / *.pyi" >&2
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" >&2
echo "" >&2
}
print_block2_overview() {
echo "" >&2
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" >&2
echo " modules pre-commit — Block 2: code review + contract tests (2 stages)" >&2
echo " 1/2 code review gate (staged paths under packages/, registry/, scripts/, tools/, tests/, openspec/changes/)" >&2
echo " 2/2 contract-first tests (contract-test-status → hatch run contract-test)" >&2
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" >&2
echo "" >&2
}
staged_files() {
git diff --cached --name-only
}
has_staged_yaml() {
staged_files | grep -E '\.ya?ml$' >/dev/null 2>&1
}
has_staged_python() {
staged_files | grep -E '\.pyi?$' >/dev/null 2>&1
}
staged_python_files() {
staged_files | grep -E '\.pyi?$' || true
}
# Paths passed to scripts/pre_commit_code_review.py (packages/, registry/, scripts/, tools/, tests/, openspec/changes/).
staged_review_gate_files() {
local line
while IFS= read -r line; do
[ -z "${line}" ] && continue
case "${line}" in
*/TDD_EVIDENCE.md|TDD_EVIDENCE.md) continue ;;
packages/*|registry/*|scripts/*|tools/*|tests/*|openspec/changes/*)
printf '%s\n' "${line}"
;;
esac
done < <(staged_files)
}
staged_docs_validation_paths() {
local line
while IFS= read -r line; do
[ -z "${line}" ] && continue
case "${line}" in
docs/*|*.md|requirements-docs-ci.txt|scripts/check-docs-commands.py|scripts/docs_site_validation.py)
printf '%s\n' "${line}"
;;
esac
done < <(staged_files)
}
needs_docs_site_validation() {
local line
while IFS= read -r line; do
[ -z "${line}" ] && continue
return 0
done < <(staged_docs_validation_paths)
return 1
}
run_docs_site_validation_gate() {
if ! needs_docs_site_validation; then
return 0
fi
info "📄 Docs site validation — running \`hatch run python scripts/check-docs-commands.py\` (staged docs or docs validation scripts)"
if hatch run python scripts/check-docs-commands.py; then
success "✅ Docs site validation passed"
else
error "❌ Docs site validation failed"
warn "💡 Run: hatch run python scripts/check-docs-commands.py"
exit 1
fi
}
check_safe_change() {
local files
files=$(staged_files)
if [ -z "${files}" ]; then
return 0
fi
local other_changes=0
local file
for file in ${files}; do
case "${file}" in
*.md|*.rst|*.txt|docs/*|images/*|papers/*|presentations/*)
;;
.github/workflows/*|*.yaml|*.yml)
;;
pyproject.toml|README.md|CHANGELOG.md|.pre-commit-config.yaml)
;;
scripts/pre-commit-quality-checks.sh)
;;
*)
other_changes=$((other_changes + 1))
;;
esac
done
[ "${other_changes}" -eq 0 ]
}
run_format_safety() {
info "📦 Block 1 — stage 1/4: format — running \`hatch run format\` (fails if working tree would change)"
local before_unstaged after_unstaged
before_unstaged=$(git diff --binary -- . || true)
if hatch run format; then
after_unstaged=$(git diff --binary -- . || true)
if [ "${before_unstaged}" != "${after_unstaged}" ]; then
error "❌ Formatter changed files. Review and re-stage before committing."
warn "💡 Run: hatch run format && git add -A"
exit 1
fi
success "✅ Block 1 — stage 1/4: format passed"
else
error "❌ Block 1 — stage 1/4: format failed"
exit 1
fi
}
run_yaml_lint_if_needed() {
if has_staged_yaml; then
info "📦 Block 1 — stage 2/4: YAML — running \`hatch run yaml-lint\` (staged YAML detected)"
if hatch run yaml-lint; then
success "✅ Block 1 — stage 2/4: YAML validation passed"
else
error "❌ Block 1 — stage 2/4: YAML validation failed"
exit 1
fi
else
info "📦 Block 1 — stage 2/4: YAML — skipped (no staged *.yaml / *.yml)"
fi
}
run_bundle_import_checks() {
info "📦 Block 1 — stage 3/4: bundle imports — running \`hatch run check-bundle-imports\`"
if hatch run check-bundle-imports; then
success "✅ Block 1 — stage 3/4: bundle import boundaries passed"
else
error "❌ Block 1 — stage 3/4: bundle import boundary check failed"
exit 1
fi
}
# Parity with CI quality job (.github/workflows/pr-orchestrator.yml: hatch run lint).
run_lint_if_staged_python() {
if ! has_staged_python; then
info "📦 Block 1 — stage 4/4: lint — skipped (no staged *.py / *.pyi)"
return 0
fi
info "📦 Block 1 — stage 4/4: lint — running \`hatch run lint\` (ruff, basedpyright, pylint)"
if hatch run lint; then
success "✅ Block 1 — stage 4/4: lint passed"
else
error "❌ Block 1 — stage 4/4: lint failed (matches CI quality gate)"
warn "💡 Run: hatch run lint"
exit 1
fi
}
run_code_review_gate() {
local review_array=()
while IFS= read -r line; do
[ -z "${line}" ] && continue
review_array+=("${line}")
done < <(staged_review_gate_files)
if [ ${#review_array[@]} -eq 0 ]; then
info "📦 Block 2 — stage 1/2: code review — skipped (no staged paths under packages/, registry/, scripts/, tools/, tests/, or openspec/changes/)"
return
fi
info "📦 Block 2 — stage 1/2: code review — running \`hatch run python scripts/pre_commit_code_review.py\` (${#review_array[@]} path(s))"
if hatch run python scripts/pre_commit_code_review.py "${review_array[@]}"; then
success "✅ Block 2 — stage 1/2: code review gate passed"
else
error "❌ Block 2 — stage 1/2: code review gate failed"
warn "💡 Fix blocking review findings or run: hatch run python scripts/pre_commit_code_review.py <paths>"
exit 1
fi
}
run_contract_tests_visible() {
info "📦 Block 2 — stage 2/2: contract tests — running \`hatch run contract-test-status\`"
if hatch run contract-test-status > /dev/null 2>&1; then
success "✅ Block 2 — stage 2/2: contract tests — skipped (contract-test-status: no input changes)"
else
info "📦 Block 2 — stage 2/2: contract tests — running \`hatch run contract-test\`"
if hatch run contract-test; then
success "✅ Block 2 — stage 2/2: contract-first tests passed"
warn "💡 CI may still run the full quality matrix"
else
error "❌ Block 2 — stage 2/2: contract-first tests failed"
warn "💡 Run: hatch run contract-test-status"
exit 1
fi
fi
}
run_block1_format() {
warn "🔍 modules pre-commit — Block 1 — hook: format (1/4)"
print_block1_overview
run_format_safety
}
run_block1_yaml() {
warn "🔍 modules pre-commit — Block 1 — hook: YAML (2/4)"
run_yaml_lint_if_needed
}
run_block1_bundle() {
warn "🔍 modules pre-commit — Block 1 — hook: bundle imports (3/4)"
run_bundle_import_checks
}
run_block1_lint() {
warn "🔍 modules pre-commit — Block 1 — hook: lint (4/4)"
run_lint_if_staged_python
}
run_block2() {
warn "🔍 modules pre-commit — Block 2 — hook: review + contract tests"
run_docs_site_validation_gate
if check_safe_change; then
success "✅ Safe change detected — skipping Block 2 (code review + contract tests)"
info "💡 Only docs, workflow, version, or pre-commit metadata changed"
exit 0
fi
print_block2_overview
run_code_review_gate
run_contract_tests_visible
}
run_all() {
warn "🔍 Running full modules pre-commit pipeline (\`all\` — manual or CI)"
print_block1_overview
run_format_safety
run_yaml_lint_if_needed
run_bundle_import_checks
run_lint_if_staged_python
success "✅ Block 1 complete (all stages passed or skipped as expected)"
run_docs_site_validation_gate
if check_safe_change; then
success "✅ Safe change detected — skipping Block 2 (code review + contract tests)"
info "💡 Only docs, workflow, version, or pre-commit metadata changed"
exit 0
fi
print_block2_overview
run_code_review_gate
run_contract_tests_visible
}
usage_error() {
error "Usage: $0 {block1-format|block1-yaml|block1-bundle|block1-lint|block2|all} (also: -h | --help | help)"
exit 2
}
show_help() {
echo "Usage: $0 {block1-format|block1-yaml|block1-bundle|block1-lint|block2|all}" >&2
echo "Help aliases: -h, --help, help" >&2
exit 0
}
main() {
case "${1:-all}" in
block1-format)
run_block1_format
;;
block1-yaml)
run_block1_yaml
;;
block1-bundle)
run_block1_bundle
;;
block1-lint)
run_block1_lint
;;
block2)
run_block2
;;
all)
run_all
;;
-h|--help|help)
show_help
;;
*)
usage_error
;;
esac
}
main "$@"