Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
323 lines (276 loc) · 14 KB
/
Copy pathMakefile
File metadata and controls
323 lines (276 loc) · 14 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
.PHONY: help build test lint clean docker-build docker-up docker-down migrate migrate-rollback migrate-status migrate-force seed seed-dev dev install setup version sync-version check-version
# ============================================================================
# VERSION — single source of truth is the root VERSION file (see docs/VERSIONING.md)
# ============================================================================
VERSION := $(shell cat VERSION 2>/dev/null | tr -d '[:space:]')
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
LDFLAGS := -X main.Version=$(VERSION) -X main.Commit=$(GIT_COMMIT)
COMPOSE ?= docker compose
help:
@echo "╔════════════════════════════════════════════════════════════════╗"
@echo "║ OpenRisk Development Commands ║"
@echo "╚════════════════════════════════════════════════════════════════╝"
@echo ""
@echo " 🚀 QUICK START"
@echo " make setup - Initial setup (install dependencies)"
@echo " make dev - Start full development environment"
@echo " make docker-up - Start all Docker services"
@echo ""
@echo " 🔨 BUILD & COMPILE"
@echo " make build - Build backend binary"
@echo " make frontend-build - Build frontend for production"
@echo ""
@echo " 🧪 TESTING"
@echo " make test - Run all backend tests with coverage"
@echo " make test-unit - Run unit tests only (fast)"
@echo " make test-integration - Run integration tests (slow)"
@echo " make frontend-test - Run frontend tests"
@echo ""
@echo " 📝 CODE QUALITY"
@echo " make lint - Run all linters"
@echo " make lint-backend - Run backend linter (golangci-lint)"
@echo " make lint-frontend - Run frontend linter (ESLint)"
@echo " make format - Format code (gofmt + prettier)"
@echo ""
@echo " 🐳 DOCKER"
@echo " make docker-up - Start all containers"
@echo " make docker-down - Stop all containers"
@echo " make docker-build - Build Docker image"
@echo " make docker-logs - View live container logs"
@echo " make docker-status - Show container status"
@echo " make docker-clean - Remove containers and volumes"
@echo ""
@echo " 💾 DATABASE"
@echo " make migrate - Run database migrations"
@echo " make migrate-rollback - Rollback last migration"
@echo " make migrate-status - Show current migration version + dirty flag"
@echo " make migrate-force VERSION=<n> - Recover a DIRTY database (set version, clear dirty)"
@echo " make seed - Seed database with sample data"
@echo " make seed-dev - Fill the running stack with the dev dataset (docs/DEV_DATASET.md)"
@echo " make db-shell - Open database shell (psql)"
@echo " make db-test-shell - Open test database shell"
@echo ""
@echo " 🧹 CLEANUP"
@echo " make clean - Clean build artifacts"
@echo " make clean-all - Clean everything (DESTRUCTIVE)"
@echo ""
# ============================================================================
# SETUP & INSTALLATION
# ============================================================================
setup: install frontend-install
@echo "✅ Setup complete! Run 'make dev' to start developing."
install:
@echo "📦 Installing backend dependencies..."
cd backend && go mod download
@echo "✅ Backend dependencies installed"
frontend-install:
@echo "📦 Installing frontend dependencies..."
cd frontend && npm ci
@echo "✅ Frontend dependencies installed"
# ============================================================================
# BUILD & COMPILE
# ============================================================================
build:
@echo "🔨 Building backend binary (v$(VERSION), commit $(GIT_COMMIT))..."
cd backend && CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o openrisk ./cmd/server
@echo "✅ Backend binary built: backend/openrisk"
# ----------------------------------------------------------------------------
# VERSION propagation & verification
# ----------------------------------------------------------------------------
version: ## Print the single-source version
@echo "$(VERSION)"
sync-version: ## Write VERSION into the Helm chart, frontend package.json and the lockfile root
@echo "🔁 Propagating VERSION=$(VERSION) to Helm chart + frontend + lockfile..."
@sed -i.bak -E 's/^version: .*/version: $(VERSION)/' helm/openrisk/Chart.yaml && rm -f helm/openrisk/Chart.yaml.bak
@sed -i.bak -E 's/^appVersion: .*/appVersion: "$(VERSION)"/' helm/openrisk/Chart.yaml && rm -f helm/openrisk/Chart.yaml.bak
@sed -i.bak -E '0,/"version":/ s/("version": *")[^"]*"/\1$(VERSION)"/' frontend/package.json && rm -f frontend/package.json.bak
@sed -i.bak -E -e 's/^( "version": *")[^"]*"/\1$(VERSION)"/' \
-e '/^ "": [{]/,/^ }/ s/^( "version": *")[^"]*"/\1$(VERSION)"/' frontend/package-lock.json && rm -f frontend/package-lock.json.bak
@echo "✅ Synced. Review the diff, then commit."
check-version: ## Fail if Helm chart / frontend / lockfile version drift from VERSION (used by release CI)
@chart="$$(grep -E '^version:' helm/openrisk/Chart.yaml | head -1 | awk '{print $$2}')"; \
app="$$(grep -E '^appVersion:' helm/openrisk/Chart.yaml | head -1 | sed -E 's/appVersion: *"?([^"]*)"?/\1/')"; \
fe="$$(grep -E '"version"' frontend/package.json | head -1 | sed -E 's/.*: *"([^"]*)".*/\1/')"; \
lock="$$(grep -E '^ "version":' frontend/package-lock.json | head -1 | sed -E 's/.*: *"([^"]*)".*/\1/')"; \
lockroot="$$(sed -n -E '/^ "": [{]/,/^ }/ s/^ "version": *"([^"]*)".*/\1/p' frontend/package-lock.json)"; \
fail=0; \
[ "$$chart" = "$(VERSION)" ] || { echo "::error::Helm chart version '$$chart' != VERSION '$(VERSION)'"; fail=1; }; \
[ "$$app" = "$(VERSION)" ] || { echo "::error::Helm appVersion '$$app' != VERSION '$(VERSION)'"; fail=1; }; \
[ "$$fe" = "$(VERSION)" ] || { echo "::error::frontend version '$$fe' != VERSION '$(VERSION)'"; fail=1; }; \
[ "$$lock" = "$(VERSION)" ] || { echo "::error::frontend/package-lock.json version '$$lock' != VERSION '$(VERSION)'"; fail=1; }; \
[ "$$lockroot" = "$(VERSION)" ] || { echo "::error::frontend/package-lock.json packages[\"\"].version '$$lockroot' != VERSION '$(VERSION)'"; fail=1; }; \
if [ "$$fail" -ne 0 ]; then exit 1; fi; \
echo "✅ Versions aligned on $(VERSION) (chart/appVersion/frontend/lockfile)"
frontend-build:
@echo "🔨 Building frontend for production..."
cd frontend && npm run build
@echo "✅ Frontend built: frontend/dist/"
# ============================================================================
# TESTING
# ============================================================================
test:
@echo "🧪 Running all backend tests with coverage..."
cd backend && go test -v -coverprofile=coverage.out ./...
@echo ""
@echo "📊 Coverage report:"
cd backend && go tool cover -func=coverage.out | tail -1
test-unit:
@echo "🧪 Running unit tests (fast)..."
cd backend && go test -v -short ./...
# Same order as CI (#1491): a fresh Postgres 16 and Redis 7 (throwaway, tmpfs,
# like the CI services), the schema built by one server boot
# (scripts/ci/build-schema.sh), then the tests. Without the boot every *_Postgres
# test that reads the real schema fails on "relation ... does not exist".
# The containers are removed whatever the outcome.
IT_DB_PORT ?= 5437
IT_REDIS_PORT ?= 6381
IT_SERVER_PORT ?= 18080
IT_DB_URL := postgres://test:test@localhost:$(IT_DB_PORT)/openrisk_test?sslmode=disable
test-integration:
@echo "🧪 Running integration tests..."
@set -e; \
cleanup() { docker rm -f openrisk-it-pg openrisk-it-redis >/dev/null 2>&1 || true; }; \
trap cleanup EXIT; cleanup; \
docker run -d --rm --name openrisk-it-pg -p 127.0.0.1:$(IT_DB_PORT):5432 \
-e POSTGRES_USER=test -e POSTGRES_PASSWORD=test -e POSTGRES_DB=openrisk_test \
--tmpfs /var/lib/postgresql/data postgres:16-alpine >/dev/null; \
docker run -d --rm --name openrisk-it-redis -p 127.0.0.1:$(IT_REDIS_PORT):6379 redis:7-alpine >/dev/null; \
for i in $$(seq 1 30); do docker exec openrisk-it-pg pg_isready -h 127.0.0.1 -U test -d openrisk_test >/dev/null 2>&1 && break; sleep 1; done; \
docker exec openrisk-it-pg pg_isready -h 127.0.0.1 -U test -d openrisk_test; \
export DATABASE_URL="$(IT_DB_URL)" DB_HOST=localhost DB_PORT=$(IT_DB_PORT) DB_USER=test DB_PASSWORD=test \
DB_NAME=openrisk_test REDIS_HOST=localhost REDIS_PORT=$(IT_REDIS_PORT) \
REDIS_URL=redis://localhost:$(IT_REDIS_PORT) APP_ENV=test; \
PORT=$(IT_SERVER_PORT) scripts/ci/build-schema.sh; \
cd backend && go test -race -tags=integration ./...
frontend-test:
@echo "🧪 Running frontend tests..."
cd frontend && npm run test
# ============================================================================
# CODE QUALITY & FORMATTING
# ============================================================================
lint-backend:
@echo "📝 Running backend linter..."
cd backend && golangci-lint run ./...
lint-frontend:
@echo "📝 Running frontend linter..."
cd frontend && npm run lint
lint: lint-backend lint-frontend
@echo "✅ All linting passed!"
format:
@echo "🎨 Formatting code..."
cd backend && gofmt -s -w .
cd frontend && npm run format
@echo "✅ Code formatted!"
# ============================================================================
# DOCKER MANAGEMENT
# ============================================================================
docker-up:
@echo "🐳 Starting all containers..."
$(COMPOSE) up -d --build
@echo "✅ Containers started"
@echo ""
@echo " Frontend: http://localhost:5173"
@echo " Backend: http://localhost:8080"
@echo " Postgres: localhost:5434 (openrisk)"
@echo " Redis: localhost:6380"
docker-down:
@echo "🐳 Stopping all containers..."
$(COMPOSE) down
docker-build:
@echo "🐳 Building Docker image..."
docker build -t openrisk:latest .
@echo "✅ Docker image built"
docker-logs:
@echo "📋 Showing live container logs..."
$(COMPOSE) logs -f
docker-status:
@echo "📊 Container status:"
$(COMPOSE) ps
docker-clean:
@echo "🗑️ Removing containers and volumes (DESTRUCTIVE)..."
$(COMPOSE) down -v
@echo "✅ Cleanup complete"
# ============================================================================
# DATABASE MANAGEMENT
# ============================================================================
migrate:
@echo "📊 Running database migrations..."
$(COMPOSE) up -d db
@sleep 2
cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate up
@echo "✅ Migrations complete"
migrate-rollback:
@echo "⏮️ Rolling back last migration..."
$(COMPOSE) up -d db
@sleep 2
cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate down
migrate-status:
@echo "🔎 Current migration version..."
cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate version
# Recovery for a DIRTY database (a migration that failed mid-apply). Set VERSION
# to the last known-good migration, then re-run `make migrate`.
# Example: make migrate-force VERSION=47
migrate-force:
@if [ -z "$(VERSION)" ]; then echo "Usage: make migrate-force VERSION=<known_good_version>"; exit 1; fi
@echo "🔧 Forcing migration version to $(VERSION) (clears dirty flag)..."
cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate force $(VERSION)
@echo "✅ Rollback complete"
seed:
@echo "🌱 Seeding database with sample data..."
cd backend && go run ./cmd/server seed
@echo "✅ Database seeded"
# Fills the running compose stack through its API (#1337, docs/DEV_DATASET.md).
# The seed admin password is read from the backend container unless
# SEED_ADMIN_PASSWORD is already set. It goes through the environment and is
# never echoed.
seed-dev:
@SEED_ADMIN_PASSWORD="$${SEED_ADMIN_PASSWORD:-$$($(COMPOSE) exec -T backend cat /app/secrets/initial_admin_password 2>/dev/null)}" \
SEED_API_URL="$${SEED_API_URL:-http://localhost:$${OPENRISK_DEV_BACKEND_PORT:-8080}/api/v1}" \
node scripts/seed-dev.mjs
db-shell:
@echo "🔌 Connecting to production database..."
$(COMPOSE) exec db psql -U openrisk -d openrisk
db-test-shell:
@echo "🔌 Connecting to test database..."
$(COMPOSE) exec test_db psql -U test -d openrisk_test
# ============================================================================
# DEVELOPMENT
# ============================================================================
dev:
@echo "🚀 Starting development environment..."
@echo ""
@echo " Starting Docker services (db, redis)..."
$(COMPOSE) up -d db redis test_db
@sleep 2
@echo ""
@echo " Frontend will be available at: http://localhost:5173"
@echo " Backend API at: http://localhost:8080/api/v1"
@echo ""
@echo " Press Ctrl+C to stop. Use 'make docker-down' to stop Docker services."
@echo ""
@sleep 1
@echo "Starting backend (in background)..."
cd backend && OPENRISK_DEPLOYMENT=$${OPENRISK_DEPLOYMENT:-self_hosted} go run ./cmd/server &
@sleep 2
@echo ""
@echo "Starting frontend..."
cd frontend && npm run dev
dev-docker:
@echo "🐳 Starting full development environment in Docker..."
$(COMPOSE) up
dev-logs:
@echo "📋 Following all container logs..."
$(COMPOSE) logs -f
# ============================================================================
# CLEANUP
# ============================================================================
clean:
@echo "🧹 Cleaning build artifacts..."
cd backend && go clean
rm -f backend/openrisk
rm -f backend/coverage.out
rm -rf frontend/dist
@echo "✅ Cleanup complete"
clean-all: clean docker-clean
@echo "🗑️ Full cleanup complete (DESTRUCTIVE)"
.DEFAULT_GOAL := help