# Use this workflow to trigger beta releases, both initial beta.1 and subsequent beta.x releases

name: Beta release

run-name: "Beta release (publish: ${{ inputs.publish }}, bump version: ${{ inputs.bump-version }})"

on:
  workflow_dispatch:
    inputs:
      publish:
        required: false
        type: boolean
        default: false
        description: "Publish the beta release"
      bump-version:
        required: false
        type: boolean
        default: false
        description: "Bump the version of the current beta if this is not the initial one"

defaults:
  run:
    shell: bash

permissions:
  contents: write

jobs:
  checks:
    name: Pre-release checks
    runs-on: ubuntu-latest
    outputs:
      branch: ${{ steps.outputs.outputs.branch }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Determine the correct branch
        id: outputs
        run: |
          set -x
          if git branch -r | grep -w 'releases/beta'; then
            echo "branch=releases/beta" >> $GITHUB_OUTPUT
          else
            echo "branch=main" >> $GITHUB_OUTPUT
          fi

  release:
    name: Prepare beta release
    needs: [checks]
    uses: ./.github/workflows/reusable_publish_version.yml
    with:
      environment: beta
      git-ref: ${{ needs.checks.outputs.branch }}
      bump-version: ${{ inputs.bump-version }}
      publish: ${{ inputs.publish }}
      create-release: ${{ inputs.publish }}
    secrets: inherit

  bump-version:
    name: Bump main version
    if: ${{ inputs.publish }}
    needs: [checks, release]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          ref: releases/beta

      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable

      - name: Install a TOML parser
        run: |
          curl -L https://github.com/tamasfe/taplo/releases/download/0.8.1/taplo-full-linux-x86_64.gz | gunzip - > taplo
          chmod +x taplo
          sudo mv taplo /usr/bin/taplo

      - name: Get version info
        id: bump
        run: |
          set -x

          # Retrieve just released version
          betaVersion=$(taplo get -f Cargo.toml "package.version")
          echo "current-version=${betaVersion}" >> $GITHUB_OUTPUT
          major=$(echo $betaVersion | tr "." "\n" | sed -n 1p)
          minor=$(echo $betaVersion | tr "." "\n" | sed -n 2p)
          betaNum=$(echo $betaVersion | tr "." "\n" | sed -n 4p)
          nightlyVersion=${major}.$(($minor + 1)).0
          echo "version=${nightlyVersion}" >> $GITHUB_OUTPUT
          echo "beta-num=${betaNum}"  >> $GITHUB_OUTPUT

      - name: Create version bump branch
        if: ${{ steps.bump.outputs.beta-num == '1' }}
        run: |
          set -x

          # Checkout the main branch
          git fetch origin main
          git checkout main

          # Switch to version bump branch
          git checkout -b version-bump/v${{ steps.bump.outputs.version }}

          # Bump the crate version
          sed -i "s#^version = \".*\"#version = \"${{ steps.bump.outputs.version }}\"#" Cargo.toml
          sed -i "s#^version = \".*\"#version = \"${{ steps.bump.outputs.version }}\"#" lib/Cargo.toml
          sed -i "s#^version = \".*\"#version = \"2.0.0-${{ steps.bump.outputs.version }}\"#" core/Cargo.toml

          # Update dependency versions
          sed -i "s#surrealdb-core2 = { version = \"=2.0.0-${{ steps.bump.outputs.current-version }}\"#surrealdb-core2 = { version = \"=2.0.0-${{ steps.bump.outputs.core-version }}\"#" lib/Cargo.toml

          # Update Cargo.lock without updating dependency versions
          cargo check --no-default-features --features storage-mem

      - name: Push the branch
        if: ${{ steps.bump.outputs.beta-num == '1' }}
        run: |
          # Configure git
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config user.name "github-actions[bot]"
          git config --add --bool push.autoSetupRemote true

          # Commit changes
          git commit -am "Bump version to v${{ steps.bump.outputs.version }}"
          git push

      - name: Create a PR
        if: ${{ steps.bump.outputs.beta-num == '1' }}
        id: pr
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_PLZ_TOKEN }}
        run: |
          set -x
          url=$(gh pr create --base main --title "Bump version to v${{ steps.bump.outputs.version }}" --body "Update main version")
          echo "url=${url}" >> $GITHUB_OUTPUT

      - name: Merge the PR
        if: ${{ steps.bump.outputs.beta-num == '1' }}
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_PLZ_TOKEN }} # Need the custom user token here so we can approve and merge the PR
        run: |
          set -x
          gh pr merge ${{ steps.pr.outputs.url }} --delete-branch --admin --squash