name: pr

on:
  pull_request:
    branches: [ "main" ]

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

jobs:
  changed-files:
    name: changed-files
    runs-on: ubuntu-latest
    outputs:
      src: ${{ steps.changed-files-yaml.outputs.src_any_changed }}
    steps:
      - uses: actions/checkout@v4
      - id: changed-files-yaml
        uses: tj-actions/changed-files@v46
        with:
          files_yaml: |
            src:
              - '**/*.rs'
              - '**/Cargo.toml'

  test:
    needs: changed-files
    if: ${{ needs.changed-files.outputs.src == 'true' }}
    name: test-on-${{ matrix.os }}${{ matrix.features && '-' }}${{ matrix.features }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
          - os: ubuntu-latest
          - os: macos-latest
            features: "gpu"
          - os: ubuntu-latest
            features: "inplace"
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup-build
      - uses: taiki-e/install-action@v2
        with:
          tool: cargo-nextest
      - run: cargo make check --features "${{ matrix.features }}"
        shell: bash

  all-tests-passed:
    if: ${{ always() }}
    name: all-tests-passed
    runs-on: ubuntu-latest
    needs: test
    steps:
      - if: ${{ needs.test.result == 'failure' || needs.test.result == 'cancelled' }}
        run: exit 1

  build-docs:
    needs: changed-files
    if: ${{ needs.changed-files.outputs.src == 'true' }}
    name: build-docs
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup-build
        with:
          toolchain: nightly
      - run: cargo make doc
      - uses: dtolnay/install@cargo-docs-rs
      - run: | 
          cargo +nightly docs-rs

  auto-merge:
    needs:
      - all-tests-passed
      - build-docs
    permissions:
      pull-requests: write
      contents: write
    runs-on: ubuntu-latest
    if: ${{ always() && !cancelled() && !failure() && github.actor == 'dependabot[bot]' }}
    steps:
      - run: gh pr merge --rebase --auto "$PR_URL"
        env:
          PR_URL: ${{github.event.pull_request.html_url}}
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
