.PHONY: all
all: develop

WEBPACK := pnpm run build-acceptance

freeze-requirements:
	@uv lock

bootstrap:
	@echo "devenv bootstrap is typically run on new machines."
	@echo "you probably want to run devenv sync to bring the"
	@echo "sentry dev environment up to date!"

.PHONY: init-config run-dependent-services build-platform-assets clean create-db drop-db apply-migrations reset-db
init-config:
	sentry init --dev --no-clobber

run-dependent-services:
	devservices up

build-platform-assets:
	@echo "--> Building platform assets"
	python3 -m sentry.build._integration_docs
	@# make sure this didn't silently do nothing
	@test -f src/sentry/integration-docs/android.json

clean:
	@echo "--> Cleaning static cache"
	rm -rf dist/* src/sentry/static/sentry/dist/*
	@echo "--> Cleaning integration docs cache"
	rm -rf src/sentry/integration-docs
	@echo "--> Cleaning pyc files"
	find . -name "*.pyc" -delete
	@echo "--> Cleaning python build artifacts"
	rm -rf build/ dist/ src/sentry/assets.json

create-db:
	@python3 -m tools.dev_db create

drop-db:
	@python3 -m tools.dev_db drop

apply-migrations:
	@python3 -m tools.dev_db migrate

# One invocation keeps drop, migrate and createuser ordered under `make -j`.
reset-db:
	@python3 -m tools.dev_db reset

develop \
install-js-dev \
install-py-dev :
	@make devenv-sync

# This is to ensure devenv sync's only called once if the above
# macros are combined e.g. `make install-js-dev install-py-dev`
.PHONY: devenv-sync
devenv-sync:
	devenv sync

build-js-po:
	mkdir -p build
	pnpm run build-js-po

build-spectacular-docs:
	@echo "--> Building drf-spectacular openapi spec (combines with deprecated docs)"
	@OPENAPIGENERATE=1 sentry django spectacular --file tests/apidocs/openapi-spectacular.json --format openapi-json --validate --fail-on-warn

build-deprecated-docs:
	@echo "--> Building deprecated openapi spec from json files"
	pnpm run build-deprecated-docs

build-api-docs: build-deprecated-docs build-spectacular-docs
	@echo "--> Dereference the json schema for ease of use"
	pnpm run deref-api-docs

# The internal RPC method schema, which CI's openapi-diff checks for breaking changes.
# It also ships with @sentry/api releases (static/packages/api) next to the API spec.
build-rpc-schema:
	@mkdir -p .artifacts
	sentry rpcschema --loglevel=WARNING > .artifacts/rpc_method_schema.json

# Opt-in private schema for local review; never included in the public docs build.
build-investigation-api-docs: build-deprecated-docs
	@mkdir -p .artifacts/investigation-api-docs
	@OPENAPIGENERATE=1 sentry django spectacular --custom-settings sentry.investigations.apidocs.PRIVATE_SCHEMA_SETTINGS --file .artifacts/investigation-api-docs/openapi.json --format openapi-json --validate --fail-on-warn
	pnpm exec node api-docs/index.ts .artifacts/investigation-api-docs/openapi.json .artifacts/investigation-api-docs/openapi-derefed.json
	pnpm --filter @sentry-internal/api-docs exec openapi-examples-validator ../.artifacts/investigation-api-docs/openapi-derefed.json --no-additional-properties

api-owner-map:
	@echo "--> Generating API endpoint owner map"
	@mkdir -p .artifacts
	@sentry django api_endpoint_owners --output .artifacts/api-endpoint-owners.json

watch-api-docs:
	@cd api-docs/ && node ./watch.ts

# Compares tests/apidocs/openapi-derefed.json (from build-api-docs) with the
# master spec where this branch started, the same way CI's openapi-diff does.
# Needs gh (logged in) and docker.
diff-api-docs:
	@echo "--> diffing local api docs against the merge-base's spec"
	.github/workflows/scripts/openapi-diff.sh baseline .artifacts/openapi-baseline
	.github/workflows/scripts/openapi-diff.sh changelog .artifacts/openapi-baseline/openapi-derefed.json tests/apidocs/openapi-derefed.json

build: locale

merge-locale-catalogs: build-js-po
	uv pip install Babel
	cd src/sentry && sentry django makemessages -i static -l en
	./bin/merge-catalogs en

compile-locale:
	uv pip install Babel
	./bin/find-good-catalogs src/sentry/locale/catalogs.json
	cd src/sentry && sentry django compilemessages

install-transifex:
	uv pip install transifex-client

push-transifex: merge-locale-catalogs install-transifex
	tx push -s

pull-transifex: install-transifex
	tx pull -a

# Update transifex with new strings that need to be translated
update-transifex: push-transifex

# Pulls new translations from transifex and compiles for usage
update-local-locales: pull-transifex compile-locale

build-chartcuterie-config:
	@echo "--> Building chartcuterie config module"
	pnpm run build-chartcuterie-config

run-acceptance:
	@echo "--> Running acceptance tests"
	python3 -b -m pytest tests/acceptance --reuse-db --json-report --json-report-file=".artifacts/pytest.acceptance.json" --json-report-omit=log --junit-xml=".artifacts/acceptance.junit.xml" -o junit_suite_name=acceptance
	@echo ""

test-cli: create-db
	@echo "--> Testing CLI"
	rm -rf test_cli
	mkdir test_cli
	cd test_cli && sentry init test_conf
	cd test_cli && sentry --config=test_conf help
	cd test_cli && sentry --config=test_conf upgrade --traceback --noinput
	cd test_cli && sentry --config=test_conf export --help
	rm -r test_cli
	@echo ""

test-js-build:
	@echo "--> Running type check"
	@pnpm run tsc -p tsconfig.json
	@echo "--> Building static assets"
	@NODE_ENV=production pnpm run build-profile > .artifacts/webpack-stats.json

test-js:
	@echo "--> Running JavaScript tests"
	@pnpm run test
	@echo ""

test-js-ci:
	@echo "--> Running CI JavaScript tests"
	@pnpm run test-ci
	@echo ""

test-python-ci:
	@echo "--> Running CI Python tests"
	python3 -b .github/workflows/scripts/pytest-selected.py \
		tests \
		--reuse-db \
		--ignore tests/acceptance \
		--ignore tests/apidocs \
		--ignore tests/js \
		--ignore tests/tools \
		--json-report \
		--json-report-file=".artifacts/pytest.json" \
		--json-report-omit=log \
		--junit-xml=.artifacts/pytest.junit.xml \
		-o junit_suite_name=pytest
	@echo ""

test-migrations-ci:
	@echo "--> Running CI Python migration tests"
	python3 -b -m pytest \
		$$(find tests -type d -name migrations) \
		-m migrations \
		--migrations \
		--reruns 0 \
		--fail-slow=150s \
		--reuse-db \
		--json-report \
		--json-report-file=".artifacts/pytest.json" \
		--json-report-omit=log \
		--junit-xml=.artifacts/pytest.junit.xml \
		-o junit_suite_name=pytest
	@echo ""

test-backend-ci-with-coverage:
	@echo "--> Running CI Python tests with coverage"
	python3 -b -m pytest \
		tests \
		--reuse-db \
		--ignore tests/acceptance \
		--ignore tests/apidocs \
		--ignore tests/js \
		--ignore tests/tools \
		--cov . \
		--cov-context=test \
		--json-report \
		--json-report-file=".artifacts/pytest.json" \
		--json-report-omit=log \
		--junit-xml=.artifacts/pytest.junit.xml \
		-o junit_suite_name=pytest
	@echo ""

test-selective:
	@echo "--> Running selective tests based on branch changes"
	python3 .github/workflows/scripts/selective-testing/fetch-coverage.py \
		--output .cache/coverage.db
	mkdir -p .cache && > .cache/selected-tests.txt
	python3 .github/workflows/scripts/compute-sentry-selected-tests.py \
		--coverage-db .cache/coverage.db \
		--changed-files "$$(git diff --name-only $$(git merge-base origin/master HEAD))" \
		--output .cache/selected-tests.txt
	python3 .github/workflows/scripts/selective-testing/confirm-test-selection.py \
		.cache/selected-tests.txt
	SELECTED_TESTS_FILE=.cache/selected-tests.txt \
	python3 -b .github/workflows/scripts/pytest-selected.py \
		-n3 \
		tests \
		--reuse-db \
		--ignore tests/acceptance \
		--ignore tests/apidocs \
		--ignore tests/js \
		--ignore tests/tools \
		--quiet
	@echo ""

# it's not possible to change settings.DATABASE after django startup, so
# unfortunately these tests must be run in a separate pytest process. References:
#   * https://docs.djangoproject.com/en/4.2/topics/testing/tools/#overriding-settings
#   * https://code.djangoproject.com/ticket/19031
#   * https://github.com/pombredanne/django-database-constraints/blob/master/runtests.py#L61-L77
test-monolith-dbs:
	@echo "--> Running CI Python tests (SENTRY_USE_MONOLITH_DBS=1)"
	SENTRY_LEGACY_TEST_SUITE=1 \
	SENTRY_USE_MONOLITH_DBS=1 \
	python3 -b -m pytest \
	  tests/sentry/backup/test_exhaustive.py \
	  tests/sentry/backup/test_exports.py \
	  tests/sentry/backup/test_imports.py \
	  tests/sentry/runner/commands/test_backup.py \
	  --reuse-db \
	  --json-report \
	  --json-report-file=".artifacts/pytest.monolith-dbs.json" \
	  --json-report-omit=log \
	  --junit-xml=.artifacts/monolith-dbs.junit.xml \
	  -o junit_suite_name=monolith-dbs \
	;
	@echo ""

test-tools:
	@echo "--> Running tools tests"
	@# bogus configuration to force vanilla pytest
	python3 -b -m pytest -c setup.cfg --confcutdir tests/tools tests/tools .github/workflows/scripts --reuse-db -vv --junit-xml=.artifacts/tools.junit.xml -o junit_suite_name=tools
	@echo ""

# JavaScript relay tests are meant to be run within Symbolicator test suite, as they are parametrized to verify both processing pipelines during migration process.
# Running Locally: Run `devservices up` before starting these tests
test-symbolicator:
	@echo "--> Running symbolicator tests"
	python3 -b -m pytest tests/symbolicator tests/relay_integration/lang/javascript/ tests/relay_integration/lang/java/ tests/sentry/ -m symbolicator --reuse-db -vv --junit-xml=.artifacts/symbolicator.junit.xml -o junit_suite_name=symbolicator
	@echo ""

test-acceptance:
	@echo "--> Building static assets"
	@$(WEBPACK)
	make run-acceptance

# XXX: this is called by `getsentry/relay`
test-relay-integration:
	@echo "--> Running Relay integration tests"
	python3 -b -m pytest \
		tests/relay_integration \
		tests/sentry/ingest/ingest_consumer/test_ingest_consumer_kafka.py \
		--reuse-db \
		-vv
	@echo ""

validate-api-docs: build-api-docs build-investigation-api-docs
	pnpm run validate-api-examples

test-api-docs: validate-api-docs
	python3 -b -m pytest tests/apidocs --reuse-db
	@echo ""

review-python-snapshots:
	@cargo insta --version &> /dev/null || cargo install cargo-insta
	@cargo insta review --workspace-root `pwd` -e pysnap

accept-python-snapshots:
	@cargo insta --version &> /dev/null || cargo install cargo-insta
	@cargo insta accept --workspace-root `pwd` -e pysnap

reject-python-snapshots:
	@cargo insta --version &> /dev/null || cargo install cargo-insta
	@cargo insta reject --workspace-root `pwd` -e pysnap

.PHONY: build
