name: ci

concurrency: ci-${{ github.ref }}

on:
  pull_request:
  push:
  schedule:
    - cron:  '0 1 * * *'

env:
  BUNDLE_RETRY: 6
  BUNDLE_JOBS: 4

jobs:
  diffend:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.1
          bundler-cache: true

      - name: Install Diffend plugin
        run: bundle plugin install diffend

      - name: Bundle Secure
        run: bundle secure

  coditsu:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Run Coditsu
        run: \curl -sSL https://api.coditsu.io/run/ci | bash

  specs:
    runs-on: ubuntu-latest
    needs: diffend
    strategy:
      fail-fast: false
      matrix:
        ruby:
          # We run it against the oldest and the newest of a given major to make sure, that there
          # are no syntax-sugars that we would use that were introduced down the road
          - '3.1'
          - '3.1.0'
          - '3.0'
          - '3.0.0'
          - '2.7'
          - '2.7.0'
        include:
          - ruby: '3.1'
            coverage: 'true'
    steps:
      - uses: actions/checkout@v3
      - name: Install package dependencies
        run: "[ -e $APT_DEPS ] || sudo apt-get install -y --no-install-recommends $APT_DEPS"

      - name: Start Kafka with docker-compose
        run: |
          docker-compose up -d

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{matrix.ruby}}
          bundler-cache: true

      - name: Run all specs
        env:
          GITHUB_COVERAGE: ${{matrix.coverage}}
        run: bin/rspecs

  integrations:
    runs-on: ubuntu-latest
    needs: diffend
    strategy:
      fail-fast: false
      matrix:
        ruby:
          - '3.1'
          - '3.0'
          - '2.7'
        include:
          - ruby: '3.1'
            coverage: 'true'
    steps:
      - uses: actions/checkout@v3
      - name: Install package dependencies
        run: "[ -e $APT_DEPS ] || sudo apt-get install -y --no-install-recommends $APT_DEPS"

      - name: Remove libzstd-dev to check no supported compressions
        run: sudo apt-get -y remove libzstd-dev

      - name: Start Kafka with docker-compose
        run: |
          docker-compose up -d

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{matrix.ruby}}

      - name: Install latest Bundler
        run: |
          gem install bundler --no-document
          gem update --system --no-document
          bundle config set without 'tools benchmarks docs'

      - name: Bundle install
        run: |
          bundle config set without development
          bundle install

      - name: Run integration tests
        env:
          KARAFKA_PRO_LICENSE_TOKEN: ${{ secrets.KARAFKA_PRO_LICENSE_TOKEN }}
          KARAFKA_PRO_USERNAME: ${{ secrets.KARAFKA_PRO_USERNAME }}
          KARAFKA_PRO_PASSWORD: ${{ secrets.KARAFKA_PRO_PASSWORD }}
          KARAFKA_PRO_VERSION: ${{ secrets.KARAFKA_PRO_VERSION }}
          GITHUB_COVERAGE: ${{matrix.coverage}}
        run: bin/integrations