name: Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to release (e.g., v2.99.0-rc1 or v2.99.0)'
        required: true
        type: string

# Only one release workflow runs at a time; additional runs are queued.
concurrency:
  group: release
  cancel-in-progress: false

jobs:
  parse-validate-version:
    runs-on: ubuntu-slim
    outputs:
      version: ${{ steps.parse-validate.outputs.version }}
      major_minor: ${{ steps.parse-validate.outputs.major_minor }}
      release_branch: ${{ steps.parse-validate.outputs.release_branch }}
      is_first_rc: ${{ steps.parse-validate.outputs.is_first_rc }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0  # needed to fetch tags and branches

      - name: Parse and validate version
        id: parse-validate
        env:
          GH_TOKEN: ${{ github.token }}
        run: .github/utils/parse_validate_version.sh "${{ inputs.version }}"

  branch-off:
    needs: ["parse-validate-version"]
    if: needs.parse-validate-version.outputs.is_first_rc == 'true'
    uses: ./.github/workflows/branch_off.yml
    # https://docs.github.com/en/actions/how-tos/reuse-automations/reuse-workflows#passing-secrets-to-nested-workflows
    secrets: inherit

  create-release-tag:
    needs: ["parse-validate-version", "branch-off"]
    if: always() && needs.parse-validate-version.result == 'success' && needs.branch-off.result != 'failure'
    runs-on: ubuntu-slim
    permissions:
      contents: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          # needed to fetch tags and branches
          fetch-depth: 0
          # use this token so the created tag triggers workflows (does not happen with the default github.token)
          token: ${{ secrets.HAYSTACK_BOT_TOKEN }}

      - name: Update VERSION.txt and create tag
        env:
          GITHUB_TOKEN: ${{ secrets.HAYSTACK_BOT_TOKEN }}
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"

          git checkout ${{ needs.parse-validate-version.outputs.release_branch }}
          git pull origin ${{ needs.parse-validate-version.outputs.release_branch }}

          echo "${{ needs.parse-validate-version.outputs.version }}" > VERSION.txt
          git add VERSION.txt
          git commit -m "bump version to ${{ needs.parse-validate-version.outputs.version }}"
          git push origin ${{ needs.parse-validate-version.outputs.release_branch }}

          TAG="v${{ needs.parse-validate-version.outputs.version }}"
          git tag -m "$TAG" "$TAG"
          git push origin "$TAG"

  check-artifacts:
    needs: ["parse-validate-version", "create-release-tag"]
    if: always() && needs.parse-validate-version.result == 'success' && needs.create-release-tag.result == 'success'
    runs-on: ubuntu-latest
    outputs:
      github_url: ${{ steps.set-outputs.outputs.github_url }}
      pypi_url: ${{ steps.set-outputs.outputs.pypi_url }}
      docker_url: ${{ steps.set-outputs.outputs.docker_url }}
    env:
      GH_TOKEN: ${{ github.token }}
      VERSION: ${{ needs.parse-validate-version.outputs.version }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0  # needed to fetch tags

      - name: Wait for release workflows
        run: |
          .github/utils/wait_for_workflows.sh "v${{ env.VERSION }}" \
            "Project release on PyPi" \
            "Project release on Github" \
            "Docker image release"

      - name: Check artifacts
        run: |
          check() {
            for _ in {1..5}; do curl -sf "$2" > /dev/null && echo "✅ $1" && return 0; sleep 30; done
            echo "❌ $1 not found" && return 1
          }
          check "GitHub Release" "https://api.github.com/repos/${{ github.repository }}/releases/tags/v${{ env.VERSION }}"
          check "PyPI package" "https://pypi.org/pypi/haystack-ai/${{ env.VERSION }}/json"
          check "Docker image" "https://hub.docker.com/v2/repositories/deepset/haystack/tags/base-v${{ env.VERSION }}"

      - name: Set artifact URLs
        id: set-outputs
        run: |
          {
            echo "github_url=https://github.com/${{ github.repository }}/releases/tag/v${{ env.VERSION }}"
            echo "pypi_url=https://pypi.org/project/haystack-ai/${{ env.VERSION }}/"
            echo "docker_url=https://hub.docker.com/r/deepset/haystack/tags?name=base-v${{ env.VERSION }}"
          } >> "$GITHUB_OUTPUT"

  notify:
    needs: ["parse-validate-version", "branch-off", "create-release-tag", "check-artifacts"]
    if: always()
    runs-on: ubuntu-slim
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Prepare release notification
        id: prepare-notification
        env:
          VERSION: ${{ inputs.version }}
          GH_TOKEN: ${{ github.token }}
          RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
          HAS_FAILURE: ${{ contains(needs.*.result, 'failure') }}
          IS_FIRST_RC: ${{ needs.parse-validate-version.outputs.is_first_rc }}
          MAJOR_MINOR: ${{ needs.parse-validate-version.outputs.major_minor }}
          GITHUB_URL: ${{ needs.check-artifacts.outputs.github_url }}
          PYPI_URL: ${{ needs.check-artifacts.outputs.pypi_url }}
          DOCKER_URL: ${{ needs.check-artifacts.outputs.docker_url }}
          BUMP_VERSION_PR_URL: ${{ needs.branch-off.outputs.bump_version_pr_url }}
        run: .github/utils/prepare_release_notification.sh

      - name: Send event to Datadog
        uses: masci/datadog@v1
        with:
          api-key: ${{ secrets.CORE_DATADOG_API_KEY }}
          api-url: https://api.datadoghq.eu
          events: |
            - title: "${{ steps.prepare-notification.outputs.title }}"
              text: "${{ steps.prepare-notification.outputs.event_text }}"
              alert_type: "${{ steps.prepare-notification.outputs.alert_type }}"
              source_type_name: "Github"
              host: ${{ github.repository_owner }}
              tags:
                - "project:${{ github.repository }}"
                - "job:${{ github.job }}"
                - "run_id:${{ github.run_id }}"
                - "workflow:${{ github.workflow }}"
                - "version:${{ inputs.version }}"
                - "released_rc:${{ steps.prepare-notification.outputs.released_rc }}"
