---
name: CI

on: [push, pull_request]

jobs:
  test:
    name: Go ${{ matrix.go-version }} with Kafka ${{ matrix.kafka-version }}
    runs-on: ubuntu-latest
    # 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.
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
    strategy:
      fail-fast: false
      matrix:
        go-version: [1.16.x, 1.17.x]
        kafka-version: [2.8.1, 3.0.0, 3.1.0]
        platform: [ubuntu-latest]

    env:
      DEBUG: true
      GOFLAGS: -trimpath
      KAFKA_VERSION: ${{ matrix.kafka-version }}

    steps:
    - uses: actions/checkout@v2

    - name: Setup Go
      uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go-version }}

    - name: Get Go environment
      id: go-env
      run: |
        echo "::set-output name=cache::$(go env GOCACHE)"
        echo "::set-output name=modcache::$(go env GOMODCACHE)"

    - name: Set up cache
      uses: actions/cache@v2
      with:
        path: |
          ${{ steps.go-env.outputs.cache }}
          ${{ steps.go-env.outputs.modcache }}
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-

    - name: Lint
      uses: golangci/golangci-lint-action@v2
      if: ${{ matrix.go-version == '1.17.x' }}
      env:
        GOFLAGS: -tags=functional
      with:
        version: v1.44.0

    - name: Test (Unit)
      run: make test
      if: ${{ matrix.go-version != '1.17.x' }}

    - name: Test (Functional)
      run: make test_functional
      if: ${{ matrix.go-version == '1.17.x' }}
