name: Publish Documentation

on:
  push:
    branches:
      - main
      - docs-update
    tags:
      - '**'
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  COLUMNS: 150
  UV_FROZEN: true

permissions:
  contents: read

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
        with:
          python-version: '3.13'
          enable-cache: true # zizmor: ignore[cache-poisoning] -- Job does not produce release artifacts and does not have sensitive permissions

      - name: Install dependencies
        # Installing pip is required for the pre-commit action:
        run: |
          uv sync --all-packages --group linting --all-extras
          uv pip install pip

      - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
        with:
          extra_args: --all-files --verbose
        env:
          SKIP: no-commit-to-branch
          UV_NO_PROGRESS: '1'

  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
        with:
          python-version: '3.13'
          enable-cache: true # zizmor: ignore[cache-poisoning] -- Job does not produce release artifacts and does not have sensitive permissions

      - name: Install dependencies
        run: uv sync --all-packages --group testing-extra --all-extras

      - run: 'uv run python -c "import pydantic.version; print(pydantic.version.version_info())"'

      - run: make test

  publish:
    # Compare with the docs-build job in .github/workflows/ci.yml
    needs: [lint, test]
    runs-on: ubuntu-latest
    timeout-minutes: 30
    permissions:
      contents: write

    steps:
      - name: Checkout docs-site
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: docs-site
          persist-credentials: true

      - name: Checkout current branch
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: true

      - uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
        with:
          python-version: '3.13'
          enable-cache: false

      - run: uv sync --all-packages --group docs --group docs-upload

      - run: uv pip install --default-index https://pydantic:${PPPR_TOKEN}@pppr.pydantic.dev/simple/ mkdocs-material
        env:
          PPPR_TOKEN: ${{ secrets.PPPR_TOKEN }}

      - run: uv run python -c 'import docs.plugins.main'

      # Taken from docs-build.sh
      - name: Prepare shortcuts for extra modules
        run: |
          ln -s pydantic-core/python/pydantic_core pydantic_core
          ln -s .venv/lib/python*/site-packages/pydantic_settings pydantic_settings
          ln -s .venv/lib/python*/site-packages/pydantic_extra_types pydantic_extra_types

      - name: Set git credentials
        run: |
          git config --global user.name "${GITHUB_ACTOR}"
          git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"

      - run: PYTHONPATH="$PWD${PYTHONPATH:+:${PYTHONPATH}}" uv run mike deploy -b docs-site dev --push
        if: github.ref == 'refs/heads/main'

      - if: github.ref == 'refs/heads/docs-update' || startsWith(github.ref, 'refs/tags/')
        id: check-version
        uses: samuelcolvin/check-python-version@ee87cddb8049d2694cc03badc8569765a05cef00 # v5
        with:
          version_file_path: 'pydantic/version.py'
          skip_env_check: true

      - run: PYTHONPATH="$PWD${PYTHONPATH:+:${PYTHONPATH}}" uv run mike deploy -b docs-site ${STEPS_CHECK_VERSION_OUTPUTS_VERSION_MAJOR_MINOR} latest --update-aliases --push
        if: ${{ (github.ref == 'refs/heads/docs-update' || startsWith(github.ref, 'refs/tags/')) && !fromJSON(steps.check-version.outputs.IS_PRERELEASE) }}
        env:
          PYDANTIC_VERSION: v${{ steps.check-version.outputs.VERSION }}
          STEPS_CHECK_VERSION_OUTPUTS_VERSION_MAJOR_MINOR: ${{ steps.check-version.outputs.VERSION_MAJOR_MINOR }}

      - run: uv run python docs/plugins/algolia.py upload
        env:
          ALGOLIA_WRITE_API_KEY: ${{ secrets.ALGOLIA_WRITE_API_KEY }}
