name: Draft release

on:
  workflow_dispatch:
    inputs:
      versionBump:
        description: Version bump
        type: choice
        required: true
        default: patch
        options:
          - patch
          - minor
          - major
          - exact-version

      exactVersion:
        description: 'Exact version: (Only effective selecting "exact-version" as version bump)'
        required: false

description: |
  Run manually to prepare a draft release for the next version of the extension. The workflow will create a draft
  github release where the .vsix can be downloaded and manually tested before publishing. To release the version,
  publish the draft release, which will trigger the publish-release workflow.

permissions:
  contents: write

jobs:
  prepare-release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          # NOTE: this is necessary to get the full history
          # and check if tags are already present
          fetch-depth: 0

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js Environment
        uses: actions/setup-node@v4
        with:
          node-version: 22.21.1
          cache: pnpm

      - name: Install Dependencies
        run: pnpm install --frozen-lockfile

      - name: Determine Next Version
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          set -e

          VERSION_BUMP=${{ github.event.inputs.versionBump }}

          if [[ "$VERSION_BUMP" == "major" || "$VERSION_BUMP" == "minor" || "$VERSION_BUMP" == "patch" ]]; then
            PREV_VERSION_TAG=$(gh api repos/:owner/:repo/releases --jq '. | map(select(.draft == false)) | .[0] | .tag_name')
            PREV_VERSION=$(pnpm exec semver --coerce ${PREV_VERSION_TAG})

            NEXT_VERSION=$(pnpm exec semver -i $VERSION_BUMP $PREV_VERSION)
          else
            NEXT_VERSION=${{ github.event.inputs.exactVersion }}
          fi

          # Remove the 'v' prefix from NEXT_VERSION if it exists
          NEXT_VERSION="${NEXT_VERSION#v}"

          # Validates the version before using it
          pnpm exec semver v"${NEXT_VERSION}"

          pnpm version "${NEXT_VERSION}" --no-git-tag-version
          echo "RELEASE_TAG=v${NEXT_VERSION}" >> "$GITHUB_ENV"

      - name: Validate release tag
        shell: bash
        run: |
          if [ -z "${RELEASE_TAG}" ]; then
            echo "RELEASE_TAG is not set or is empty"
            exit 1
          fi

          if git rev-parse "$RELEASE_TAG" >/dev/null 2>&1; then
            echo "Error: Tag $RELEASE_TAG already existing"
            echo "If you are trying to re-create a draft release with this version, please delete the release and the tag first."
            echo "If this version has already been release consider using a different one."
            exit 1
          fi

      - name: Run tests and build
        uses: ./.github/workflows/actions/test-and-build
        with:
          SEGMENT_KEY: ${{ secrets.SEGMENT_KEY_PROD }}
          ARTIFACTORY_HOST: ${{ secrets.ARTIFACTORY_HOST }}
          ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}
          ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
          GARASIGN_PASSWORD: ${{ secrets.GARASIGN_PASSWORD }}
          GARASIGN_USERNAME: ${{ secrets.GARASIGN_USERNAME }}
          SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
          JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
          MDB_IS_TEST: "true"

      - name: Create Draft Release
        run: |
          set -e
          echo Creating draft release for: "${RELEASE_TAG}"
          ls *.vsix
          ls *.vsix.sig

          gh release create "${RELEASE_TAG}" \
            --title "${RELEASE_TAG}" \
            --notes "Edit the release notes before publishing." \
            --target main \
            --draft \
            *.vsix \
            *.vsix.sig
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
