name: Prepare release branch
on:
  workflow_dispatch:

jobs:
  prereqs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - run: |
          version=$(.github/scripts/get-version.sh)
          version=${version//-SNAPSHOT/}
          if ! grep --quiet "^## Version $version" CHANGELOG.md; then
            echo the change log needs to be updated
            exit 1
          fi

  create-pull-request-against-release-branch:
    runs-on: ubuntu-latest
    needs: prereqs
    steps:
      - uses: actions/checkout@v3

      - name: Create release branch
        id: create-release-branch
        run: |
          version=$(.github/scripts/get-version.sh)
          version=${version//-SNAPSHOT/}
          release_branch_name=$(echo $version | sed -E 's/([0-9]+)\.([0-9]+)\.0/release\/v\1.\2.x/')

          git push origin HEAD:$release_branch_name

          echo "VERSION=$version" >> $GITHUB_ENV
          echo "RELEASE_BRANCH_NAME=$release_branch_name" >> $GITHUB_ENV

      - name: Update version
        run: .github/scripts/update-version.sh $VERSION

      - name: Set git user
        run: .github/scripts/set-git-user.sh

      - name: Create pull request against the release branch
        env:
          # not using the default GITHUB_TOKEN because pull requests generated by it do not run any workflows
          GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
        run: |
          message="Prepare release $VERSION"
          branch="prepare-release-${VERSION}"

          git commit -a -m "$message"
          git push origin HEAD:$branch
          gh pr create --title "[$RELEASE_BRANCH_NAME] $message" \
                       --body "$message." \
                       --head $branch \
                       --base $RELEASE_BRANCH_NAME

  create-pull-request-against-main:
    runs-on: ubuntu-latest
    needs: prereqs
    steps:
      - uses: actions/checkout@v3

      - name: Set environment variables
        run: |
          version=$(.github/scripts/get-version.sh)
          if [[ $version =~ ([0-9]+)\.([0-9]+)\.0 ]]; then
            major="${BASH_REMATCH[1]}"
            minor="${BASH_REMATCH[2]}"
          else
            echo "unexpected version: $version"
            exit 1
          fi
          next_version="$major.$((minor + 1)).0"
          next_version="${next_version}-SNAPSHOT"
          echo "NEXT_VERSION=$next_version" >> $GITHUB_ENV

      - name: Update version
        run: .github/scripts/update-version.sh $NEXT_VERSION

      - name: Set git user
        run: .github/scripts/set-git-user.sh

      - name: Create pull request against main
        env:
          # not using the default GITHUB_TOKEN because pull requests generated by it do not run any workflows
          GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
        run: |
          message="Update version to $NEXT_VERSION"
          body="Update version to \`$NEXT_VERSION\`."
          branch="update-version-to-${NEXT_VERSION}"

          git commit -a -m "$message"
          git push origin HEAD:$branch
          gh pr create --title "$message" \
                       --body "$body" \
                       --head $branch \
                       --base main
