version: 2

jobs:
  build:
    working_directory: ~/{{project_name}}
    docker:
      - image: circleci/python:3.7-stretch-node-browsers # remeber to update those!
    steps:
      - checkout
      # this updates git-lfs to make pre-commit large files check hook work properly
      # more details in https://github.com/pre-commit/pre-commit-hooks/issues/252
      - run:
          command: curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
      - run:
          command: sudo apt-get install git-lfs --upgrade
      - run:
          command: sudo chown -R circleci:circleci /usr/local/bin
      - run:
          command: sudo chown -R circleci:circleci /usr/local/lib/python3.7/site-packages
      - run:
          command: pip install requests pip-tools --upgrade
      {% verbatim %}
      - restore_cache:
          keys:
            - v1-pip-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
            - v1-pip-cache-{{ .Branch }}-
            - v1-pip-cache-
      - run:
          command: |
            pip-compile requirements.in > requirements.txt;
            pip-compile dev-requirements.in > dev-requirements.txt;
            pip install --user -r requirements.txt && pip install -r dev-requirements.txt
      - save_cache:
          key: pip-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - .venv
            - /usr/local/bin
            - /usr/local/lib/python3.7/site-packages
      - restore_cache:
          keys:
            - v1-npm-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
            - v1-npm-cache-{{ .Branch }}-
            - v1-npm-cache-
      - run:
          command: npm install
      - save_cache:
          key: npm-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - node_modules
      {% endverbatim %}
      - run:
          command: npm run build
      - run:
          command: npm run lint
      # style check
      - run:
          command: prospector --messages-only
      # security checks
      - run:
          command: bandit -r .
      # imports check
      - run:
          command: isort **/*.py --check-only
      - run:
          command: pre-commit run --all-files
          environment:
            SKIP: prospector,isort,eslint,missing-migrations
      - run:
          command: (cd backend; python manage.py has_missing_migrations --ignore authtools)
          environment:
            DJANGO_SETTINGS_MODULE: '{{project_name}}.settings.local_base'
      - run:
          command: (cd backend; python manage.py check --deploy)
          environment:
            DJANGO_SETTINGS_MODULE: '{{project_name}}.settings.production'
            SECRET_KEY: "$(python -c 'import uuid; print(uuid.uuid4().hex + uuid.uuid4().hex)')"
            DATABASE_URL: 'sqlite:///'
            ALLOWED_HOSTS: '.example.org'
            SENDGRID_USERNAME: 'test'
            SENDGRID_PASSWORD: 'test'
            REDIS_URL: 'redis://'
      - run:
          command: (cd backend; coverage run manage.py test)
      - run:
          command: npm run test
      - run:
          command: |
            mkdir -p test-reports/
            coverage xml -o test-reports/results.xml
          when: always
      - store_test_results:
          path: test-reports
      - store_artifacts:
          path: test-reports
