name: Release Preparation

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Selenium version to release'
        required: true
      chrome_channel:
        description: 'Chrome Channel for CDP'
        required: true
        type: choice
        default: "stable"
        options:
          - stable
          - early-stable

jobs:
  update-rust:
    name: Update Rust Version
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout repo"
        uses: actions/checkout@v4
        with:
          persist-credentials: false
          fetch-depth: 0
      - name: "Prep git"
        run: |
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
          if git rev-parse --verify rust-release-${{ github.event.inputs.version }} >/dev/null 2>&1; then
            git branch -D rust-release-${{ github.event.inputs.version }}
          fi
          git checkout -b rust-release-${{ github.event.inputs.version }}
      - name: Update Rust Version
        run: ./go rust:version ${{ github.event.inputs.version }}
      - name: Commit Rust updates
        run: git commit -m "update selenium manager version and rust changelog"
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.SELENIUM_CI_TOKEN }}
          branch: rust-release-${{ github.event.inputs.version }}
          force: true

  selenium-manager:
    name: Release Selenium Manager
    needs: update-rust
    uses: ./.github/workflows/ci-rust.yml
    with:
      release: true
      branch: rust-release-${{ github.event.inputs.version }}
    secrets:
      SELENIUM_CI_TOKEN: ${{ secrets.SELENIUM_CI_TOKEN }}

  cleanup-rust-branch:
    name: Cleanup Rust Branch
    needs: selenium-manager
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout repo"
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.SELENIUM_CI_TOKEN }}
      - name: "Delete rust release branch"
        run: |
          git push origin --delete rust-release-${{ github.event.inputs.version }}

  update-files:
    name: Update Files
    runs-on: ubuntu-latest
    needs: selenium-manager
    steps:
      - name: "Checkout project"
        uses: actions/checkout@v4
        with:
          persist-credentials: false
          fetch-depth: 0
          fetch-tags: true
          ref: trunk
      - name: Install Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.2'
          working-directory: 'rb'
      - name: Setup curl for Ubuntu
        run: sudo apt-get update && sudo apt-get install -y libcurl4-openssl-dev
      - name: "Prep git"
        run: |
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
      - name: Update browser versions
        run: ./go update_browsers ${{ github.event.inputs.chrome_channel }}
      - name: Commit browser updates
        run: git commit -m "update pinned browser versions" || true
      - name: Update devtools versions
        run: ./go all:update_cdp ${{ github.event.inputs.chrome_channel }}
      - name: Commit DevTools updates
        run: git commit -m "update devtools versions"
      - name: Update Selenium Manager versions
        run: ./go update_manager
      - name: Commit Selenium Manager updates
        run: git commit -m "update selenium manager versions"
      - name: Update Maven dependency versions
        run: ./go java:update
      - name: Commit Maven version updates
        run: git commit -m "update maven dependency versions"
      - name: Update Authors file
        run: ./go authors
      - name: Commit Authors updates
        run: git commit -m "update authors file"
      - name: Bump minor version
        run: ./go all:version ${{ github.event.inputs.version }}
      - name: Commit version updates
        run: git commit -m "bump versions in preparation for release"
      - name: Update changelog
        run: ./go all:changelogs
      - name: Commit changelog updates
        run: git commit -m "WIP - rough auto-update of changelog, please edit"
      - name: List untracked files
        run: git status -u
      - name: Clean working directory before PR
        run: git clean -fd
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v6
        with:
          token: ${{ secrets.SELENIUM_CI_TOKEN }}
          author: Selenium CI Bot <selenium-ci@users.noreply.github.com>
          delete-branch: true
          branch: release-preparation-${{ github.event.inputs.version }}
          base: trunk
          title: "[build] Prepare for release of Selenium ${{ github.event.inputs.version }}"
          body: |
            **Warning: Manually update the changelogs before merging**

            This PR:
              * Updates Pinned browser version to coincide with new CDP release
              * Adds support for new CDP version and removes old CDP version
              * Selenium Manager references the new Selenium Manager release
              * Updates Maven Dependencies
              * Adds new authors to authors file
              * Updates all versions for all bindings
              * Generates *rough* change logs for each bindings (please tidy them up before merging this)
                  * Not all commits need to be in the change log — users do not need to see anything about building or testing Selenium
                  * Remove references to updating versions including nightly
                  * The code now outputs the complete body of the commit message; make sure the changelog message is complete and correct

            - This PR was auto-generated by workflow run: [release-preparation][1]

            [1]: https://github.com/SeleniumHQ/selenium/actions/runs/${{ github.run_id }}
          labels: C-build
          draft: true
