name: Release - Post Release Ceremony

#
# Post Release Workflow
#
# 0. trigger the generate GUCs workflow in the docs repo
# 1. generate the artefacts to forward port to main and open PR for it
# 2. bump the next patch number + 1 on the release branch and open PR for it
# 

"on":
  release:
    types: [published]

# The workflow needs the permission to a PR
permissions:
  contents: write
  pull-requests: write

jobs:
  docs:
    name: Post Release Ceremonies
    runs-on: ubuntu-latest

    steps:
      - name: Generate list of GUCs for docs
        env:
          GITHUB_TOKEN: ${{ secrets.ORG_AUTOMATION_TOKEN }}
        run: |
          if gh workflow run tsdb-refresh-gucs-list.yaml -R timescale/docs -f tag=${{ github.event.release.tag_name }}; then
            echo "✅ Triggered generation of GUC list in docs" >> $GITHUB_STEP_SUMMARY
          else
            echo "❌ docs workflow to generate the GUCs failed" >> $GITHUB_STEP_SUMMARY
          fi

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.release.tag_name }}
      
      # git user config timescale-automation
      - run: |
          git config user.name "timescale-automation"
          git config user.email "123763385+github-actions[bot]@users.noreply.github.com"

      - name: Forward port changes to main
        env:
          GITHUB_TOKEN: ${{ secrets.ORG_AUTOMATION_TOKEN }}
        run: |
          ./scripts/release/build_post_release_artefacts.sh ${{ github.event.release.tag_name }}

          BRANCH="release/${{ github.event.release.tag_name }}-main"
          git checkout -b $BRANCH
          git add version.config sql/updates/*.sql sql/CMakeLists.txt .unreleased/* CHANGELOG.md
          git commit -m "forward port release artefacts of ${{ github.event.release.tag_name }} to main"
          git push origin $BRANCH

          printf '%b- CHANGELOG\n- set previous_version in version.config\n- adjust CMakeList.txt with up & down grade files\n- removed .unreleased files' > $BODY

          PR_URL=$(gh pr create \
            --title "Forwardport ${{ github.event.release.tag_name }}" \
            --body "$BODY" \
            --base main \
            --head $BRANCH \
            --label "release")
          echo "✅ Forward ported release artefacts: $PR_URL" >> $GITHUB_STEP_SUMMARY

      - name: Get next versions
        run: |
          TAGGED_VERSION=$(head -1 version.config | cut -d ' ' -f 3 | cut -d '-' -f 1)
          if [[ "$TAGGED_VERSION" != "$${{ github.event.release.tag_name }}" ]]; then
            echo "The tag: ${{ github.event.release.tag_name }} and the release version ${TAGGED_VERSION} do not match." >&2
            echo "❌ tagged version ${{ github.event.release.tag_name }} is not matching the current version $TAGGED_VERSION" >> $GITHUB_STEP_SUMMARY
            exit 1
          fi

          RELEASE_BRANCH="${TAGGED_VERSION/%.d/.x}"
          NEXT_VERSION=$(echo "$TAGGED_VERSION" | awk -F. '{printf "%d.%d.%d", $1, $2, $3+1}')

          echo "TAGGED_VERSION=${TAGGED_VERSION}" >> $GITHUB_ENV
          echo "RELEASE_BRANCH=${RELEASE_BRANCH}" >> $GITHUB_ENV
          echo "NEXT_VERSION=${NEXT_VERSION}" >> $GITHUB_ENV

      - uses: actions/checkout@v4
        with:
          ref: ${{ env.RELEASE_BRANCH }}

      - name: Bump to next version on release branch
        env:
          GITHUB_TOKEN: ${{ secrets.ORG_AUTOMATION_TOKEN }}
        run: |
          echo "Tagged version: ${{ env.TAGGED_VERSION }}"
          echo "Next version: ${{ env.NEXT_VERSION }}" 

          # adjust version.config file
          sed -i \
            -e "s/^version = .*/version = ${{ env.NEXT_VERSION }}/" \
            -e "s/^previous_version = .*/previous_version = ${{ env.TAGGED_VERSION }}/" \
            version.config

          echo "✅ setting next patch version ${{ env.TAGGED_VERSION }} in version.config" >> $GITHUB_STEP_SUMMARY
          cat version.config >> $GITHUB_STEP_SUMMARY

          BRANCH="release/bump-${{ github.event.release.tag_name }}-to-${{ env.NEXT_VERSION }}"
          git checkout -b $BRANCH
          git add version.config
          git commit -m "Bump to next patch version ${{ env.NEXT_VERSION }}"
          git push origin $BRANCH

          PR_URL=$(gh pr create \
            --title "Forwardport ${{ github.event.release.tag_name }}" \
            --body "Change to the next patch version ${{ env.NEXT_VERSION }} after the release of ${{ github.event.release.tag_name }}." \
            --base $RELEASE_BRANCH \
            --head $BRANCH \
            --label "release")
          echo "✅ Bump to next patch version: $PR_URL" >> $GITHUB_STEP_SUMMARY
