###
# Please keep /.mergify.yml synced!
version: 2

common_env: &common_env
  # CircleCI container has two cores, but Ruby can see 32 cores. So we
  # configure test-queue.
  # See https://github.com/tmm1/test-queue#environment-variables
  TEST_QUEUE_WORKERS: 2

spec_steps: &spec_steps
  - checkout
  - attach_workspace:
      at: ~/project/tmp
  - run: bundle install
  - run:
      name: Run specs
      command: |
        ./tmp/cc-test-reporter before-build
        COVERAGE=true bundle exec rake spec
        ./tmp/cc-test-reporter format-coverage --output tmp/codeclimate.$CIRCLE_JOB.json
  - persist_to_workspace:
      root: tmp
      paths:
        - codeclimate.*.json

ascii_spec_steps: &ascii_spec_steps
  - checkout
  - run: bundle install
  - run: bundle exec rake ascii_spec

rubocop_steps: &rubocop_steps
  - checkout
  - run: bundle install
  - run: bundle exec rake internal_investigation
  - run:
      name: Check requiring libraries successfully
      # See https://github.com/rubocop/rubocop/pull/4523#issuecomment-309136113
      command: |
        ruby -I lib -r bundler/setup -r rubocop -e 'exit 0'

jobs:

  # Ruby 2.6
  ruby-2.6-spec:
    docker:
      - image: cimg/ruby:2.6
    environment:
      <<: *common_env
    steps:
      *spec_steps
  ruby-2.6-ascii_spec:
    docker:
      - image: cimg/ruby:2.6
    environment:
      <<: *common_env
    steps:
      *ascii_spec_steps
  ruby-2.6-rubocop:
    docker:
      - image: cimg/ruby:2.6
    environment:
      <<: *common_env
    steps:
      *rubocop_steps

  # Ruby 2.7
  ruby-2.7-spec:
    docker:
      - image: cimg/ruby:2.7
    environment:
      <<: *common_env
    steps:
      *spec_steps
  ruby-2.7-ascii_spec:
    docker:
      - image: cimg/ruby:2.7
    environment:
      <<: *common_env
    steps:
      *ascii_spec_steps
  ruby-2.7-rubocop:
    docker:
      - image: cimg/ruby:2.7
    environment:
      <<: *common_env
    steps:
      *rubocop_steps

  # Ruby 3.0
  ruby-3.0-spec:
    docker:
      - image: cimg/ruby:3.0
    environment:
      <<: *common_env
    steps:
      *spec_steps
  ruby-3.0-ascii_spec:
    docker:
      - image: cimg/ruby:3.0
    environment:
      <<: *common_env
    steps:
      *ascii_spec_steps
  ruby-3.0-rubocop:
    docker:
      - image: cimg/ruby:3.0
    environment:
      <<: *common_env
    steps:
      *rubocop_steps

  # Ruby 3.1
  ruby-3.1-spec:
    docker:
      - image: cimg/ruby:3.1
    environment:
      <<: *common_env
    steps:
      *spec_steps
  ruby-3.1-ascii_spec:
    docker:
      - image: cimg/ruby:3.1
    environment:
      <<: *common_env
    steps:
      *ascii_spec_steps
  ruby-3.1-rubocop:
    docker:
      - image: cimg/ruby:3.1
    environment:
      <<: *common_env
    steps:
      *rubocop_steps

  # ruby-head (nightly snapshot build)
  ruby-head-spec:
    docker:
      - image: rubocophq/circleci-ruby-snapshot:latest
    environment:
      <<: *common_env
    steps:
      *spec_steps
  ruby-head-ascii_spec:
    docker:
      - image: rubocophq/circleci-ruby-snapshot:latest
    environment:
      <<: *common_env
    steps:
      *ascii_spec_steps
  ruby-head-rubocop:
    docker:
      - image: rubocophq/circleci-ruby-snapshot:latest
    environment:
      <<: *common_env
    steps:
      *rubocop_steps

  # Job for downloading the Code Climate test reporter
  cc-setup:
    docker:
      # Specify the latest version to prevent "cimg/ruby:latest not found: manifest unknown: manifest unknown" error.
      - image: cimg/ruby:3.1
    environment:
      <<: *common_env
    steps:
      - run:
          name: Download Code Climate test-reporter
          command: |
            mkdir -p tmp/
            curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./tmp/cc-test-reporter
            chmod +x ./tmp/cc-test-reporter
      - persist_to_workspace:
          root: tmp
          paths:
            - cc-test-reporter

  # Job for merging code coverage results and sending them to Code Climate
  cc-upload-coverage:
    docker:
      # Specify the latest version to prevent "cimg/ruby:latest not found: manifest unknown: manifest unknown" error.
      - image: cimg/ruby:3.1
        environment:
          CC_TEST_REPORTER_ID: a11b66bfbb1acdf220d5cb317b2e945a986fd85adebe29a76d411ad6d74ec31f
    environment:
      <<: *common_env
    steps:
      - attach_workspace:
          at: ~/project/tmp
      - run:
          name: Upload coverage results to Code Climate
          command: |
            ./tmp/cc-test-reporter sum-coverage tmp/codeclimate.*.json --parts 4 --output tmp/codeclimate.total.json
            ./tmp/cc-test-reporter upload-coverage --input tmp/codeclimate.total.json

  # Miscellaneous tasks
  documentation-checks:
    docker:
      # Specify the latest version to prevent "cimg/ruby:latest not found: manifest unknown: manifest unknown" error.
      - image: cimg/ruby:3.1
    environment:
      <<: *common_env
    steps:
      - checkout
      - run: bundle install
      - run:
          name: Check documentation syntax
          command: bundle exec rake documentation_syntax_check

workflows:
  version: 2
  build:
    jobs:
      - documentation-checks
      - cc-setup
      - ruby-2.6-spec:
          requires:
            - cc-setup
      - ruby-2.6-ascii_spec
      - ruby-2.6-rubocop
      - ruby-2.7-spec:
          requires:
            - cc-setup
      - ruby-2.7-ascii_spec
      - ruby-2.7-rubocop
      - ruby-3.0-spec:
          requires:
            - cc-setup
      - ruby-3.0-ascii_spec
      - ruby-3.0-rubocop
      - ruby-head-spec:
          requires:
            - cc-setup
      - ruby-3.1-spec:
          requires:
            - cc-setup
      - ruby-3.1-ascii_spec
      - ruby-3.1-rubocop
      - ruby-head-spec:
          requires:
            - cc-setup
      - ruby-head-ascii_spec
      - ruby-head-rubocop

      - cc-upload-coverage:
          requires:
            - ruby-2.6-spec
            - ruby-2.7-spec
            - ruby-3.0-spec
            - ruby-3.1-spec
