name: Release

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

jobs:
  format-version:
    runs-on: ubuntu-18.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 "::set-output name=version::$version"

  create-release:
    needs: format-version
    name: Create release
    runs-on: ubuntu-18.04
    outputs:
      release_id: ${{ steps.create_release.outputs.id }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - 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 }}
      - uses: EndBug/add-and-commit@v7
        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', github.workspace) }}
          cwd: "."
      - name: Build Binaries
        id: build_binaries
        run: |
          latest=$(git rev-parse HEAD)
          echo "::set-output name=commitish::$latest"
          GO_BUILD_VERSION=1.18.0 go/utils/publishrelease/buildbinaries.sh
      - name: Create Release
        id: create_release
        uses: actions/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 Distro
        id: upload-linux-distro
        uses: actions/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 OSX AMD64 Distro
        id: upload-osx-amd64-distro
        uses: actions/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: actions/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: actions/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 Install Script
        id: upload-install-script
        uses: actions/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-18.04
    steps:
      - name: Trigger Upload MSI
        uses: peter-evans/repository-dispatch@v1
        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-18.04
    steps:
      - name: Trigger Release Notes
        uses: peter-evans/repository-dispatch@v1
        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-18.04
    steps:
      - name: Trigger Bump Homebrew
        uses: peter-evans/repository-dispatch@v1
        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-18.04
    steps:
      - name: Trigger Performance Benchmarks
        uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: release-dolt
          client-payload: '{"version": "${{ needs.format-version.outputs.version }}", "actor": "${{ github.actor }}"}'
