name: 'Rollback console'

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version that should be set on rollback'
        required: true
        type: string

jobs:
  rollback:
    runs-on: ubuntu-22.04
    timeout-minutes: 10
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Log in to the Container registry (docker hub)
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Retag mainnet
        run: |
          docker pull vegaprotocol/trading:${{ inputs.version }}
          docker tag vegaprotocol/trading:${{ inputs.version }} vegaprotocol/trading:mainnet
          docker push vegaprotocol/trading:mainnet
          docker run --rm vegaprotocol/trading:mainnet cat /ipfs-hash > ipfs-hash

      - name: Trigger fleek deployment
        run: |
          # display info about app
          curl -H "Authorization: ${{ secrets.FLEEK_API_KEY }}" \
            -H "Content-Type: application/json" \
            -d '{"query": "query{getSiteById(siteId:\"f8f2e051-f18e-49e6-b876-0a39369dc0d8\"){id latestDeploy{id status}}}"}' \
            https://api.fleek.co/graphql

          # trigger new deployment as base image is always set to vegaprotocol/trading:mainnet
          curl -H "Authorization: ${{ secrets.FLEEK_API_KEY }}" \
            -H "Content-Type: application/json" \
            -d '{"query": "mutation{triggerDeploy(siteId:\"f8f2e051-f18e-49e6-b876-0a39369dc0d8\"){id status}}"}' \
            https://api.fleek.co/graphql

      - name: Check out ipfs-redirect
        uses: actions/checkout@v3
        with:
          repository: 'vegaprotocol/ipfs-redirect'
          path: 'ipfs-redirect'
          fetch-depth: '0'
          token: ${{ secrets.VEGA_CI_BOT_GITHUB_TOKEN }}

      - name: Update console.vega.xyz DNS to redirect to the new console
        env:
          GH_TOKEN: ${{ secrets.VEGA_CI_BOT_GITHUB_TOKEN }}
        run: |
          curl -L https://dist.ipfs.tech/kubo/v0.20.0/kubo_v0.20.0_linux-amd64.tar.gz -o kubo.tgz
          tar -xzf kubo.tgz
          export PATH="$PATH:$PWD/kubo"
          which ipfs
          new_hash=$(cat ipfs-hash)
          new_cid=$(ipfs cid format -v 1 -b base32 $new_hash)
          git config --global user.email "vega-ci-bot@vega.xyz"
          git config --global user.name "vega-ci-bot"
          echo $new_hash > ipfs-redirect/cidv0.txt
          echo $new_cid > ipfs-redirect/cidv1.txt

          (
            cd ipfs-redirect

            git status
            branch_name="rollback-to-$new_hash"
            git checkout -b "$branch_name"
            commit_msg="hash rollback to $new_hash"
            git add cidv0.txt cidv1.txt
            git commit -m "$commit_msg"
            git push -u origin "$branch_name" --force-with-lease
            pr_url="$(gh pr create --title "${commit_msg}" --body 'automated pull request to update CIDs')"
            echo $pr_url
            # once auto merge get's enabled on documentation repo let's do follow up
            sleep 5
            gh pr merge "${pr_url}" --delete-branch --squash --admin
          )
