name: CI

on:
  workflow_dispatch:

  push:
    branches:
      - master
      - beta
      - alpha
      - '*.x'

  pull_request:

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

env:
  FORCE_COLOR: 3

jobs:
  pre-commit:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: 3.x

      - uses: pre-commit/action@v3.0.1
        with:
          extra_args: --hook-stage manual --all-files

      - name: Run PyLint
        run: |
          echo "::add-matcher::$GITHUB_WORKSPACE/.github/matchers/pylint.json"
          pipx run nox -s pylint
[%- if repo_name == 'ss-pybind11' %]


  consistency:
    if: {{ '${{ !cancelled() && ! failure() }}' }}
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: 3.x

      - name: Install copier
        run: pipx install copier==9.6.0

      - name: Verify template
        run: pipx run nox -s template

      - name: Test for consistency
        run: |
          echo "List inconsistent files:"
          git status --porcelain
          git diff --exit-code || ( \
            echo "# :warning: Inconsistent files found" >> $GITHUB_STEP_SUMMARY && \
            echo "The following files are inconsistent with the template:" >> $GITHUB_STEP_SUMMARY && \
            git status --porcelain | while read file; do echo "- $file"; done >> $GITHUB_STEP_SUMMARY && \
            echo "" >> $GITHUB_STEP_SUMMARY && \
            echo "Please run 'copier copy -r HEAD -f . .' to fix them." >>$GITHUB_STEP_SUMMARY && \
            exit 1 \
          )
[%- endif %]


  wheels:
    needs: [pre-commit
[%- if repo_name == 'ss-pybind11' -%]
    , consistency
[%- endif -%]
    ]
    uses: ./.github/workflows/reuseable_cibuildwheel.yml
    secrets: inherit
    with:
      auto: true
      extra: false
[%- if use_codecov == true %]


  codecov:
    runs-on: ubuntu-24.04
    needs: [pre-commit]

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/vcpkg
            ~/.cache/pip
            ~/.cache/vcpkg
          key: codecov-{{ '${{ hashFiles(\'vcpkg.json\') }}' }}
          restore-keys: codecov-{{ '${{ hashFiles(\'vcpkg.json\') }}' }}

      - uses: actions/setup-python@v5
        with:
          python-version: 3.x

      - name: Install package
        run: python -m pip install .[test] -v

      - name: Test package
        run: >-
          python -m pytest -ra --cov --cov-report=xml --cov-report=term
          --durations=20

      - name: Upload coverage report
        uses: codecov/codecov-action@v5
        with:
          token: {{ '${{ secrets.CODECOV_TOKEN }}' }}
[%- endif %]


  check-docs:
    runs-on: ubuntu-24.04
    needs: [pre-commit
[%- if repo_name == 'ss-pybind11' -%]
    , consistency
[%- endif -%]
    ]

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/vcpkg
            ~/.cache/pip
            ~/.cache/vcpkg
          key: docs-{{ '${{ hashFiles(\'vcpkg.json\') }}' }}
          restore-keys: docs-{{ '${{ hashFiles(\'vcpkg.json\') }}' }}

      - uses: actions/setup-python@v5
        with:
          python-version: 3.x

      - name: Checks the docs with warnings as errors
        run: pipx run nox -s docs -- --check
[%- if docs_type == 'sphinx' %]

      - name: Checks the docs for broken links
        run: pipx run nox -s docs -- --linkcheck
[%- endif %]
[%- if repo_name == 'ss-pybind11'%]


  check-mkdocs-docs:
    runs-on: ubuntu-24.04
    needs: [pre-commit, consistency]

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: 3.x

      - name: Install copier
        run: pipx install copier==9.6.0

      - name: Apply mkdocs
        run: pipx run nox -s template -- -d docs_type=mkdocs

      - name: Checks the docs with warnings as errors
        run: pipx run nox -s docs -- --check
[%- endif %]


  pass:
    if: always()
    needs:
      - wheels
[%- if use_codecov == true %]
      - codecov
[%- endif %]
      - check-docs
[%- if repo_name == 'ss-pybind11' %]
      - check-mkdocs-docs
[%- endif %]
    runs-on: ubuntu-24.04
    timeout-minutes: 2
    permissions:
      pull-requests: write

    steps:
      - name: Decide whether the needed jobs succeeded or failed
        uses: re-actors/alls-green@release/v1
        with:
          jobs: {{ '${{ toJSON(needs) }}' }}

      - name: Approve pr if all jobs succeeded
        if: contains(github.event.pull_request.labels.*.name, 'auto-approval') && contains(github.actor, '[bot]')
        uses: hmarr/auto-approve-action@v4
