name: CI Pull Request

on:
  pull_request:

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

jobs:
  check_wrapper:
    name: Validate Gradle Wrapper
    runs-on: ubuntu-latest

    steps:
      - name: Clone repo
        uses: actions/checkout@v5

      - name: Validate Gradle Wrapper
        uses: gradle/actions/wrapper-validation@v4

  build:
    name: Build pull request
    needs: check_wrapper
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

    steps:
      - name: Checkout pull request
        uses: actions/checkout@v5
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          path: master
          fetch-depth: 0

      - name: Set up JDK
        uses: actions/setup-java@v5
        with:
          java-version: 21
          distribution: 'zulu'

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

      - name: Copy CI gradle.properties
        run: |
          cd master
          mkdir -p ~/.gradle
          cp .github/runner-files/ci-gradle.properties ~/.gradle/gradle.properties

      - name: Build And Run Jar
        working-directory: master
        run: |
          ./gradlew ktlintCheck :server:shadowJar --stacktrace
          gcc -fPIC -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -shared scripts/resources/catch_abort.c -lpthread -o scripts/resources/catch_abort.so
          export LD_PRELOAD="$(pwd)/scripts/resources/catch_abort.so"
          JAR=$(ls ./server/build/*.jar| head -1)
          set +e
          timeout 30s java -DcrashOnFailedMigration=true \
              -Dsuwayomi.tachidesk.config.server.systemTrayEnabled=false \
              -Dsuwayomi.tachidesk.config.server.initialOpenInBrowserEnabled=false \
              -Dsuwayomi.tachidesk.config.server.databaseType=POSTGRESQL \
              -Dsuwayomi.tachidesk.config.server.databaseUrl=postgresql://localhost:5432/postgres \
              -Dsuwayomi.tachidesk.config.server.databaseUsername=postgres \
              -Dsuwayomi.tachidesk.config.server.databasePassword=postgres \
              -jar "$JAR"

          ecode="$?"
          # if exit code is not 124 or 125, then error
          if ! [ "$ecode" -eq 124 -o "$ecode" -eq 125 ]; then 
            printf "exit code '%s' - exiting.\n" "$ecode"
            exit "$ecode"
          fi

          timeout 30s java -DcrashOnFailedMigration=true \
              -Dsuwayomi.tachidesk.config.server.systemTrayEnabled=false \
              -Dsuwayomi.tachidesk.config.server.initialOpenInBrowserEnabled=false \
              -jar "$JAR"

          ecode="$?"
          # if exit code is not 124 or 125, then error
          if ! [ "$ecode" -eq 124 -o "$ecode" -eq 125 ]; then 
            printf "exit code '%s' - exiting.\n" "$ecode"
            exit "$ecode"
          fi
          exit 0
