name: Prepare release

on:
  release:
    types: [published]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  update-package-version-and-push-commit:
    runs-on: ubuntu-22.04
    if: ${{ startsWith(github.event.release.tag_name, 'v') }}
    env:
      TAG_NAME: ${{ github.event.release.tag_name }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ssh-key: ${{ secrets.GH_ACTIONS_PUSH_TO_MAIN }}
          persist-credentials: false

      - name: Update package.json
        run: |
          version_number=$(echo "$TAG_NAME" | sed 's/^v//')
          yq -i -o=json ".version = \"${version_number}\"" package.json

      - name: Update manifest-binary.json
        run: |
          version_number=$(echo "$TAG_NAME" | sed 's/^v//')
          yq -i -o=json ".version = \"${version_number}\"" manifest-binary.json

      - name: Update version.ts
        run: |
          version_number=$(echo "$TAG_NAME" | sed 's/^v//')
          sed -i "s/export const VERSION = \".*\";/export const VERSION = \"${version_number}\";/" src/version.ts

      - name: Commit and push changes
        run: |
          git config user.name 'github-actions[bot]'
          git config user.email 'github-actions[bot]@users.noreply.github.com'

          git add package.json manifest-binary.json src/version.ts

          git commit -m "Release version '$TAG_NAME'"

          # Fail if there are any uncommitted changes
          if [[ -n $(git status --porcelain) ]]; then
            echo "Error: Git status is dirty. There are uncommitted changes:"
            git status
            exit 1
          fi

      - name: Setup SSH for push
        uses: webfactory/ssh-agent@v0.9.0
        with:
          ssh-private-key: ${{ secrets.GH_ACTIONS_PUSH_TO_MAIN }}

      - name: Configure git remote for SSH
        run: git remote set-url origin git@github.com:${{ github.repository }}.git

      - name: Push changes
        uses: ad-m/github-push-action@77c5b412c50b723d2a4fbc6d71fb5723bcd439aa
        with:
          ssh: true
          branch: main