# This circle.yml file does some basic testing on CI for the boilerplate itself.
#
# Although this file is present on the "boilerplate-release" branch, this file is
#   not restored from the django-admin startproject command.
#
# There are instructions on README.md for the user to replace this path with the
#   correct file.
#

version: 2

jobs:
  build:
    working_directory: ~/django-react-boilerplate
    docker:
      - image: circleci/python:3.7-stretch-node-browsers
    steps:
      - checkout
      - run:
          command: sudo chown -R circleci:circleci /usr/local/bin
      - run:
          command: sudo chown -R circleci:circleci /usr/local/lib/python3.7/site-packages
          # create testproject
      - run:
          command: pip install django==2.*
      - run:
          command: django-admin startproject testproject --extension py,yml,json,example --name Procfile,README.md --template=.
          # build and test frontend
      - restore_cache:
          keys:
            - v1-npm-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
            - v1-npm-cache-{{ .Branch }}-
            - v1-npm-cache-
      - run:
          command: npm update --save
          path: testproject
      - run:
          command: npm update --save-dev
          path: testproject
      - run:
          name: "hotfix for #133"
          command: |
            sed -i 's/"bootstrap-loader": "^2.1.0"/"bootstrap-loader": "2.1.0"/g' package.json
          path: testproject
      - run:
          command: npm install --no-optional
          path: testproject
      - run:
          command: npm dedupe
          path: testproject
      # npm ls will error on dependencies errors
      - run:
          command: npm ls
          path: testproject
      - save_cache:
          key: dependency-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - testproject/node_modules
      - run:
          command: npm run test
          path: testproject
      - run:
          command: npm run lint
          path: testproject
      - run:
          command: npm run build
          path: testproject
          # build and test backend
      - run:
          command: pip install requests pip-tools --upgrade
          path: testproject
      - run:
          command: pip-compile requirements.in > requirements.txt && pip-compile dev-requirements.in > dev-requirements.txt
          path: testproject
      - restore_cache:
          keys:
            - v1-pip-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
            - v1-pip-cache-{{ .Branch }}-
            - v1-pip-cache-
      - run:
          command: pip install --user -r requirements.txt && pip install -r dev-requirements.txt
          path: testproject
      - save_cache:
          key: pip-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - testproject/.venv
            - /usr/local/bin
            - /usr/local/lib/python3.7/site-packages
      - run:
          command: cp testproject/settings/local.py.example testproject/settings/local.py
          path: testproject/backend
      - run:
          command: cp .env.example .env
          path: testproject/backend
      - run:
          command: python manage.py makemigrations
          path: testproject/backend
      - run:
          command: python manage.py migrate
          path: testproject/backend
      - run:
          command: python manage.py test
          path: testproject/backend
      - run:
          command: prospector
          path: testproject/backend
      - run:
          command: python manage.py has_missing_migrations
          path: testproject/backend
      - run:
          command: python manage.py check --deploy --fail-level WARNING
          path: testproject/backend
          environment:
            SECRET_KEY: '$(python -c "import uuid; print(uuid.uuid4().hex + uuid.uuid4().hex)")'
            SENDGRID_USERNAME: foo
            SENDGRID_PASSWORD: password
            DJANGO_SETTINGS_MODULE: 'testproject.settings.production'
            DATABASE_URL: 'sqlite:///'
            ALLOWED_HOSTS: '.example.org'
            REDIS_URL: 'redis://'
  deploy:
    working_directory: ~/django-react-boilerplate
    docker:
      - image: circleci/python:3.6-stretch-node-browsers
    steps:
      - checkout
      - run:
          command: mkdir -p circleci
      - run:
          command: mv proj_circle.yml circleci/config.yml
      - run:
          command: git checkout -b boilerplate-release
      - run:
          command: git add circleci/config.yml
      - run:
          command: git rm proj_circle.yml
      - run:
          command: git commit -m "Replacing circle.yml" --author "Vinta Software <contact@vinta.com.br>"
          environment:
            GIT_COMMITTER_NAME: "Vinta Software"
            GIT_COMMITTER_EMAIL: "contact@vinta.com.br"
      - run:
          command: git push origin boilerplate-release --force

workflows:
  version: 2
  build_and_deploy:
    jobs:
      - build:
          filters:
            branches:
              ignore: boilerplate-release
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: master
  nightly:
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              only: master
    jobs:
      - build
