name: fast_testing

on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  linux:
    # We want to run on external PRs, but not on our own internal
    # PRs as they'll be run by the push to the branch.
    #
    # The main trick is described here:
    # https://github.com/Dart-Code/Dart-Code/pull/2375
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository

    strategy:
      fail-fast: false
      matrix:
        tarantool:
          - '1.10'
          - '2.4'
          - '2.5'
          - '2.6'
          - '2.7'
          - '2.8'
          - '2.10'
        cartridge-version:
          - ''
          - '2.7.4'
        metrics-version:
          - ''
          - '0.10.0'
          - '0.11.0'
          - '0.13.0'
        coveralls: [false]
        include:
          - tarantool: '2.10'
            cartridge-version: '2.7.4'
            metrics-version: '0.13.0'
            coveralls: true
          - tarantool: 'debug-master'
            cartridge-version: ''
            metrics-version: '0.13.0'
            coveralls: false

    env:
      TNT_DEBUG_PATH: /home/runner/tnt-debug

    runs-on: ubuntu-20.04
    steps:
      - name: Install tarantool ${{ matrix.tarantool }}
        if: startsWith(matrix.tarantool, 'debug') != true
        uses: tarantool/setup-tarantool@v1
        with:
          tarantool-version: ${{ matrix.tarantool }}

      - name: Create variables for Tarantool ${{ matrix.tarantool }}
        if: startsWith(matrix.tarantool, 'debug')
        run: |
          branch=$(echo ${{ matrix.tarantool }} | cut -d- -f2)
          commit_hash=$(git ls-remote https://github.com/tarantool/tarantool.git --branch ${branch} | head -c 8)
          echo "TNT_BRANCH=${branch}" >> $GITHUB_ENV
          echo "VERSION_POSTFIX=-${commit_hash}" >> $GITHUB_ENV
        shell: bash

      - name: Cache tarantool build
        if: startsWith(matrix.tarantool, 'debug')
        id: cache-tnt-debug
        uses: actions/cache@v3
        with:
          path: ${{ env.TNT_DEBUG_PATH }}
          key: cache-tnt-${{ matrix.tarantool }}${{ env.VERSION_POSTFIX }}

      - name: Clone tarantool ${{ matrix.tarantool }}
        if: startsWith(matrix.tarantool, 'debug') && steps.cache-tnt-debug.outputs.cache-hit != 'true'
        uses: actions/checkout@v3
        with:
          repository: tarantool/tarantool
          ref: ${{ env.TNT_BRANCH }}
          path: tarantool
          fetch-depth: 0
          submodules: true

      - name: Build tarantool ${{ matrix.tarantool }}
        if: startsWith(matrix.tarantool, 'debug') && steps.cache-tnt-debug.outputs.cache-hit != 'true'
        run: |
          sudo apt-get -y install git build-essential cmake make zlib1g-dev \
            libreadline-dev libncurses5-dev libssl-dev \
            libunwind-dev libicu-dev python3 python3-yaml \
            python3-six python3-gevent
          cd ${GITHUB_WORKSPACE}/tarantool
          mkdir build && cd build
          cmake .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_DIST=ON
          make
          make DESTDIR=${TNT_DEBUG_PATH} install

      - name: Install tarantool ${{ matrix.tarantool }}
        if: startsWith(matrix.tarantool, 'debug')
        run: |
          sudo cp -rvP ${TNT_DEBUG_PATH}/usr/local/* /usr/local/

      - name: Clone the module
        uses: actions/checkout@v3

      - name: Cache rocks
        uses: actions/cache@v3
        id: cache-rocks
        with:
          path: .rocks/
          key: "cache-rocks-${{ matrix.tarantool }}${{ env.VERSION_POSTFIX }}-\
            ${{ matrix.cartridge-version }}-\
            ${{ matrix.metrics-version }}"

      - name: Setup tt
        run: |
          curl -L https://tarantool.io/release/2/installer.sh | sudo bash
          sudo apt install -y tt
          tt version

      - name: Install requirements
        run: make deps
        if: steps.cache-rocks.outputs.cache-hit != 'true'

      - name: Install metrics
        if: matrix.metrics-version != ''
        run: |
          tt rocks install metrics ${{ matrix.metrics-version }}

      - name: Install cartridge
        if: matrix.cartridge-version != ''
        run: |
          tt rocks install cartridge ${{ matrix.cartridge-version }}

      - run: echo $PWD/.rocks/bin >> $GITHUB_PATH

      - run: make check

      - run: make coverage

      - name: Send code coverage to coveralls.io
        run: make coveralls
        if: ${{ matrix.coveralls }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}