# this test is based on the docker image described in env/bitbucket/Dockerfile
image: iterativ/vbv-lernwelt-bitbucket

## job definitions
e2e: &e2e
  name: cypress test worker
  max-time: 45
  services:
    - postgres
  caches:
    - node
    - clientnode
    - vbvpip
    - cypress
  script:
    - export CURRENTS_RECORD_KEY=etzp5VXtJcSX8Z4H
    - export IT_SERVE_VUE=false
    - export IT_ALLOW_LOCAL_LOGIN=true
    - source ./env/bitbucket/prepare_for_test.sh
    - pip install -r server/requirements/requirements-dev.txt
    - npm install
    - CYPRESS_DOWNLOAD_MIRROR=https://cy-cdn.currents.dev npx cypress install --force
    - npm run build
    - ./prepare_server_cypress.sh --start-background
    - npm run cypress:ci
  artifacts:
    - cypress/**/*.png
    - cypress/**/*.mp4

deploy: &deploy
  name: deploy
  max-time: 30
  caches:
    - node
    - clientnode
    - vbvpip
    - docker
  services:
    - docker
  script:
    - source ./env/bitbucket/prepare_for_deployment.sh
    - ./caprover_deploy.sh ${DEPLOY_ENVIRONMENT:-}
    - cat /tmp/caprover_app_url.txt

python-tests: &python-tests
  name: python tests
  max-time: 15
  services:
    - postgres
  caches:
    - vbvpip
  script:
    - source ./env/bitbucket/prepare_for_test.sh
    - pip install -r server/requirements/requirements-dev.txt
    - ./server/run_tests_coverage.sh

python-linting: &python-linting
  name: python linting
  max-time: 15
  services:
    - postgres
  caches:
    - vbvpip
  script:
    - source ./env/bitbucket/prepare_for_test.sh
    - pip install -r server/requirements/requirements-dev.txt
    - git-crypt status -e | sort > git-crypt-encrypted-files-check.txt && diff -w git-crypt-encrypted-files.txt git-crypt-encrypted-files-check.txt
    - trufflehog --exclude_paths trufflehog-exclude-patterns.txt --allow trufflehog-allow.json --entropy=True --max_depth=100 .
    - ufmt check server

js-tests: &js-tests
  name: js tests
  max-time: 15
  caches:
    - node
    - clientnode
  script:
    - cd client
    - pwd
    - npm install
    - npm run codegen
    - npm test

js-linting: &js-linting
  name: js linting
  max-time: 15
  caches:
    - node
    - clientnode
  script:
    - cd client
    - pwd
    - npm install
    - npm run prettier:check
    - npm run lint
    - npm run typecheck

default-steps: &default-steps
  - parallel:
    - step: *e2e
    - step: *e2e
    - step: *python-tests
    - step: *python-linting
    - step: *js-tests
    - step: *js-linting

# main pipelines definitions
pipelines:
  default:
    - <<: *default-steps
    - step:
        <<: *deploy
        name: deploy feature
        trigger: manual
  branches:
    develop:
      - <<: *default-steps
      - step:
          <<: *deploy
          deployment: develop
          name: deploy develop
    master:
      - <<: *default-steps
      - step:
          <<: *deploy
          deployment: master
          name: deploy master
  tags:
    v202*:
      - step:
          name: prepare prod deployment
          script:
            - echo "Release ready!"
      - parallel:
        - step:
            <<: *deploy
            name: deploy prod
            deployment: prod
            trigger: manual
        - step:
            <<: *deploy
            name: deploy prod-azure
            deployment: prod-azure
            trigger: manual
  custom:
    deploy-feature-branch:
      - step:
          <<: *deploy
          name: deploy feature
    deploy-stage:
      - step:
          <<: *deploy
          deployment: stage

definitions:
  caches:
    cypress: /root/.cache/Cypress
    vbvpip: /opt/atlassian/pipelines/agent/build/vbvvenv/
    clientnode: /opt/atlassian/pipelines/agent/build/client/node_modules/
  services:
    postgres:
      image: postgres
      memory: 512
      variables:
        POSTGRES_HOST_AUTH_METHOD: trust
