name: master
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  changed-files:
    runs-on: ubuntu-latest
    outputs:
      any_rs_changed: ${{ steps.changed-files.outputs.any_changed }}
    steps:
      - uses: actions/checkout@v4
      - uses: tj-actions/changed-files@v47
        id: changed-files
        with:
          files: |
            .github/workflows/master.yml
            src/**/*.rs
            Cargo.toml
            Cargo.lock
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: rm -fr .mise/config.toml
      - uses: jdx/mise-action@v2
        with:
          mise_toml: |
            [tasks]
            "check-format" = { run = "dprint check --excludes mise.toml" }
            "check-lint" = { run = "ansible-lint", dir = "ansible" }
            "check" = { depends = "check-*" }
            [tools]
            dprint = "latest"
            pipx = "latest"
            "pipx:ansible-core" = "latest"
            "pipx:ansible-lint" = "latest"
      - run: mise settings experimental=true

      - name: Install Ansible collections with retry
        working-directory: ansible
        shell: bash
        run: |
          max_attempts=3
          attempt=1
          until ansible-galaxy collection install -r requirements.yml || [ $attempt -eq $max_attempts ]; do
            echo "Attempt $attempt failed. Retrying in 10 seconds..."
            sleep 10
            attempt=$((attempt + 1))
          done
          if [ $attempt -eq $max_attempts ]; then
            echo "Failed after $max_attempts attempts"
            exit 1
          fi

      - run: mise r check

  _test:
    needs: changed-files
    if: needs.changed-files.outputs.any_rs_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview
      - uses: taiki-e/install-action@nextest
      - run: cargo nextest run

  test:
    needs: _test
    # workaround for https://github.com/orgs/community/discussions/13690
    # https://stackoverflow.com/a/77066140/9771158
    if: ${{ !(failure() || cancelled()) }}
    runs-on: ubuntu-latest
    steps:
      - name: Tests OK (passed or skipped)
        run: exit 0
