name: 🔖 Release
run-name: 🔖 Release (${{ github.ref_name }})

on:
  workflow_dispatch:
  push:
    branches:
      - release
      - release-*
    tags-ignore:
      - "**"

jobs:
  release:
    name: 🔖 Release (${{ github.ref_name }})
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      issues: write
    outputs:
      channel: ${{ steps.release.outputs.new_release_channel }}
      released: ${{ steps.release.outputs.new_release_published }}
      tag: ${{ steps.release.outputs.new_release_git_tag }}
      version: ${{ steps.release.outputs.new_release_version }}
      merge_to: ${{ steps.summary.outputs.merge_to }}
      split_to: ${{ steps.summary.outputs.split_to }}
    steps:
      - name: 🚚 Checkout (${{ github.ref_name }})
        uses: actions/checkout@v4

      - name: 🔖 Run semantic release
        uses: cycjimmy/semantic-release-action@v4
        id: release
        with:
          working_directory: Packages/src
          extra_plugins: |
            @semantic-release/changelog
            @semantic-release/git
        env:
          GITHUB_TOKEN: ${{ github.token }}

      - id: summary
        run: |
          echo "🔖 New release published: '${{ steps.release.outputs.new_release_published }}'" | tee -a $GITHUB_STEP_SUMMARY
          echo "🔖 New release version: '${{ steps.release.outputs.new_release_version }}'" | tee -a $GITHUB_STEP_SUMMARY
          echo "🔖 New release channel: '${{ steps.release.outputs.new_release_channel }}'" | tee -a $GITHUB_STEP_SUMMARY
          echo "🔖 New release git tag: '${{ steps.release.outputs.new_release_git_tag }}'" | tee -a $GITHUB_STEP_SUMMARY
          
          if [ '${{ steps.release.outputs.new_release_published }}' = 'false' ]; then
            echo "No new release published." | tee -a $GITHUB_STEP_SUMMARY
          elif [ '${{ github.ref_name }}' = 'release' ]; then
            echo "merge_to=develop" | tee -a $GITHUB_OUTPUT $GITHUB_STEP_SUMMARY
            echo "split_to=main" | tee -a $GITHUB_OUTPUT $GITHUB_STEP_SUMMARY
          else
            channel=$(echo ${{ github.ref_name }} | sed 's/^release-//')
            echo "merge_to=develop-${channel}" | tee -a $GITHUB_OUTPUT $GITHUB_STEP_SUMMARY
            echo "split_to=${channel}" | tee -a $GITHUB_OUTPUT $GITHUB_STEP_SUMMARY
          fi

  merge-to:
    if: needs.release.outputs.merge_to != ''
    needs: release
    name: 🔀 Merge to ${{ needs.release.outputs.merge_to }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: 🚚 Checkout (${{ needs.release.outputs.merge_to }})
        uses: actions/checkout@v4
        with:
          ref: ${{ needs.release.outputs.merge_to }}
          fetch-depth: 0
      - name: 🔀 Merge '${{ needs.release.outputs.tag }}' into '${{ needs.release.outputs.merge_to }}'
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          
          git merge ${{ needs.release.outputs.tag }}
          git push origin ${{ needs.release.outputs.merge_to }}

  split-to:
    if: needs.release.outputs.split_to != ''
    needs: release
    name: 🔀 Split package to ${{ needs.release.outputs.split_to }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: 🚚 Checkout (${{ needs.release.outputs.tag }})
        uses: actions/checkout@v4
        with:
          ref: ${{ needs.release.outputs.tag }}
          fetch-depth: 0
      - name: 🔀 Split subtree 'Packages/src' to '${{ needs.release.outputs.split_to }}'
        run: |
          split_to=${{ needs.release.outputs.split_to }}
          git branch $split_to origin/$split_to
          git subtree split --prefix=Packages/src --branch $split_to
          git tag ${{ needs.release.outputs.version }} $split_to
          git push origin ${{ needs.release.outputs.version }} $split_to:$split_to