name: E2E Tests for PR

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]
    paths-ignore:
      - "docs/**"
      - "**.md"
      - ".circleci/**"
      - "**.unit.spec.*"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:

  build:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-20.04
    timeout-minutes: 25
    strategy:
      matrix:
        edition: [oss, ee]
    env:
      MB_EDITION: ${{ matrix.edition }}
      INTERACTIVE: false
    steps:
    - uses: actions/checkout@v3
    - name: Prepare front-end environment
      uses: ./.github/actions/prepare-frontend
    - name: Prepare back-end environment
      uses: ./.github/actions/prepare-backend

    - run: ./bin/build version
    - run: ./bin/build translations
    - run: ./bin/build frontend
    - run: ./bin/build licenses
    - run: ./bin/build drivers
    - run: ./bin/build uberjar

    - name: Prepare uberjar artifact
      uses: ./.github/actions/prepare-uberjar-artifact

  e2e-tests:
    runs-on: ubuntu-20.04
    timeout-minutes: 45
    needs: build
    name: e2e-tests-${{ matrix.folder }}${{ matrix.context }}-${{ matrix.edition }}
    env:
      MB_EDITION: ${{ matrix.edition }}
      DISPLAY: ""
      QA_DB_ENABLED: true
      ENTERPRISE_TOKEN: ${{ secrets.ENTERPRISE_TOKEN }}
      MB_SNOWPLOW_AVAILABLE: true
      MB_SNOWPLOW_URL: "http://localhost:9090"  # Snowplow micro
    strategy:
      fail-fast: false
      matrix:
        java-version: [11]
        edition: [ee]
        folder:
          - "admin"
          - "binning"
          - "collections"
          - "custom-column"
          - "dashboard"
          - "dashboard-filters"
          - "downloads"
          - "embedding"
          - "filters"
          - "joins"
          - "models"
          - "native"
          - "native-filters"
          - "onboarding"
          - "organization"
          - "permissions"
          - "question"
          - "sharing"
          - "visualizations"
        include:
          - edition: oss
            context: grep
            java-version: 11
    services:
      maildev:
        image: maildev/maildev:1.1.0
        ports:
          - "80:80"
          - "25:25"
        credentials:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      postgres-sample:
        image: metabase/qa-databases:postgres-sample-12
        ports:
          - "5432:5432"
        credentials:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      mongo-sample:
        image: metabase/qa-databases:mongo-sample-4.0
        ports:
          - 27017:27017
        credentials:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      mysql-sample:
        image: metabase/qa-databases:mysql-sample-8
        ports:
          - 3306:3306
        credentials:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
    steps:
    - uses: actions/checkout@v3
    - name: Prepare front-end environment
      uses: ./.github/actions/prepare-frontend
    - name: Prepare JDK ${{ matrix.java-version }}
      uses: actions/setup-java@v3
      with:
        java-version: ${{ matrix.java-version }}
        distribution: 'temurin'
    - name: Prepare Cypress environment
      uses: ./.github/actions/prepare-cypress
    - name: Run Snowplow micro
      uses: ./.github/actions/run-snowplow-micro

    - uses: actions/download-artifact@v2
      name: Retrieve uberjar artifact for ${{ matrix.edition }}
      with:
        name: metabase-${{ matrix.edition }}-uberjar
    - name: Get the version info
      run: |
        jar xf target/uberjar/metabase.jar version.properties
        mv version.properties resources/

    - name: Run OSS-specific Cypress tests
      if: matrix.edition == 'oss'
      run: |
        yarn run test-cypress-run \
        --env grepTags=@OSS,grepFilterSpecs=true \
        --spec './frontend/test/metabase/scenarios/**/*.cy.spec.js'
      env:
        TERM: xterm

    - name: Run EE Cypress tests on ${{ matrix.folder }}
      if: matrix.edition == 'ee'
      run: |
        yarn run test-cypress-run \
        --env grepTags="-@quarantine" \
        --folder ${{ matrix.folder }}
      env:
        TERM: xterm

    - name: Upload Cypress Artifacts upon failure
      uses: actions/upload-artifact@v2
      if: failure()
      with:
        name: cypress-recording-${{ matrix.folder }}${{ matrix.context }}-${{ matrix.edition }}
        path: |
          ./cypress
          ./logs/test.log
        if-no-files-found: ignore

  visual-regression-tests:
    runs-on: ubuntu-20.04
    timeout-minutes: 30
    needs: build
    name: percy-tests-sanity-check-${{ matrix.edition }}
    env:
      MB_EDITION: ${{ matrix.edition }}
      ENTERPRISE_TOKEN: ${{ secrets.ENTERPRISE_TOKEN }}
    strategy:
      matrix:
        java-version: [11]
        edition: [ee]
    services:
      maildev:
        image: maildev/maildev:1.1.0
        ports:
          - "80:80"
          - "25:25"
        credentials:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
    steps:
    - uses: actions/checkout@v3
    - name: Prepare front-end environment
      uses: ./.github/actions/prepare-frontend
    - name: Prepare JDK ${{ matrix.java-version }}
      uses: actions/setup-java@v3
      with:
        java-version: ${{ matrix.java-version }}
        distribution: 'temurin'
    - name: Prepare Cypress environment
      uses: ./.github/actions/prepare-cypress

    - uses: actions/download-artifact@v2
      name: Retrieve uberjar artifact for ${{ matrix.edition }}
      with:
        name: metabase-${{ matrix.edition }}-uberjar
    - name: Get the version info
      run: |
        jar xf target/uberjar/metabase.jar version.properties
        mv version.properties resources/
    - name: Percy Test
      run: yarn run test-visual-run
