name: Create Release

on:
  schedule:
    - cron: "20 3 * * *" # run every night
  workflow_dispatch: # allow manual running

jobs:
  BranchAndRebase:
    outputs:
      version: ${{ steps.version.outputs.version }}
    runs-on: ubuntu-latest
    steps:
      - name: Retrieve version
        run: echo "version=$(git ls-remote --sort='-version:refname' --tags --refs https://github.com/HabitRPG/habitica.git | head -n1 | sed 's/.*\/v\?//')" >> $GITHUB_OUTPUT
        id: version
      - name: Check release branch exists already
        run: |
          if git ls-remote --sort='-version:refname' --refs https://github.com/awinterstein/habitica.git | grep "releases/v${{ steps.version.outputs.version }}"; then
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "exists=false" >> $GITHUB_OUTPUT
          fi
        id: exists
      - name: Checkout
        if: steps.exists.outputs.exists != 'true'
        uses: actions/checkout@v4
        with:
          fetch-depth: 1000 # greater than the number of commits you made
      - id: rebase
        if: steps.exists.outputs.exists != 'true'
        name: Rebase on upstream release branch
        run: |
          set -ex

          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name  "GitHub Actions"

          git remote add upstream https://github.com/HabitRPG/habitica.git
          git fetch --tags upstream v${{ steps.version.outputs.version }}

          git checkout -b "releases/v${{ steps.version.outputs.version }}"
          git rebase "v${{ steps.version.outputs.version }}"

          git push origin $(git branch --show-current) --force-with-lease

        shell: bash

  CheckContainerExistence:
    outputs:
      exists: ${{ steps.exists.outputs.exists }}
    runs-on: ubuntu-latest
    needs: BranchAndRebase
    steps:
      - name: Check if container exists already
        run: |
          if wget -q -O - "https://hub.docker.com/v2/namespaces/${{ secrets.DOCKER_HUB_USER }}/repositories/habitica-server/tags?page_size=10" | grep -o '"name": *"[^"]*' | grep -q ${{ needs.BranchAndRebase.outputs.version }} \
              && wget -q -O - "https://hub.docker.com/v2/namespaces/${{ secrets.DOCKER_HUB_USER }}/repositories/habitica-client/tags?page_size=10" | grep -o '"name": *"[^"]*' | grep -q ${{ needs.BranchAndRebase.outputs.version }}; then
            echo "Container images exist already."
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "exists=false" >> $GITHUB_OUTPUT
          fi
        id: exists

  BuildAndPushContainer:
    runs-on: ubuntu-latest
    needs: [BranchAndRebase, CheckContainerExistence]
    if: needs.CheckContainerExistence.outputs.exists == 'false'
    steps:
      - uses: actions/checkout@v4
      - name: Build and push container
        uses: ./.github/actions/build-container
        with:
          version: ${{ needs.BranchAndRebase.outputs.version }}
          push_containers: "true"
          create_release: "true"
          registry_user: ${{ secrets.DOCKER_HUB_USER }}
          registry_token: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
          release_access_token: ${{ secrets.ACTIONS_ACCESS_TOKEN }}
