name: Merge change log to main
on:
  workflow_dispatch:

jobs:
  create-pull-request:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
        with:
          # this workflow is run against the release branch (see usage of GITHUB_REF_NAME below)
          # but it is creating a pull request against main
          ref: main
          # history is needed to run format-patch below
          fetch-depth: 0

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

        # this will fail if there have been conflicting change log updates introduced in main
      - name: Create pull request against main
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git format-patch --stdout HEAD..origin/$GITHUB_REF_NAME CHANGELOG.md | git apply
          msg="Merge change log updates from $GITHUB_REF_NAME to main"
          git commit -a -m "$msg"
          git push origin HEAD:merge-change-log-updates-to-main
          gh pr create --title "$msg" \
                       --body "$msg" \
                       --head merge-change-log-updates-to-main \
                       --base main
