name: Prepare Release Branch
on:
  workflow_dispatch:

jobs:
  prepare-release-branch:
    runs-on: ubuntu-latest
    outputs:
      release-branch-name: ${{ steps.set-release-branch-name.outputs.release-branch-name }}
    steps:
      - uses: actions/checkout@v3

      - name: Set release branch name
        id: set-release-branch-name
        run: |
          release_branch_name=$(grep -Eo "[0-9.]+-SNAPSHOT" version.gradle.kts | sed -E 's/([0-9]+)\.([0-9]+)\.0-SNAPSHOT/v\1.\2.x/')
          echo "::set-output name=release-branch-name::$release_branch_name"

      - name: Create release branch
        run: |
          git checkout -b ${{ steps.set-release-branch-name.outputs.release-branch-name }}
          git push origin ${{ steps.set-release-branch-name.outputs.release-branch-name }}

  create-pull-request-against-release-branch:
    needs: prepare-release-branch
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          ref: ${{ needs.prepare-release-branch.outputs.release-branch-name }}

      - name: Remove SNAPSHOT from version
        run: |
          v=$(grep -Eo "[0-9]+.[0-9]+.0-SNAPSHOT" version.gradle.kts | sed 's/-SNAPSHOT//')
          .github/scripts/update-versions.sh "$v-SNAPSHOT" "$v-alpha-SNAPSHOT" "$v" "$v-alpha"

      - name: Bump download link version
        run: |
          new_version=$(grep -Eo "[0-9]+.[0-9]+.0" version.gradle.kts | head -1)
          sed -Ei "s,https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/,https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v$new_version/," README.md

      - name: Setup git name
        run: |
          git config user.name opentelemetry-java-bot
          git config user.email 97938252+opentelemetry-java-bot@users.noreply.github.com

      - name: Create pull request
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          msg="Prepare release branch ${{ needs.prepare-release-branch.outputs.release-branch-name }}"
          git add -u
          git commit -m "$msg"
          git push origin HEAD:prepare-release-branch-${{ needs.prepare-release-branch.outputs.release-branch-name }}
          gh pr create --title "$msg" \
                       --body "$msg" \
                       --head prepare-release-branch-${{ needs.prepare-release-branch.outputs.release-branch-name }} \
                       --base ${{ needs.prepare-release-branch.outputs.release-branch-name }}

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

      - name: Bump SNAPSHOT version
        run: |
          v=$(grep -Eo "[0-9]+.[0-9]+.0-SNAPSHOT" version.gradle.kts | sed 's/-SNAPSHOT//')
          if [[ $v =~ ([0-9]+).([0-9]+).0 ]]; then
            major="${BASH_REMATCH[1]}"
            minor="${BASH_REMATCH[2]}"
          else
            echo "unexpected version: $v"
            exit 1
          fi
          bump="$major.$((minor + 1)).0"
          .github/scripts/update-versions.sh "$v-SNAPSHOT" "$v-alpha-SNAPSHOT" "$bump-SNAPSHOT" "$bump-alpha-SNAPSHOT"

      - name: Setup git name
        run: |
          git config user.name opentelemetry-java-bot
          git config user.email 97938252+opentelemetry-java-bot@users.noreply.github.com

      - name: Create pull request
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          msg="Bump snapshot version"
          git add -u
          git commit -m "$msg"
          git push origin HEAD:bump-snapshot-version
          gh pr create --title "$msg" --body "$msg" --head bump-snapshot-version --base main
