name: Check

on: [ pull_request ]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: ashutoshgngwr/validate-fastlane-supply-metadata@v2
        with:
          fastlaneDir: ./fastlane/metadata/android
          usePlayStoreLocales: true

      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v3

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3

      - name: Checks
        run: ./gradlew check :app:koverXmlReportRelease :app:koverVerifyRelease

      - name: SonarCloud
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: ./gradlew sonar

      - name: Publish test reports
        uses: mikepenz/action-junit-report@v4
        if: always() # always run even if the previous step fails
        with:
          report_paths: './**/build/test-results/**/TEST-*.xml'

  uiTests:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        api-level: [ 30 ]
    steps:
      - name: Free Disk Space (Ubuntu)
        uses: jlumbroso/free-disk-space@v1.3.1
        with:
          android: false
          large-packages: true
          tool-cache: true
          dotnet: true
          haskell: true
          docker-images: true
          swap-storage: true

      - name: Enable KVM group perms
        run: |
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm

      - uses: actions/checkout@v4
        name: Checkout the code
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v4
        name: Setup JDK 17
        with:
          distribution: 'zulu'
          java-version: 17

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v3
        with:
          validate-wrappers: true
          gradle-home-cache-cleanup: true

      - name: Run tests
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: ${{ matrix.api-level }}
          arch: x86_64
          disable-animations: true
          disk-size: 6000M
          heap-size: 600M
          script: ./gradlew :app:connectedCheck
