name: Release Dolt

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'SemVer format release tag, i.e. 0.24.5'
        required: true

jobs:
  format-version:
    runs-on: ubuntu-22.04
    outputs:
      version: ${{ steps.format_version.outputs.version }}
    steps:
      - name: Format Input
        id: format_version
        run: |
          version="${{ github.event.inputs.version }}"
          if [[ $version == v* ]];
          then
            version="${version:1}"
          fi
          echo "version=$version" >> $GITHUB_OUTPUT

  create-release:
    needs: format-version
    name: Create release
    runs-on: ubuntu-22.04
    outputs:
      release_id: ${{ steps.create_release.outputs.id }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Go 1.x
        uses: actions/setup-go@v3
        with:
          go-version: ^1.21
      - name: Update dolt version command
        run: sed -i -e 's/	Version = ".*"/	Version = "'"$NEW_VERSION"'"/' "$FILE"
        env:
          FILE: ${{ format('{0}/go/cmd/dolt/dolt.go', github.workspace) }}
          NEW_VERSION: ${{ needs.format-version.outputs.version }}
      - name: Set minver TBD to version
        run: sed -i -e 's/minver:"TBD"/minver:"'"$NEW_VERSION"'"/' "$FILE"
        env:
          FILE: ${{ format('{0}/go/cmd/dolt/commands/sqlserver/yaml_config.go', github.workspace) }}
          NEW_VERSION: ${{ needs.format-version.outputs.version }}
      - name: update minver_validation.txt
        working-directory: ./go
        run: go run -mod=readonly ./utils/genminver_validation/ $FILE
        env:
          FILE: ${{ format('{0}/go/cmd/dolt/commands/sqlserver/testdata/minver_validation.txt', github.workspace) }}
      - uses: EndBug/add-and-commit@v9.1.1
        with:
          message: ${{ format('[ga-bump-release] Update Dolt version to {0} and release v{0}', needs.format-version.outputs.version) }}
          add: ${{ format('["{0}/go/cmd/dolt/dolt.go", "{0}/go/cmd/dolt/commands/sqlserver/yaml_config.go", "{0}/go/cmd/dolt/commands/sqlserver/testdata/minver_validation.txt"]', github.workspace) }}
          cwd: "."
          pull: "--ff"
      - name: Build Binaries
        id: build_binaries
        run: |
          latest=$(git rev-parse HEAD)
          echo "commitish=$latest" >> $GITHUB_OUTPUT
          GO_BUILD_VERSION=1.21 go/utils/publishrelease/buildbinaries.sh
      - name: Create Release
        id: create_release
        uses: dolthub/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ needs.format-version.outputs.version }}
          release_name: ${{ needs.format-version.outputs.version }}
          draft: false
          prerelease: false
          commitish: ${{ steps.build_binaries.outputs.commitish }}
      - name: Upload Linux AMD64 Distro
        id: upload-linux-amd64-distro
        uses: dolthub/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: go/out/dolt-linux-amd64.tar.gz
          asset_name: dolt-linux-amd64.tar.gz
          asset_content_type: application/zip
      - name: Upload Linux ARM64 Distro
        id: upload-linux-arm64-distro
        uses: dolthub/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: go/out/dolt-linux-arm64.tar.gz
          asset_name: dolt-linux-arm64.tar.gz
          asset_content_type: application/zip
      - name: Upload OSX AMD64 Distro
        id: upload-osx-amd64-distro
        uses: dolthub/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: go/out/dolt-darwin-amd64.tar.gz
          asset_name: dolt-darwin-amd64.tar.gz
          asset_content_type: application/zip
      - name: Upload OSX ARM64 Distro
        id: upload-osx-arm64-distro
        uses: dolthub/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: go/out/dolt-darwin-arm64.tar.gz
          asset_name: dolt-darwin-arm64.tar.gz
          asset_content_type: application/zip
      - name: Upload Windows Distro
        id: upload-windows-distro
        uses: dolthub/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: go/out/dolt-windows-amd64.zip
          asset_name: dolt-windows-amd64.zip
          asset_content_type: application/zip
      - name: Upload Windows Distro 7z
        id: upload-windows-distro-7z
        uses: dolthub/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: go/out/dolt-windows-amd64.7z
          asset_name: dolt-windows-amd64.7z
          asset_content_type: application/x-7z-compressed
      - name: Upload Install Script
        id: upload-install-script
        uses: dolthub/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: go/out/install.sh
          asset_name: install.sh
          asset_content_type: text/plain

  create-windows-msi:
    needs: [format-version, create-release]
    runs-on: ubuntu-22.04
    steps:
      - name: Trigger Upload MSI
        uses: peter-evans/repository-dispatch@v2.0.0
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: upload-msi
          repository: dolthub/windows-msi-creator
          client-payload: '{"tag": "${{ needs.format-version.outputs.version }}", "release_id": "${{ needs.create-release.outputs.release_id }}", "actor": "${{ github.actor }}"}'

  create-release-notes:
    needs: [format-version, create-release]
    runs-on: ubuntu-22.04
    steps:
      - name: Trigger Release Notes
        uses: peter-evans/repository-dispatch@v2.0.0
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: release-notes
          client-payload: '{"version": "${{ needs.format-version.outputs.version }}", "release_id": "${{ needs.create-release.outputs.release_id }}"}'

  homebrew-bump:
    needs: [format-version, create-release]
    runs-on: ubuntu-22.04
    steps:
      - name: Trigger Bump Homebrew
        uses: peter-evans/repository-dispatch@v2.0.0
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: bump-homebrew
          client-payload: '{"version": "${{ needs.format-version.outputs.version }}"}'

  trigger-performance-benchmark-email:
    needs: [format-version, create-release]
    runs-on: ubuntu-22.04
    steps:
      - name: Trigger Performance Benchmarks
        uses: peter-evans/repository-dispatch@v2.0.0
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: release-dolt
          client-payload: '{"version": "${{ needs.format-version.outputs.version }}", "actor": "${{ github.actor }}"}'

  docker-image-push:
    needs: [ format-version, create-release ]
    runs-on: ubuntu-22.04
    steps:
      - name: Trigger Push Docker Image
        uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: push-docker-image
          client-payload: '{"version": "${{ needs.format-version.outputs.version }}"}'
