version: 2
jobs:
  build:
    docker:
      - image: circleci/node:10.14.2
    working_directory: ~/repo

    steps:
      - checkout

      - restore_cache:
          keys:
          - v2-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v2-dependencies-

      - run:
          name: Install dependencies
          command: npm install

      - save_cache:
          paths:
            - node_modules
          key: v2-dependencies-{{ checksum "package.json" }}

      - run:
          name: Run tests
          command: npm test

      - run:
          name: Run Coveralls.io
          command: npm run coverage

  generate-docs:
    docker:
      - image: circleci/node:10.14.2
    working_directory: ~/repo

    steps:
      - checkout
  
      - run:
          name: Install dependencies
          command: npm install node-fetch

      - run:
          name: Generate docs
          command: npm run esdoc-create

  publishImage:
    docker:
      - image: mythril/api-k8s-ci:v0.1.3
    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
      - run:
          command: |
            IMAGE=mythrilapiplatform/truffle-security
            TAG=${CIRCLE_SHA1}

            docker build -t ${IMAGE}:${TAG} .
            docker login -u ${DOCKER_USER} -p ${DOCKER_PASSWORD}
            docker push ${IMAGE}:${TAG}

  integrationTests:
    docker:
      - image: mythrilapiplatform/truffle-security:${CIRCLE_SHA1}
    steps:
      - checkout
      - run:
          command: |
            cd integration-tests/project
            MYTHX_API_URL=https://staging.api.mythx.io
            if [ ! -z "${CIRCLE_TAG}" ]; then
              MYTHX_API_URL=https://api.mythx.io
            fi
            export MYTHX_API_URL
            truffle compile
            CONTRACT_NAME=$(pwgen 16)
            sed -i "s/\"contractName\": \"FunctionTypes\"/\"contractName\": \"${CONTRACT_NAME}\"/" build/contracts/FunctionTypes.json
            echo $(truffle run verify --style json --no-progress) | tee result.json
            sed -i '/^\[/! s/.*//' result.json
            ordered_messages=$(cat result.json | jq .[0].messages | jq 'sort_by(.ruleId)')

            firstID=$(echo $ordered_messages | jq -r .[0].ruleId)
            if [ $firstID != "SWC-101" ]; then
              echo "Unexpected swcID, expected SWC-101, got $firstID"
              exit 1
            fi
            secondID=$(echo $ordered_messages | jq -r .[1].ruleId)
            if [ $secondID != "SWC-103" ]; then
              echo "Unexpected swcID, expected SWC-103, got $secondID"
              exit 1
            fi
            thirdID=$(echo $ordered_messages | jq -r .[2].ruleId)
            if [ $thirdID != "SWC-127" ]; then
              echo "Unexpected swcID, expected SWC-127, got $thirdID"
              exit 1
            fi

  tagImage:
    docker:
      - image: mythril/api-k8s-ci:v0.1.3
    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
      - run:
          command: |
            IMAGE=mythrilapiplatform/truffle-security
            TAG=${CIRCLE_TAG:-$(git describe --tags)}

            docker pull ${IMAGE}:${CIRCLE_SHA1}
            docker login -u ${DOCKER_USER} -p ${DOCKER_PASSWORD}
            docker tag ${IMAGE}:${CIRCLE_SHA1} ${IMAGE}:${TAG}
            docker push ${IMAGE}:${TAG}

            if [ ! -z "${CIRCLE_TAG}" ]; then
              docker tag ${IMAGE}:${CIRCLE_SHA1} ${IMAGE}:latest
              docker push ${IMAGE}:latest
            fi

workflows:
  version: 2
  build-deploy:
    jobs:
      - build
      - generate-docs:
          filters:
            branches:
              only: master
      - publishImage:
          requires:
            - build
      - integrationTests:
          requires:
            - publishImage
      - tagImage:
          requires:
            - integrationTests
