THREADS ?= 5

BASE_AWS_PROFILE    = gdb-infra-dev
BASE_AWS_ACCOUNT_ID = 483193324480
BASE_AWS_REGION     = us-east-1
BASE_ECR_REGISTRY   = $(BASE_AWS_ACCOUNT_ID).dkr.ecr.$(BASE_AWS_REGION).amazonaws.com

AWS_PROFILE    ?= gdb-whitelist-dev
AWS_ACCOUNT_ID ?= 814622134907
AWS_REGION     ?= us-east-1

ECR_REGISTRY        ?= $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
ECR_REPOSITORY_NAME ?= dna/pytest
ECR_REGISTRY_URI    ?= $(ECR_REGISTRY)/$(ECR_REPOSITORY_NAME)

REVISION  ?= $(shell git rev-parse --short=8 HEAD)

VENV     := ./venv
PYTHON    = $(VENV)/bin/python
PIP       = $(VENV)/bin/pip
PYTEST    = $(VENV)/bin/pytest
PYLINT    = $(VENV)/bin/pylint
ISORT     = $(VENV)/bin/isort
MYPY      = $(VENV)/bin/mypy
PIP_AUDIT = $(VENV)/bin/pip-audit
FLAKE     = $(VENV)/bin/flake8
BANDIT    = $(VENV)/bin/bandit
BLACK     = $(VENV)/bin/black

## -
## help - this message
.PHONY: help
help: Makefile
	@echo "Application: ${APP}\n"
	@echo "Run command:\n  make <target>\n"
	@grep -E -h '^## .*' $(MAKEFILE_LIST) | sed -n 's/^##//p'  | column -t -s '-' |  sed -e 's/^/ /'

## -
## Local commands:
$(VENV)/bin/activate:
	python -m venv $(VENV)/

##   venv - create local virtual env
venv: $(VENV)/bin/activate
	@echo "Install dependencies"
	$(PIP) install -r requirements/base.txt

##   test - run tests locally
.PHONY:test
test:
	$(PYTEST) --html=report.html --self-contained-html --alluredir=app/src/allure_report app/src/tests/ -n $(THREADS) --reruns 2

##   report - make report locally
.PHONY:report
report:
	allure serve app/src/allure_report/

##   clean-up - clean up build artifacts
.PHONY:clean-up
clean-up:
	rm -rf .pytest_cache allure_report automation.log report.html

##   checkstyle - run checkstyle locally
.PHONY:checkstyle
checkstyle:
	$(ISORT) -rc -w 120 --check-only --quiet app/src/
	$(FLAKE) app/src/ --max-line-length=120

##   dependencies-check - run safety locally
.PHONY:dependencies-check
dependencies-check:
	$(PIP_AUDIT) -r requirements/base.txt

##   dependencies-check - run bandit locally
.PHONY:owasp
owasp:
	$(BANDIT) -r app/src --skip B101,B311

##   security - run security checks locally
.PHONY:security
security: dependencies-check owasp

##   prettify - run prettify locally
.PHONY:prettify
prettify:
	$(BLACK) app/src --exclude='/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|venv|_build|buck-out|build|dist|migrations|.pytest_cache)/' --line-length=120 --target-version=py37
	$(ISORT) -rc -w 120 app/src

.PHONY:pre-commit
pre-commit: prettify checkstyle security

## -
## Docker commands:

##   docker/login/base - login to AWS ECR registry using docker, aws configuration need to be set first
.PHONY:docker/login/base
docker/login/base:
	@aws ecr get-login \
		--profile ${BASE_AWS_PROFILE} \
		--no-include-email \
		--region ${BASE_AWS_REGION} | awk '{print $$6}' | \
		docker login -u AWS --password-stdin https://$(BASE_ECR_REGISTRY) || aws ecr get-login-password \
			--profile ${BASE_AWS_PROFILE} \
			--region ${BASE_AWS_REGION} | \
			docker login --username AWS --password-stdin https://$(BASE_ECR_REGISTRY)

##   docker/login - login to AWS ECR registry using docker, aws configuration need to be set first
.PHONY:docker/login
docker/login:
	@aws ecr get-login \
		--profile ${AWS_PROFILE} \
		--no-include-email \
		--region ${AWS_REGION} | awk '{print $$6}' | \
		docker login -u AWS --password-stdin https://$(ECR_REGISTRY) || aws ecr get-login-password \
			--profile ${AWS_PROFILE} \
			--region ${AWS_REGION} | \
			docker login --username AWS --password-stdin https://$(ECR_REGISTRY)

##   docker/base/image/pull - pull base image from ECR
docker/base/image/pull: docker/login/base
	docker pull $(BASE_ECR_REGISTRY)/python:3.8-alpine3.13-runner
	docker pull $(BASE_ECR_REGISTRY)/python:3.8-alpine3.13-builder

##   docker/image/build - build lambda package for each environment and put all of them into one docker container
.PHONY: docker/image/build
docker/image/build: docker/login/base docker/login
	@echo "Building docker image"
	docker build \
		--cache-from $(ECR_REGISTRY_URI):latest \
		--target main \
		-t $(ECR_REGISTRY_URI):latest \
		-t $(ECR_REGISTRY_URI):$(REVISION) .

##   docker/image/push - push an image to a registry
.PHONY: docker/image/push
docker/image/push: docker/login
	@echo "Push docker image"
	docker push $(ECR_REGISTRY_URI):latest
	docker push $(ECR_REGISTRY_URI):$(REVISION)

##   docker/image/pull - pull an image from a registry
.PHONY: docker/image/pull
docker/image/pull: docker/login
	@echo "Pull docker image"
	docker pull $(ECR_REGISTRY_URI):$(REVISION)

.PHONY:docker/ui_smoke_test
docker/ui_smoke_test:
	@docker run \
		-e AUDIENCE \
		-e CLIENT_ID \
		-e CLIENT_SECRET \
		-e GRANT_TYPE \
		-e AUTH_HOST \
		-e AUTH_PROXY \
		-e BROWSER \
		-e ENV_BASE_URL \
		-e NO_MFA \
		-e TARGET_API_URL \
		-e USER_EMAIL \
		-i \
		--rm \
		-v $(dir $(abspath $(lastword $(MAKEFILE_LIST))))output/:/usr/src/output/ \
		$(ECR_REGISTRY_URI):$(REVISION) \
			/venv/bin/pytest \
			--html=output/report.html \
			--self-contained-html \
			--alluredir=output/allure_report \
			-n ${THREADS} \
			--reruns 2 \
			-m smoke \
			app/src/tests/

.PHONY:docker/ui_full_test
docker/ui_full_test:
	@docker run \
		-e AUDIENCE \
		-e CLIENT_ID \
		-e CLIENT_SECRET \
		-e GRANT_TYPE \
		-e AUTH_HOST \
		-e AUTH_PROXY \
		-e BROWSER \
		-e ENV_BASE_URL \
		-e NO_MFA \
		-e TARGET_API_URL \
		-e USER_EMAIL \
		-i \
		--rm \
		-v $(dir $(abspath $(lastword $(MAKEFILE_LIST))))output/:/usr/src/output/ \
		$(ECR_REGISTRY_URI):$(REVISION) \
			/venv/bin/pytest \
			--html=output/report.html \
			--self-contained-html \
			--alluredir=output/allure_report \
			-n ${THREADS} \
			--reruns 2 \
			app/src/tests/

