name: Update OTP Patch Versions for Bazel Based Workflows
on:
  schedule:
  - cron: '0 3 * * *'
  workflow_dispatch:
jobs:
  update-toolchains:
    name: Update OTP Versions
    runs-on: ubuntu-22.04
    strategy:
      max-parallel: 1
      fail-fast: false
      matrix:
        erlang_version:
        - "25.0"
        - "25.1"
        - "25.2"
        - "25.3"
        - "26.1"
        - "26.2"
        include:
        - erlang_version: "25.0"
          name: '25_0'
          branch: main
          labels: |
            backport-v3.12.x
            backport-v3.11.x
        - erlang_version: "25.1"
          name: '25_1'
          branch: main
          labels: |
            backport-v3.12.x
            backport-v3.11.x
        - erlang_version: "25.2"
          name: '25_2'
          branch: main
          labels: |
            backport-v3.12.x
            backport-v3.11.x
        - erlang_version: "25.3"
          name: '25_3'
          branch: main
          labels: |
            backport-v3.12.x
            backport-v3.11.x
        - erlang_version: "26.1"
          name: '26_1'
          branch: main
          labels: |
            backport-v3.13.x
            backport-v3.12.x
        - erlang_version: "26.2"
          name: '26_2'
          branch: main
          labels: |
            backport-v3.13.x
            backport-v3.12.x
    timeout-minutes: 10
    env:
      branch: bump-otp-${{ matrix.erlang_version }}
    steps:
    - name: CHECKOUT REPOSITORY
      uses: actions/checkout@v4
      with:
        ref: ${{ matrix.branch }}
    - name: FAIL IF THE PR ALREADY EXISTS
      id: check-for-branch
      run: |
        set +e
        if git ls-remote --exit-code --heads origin ${{ env.branch }}; then
          echo "Branch ${{ env.branch }} already exits"
          exit 1
        fi
    - name: DETERMINE LATEST PATCH & SHA
      id: fetch-version
      run: |
        TAG_NAME=$(curl -s GET https://api.github.com/repos/erlang/otp/tags?per_page=100 \
          | jq -r 'map(select(.name | contains("OTP-${{ matrix.erlang_version }}"))) | first | .name')
        VERSION=${TAG_NAME#OTP-}

        if [[ -z "${VERSION}" ]]; then
          echo "Failed to determine latest VERSION for OTP-${{ matrix.erlang_version }}"
          exit 1
        fi

        ARCHIVE_RBE_URL="https://github.com/erlang/otp/releases/download/${TAG_NAME}/otp_src_${VERSION}.tar.gz"
        wget --continue --quiet --output-document="/tmp/otp_src_${VERSION}.tar.gz" "${ARCHIVE_RBE_URL}"
        SHA="$(shasum -a 256 "/tmp/otp_src_${VERSION}.tar.gz" | awk '{print $1}')"

        if [[ -z "${SHA}" ]]; then
          echo "Failed to determine SHA for ${TAG_NAME}"
          exit 1
        fi

        ARCHIVE_OCI_URL="https://github.com/erlang/otp/archive/OTP-${VERSION}.tar.gz"
        wget --continue --quiet --output-document="/tmp/OTP-${VERSION}.tar.gz" "${ARCHIVE_OCI_URL}"
        SHA2="$(shasum -a 256 "/tmp/OTP-${VERSION}.tar.gz" | awk '{print $1}')"

        if [[ -z "${SHA2}" ]]; then
          echo "Failed to determine SHA2 for ${TAG_NAME}"
          exit 1
        fi

        echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
        echo "SHA=${SHA}" >> $GITHUB_OUTPUT
        echo "SHA2=${SHA2}" >> $GITHUB_OUTPUT
    - name: MODIFY VERSION FILE
      run: |
        sudo npm install --global --silent @bazel/buildozer

        OLD_SHA="$(cat MODULE.bazel | buildozer 'print sha256' -:${{ matrix.name }})"
        OLD_VERSION="$(cat MODULE.bazel | buildozer 'print version' -:${{ matrix.name }})"

        echo "OLD_SHA: $OLD_SHA"
        echo "OLD_VERSION: $OLD_VERSION"

        echo "$(cat MODULE.bazel | buildozer 'set sha256 "${{ steps.fetch-version.outputs.SHA }}"' -:${{ matrix.name }})" > MODULE.bazel
        echo "$(cat MODULE.bazel | buildozer 'set version "${{ steps.fetch-version.outputs.VERSION }}"' -:${{ matrix.name }})" > MODULE.bazel
        echo "MODULE.bazel updated"

        buildozer 'set downloaded_file_path "OTP-${{ steps.fetch-version.outputs.VERSION }}.tar.gz"' //WORKSPACE:otp_src_${{ matrix.name }} || test $? -eq 3
        buildozer 'set urls ["https://github.com/erlang/otp/archive/OTP-${{ steps.fetch-version.outputs.VERSION }}.tar.gz"]' //WORKSPACE:otp_src_${{ matrix.name }} || test $? -eq 3
        buildozer 'set sha256 "${{ steps.fetch-version.outputs.SHA2 }}"' //WORKSPACE:otp_src_${{ matrix.name }} || test $? -eq 3

        echo "WORKSPACE updated"

        set -x
        git diff
    - name: CREATE PULL REQUEST
      uses: peter-evans/create-pull-request@v6.0.5
      with:
        token: ${{ secrets.REPO_SCOPED_TOKEN }}
        committer: GitHub <noreply@github.com>
        author: GitHub <noreply@github.com>
        title: Adopt otp ${{ steps.fetch-version.outputs.VERSION }}
        body: >
           Automated changes created by
           ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
           using the [create-pull-request](https://github.com/peter-evans/create-pull-request)
           GitHub action in the ${{ github.workflow }} workflow.
        commit-message: |
          Adopt otp ${{ steps.fetch-version.outputs.VERSION }}
        labels: ${{ matrix.labels }}
        branch: ${{ env.branch }}
        delete-branch: true
