name: Release new minor version docs

on:
  push:
    tags:
      # Trigger this only for new minor version tags (e.g. v2.99.0)
      - "v[0-9]+.[0-9]+.0"
      # Exclude 1.x tags
      - "!v1.[0-9]+.[0-9]+"

env:
  PYTHON_VERSION: "3.10"

jobs:
  promote:
    runs-on: ubuntu-slim
    steps:
      - name: Checkout this repo
        uses: actions/checkout@v6
        # use VERSION.txt file from main branch
        with:
          ref: main

      - name: Get version to release
        id: version
        shell: bash
        # We only need `major.minor`. At this point, VERSION.txt contains the next version.
        # For example, if we are releasing 2.20.0, VERSION.txt contains 2.21.0-rc0.
        run: |
          MAJOR=$(cut -d "." -f 1 < VERSION.txt)
          MINOR=$(cut -d "." -f 2 < VERSION.txt)
          MINOR=$((MINOR - 1))
          echo "version=${MAJOR}.${MINOR}" >> "$GITHUB_OUTPUT"

      - uses: actions/setup-python@v6
        with:
          python-version: "${{ env.PYTHON_VERSION }}"

      - name: Promote unstable docs for Docusaurus
        run: |
          python ./.github/utils/promote_unstable_docs_docusaurus.py --version ${{ steps.version.outputs.version }}

      - name: Create Pull Request with Docusaurus docs updates
        uses: peter-evans/create-pull-request@v8
        with:
          token: ${{ secrets.HAYSTACK_BOT_TOKEN }}
          commit-message: "Promote unstable docs for Haystack ${{ steps.version.outputs.version }}"
          branch: promote-unstable-docs-${{ steps.version.outputs.version }}
          base: main
          title: "docs: promote unstable docs for Haystack ${{ steps.version.outputs.version }}"
          add-paths: |
            docs-website
          body: |
            This PR promotes the unstable docs for Haystack ${{ steps.version.outputs.version }} to stable.
            It is expected to run at the time of the release.
            You can inspect the docs preview and merge it. There should now be only one unstable version representing the next (main) branch.
          # This workflow is triggered by a tag pushed by the HaystackBot in release.yml > create-release-tag.
          # GitHub requires reviewers to be different from the PR author, so setting `github.actor`
          # would fail (it would request a review from the HaystackBot itself).
          # So we don't set any reviewers and instead notify the Release Manager
          # (see .github/utils/prepare_release_notification.sh).
