-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit-check.sh
More file actions
executable file
·304 lines (252 loc) · 10.5 KB
/
Copy pathpre-commit-check.sh
File metadata and controls
executable file
·304 lines (252 loc) · 10.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
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
#!/usr/bin/env bash
# Local CI gate mirroring .github/workflows/full.yml as closely as practical.
# Stages run sequentially; work within each stage runs in parallel.
set -euo pipefail
SERVICES_INFRA="postgres valkey rabbitmq"
SERVICES_FULL="postgres valkey rabbitmq vault auth-api agents-api traefik auth-ui agents-ui app-ui n8n grafana uptime-kuma stalwart"
STACK_STARTED=0
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() { echo -e "${GREEN}[CHECK]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
fail() { echo -e "${RED}[FAIL]${NC} $*" >&2; }
TMPDIR_JOBS=$(mktemp -d)
trap 'rm -rf "$TMPDIR_JOBS"; teardown_stack' EXIT
dump_stack_logs() {
local services=(
postgres valkey rabbitmq vault auth-api agents-api traefik
auth-ui agents-ui app-ui n8n grafana uptime-kuma stalwart
vault-oidc-init uptime-kuma-init
)
warn "Dumping stack logs for diagnosis..."
for svc in "${services[@]}"; do
echo "========== $svc =========="
docker compose logs "$svc" --tail 80 2>&1 || true
done
}
teardown_stack() {
if docker compose ps --quiet 2>/dev/null | grep -q .; then
log "Tearing down stack..."
docker compose down --remove-orphans --timeout 10 2>/dev/null || true
fi
STACK_STARTED=0
}
select_java_21() {
local java21_home=""
if [[ "$(uname -s)" == "Darwin" ]] && command -v /usr/libexec/java_home >/dev/null 2>&1; then
java21_home=$(/usr/libexec/java_home -v 21 2>/dev/null || true)
elif [[ -n "${JAVA_HOME_21_X64:-}" ]]; then
java21_home="${JAVA_HOME_21_X64}"
fi
if [[ -n "$java21_home" ]]; then
export JAVA_HOME="$java21_home"
export PATH="$JAVA_HOME/bin:$PATH"
log "Using Java 21 at $JAVA_HOME"
fi
}
run_pnpm_audit() {
if ! pnpm audit --audit-level=high; then
warn "pnpm audit reported high severity findings (non-blocking, mirroring CI)."
fi
}
run_trivy_scan() {
if command -v trivy >/dev/null 2>&1; then
trivy fs --severity HIGH,CRITICAL .
return
fi
docker run --rm \
-v "$PWD:/workspace" \
-w /workspace \
aquasec/trivy:0.61.0 \
fs --severity HIGH,CRITICAL .
}
run_gitleaks_scan() {
if command -v gitleaks >/dev/null 2>&1; then
gitleaks detect --source . --config .gitleaks.toml --no-banner
return
fi
docker run --rm \
-v "$PWD:/repo" \
-w /repo \
zricethezav/gitleaks:v8.25.1 \
detect --source . --config .gitleaks.toml --no-banner
}
install_playwright_browser() {
if [[ "$(uname -s)" == "Linux" ]]; then
npx playwright install --with-deps chromium
else
npx playwright install chromium
fi
}
verify_services_healthy() {
local unhealthy=0
log " Verifying service health..."
docker compose ps --format "table {{.Name}}\t{{.Status}}\t{{.Health}}"
for svc in $SERVICES_FULL; do
local container="personal-stack-$svc"
local health
health=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$container" 2>/dev/null || echo "missing")
if [[ "$health" == "unhealthy" || "$health" == "missing" ]]; then
fail "Service $container is $health"
unhealthy=1
fi
done
if [ "$unhealthy" -ne 0 ]; then
return 1
fi
}
verify_database_migrations() {
log " Verifying database migrations..."
docker exec personal-stack-postgres psql -U auth_user -d auth_db -c '\dt' | grep -q app_user
}
# Run a named job in background, capturing output. Usage: run_job <name> <cmd...>
run_job() {
local name="$1"; shift
local outfile="$TMPDIR_JOBS/${name}.out"
local rc_file="$TMPDIR_JOBS/${name}.rc"
(
local rc=0
if declare -F "$1" >/dev/null 2>&1; then
if "$@" >"$outfile" 2>&1; then
rc=0
else
rc=$?
fi
elif bash infra/scripts/run-strict-command.sh "$@" >"$outfile" 2>&1; then
rc=0
else
rc=$?
fi
echo "$rc" >"$rc_file"
exit "$rc"
) &
echo $! >"$TMPDIR_JOBS/${name}.pid"
}
# Wait for all background jobs; print failures and exit 1 if any failed.
wait_jobs() {
local stage="$1"; shift
local names=("$@")
local failed=0
for name in "${names[@]}"; do
local pid
pid=$(cat "$TMPDIR_JOBS/${name}.pid")
wait "$pid" || true
local rc_file="$TMPDIR_JOBS/${name}.rc"
local rc=0
[ -f "$rc_file" ] && rc=$(cat "$rc_file")
if [ "$rc" -ne 0 ]; then
fail "[$stage] $name FAILED (exit $rc):"
cat "$TMPDIR_JOBS/${name}.out" >&2
failed=1
fi
done
if [ "$failed" -ne 0 ]; then
fail "Stage '$stage' failed — aborting."
exit 1
fi
log "Stage '$stage' passed."
}
# ─────────────────────────────────────────────────────────────────
# Bootstrap shared dependencies once before parallel jobs.
# ─────────────────────────────────────────────────────────────────
select_java_21
log "Bootstrapping shared tooling"
pnpm install --frozen-lockfile
# ─────────────────────────────────────────────────────────────────
# STAGE 1 — Lint, formatting, and security scans
# ─────────────────────────────────────────────────────────────────
log "Stage 1: Lint, formatting, and security scans"
run_job "lint-auth-api" ./gradlew :services:auth-api:detekt :services:auth-api:ktlintCheck
run_job "lint-frontend" bash -lc 'pnpm -r lint --max-warnings 0 && pnpm format:check'
run_job "scan-trivy" run_trivy_scan
run_job "scan-pnpm-audit" run_pnpm_audit
run_job "scan-gitleaks" run_gitleaks_scan
wait_jobs "lint+security" \
lint-auth-api lint-frontend \
scan-trivy scan-pnpm-audit scan-gitleaks
# ─────────────────────────────────────────────────────────────────
# STAGE 2 — Unit tests + architecture + Docker image builds (parallel)
# ─────────────────────────────────────────────────────────────────
log "Stage 2: Unit tests, architecture checks, and Docker image builds"
run_job "unit-auth-api" ./gradlew :services:auth-api:test
run_job "unit-frontend" pnpm -r test -- --coverage
run_job "arch-frontend" pnpm -r depcruise
# Build Docker images in parallel while tests run (needed for stage 4)
log " Building Docker images in parallel..."
run_job "build-auth-api" docker build -f services/auth-api/Dockerfile -t personal-stack/auth-api:latest .
run_job "build-auth-ui" docker build -f services/auth-ui/Dockerfile -t personal-stack/auth-ui:latest .
run_job "build-app-ui" docker build --build-arg VITE_AUTH_URL=https://auth.jorisjonkers.test -f services/app-ui/Dockerfile -t personal-stack/app-ui:latest .
wait_jobs "unit+arch+build" \
unit-auth-api unit-frontend \
arch-frontend \
build-auth-api build-auth-ui build-app-ui
log "Stage 2b: Kotlin architecture tests"
bash infra/scripts/run-strict-command.sh ./gradlew :services:auth-api:test --tests "*ArchitectureTest*"
log "Stage 'arch-kotlin' passed."
# ─────────────────────────────────────────────────────────────────
# STAGE 3 — Integration tests + coverage verification
# ─────────────────────────────────────────────────────────────────
log "Stage 3: Integration tests and coverage verification"
log " Starting infrastructure services..."
docker compose up -d $SERVICES_INFRA --wait --wait-timeout 120
run_job "integ-auth-api" ./gradlew :services:auth-api:integrationTest
wait_jobs "integration" integ-auth-api
run_job "coverage-auth-api" ./gradlew :services:auth-api:jacocoTestCoverageVerification
wait_jobs "coverage" coverage-auth-api
log " Stopping infrastructure services..."
docker compose down --remove-orphans --timeout 10 2>/dev/null || true
# ─────────────────────────────────────────────────────────────────
# STAGE 4 — System tests (full stack required)
# ─────────────────────────────────────────────────────────────────
log "Stage 4: System tests (full stack)"
log " Installing Playwright browser..."
install_playwright_browser
log " Starting full stack..."
# shellcheck disable=SC2086
docker compose up -d --no-build $SERVICES_FULL --wait --wait-timeout 300
STACK_STARTED=1
if ! verify_services_healthy; then
dump_stack_logs
exit 1
fi
log " Waiting for Traefik routes to stabilize..."
check_route() {
local route="$1" url="$2"
for i in $(seq 1 30); do
if curl -sfk --max-time 2 -o /dev/null "$url" 2>/dev/null; then return 0; fi
if [ "$i" -eq 30 ]; then
fail "Route $route ($url) not reachable after 30s"
return 1
fi
sleep 1
done
}
if ! check_route app-ui "https://jorisjonkers.test/"; then dump_stack_logs; exit 1; fi
if ! check_route auth-ui "https://auth.jorisjonkers.test/"; then dump_stack_logs; exit 1; fi
if ! check_route agents-ui "https://agents.jorisjonkers.test/"; then dump_stack_logs; exit 1; fi
if ! check_route auth-api "https://auth.jorisjonkers.test/api/actuator/health"; then dump_stack_logs; exit 1; fi
if ! check_route agents-api "https://agents.jorisjonkers.test/api/actuator/health"; then dump_stack_logs; exit 1; fi
if ! verify_database_migrations; then
dump_stack_logs
exit 1
fi
log " Initializing Vault OIDC..."
if ! docker compose up --no-build vault-oidc-init; then
dump_stack_logs
exit 1
fi
log " Initializing Uptime Kuma..."
if ! docker compose up --no-build uptime-kuma-init; then
dump_stack_logs
exit 1
fi
log " Running system tests..."
if ! bash infra/scripts/run-strict-command.sh ./gradlew :services:system-tests:test \
-Dtest.auth-api.url=http://localhost:8081 \
; then
dump_stack_logs
exit 1
fi
log "All checks passed."