Repository navigation
Expand file tree
/
Copy pathpre-push.sh
More file actions
executable file
·278 lines (250 loc) · 11 KB
/
Copy pathpre-push.sh
File metadata and controls
executable file
·278 lines (250 loc) · 11 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
#!/usr/bin/env bash
# ──────────────────────────────────────────────────────────────────────
# pre-push.sh — Full validation gate before deploying RedMan
#
# Runs the complete test suite + backward compatibility checks.
# Must pass before building or pushing any changes.
#
# Usage:
# ./pre-push.sh # Full suite (compat + medium integration)
# ./pre-push.sh --quick # Compat only + small integration test
# ./pre-push.sh --compat-only # Backward compatibility checks only (no integration)
# ./pre-push.sh --skip-build # Skip Docker build verification
# ./pre-push.sh --deploy --custom # Deploy generic env target
# ./pre-push.sh --deploy --profile NAME # Deploy a local named profile
# ──────────────────────────────────────────────────────────────────────
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"
# ── Defaults ──
SCALE="medium"
RUN_INTEGRATION=true
RUN_BUILD=true
KEEP_DATA=false
RUN_DEPLOY=false
DEPLOY_TARGET_MODE=""
DEPLOY_PROFILE=""
# ── Parse args ──
while [[ $# -gt 0 ]]; do
case "$1" in
--quick) SCALE="small"; shift ;;
--compat-only) RUN_INTEGRATION=false; shift ;;
--skip-build) RUN_BUILD=false; shift ;;
--deploy) RUN_DEPLOY=true; shift ;;
--custom) DEPLOY_TARGET_MODE="custom"; shift ;;
--profile)
[[ $# -ge 2 && -n "$2" ]] || { echo "--profile requires a name" >&2; exit 2; }
DEPLOY_TARGET_MODE="profile"
DEPLOY_PROFILE="$2"
shift 2
;;
--keep-data) KEEP_DATA=true; shift ;;
--help|-h)
echo "Usage: ./pre-push.sh [--quick|--compat-only|--skip-build|--deploy (--custom|--profile NAME)|--keep-data]"
echo ""
echo " --quick Use small scale instead of medium for faster runs"
echo " --compat-only Only run backward compatibility checks (no integration)"
echo " --skip-build Skip frontend build verification"
echo " --deploy After all tests pass, deploy the explicitly selected target"
echo " --custom Use the generic REDMAN_DEPLOY_* target"
echo " --profile NAME Use a local .redman-deploy-profiles.sh target"
echo " --keep-data Don't clean up test data after integration tests"
exit 0
;;
*)
echo "Unknown option: $1 (use --help for usage)"
exit 1
;;
esac
done
if $RUN_DEPLOY && [[ -z "$DEPLOY_TARGET_MODE" ]]; then
echo "--deploy requires --custom or --profile NAME" >&2
exit 2
fi
# ── Colors ──
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
PASS=0
FAIL=0
STEPS=()
step_pass() {
PASS=$((PASS + 1))
STEPS+=("${GREEN}✅ $1${NC}")
echo -e "${GREEN}✅ $1${NC}"
}
step_fail() {
FAIL=$((FAIL + 1))
STEPS+=("${RED}❌ $1${NC}")
echo -e "${RED}❌ $1${NC}"
}
# ── Header ──
echo ""
echo -e "${BOLD}═══════════════════════════════════════════════${NC}"
echo -e "${BOLD} RedMan Pre-Push Validation${NC}"
echo -e "${BOLD}═══════════════════════════════════════════════${NC}"
echo -e " Scale: ${CYAN}$SCALE${NC} Integration: ${CYAN}$RUN_INTEGRATION${NC} Build: ${CYAN}$RUN_BUILD${NC}"
echo ""
# ──────────────────────────────────────────────────
# Step 1: Syntax check all backend files
# ──────────────────────────────────────────────────
echo -e "${YELLOW}▶ Step 1: Backend syntax check${NC}"
SYNTAX_OK=true
for f in app/backend/src/index.js app/backend/src/peerApi.js app/backend/src/db.js app/backend/src/migrations.js app/backend/src/seed.js; do
if ! node --check "$f" 2>/dev/null; then
echo " Syntax error in $f"
SYNTAX_OK=false
fi
done
for f in app/backend/src/routes/*.js app/backend/src/services/*.js; do
if ! node --check "$f" 2>/dev/null; then
echo " Syntax error in $f"
SYNTAX_OK=false
fi
done
if $SYNTAX_OK; then
step_pass "Backend syntax check"
else
step_fail "Backend syntax check"
fi
echo -e "\n${YELLOW}▶ Step 1b: Mitigation regressions${NC}"
if (cd app && npm run test:mitigations); then
step_pass "Mitigation regressions"
else
step_fail "Mitigation regressions"
fi
# ──────────────────────────────────────────────────
# Step 2: Backward compatibility — static checks
# ──────────────────────────────────────────────────
echo -e "\n${YELLOW}▶ Step 2: Backward compatibility (static)${NC}"
if node test/test_backward_compat.mjs --skip-live 2>&1 | tee /tmp/redman_compat.log | tail -5; then
if grep -q "0 failed" /tmp/redman_compat.log; then
step_pass "Backward compatibility (static)"
else
step_fail "Backward compatibility (static) — see output above"
fi
else
step_fail "Backward compatibility (static) — script crashed"
fi
# ──────────────────────────────────────────────────
# Step 3: Frontend build
# ──────────────────────────────────────────────────
if $RUN_BUILD; then
echo -e "\n${YELLOW}▶ Step 3: Frontend build${NC}"
if (cd app && npm run build) 2>&1 | tail -5; then
step_pass "Frontend build"
else
step_fail "Frontend build"
fi
else
echo -e "\n${YELLOW}▶ Step 3: Frontend build (skipped)${NC}"
fi
# ──────────────────────────────────────────────────
# Step 4: Start test instances
# ──────────────────────────────────────────────────
if $RUN_INTEGRATION; then
echo -e "\n${YELLOW}▶ Step 4: Starting test instances${NC}"
# Stop any existing instances first
./test/setup_local_test.sh --stop 2>/dev/null || true
sleep 1
if ./test/setup_local_test.sh 2>&1 | tail -3; then
# Wait for API to be ready
echo " Waiting for API..."
READY=false
for i in $(seq 1 30); do
if curl -sf http://localhost:8090/api/health >/dev/null 2>&1; then
READY=true
break
fi
sleep 1
done
if $READY; then
step_pass "Test instances started"
else
step_fail "Test instances — API not reachable after 30s"
RUN_INTEGRATION=false
fi
else
step_fail "Test instances failed to start"
RUN_INTEGRATION=false
fi
fi
# ──────────────────────────────────────────────────
# Step 5: Backward compatibility — live API checks
# ──────────────────────────────────────────────────
if $RUN_INTEGRATION; then
echo -e "\n${YELLOW}▶ Step 5: Backward compatibility (live API)${NC}"
if node test/test_backward_compat.mjs 2>&1 | tee /tmp/redman_compat_live.log | tail -5; then
if grep -q "0 failed" /tmp/redman_compat_live.log; then
step_pass "Backward compatibility (live API)"
else
step_fail "Backward compatibility (live API) — see output above"
fi
else
step_fail "Backward compatibility (live API) — script crashed"
fi
fi
# ──────────────────────────────────────────────────
# Step 6: Comprehensive integration test
# ──────────────────────────────────────────────────
if $RUN_INTEGRATION; then
INTEG_ARGS="--scale $SCALE"
if $KEEP_DATA; then
INTEG_ARGS="$INTEG_ARGS --keep-data"
fi
echo -e "\n${YELLOW}▶ Step 6: Integration test (scale=$SCALE)${NC}"
if node test/test_comprehensive.mjs $INTEG_ARGS 2>&1 | tee /tmp/redman_integration.log | tail -20; then
# Check for test pass in output
if grep -qE "(All .* passed|PASSED|0 failed)" /tmp/redman_integration.log; then
step_pass "Integration test ($SCALE)"
elif [ ${PIPESTATUS[0]} -eq 0 ]; then
step_pass "Integration test ($SCALE)"
else
step_fail "Integration test ($SCALE)"
fi
else
step_fail "Integration test ($SCALE)"
fi
fi
# ──────────────────────────────────────────────────
# Step 7: Stop test instances
# ──────────────────────────────────────────────────
if $RUN_INTEGRATION; then
echo -e "\n${YELLOW}▶ Stopping test instances${NC}"
./test/setup_local_test.sh --stop 2>/dev/null || true
fi
# ──────────────────────────────────────────────────
# Summary
# ──────────────────────────────────────────────────
echo ""
echo -e "${BOLD}═══════════════════════════════════════════════${NC}"
echo -e "${BOLD} Results: ${GREEN}$PASS passed${NC}, ${RED}$FAIL failed${NC}"
echo -e "${BOLD}═══════════════════════════════════════════════${NC}"
echo ""
for s in "${STEPS[@]}"; do
echo -e " $s"
done
echo ""
if [ $FAIL -gt 0 ]; then
echo -e "${RED}${BOLD}🚫 PUSH BLOCKED — fix failures above${NC}"
echo ""
exit 1
fi
echo -e "${GREEN}${BOLD}✅ All checks passed${NC}"
# ──────────────────────────────────────────────────
# Step 8: Deploy (if requested)
# ──────────────────────────────────────────────────
if $RUN_DEPLOY; then
echo ""
if [[ "$DEPLOY_TARGET_MODE" == "custom" ]]; then
echo -e "${YELLOW}▶ Deploying generic custom target...${NC}"
./deploy.sh --custom
else
echo -e "${YELLOW}▶ Deploying local profile ${DEPLOY_PROFILE}...${NC}"
./deploy.sh --profile "$DEPLOY_PROFILE"
fi
fi
echo ""