name: pull-request-merge-check.yml
on:
  pull_request:
    branches:
      - "develop"
    types:
      - opened
      - reopened
      - synchronize
      - ready_for_review

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions:
  contents: read
  checks: write
  id-token: write
  pull-requests: write

jobs:
  change:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-latest
    outputs:
      hideout: ${{ steps.filter.outputs.hideout }}
      owl: ${{ steps.filter.outputs.owl }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.PAT }}

      - name: Check Changes
        uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            hideout:
              - 'hideout/**'
              - 'libs.versions.toml'
            owl:
              - 'owl/**'
              - 'libs.versions.toml'

  hideout-setup:
    needs:
      - change
    if: github.event.pull_request.draft == false && needs.change.outputs.hideout == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.PAT }}

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

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

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v4
        with:
          cache-read-only: false
          gradle-home-cache-cleanup: true
      - name: Build
        run: ./gradlew :hideout:test

      - name: JUnit Test Report
        if: ${{ always() }}
        uses: mikepenz/action-junit-report@v5
        with:
          report_paths: '**/TEST-*.xml'
          check_name: 'hideout-core JUnit Test Report'

  owl-setup:
    needs:
      - change
    if: github.event.pull_request.draft == false && needs.change.outputs.owl == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.PAT }}

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

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

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v4
        with:
          cache-read-only: false
          gradle-home-cache-cleanup: true

      - name: Build
        run: ./gradlew :owl:test

      - name: JUnit Test Report
        if: ${{ always() }}
        uses: mikepenz/action-junit-report@v5
        with:
          report_paths: '**/TEST-*.xml'
          check_name: 'owl JUnit Test Report'


  lint:
    if: always() && (needs.change.outputs.hideout == 'true' || needs.change.outputs.owl == 'true')
    needs:
      - change
      - hideout-setup
      - owl-setup
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}
          token: ${{ secrets.PAT }}

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

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v4
        with:
          cache-read-only: false
          gradle-home-cache-cleanup: true

      - name: Lint
        run: ./gradlew :hideout:detekt :owl:detektMain

      - name: Auto Commit
        if: ${{ always() }}
        uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "style: fix lint (CI)"