name: Test PR

on:
  pull_request:

defaults:
  run:
    shell: 'bash -Eeuo pipefail -x {0}'

env:
  # https://github.com/docker-library/bashbrew/issues/10
  GIT_LFS_SKIP_SMUDGE: 1
  
# cancel existing runs if user makes another push
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions:
  contents: read

jobs:

  naughty:
    name: Naughty
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: ./.github/workflows/.bashbrew
      - name: Check for Common Issues
        run: .github/workflows/naughty.sh

  generate-jobs:
    name: Generate Jobs
    runs-on: ubuntu-latest
    outputs:
      strategy: ${{ steps.generate-jobs.outputs.strategy }}
      length: ${{ steps.generate-jobs.outputs.length }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: ./.github/workflows/.bashbrew
      - id: generate-jobs
        name: Generate Jobs
        run: |
          strategy="$(.github/workflows/generate.sh)"
          echo "strategy=$strategy" >> "$GITHUB_OUTPUT"
          jq . <<<"$strategy" # sanity check / debugging aid
          length="$(jq <<<"$strategy" -r '.matrix.include | length')"
          echo "length=$length" >> "$GITHUB_OUTPUT"

  test:
    needs: generate-jobs
    strategy: ${{ fromJSON(needs.generate-jobs.outputs.strategy) }}
    if: needs.generate-jobs.outputs.length > 0
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.os }}
    steps:
      - name: Prepare Git
        run: |
          # enable symlinks on Windows (https://git-scm.com/docs/git-config#Documentation/git-config.txt-coresymlinks)
          git config --global core.symlinks true
          # also, make sure Windows symlinks are *real* symlinks (https://github.com/git-for-windows/git/pull/156)
          echo 'MSYS=winsymlinks:nativestrict' >> "$GITHUB_ENV"
          # https://github.com/docker-library/bashbrew/blob/a40a54d4d81b9fd2e39b4d7ba3fe203e8b022a67/scripts/github-actions/generate.sh#L146-L149
        if: runner.os == 'Windows'
      - uses: actions/checkout@v6
      - uses: ./.github/workflows/.bashbrew
      - name: Prepare Environment
        run: ${{ matrix.runs.prepare }}
      - name: Pull Dependencies
        run: ${{ matrix.runs.pull }}
      - name: Build ${{ matrix.name }}
        run: ${{ matrix.runs.build }}
      - name: History ${{ matrix.name }}
        run: ${{ matrix.runs.history }}
      - name: Test ${{ matrix.name }}
        run: ${{ matrix.runs.test }}
      - name: '"docker images"'
        run: ${{ matrix.runs.images }}
