name: Integration Tests

# This runs for all closed pull requests against main, including those closed without merge.
# Further filtering occurs in 'should-i-run'

on:
  pull_request:
    types: [closed]
    branches: [develop]

# Enrich gradle.properties for CI/CD
env:
  GRADLE_OPTS: -Dorg.gradle.jvmargs="-Xmx3072m -Dfile.encoding=UTF-8 -XX:+HeapDumpOnOutOfMemoryError" -Dkotlin.daemon.jvm.options="-Xmx2560m" -Dkotlin.incremental=false
  CI_GRADLE_ARG_PROPERTIES: --stacktrace -PpreDexEnable=false --max-workers 2 --no-daemon

jobs:

  # More info on should-i-run:
  # If this fails to run (the IF doesn't complete) then the needs will not be satisfied for any of the
  # other jobs below, so none will run.
  # except for the notification job at the bottom which will run all the time, unless should-i-run isn't
  # successful, or all the other jobs have succeeded

  should-i-run:
    name: Check if PR is suitable for analysis
    runs-on: ubuntu-latest
    if: github.event.pull_request.merged # Additionally require PR to have been completely merged.
    steps:
      - run: echo "Run those tests!" # no-op success

  ui-tests:
    name: UI Tests (Synapse)
    needs: should-i-run
    runs-on: buildjet-4vcpu-ubuntu-2204
    timeout-minutes: 90 # We might need to increase it if the time for tests grows
    strategy:
      fail-fast: false
      matrix:
        api-level: [ 28 ]
    # No concurrency required, runs every time on a schedule.
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python 3.8
        uses: actions/setup-python@v4
        with:
          python-version: 3.8
      - name: Use JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin' # See 'Supported distributions' for available options
          java-version: '17'
      - name: Configure gradle
        uses: gradle/actions/setup-gradle@v3
        with:
          cache-read-only: ${{ github.ref != 'refs/heads/develop' }}
      - name: Start synapse server
        uses: michaelkaye/setup-matrix-synapse@v1.0.4
        with:
          uploadLogs: true
          httpPort: 8080
          disableRateLimiting: true
          public_baseurl: "http://10.0.2.2:8080/"
      - name: Run sanity tests on API ${{ matrix.api-level }}
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: ${{ matrix.api-level }}
          arch: x86
          profile: Nexus 5X
          force-avd-creation: false
          emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
          emulator-build: 7425822  # workaround to emulator bug: https://github.com/ReactiveCircus/android-emulator-runner/issues/160
          script: |
            adb root
            adb logcat -c
            touch emulator.log
            chmod 777 emulator.log
            adb logcat >> emulator.log &
            ./gradlew $CI_GRADLE_ARG_PROPERTIES connectedGplayDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=im.vector.app.ui.UiAllScreensSanityTest || (adb pull storage/emulated/0/Pictures/failure_screenshots && exit 1 )
      - name: Upload Test Report Log
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: uitest-error-results
          path: |
            emulator.log
            failure_screenshots/

# Notify the channel about delayed failures
  notify:
    name: Notify matrix
    runs-on: ubuntu-latest
    needs:
      - should-i-run
      - ui-tests
    if: always() && (needs.should-i-run.result == 'success' ) && (needs.ui-tests.result != 'success')
    # No concurrency required, runs every time on a schedule.
    steps:
      - uses: michaelkaye/matrix-hookshot-action@v1.0.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          hookshot_url: ${{ secrets.ELEMENT_ANDROID_HOOKSHOT_URL }}
          text_template: "Post-merge validation of ${{ github.head_ref }} into ${{ github.base_ref }} by ${{ github.event.pull_request.merged_by.login }} failed: {{#each job_statuses }}{{#with this }}{{#if completed }}  {{name}} {{conclusion}} at {{completed_at}}, {{/if}}{{/with}}{{/each}}"
          html_template: "Post-merge validation of ${{ github.head_ref }} into ${{ github.base_ref }} by ${{ github.event.pull_request.merged_by.login }} failed: {{#each job_statuses }}{{#with this }}{{#if completed }}<br />{{icon conclusion}} {{name}} <font color='{{color conclusion}}'>{{conclusion}} at {{completed_at}} <a href=\"{{html_url}}\">[details]</a></font>{{/if}}{{/with}}{{/each}}"