name: update-branch-version

on:
  workflow_dispatch:
    inputs:
      new_version:
        description: 'Version (x.y.z-SNASPHOT)'
        required: true
        type: string
  workflow_call:
    secrets:
      HURL_BOT_TOKEN:
        description: 'secrets.HURL_BOT_TOKEN from the caller workflow'
        required: true
      HURL_BOT_GPG_PRIVATE_KEY:
        description: 'secrets.HURL_BOT_GPG_PRIVATE_KEY from the caller workflow'
        required: true
      HURL_BOT_GPG_PRIVATE_KEY_PASSPHRASE:
        description: 'secrets.HURL_BOT_GPG_PRIVATE_KEY_PASSPHRASE from the caller workflow'
        required: true
    inputs:
      new_version:
        description: "Version (x.y.z-SNASPHOT)"
        required: true
        type: string
      branch:
        description: "ref branch for this workflow"
        default: "master"
        required: true
        type: string
    outputs:
      pr_number:
        description: "Create PR number"
        value: ${{ jobs.update-branch-version.outputs.pr_number }}

concurrency: update-branch-version

jobs:
  update-branch-version:
    env:
      GITHUB_CONTEXT: ${{ toJson(github) }}
      GITHUB_TOKEN: ${{ secrets.HURL_BOT_TOKEN }}
      REPO: ${{ github.repository }}
    outputs:
      pr_number: ${{ steps.create-new-version-pr.outputs.pr_number }}
    name: update-branch-version
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4.1.7
        with:
          ref: ${{ inputs.branch }}

      - name: Init bot branch name
        run: |
          base=$(echo "${{ github.ref }}" | sed "s#refs/heads/##g" | tr '/' '-')
          echo "BOT_UPDATE_VERSION_BRANCH_NAME=bot/update-branch-version-${base}" | tee -a $GITHUB_ENV

      - name: Update version
        run: |
          hurl_packages="hurl_core hurl hurlfmt"
          for package in ${hurl_packages} ; do
            cargo_toml="packages/${package}/Cargo.toml"
            sed -i "s/^version.*/version = \"${{ inputs.new_version }}\"/" "${cargo_toml}"
            echo "----------------------------"
            echo "  > package version for ${cargo_toml}"
            echo "      $(grep "^version =" "${cargo_toml}")"
            for dep_package in ${hurl_packages} ; do
              if [  $(grep -c "^${dep_package} =" "${cargo_toml}") -gt 0 ] ; then
                sed -i "s/^${dep_package} = { version .*/${dep_package} = { version = \"${{ inputs.new_version }}\", path = \"..\/${dep_package}\" }/" "${cargo_toml}"
                echo "  > ${dep_package} dep package version for ${cargo_toml}"
                echo "      $(grep "^${dep_package} =" "${cargo_toml}")"
              fi
            done
          done

      - name: Cargo update
        run: |
          ./bin/update_crates.sh

      - name: Update man
        run: |
          for package in hurl hurlfmt ; do
            python3 bin/release/gen_manpage.py "docs/manual/${package}.md" > "docs/manual/${package}.1"
          done

      - name: Update general docs
        run: |
          python3 bin/docs/build_man_md.py docs/manual/hurl.md > docs/manual.md
          python3 bin/docs/build_readme.py github > README.md
          python3 bin/docs/build_readme.py crates > packages/hurl/README.md

      - name: Init git bot context
        uses: crazy-max/ghaction-import-gpg@v6.1.0
        with:
          gpg_private_key: ${{ secrets.HURL_BOT_GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.HURL_BOT_GPG_PRIVATE_KEY_PASSPHRASE }}
          git_committer_name: "hurl-bot"
          git_committer_email: "bot@hurl.dev"
          git_user_signingkey: true
          git_commit_gpgsign: true

      - name: Push commits
        run: |
          git checkout -b "${BOT_UPDATE_VERSION_BRANCH_NAME}"
          git commit -am "Update hurl version to ${{ inputs.new_version }}"
          git push --set-upstream origin "${BOT_UPDATE_VERSION_BRANCH_NAME}" && git_exit_code=0 || git_exit_code=$?
          if [ ${git_exit_code} -eq 0 ] ; then
            echo "  - ✅ commits pushed to ${BOT_UPDATE_VERSION_BRANCH_NAME} branch."
          else
            echo "  - ❌ A problem occurs when attempting to push create release commits to ${BOT_UPDATE_VERSION_BRANCH_NAME} branch."
            exit 1
          fi

      - name: Create new version PR
        id: create-new-version-pr
        run: |
          GITHUB_TOKEN=${{ secrets.HURL_BOT_TOKEN }}
          git fetch
          base=$(echo "${{ github.ref }}" | sed "s#refs/heads/##g")
          gh pr create --fill --label bot --base "${base}"  --head "${BOT_UPDATE_VERSION_BRANCH_NAME}" && gh_exit_code=0 || gh_exit_code=$?
          if [ ${gh_exit_code} -eq 0 ] ; then
            NEW_PR_NUMBER=$(gh pr list --repo "${REPO}" --head "${BOT_UPDATE_VERSION_BRANCH_NAME}" --state "open" --json number --jq .[].number)
            echo "  - ✅ Creation of pull request n°${NEW_PR_NUMBER} succeeds."
            echo "pr_number=${NEW_PR_NUMBER}" | tee -a $GITHUB_OUTPUT
          else
            echo "  - ❌ A problem occurs when attempting to create new pull request."
            exit 1
          fi
