name: Integration Tests Workflow
on:
  push:
    paths-ignore:
      - '.gitignore'
      - 'LICENSE'
      - 'README*'
      - 'docs/**'
      - '.github/workflows/**'
    branches: ['2.6.x']
  pull_request:
    paths-ignore:
      - '.gitignore'
      - 'LICENSE'
      - 'README*'
      - 'docs/**'
    branches: ['2.6.x']

concurrency:
  # Only run once for latest commit per ref and cancel other (previous) runs.
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  prepare-sql-tests:
    name: Prepare for SQL Integration Tests
    runs-on: ubuntu-22.04
    if: github.repository_owner == 'Apicurio' && !contains(github.event.*.labels.*.name, 'DO NOT MERGE')
    steps:
      - name: Show Actor
        run: echo ${{github.actor}}
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'maven'

      - name: Get maven wrapper
        run: mvn -N io.takari:maven:wrapper -Dmaven=3.8.2

      - name: Build Sql Variant
        run: make SKIP_TESTS=true BUILD_FLAGS='-Dmaven.javadoc.skip=true --no-transfer-progress -Dmaven.wagon.httpconnectionManager.maxTotal=30 -Dmaven.wagon.http.retryHandler.count=5' build-sql

      - name: Build and Push Sql image
        env:
          IMAGE_REPO: ttl.sh/${{ github.sha }}
          # maximum allowed
          IMAGE_TAG: 1d
        run: make build-sql-image push-sql-image

  prepare-kafkasql-tests:
    name: Prepare for KafkaSQL Integration Tests
    runs-on: ubuntu-22.04
    if: github.repository_owner == 'Apicurio' && !contains(github.event.*.labels.*.name, 'DO NOT MERGE')
    steps:
      - name: Show Actor
        run: echo ${{github.actor}}
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'maven'

      - name: Get maven wrapper
        run: mvn -N io.takari:maven:wrapper -Dmaven=3.8.2

      - name: Build kafkasql Variant
        run: make SKIP_TESTS=true BUILD_FLAGS='-DskipUiBuild=true -Dmaven.javadoc.skip=true --no-transfer-progress -Dmaven.wagon.httpconnectionManager.maxTotal=30 -Dmaven.wagon.http.retryHandler.count=5' build-kafkasql

      - name: Build and Push kafkasql image
        env:
          IMAGE_REPO: ttl.sh/${{ github.sha }}
          # maximum allowed
          IMAGE_TAG: 1d
        run: make build-kafkasql-image push-kafkasql-image

  prepare-mem-tests:
    name: Prepare for Memory Integration Tests
    runs-on: ubuntu-22.04
    if: github.repository_owner == 'Apicurio' && !contains(github.event.*.labels.*.name, 'DO NOT MERGE')
    steps:
      - name: Show Actor
        run: echo ${{github.actor}}
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'maven'

      - name: Get maven wrapper
        run: mvn -N io.takari:maven:wrapper -Dmaven=3.8.2

      - name: Build kafkasql Variant
        run: make SKIP_TESTS=true BUILD_FLAGS='-DskipUiBuild=true -Dmaven.javadoc.skip=true --no-transfer-progress -Dmaven.wagon.httpconnectionManager.maxTotal=30 -Dmaven.wagon.http.retryHandler.count=5' build-in-memory

      - name: Build and Push mem image
        env:
          IMAGE_REPO: ttl.sh/${{ github.sha }}
          # maximum allowed
          IMAGE_TAG: 1d
        run: make build-mem-image push-mem-image

  integration-tests-sql:
    name: Integration Tests SQL
    runs-on: ubuntu-22.04
    needs: prepare-sql-tests
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'maven'

      - name: Setup Minikube
        uses: manusa/actions-setup-minikube@v2.7.2
        with:
          minikube version: 'v1.31.1'
          kubernetes version: 'v1.26.3'
          github token: ${{ secrets.GITHUB_TOKEN }}
          driver: docker

      - name: Prepare minikube tunnel
        run: minikube tunnel &> /dev/null &

      - name: Run Integration Tests - sql
        run: make REGISTRY_IMAGE='-Dregistry-sql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-sql:1d' run-sql-integration-tests

      - name: Run Integration Tests - sql - auth
        run: make REGISTRY_IMAGE='-Dregistry-sql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-sql:1d' run-sql-auth-tests

      - name: Run Integration Tests - sql - migration
        run: make REGISTRY_IMAGE='-Dregistry-sql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-sql:1d' run-sql-migration-integration-tests

      - name: Run Integration Tests - sql - db upgrade
        run: make REGISTRY_IMAGE='-Dregistry-sql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-sql:1d' run-sql-upgrade-tests

      - name: Run Integration Tests - sql - multitenancy
        run: make REGISTRY_IMAGE='-Dregistry-sql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-sql:1d' run-sql-multitenancy-integration-tests


      - name: Collect logs
        if: failure()
        run: ./.github/scripts/collect_logs.sh

      - name: Upload tests logs artifacts
        if: failure()
        uses: actions/upload-artifact@v4.0.0
        with:
          name: tests-logs
          path: artifacts

  integration-tests-kafkasql:
    name: Integration Tests KafkaSql
    runs-on: ubuntu-22.04
    needs: prepare-kafkasql-tests
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'maven'

      - name: Setup Minikube
        uses: manusa/actions-setup-minikube@v2.7.2
        with:
          minikube version: 'v1.31.1'
          kubernetes version: 'v1.26.3'
          github token: ${{ secrets.GITHUB_TOKEN }}
          driver: docker

      - name: Prepare minikube tunnel
        run: minikube tunnel &> /dev/null &

      - name: Run Integration Tests - KafkaSql
        run: make REGISTRY_IMAGE='-Dregistry-kafkasql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-kafkasql:1d' run-kafkasql-integration-tests

      - name: Run Integration Tests - KafkaSql - Migration
        run: make REGISTRY_IMAGE='-Dregistry-kafkasql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-kafkasql:1d' run-kafkasql-migration-integration-tests

      - name: Run Integration Tests - KafkaSql - DBUpgrade
        run: make REGISTRY_IMAGE='-Dregistry-kafkasql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-kafkasql:1d' run-kafkasql-upgrade-tests

      - name: Run Integration Tests - KafkaSql - Manual
        run: make REGISTRY_IMAGE='-Dregistry-kafkasql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-kafkasql:1d' run-kafkasql-manual-tests

      - name: Run Integration Tests - KafkaSql - Auth
        run: make REGISTRY_IMAGE='-Dregistry-kafkasql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-kafkasql:1d' run-kafkasql-auth-tests

      - name: Collect logs
        if: failure()
        run: ./.github/scripts/collect_logs.sh
      - name: Upload tests logs artifacts
        if: failure()
        uses: actions/upload-artifact@v4.0.0
        with:
          name: tests-logs
          path: artifacts

  integration-tests-ui:
    name: Integration Tests UI
    runs-on: ubuntu-22.04
    needs: prepare-sql-tests
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'maven'

      - name: Setup Minikube
        uses: manusa/actions-setup-minikube@v2.7.2
        with:
          minikube version: 'v1.31.1'
          kubernetes version: 'v1.26.3'
          github token: ${{ secrets.GITHUB_TOKEN }}
          driver: docker

      - name: Prepare minikube tunnel
        run: minikube tunnel &> /dev/null &

      - name: Run UI tests
        run: make REGISTRY_IMAGE='-Dregistry-sql-image=ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-sql:1d' run-ui-tests

      - name: Collect logs
        if: failure()
        run: ./.github/scripts/collect_logs.sh
      - name: Upload tests logs artifacts
        if: failure()
        uses: actions/upload-artifact@v4.0.0
        with:
          name: tests-logs
          path: artifacts

  build-examples:
    name: Build and Run Application examples
    runs-on: ubuntu-22.04
    needs: prepare-mem-tests
    steps:
      - name: Checkout Code with Ref '${{ github.ref }}'
        uses: actions/checkout@v2
        with:
          path: apicurio-registry

      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'maven'

      - name: Run Apicurio Registry application
        run: docker run -d -p 8080:8080 -it ttl.sh/${{ github.sha }}/apicurio/apicurio-registry-mem:1d

      - name: Build Apicurio Registry with Examples
        run: cd apicurio-registry && mvn clean install -DskipTests -Pexamples
