name: Record screenshots

on:
  workflow_dispatch:
  pull_request:
    types: [ labeled ]

# Enrich gradle.properties for CI/CD
env:
  GRADLE_OPTS: -Dorg.gradle.jvmargs=-Xmx9g -XX:MaxMetaspaceSize=512m -Dfile.encoding=UTF-8 -XX:+HeapDumpOnOutOfMemoryError -XX:+UseG1GC -Dkotlin.daemon.jvm.options=-Xmx4g -Dsonar.gradle.skipCompile=true
  CI_GRADLE_ARG_PROPERTIES: --no-configuration-cache

jobs:
  record:
    name: Record screenshots on branch ${{ github.event.pull_request.head.ref || github.ref_name }}
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch' || github.event.label.name == 'Record-Screenshots'

    steps:
      - name: Remove Record-Screenshots label
        if: github.event.label.name == 'Record-Screenshots'
        uses: actions-ecosystem/action-remove-labels@v1
        with:
          labels: Record-Screenshots
      - name: ⏬ Checkout with LFS (PR)
        if: github.event.label.name == 'Record-Screenshots'
        uses: nschloe/action-cached-lfs-checkout@v1.2.2
        with:
          persist-credentials: false
          ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref }}
      - name: ⏬ Checkout with LFS (Branch)
        if: github.event_name == 'workflow_dispatch'
        uses: nschloe/action-cached-lfs-checkout@v1.2.2
        with:
          persist-credentials: false
      - name: ☕️ Use JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin' # See 'Supported distributions' for available options
          java-version: '21'
      # Add gradle cache, this should speed up the process
      - name: Configure gradle
        uses: gradle/actions/setup-gradle@v4
        with:
          cache-read-only: ${{ github.ref != 'refs/heads/develop' }}
      - name: Record screenshots
        id: record
        run: ./.github/workflows/scripts/recordScreenshots.sh
        env:
          GITHUB_TOKEN: ${{ secrets.DANGER_GITHUB_API_TOKEN || secrets.GITHUB_TOKEN }}
          GITHUB_REPOSITORY: ${{ secrets.GITHUB_REPOSITORY }}
          GRADLE_ARGS: ${{ env.CI_GRADLE_ARG_PROPERTIES }}
