name: Checks

on:
  pull_request:
    types: [ opened, synchronize, reopened ]
  push:
    branches: [ master, develop ]

jobs:
  wrapper-validation:
    name: "Gradle Wrapper Validation"
    runs-on: ubuntu-latest
    timeout-minutes: 45
    steps:
      - uses: actions/checkout@v4
      - uses: gradle/wrapper-validation-action@v2

  static-analysis:
    name: "Static Code Analysis"
    runs-on: ubuntu-latest
    timeout-minutes: 45
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
      - name: Cache Gradle
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: gradle-${{ hashFiles('**/*.gradle*') }}-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
      - name: Configure parallel build
        run: echo "org.gradle.parallel=true" >> local.properties
      - name: XML code style
        run: |
          curl -s -L https://github.com/ByteHamster/android-xml-formatter/releases/download/1.1.0/android-xml-formatter.jar > android-xml-formatter.jar
          find . -wholename "*/res/layout/*.xml" | xargs java -jar android-xml-formatter.jar
          test $(git diff | wc -l) -eq 0 || (echo -e "\n\n===== Found XML code style violations! See output below how to fix them. =====\n\n" && git --no-pager diff --color=always && false)
      - name: Checkstyle, Lint, SpotBugs
        run: ./gradlew checkstyle :app:lintPlayDebug spotbugsPlayDebug spotbugsDebug
      - name: Generate readable error messages for GitHub
        if: failure()
        run: |
          git diff --name-only | xargs -I '{}' echo "::error file={},line=1,endLine=1,title=XML Format::Run android-xml-formatter.jar on this file or view CI output to see how it should be formatted."
          python .github/workflows/errorPrinter.py
          false

  unit-test:
    name: "Unit Test: ${{ matrix.variant }}"
    needs: static-analysis
    runs-on: ubuntu-latest
    timeout-minutes: 45
    strategy:
      matrix:
        include:
          - variant: "PlayDebug"
            base-variant: "Debug"
            execute-tests: true
            upload-artifact: true
          - variant: "PlayRelease"
            base-variant: "Release"
            execute-tests: true
            upload-artifact: false
          - variant: "FreeRelease"
            base-variant: "Release"
            execute-tests: false
            upload-artifact: false
    steps:
      - uses: actions/checkout@v4
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
      - name: Cache Gradle
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: gradle-${{ hashFiles('**/*.gradle*') }}-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
      - name: Configure parallel build
        run: echo "org.gradle.parallel=true" >> local.properties
      - name: Create temporary release keystore
        run: keytool -noprompt -genkey -v -keystore "app/keystore" -alias alias -storepass password -keypass password -keyalg RSA -validity 10 -dname "CN=antennapod.org, OU=dummy, O=dummy, L=dummy, S=dummy, C=US"
      - name: Build
        run: ./gradlew assemble${{ matrix.variant }}
      - name: Test
        if: matrix.execute-tests == true
        run: ./gradlew test${{ matrix.variant }}UnitTest test${{ matrix.base-variant }}UnitTest
      - uses: actions/upload-artifact@v4
        if: matrix.upload-artifact == true
        with:
          name: app-play-debug.apk
          path: app/build/outputs/apk/play/debug/app-play-debug.apk

  emulator-test:
    name: "Emulator Test"
    needs: static-analysis
    runs-on: ubuntu-latest
    timeout-minutes: 45
    env:
      api-level: 30
    steps:
      - uses: actions/checkout@v4
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
      - name: Cache Gradle
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: gradle-${{ hashFiles('**/*.gradle*') }}-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
      - name: Configure parallel build
        run: echo "org.gradle.parallel=true" >> local.properties
      - name: Build with Gradle
        run: ./gradlew assemblePlayDebugAndroidTest
      - 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
      - name: Android Emulator test
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: ${{ env.api-level }}
          target: aosp_atd
          channel: canary
          force-avd-creation: false
          emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
          disable-animations: true
          script: bash .github/workflows/runEmulatorTests.sh
      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: test-report
          path: app/build/reports/androidTests/connected/flavors/PLAY/
